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>, value: Option<u32>,
} }
use std::fmt;
impl Playfield { impl Playfield {
fn new(size: usize) -> Playfield { fn new(size: usize) -> Playfield {
let fields = vec![
vec![
Field {
possible_values: Vec::<u32>::new(),
value: None
};
size
];
size
];
Self { Self {
fields, fields: vec![vec![Field::default(); size]; size],
open_fields: Vec::<u32>::new(), open_fields: Vec::<u32>::new(),
} }
} }
} }
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 { impl Field {
fn new(value: u32) -> Field {
Self {
possible_values: Vec::<u32>::new(),
value: Some(value),
}
}
fn default() -> Field { fn default() -> Field {
Self { Self {
possible_values: Vec::<u32>::new(), possible_values: Vec::<u32>::new(),
@@ -62,6 +61,8 @@ fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let playfield = parse_playfield(&cli.field, cli.field_size); let playfield = parse_playfield(&cli.field, cli.field_size);
println!("Input:\n{}", playfield)
} }
fn parse_playfield(field: &String, field_size: usize) -> Playfield { fn parse_playfield(field: &String, field_size: usize) -> Playfield {
@@ -105,8 +106,20 @@ fn is_right_field_size(field: &Vec<&str>, field_size: usize) -> bool {
mod tests { mod tests {
use super::*; use super::*;
impl Field {
fn new(value: u32) -> Field {
Self {
possible_values: Vec::<u32>::new(),
value: Some(value),
}
}
}
mod parse_field {
use super::super::*;
#[test] #[test]
fn test_parse_field() { fn simple() {
let input = "1 2 3 0 0 0 3 2 1"; let input = "1 2 3 0 0 0 3 2 1";
let field_size = 3; let field_size = 3;
@@ -127,7 +140,7 @@ mod tests {
#[test] #[test]
#[should_panic(expected = "Input must have dimension of size 2x2")] #[should_panic(expected = "Input must have dimension of size 2x2")]
fn test_parse_field_too_long_input() { fn too_long_input() {
let input = "1 2 3 0 0 0 3 2 1"; let input = "1 2 3 0 0 0 3 2 1";
let field_size = 2; let field_size = 2;
@@ -136,7 +149,7 @@ mod tests {
#[test] #[test]
#[should_panic(expected = "Input must have dimension of size 3x3")] #[should_panic(expected = "Input must have dimension of size 3x3")]
fn test_parse_field_too_short_input() { fn too_short_input() {
let input = "1 2 3"; let input = "1 2 3";
let field_size = 3; let field_size = 3;
@@ -145,10 +158,11 @@ mod tests {
#[test] #[test]
#[should_panic(expected = "Input must contain only digits followed by space")] #[should_panic(expected = "Input must contain only digits followed by space")]
fn test_parse_field_wrong_character() { fn wrong_character() {
let input = "1 2 3 0 0 0 3 2 a"; let input = "1 2 3 0 0 0 3 2 a";
let field_size = 3; let field_size = 3;
let _ = parse_playfield(&input.to_string(), field_size); let _ = parse_playfield(&input.to_string(), field_size);
} }
} }
}