Impl: Display trait for Playfield

Ref: Move new to tests and refactor tests in nested mod
This commit is contained in:
Jonas Zeunert
2024-02-18 18:53:18 +01:00
parent 14ba619bc4
commit 27f05760aa

View File

@@ -24,32 +24,31 @@ struct Field {
value: Option<u32>,
}
use std::fmt;
impl Playfield {
fn new(size: usize) -> Playfield {
let fields = vec![
vec![
Field {
possible_values: Vec::<u32>::new(),
value: None
};
size
];
size
];
Self {
fields,
fields: vec![vec![Field::default(); size]; size],
open_fields: Vec::<u32>::new(),
}
}
}
impl Field {
fn new(value: u32) -> Field {
Self {
possible_values: Vec::<u32>::new(),
value: Some(value),
impl fmt::Display for Playfield {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = String::new();
for row in &self.fields {
for field in row {
result += &field.value.unwrap_or_else(|| 0).to_string();
result += " ";
}
result += "\n";
}
write!(f, "{}", result)
}
}
impl Field {
fn default() -> Field {
Self {
possible_values: Vec::<u32>::new(),
@@ -62,6 +61,8 @@ fn main() {
let cli = Cli::parse();
let playfield = parse_playfield(&cli.field, cli.field_size);
println!("Input:\n{}", playfield)
}
fn parse_playfield(field: &String, field_size: usize) -> Playfield {
@@ -105,50 +106,63 @@ fn is_right_field_size(field: &Vec<&str>, field_size: usize) -> bool {
mod tests {
use super::*;
#[test]
fn test_parse_field() {
let input = "1 2 3 0 0 0 3 2 1";
let field_size = 3;
let playfield = parse_playfield(&input.to_string(), field_size);
assert_eq!(
playfield,
Playfield {
fields: vec![
vec![Field::new(1), Field::new(2), Field::new(3)],
vec![Field::default(), Field::default(), Field::default()],
vec![Field::new(3), Field::new(2), Field::new(1)]
],
open_fields: vec![]
impl Field {
fn new(value: u32) -> Field {
Self {
possible_values: Vec::<u32>::new(),
value: Some(value),
}
)
}
}
#[test]
#[should_panic(expected = "Input must have dimension of size 2x2")]
fn test_parse_field_too_long_input() {
let input = "1 2 3 0 0 0 3 2 1";
let field_size = 2;
mod parse_field {
use super::super::*;
let _ = parse_playfield(&input.to_string(), field_size);
}
#[test]
fn simple() {
let input = "1 2 3 0 0 0 3 2 1";
let field_size = 3;
#[test]
#[should_panic(expected = "Input must have dimension of size 3x3")]
fn test_parse_field_too_short_input() {
let input = "1 2 3";
let field_size = 3;
let playfield = parse_playfield(&input.to_string(), field_size);
let _ = parse_playfield(&input.to_string(), field_size);
}
assert_eq!(
playfield,
Playfield {
fields: vec![
vec![Field::new(1), Field::new(2), Field::new(3)],
vec![Field::default(), Field::default(), Field::default()],
vec![Field::new(3), Field::new(2), Field::new(1)]
],
open_fields: vec![]
}
)
}
#[test]
#[should_panic(expected = "Input must contain only digits followed by space")]
fn test_parse_field_wrong_character() {
let input = "1 2 3 0 0 0 3 2 a";
let field_size = 3;
#[test]
#[should_panic(expected = "Input must have dimension of size 2x2")]
fn too_long_input() {
let input = "1 2 3 0 0 0 3 2 1";
let field_size = 2;
let _ = parse_playfield(&input.to_string(), field_size);
let _ = parse_playfield(&input.to_string(), field_size);
}
#[test]
#[should_panic(expected = "Input must have dimension of size 3x3")]
fn too_short_input() {
let input = "1 2 3";
let field_size = 3;
let _ = parse_playfield(&input.to_string(), field_size);
}
#[test]
#[should_panic(expected = "Input must contain only digits followed by space")]
fn wrong_character() {
let input = "1 2 3 0 0 0 3 2 a";
let field_size = 3;
let _ = parse_playfield(&input.to_string(), field_size);
}
}
}