From 27f05760aa7b9673ba63b0aaaaf7fd1ef1f8105f Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Sun, 18 Feb 2024 18:53:18 +0100 Subject: [PATCH] Impl: Display trait for Playfield Ref: Move new to tests and refactor tests in nested mod --- src/main.rs | 120 +++++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 53 deletions(-) diff --git a/src/main.rs b/src/main.rs index 817fe66..dc85fbc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,32 +24,31 @@ struct Field { value: Option, } +use std::fmt; impl Playfield { fn new(size: usize) -> Playfield { - let fields = vec![ - vec![ - Field { - possible_values: Vec::::new(), - value: None - }; - size - ]; - size - ]; Self { - fields, + fields: vec![vec![Field::default(); size]; size], open_fields: Vec::::new(), } } } -impl Field { - fn new(value: u32) -> Field { - Self { - possible_values: Vec::::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::::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::::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); + } } }