Impl: Display trait for Playfield
Ref: Move new to tests and refactor tests in nested mod
This commit is contained in:
120
src/main.rs
120
src/main.rs
@@ -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 Field {
|
impl fmt::Display for Playfield {
|
||||||
fn new(value: u32) -> Field {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
Self {
|
let mut result = String::new();
|
||||||
possible_values: Vec::<u32>::new(),
|
for row in &self.fields {
|
||||||
value: Some(value),
|
for field in row {
|
||||||
|
result += &field.value.unwrap_or_else(|| 0).to_string();
|
||||||
|
result += " ";
|
||||||
|
}
|
||||||
|
result += "\n";
|
||||||
}
|
}
|
||||||
|
write!(f, "{}", result)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Field {
|
||||||
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,50 +106,63 @@ fn is_right_field_size(field: &Vec<&str>, field_size: usize) -> bool {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
impl Field {
|
||||||
fn test_parse_field() {
|
fn new(value: u32) -> Field {
|
||||||
let input = "1 2 3 0 0 0 3 2 1";
|
Self {
|
||||||
let field_size = 3;
|
possible_values: Vec::<u32>::new(),
|
||||||
|
value: Some(value),
|
||||||
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![]
|
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
mod parse_field {
|
||||||
#[should_panic(expected = "Input must have dimension of size 2x2")]
|
use super::super::*;
|
||||||
fn test_parse_field_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);
|
#[test]
|
||||||
}
|
fn simple() {
|
||||||
|
let input = "1 2 3 0 0 0 3 2 1";
|
||||||
|
let field_size = 3;
|
||||||
|
|
||||||
#[test]
|
let playfield = parse_playfield(&input.to_string(), field_size);
|
||||||
#[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 _ = 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]
|
#[test]
|
||||||
#[should_panic(expected = "Input must contain only digits followed by space")]
|
#[should_panic(expected = "Input must have dimension of size 2x2")]
|
||||||
fn test_parse_field_wrong_character() {
|
fn too_long_input() {
|
||||||
let input = "1 2 3 0 0 0 3 2 a";
|
let input = "1 2 3 0 0 0 3 2 1";
|
||||||
let field_size = 3;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user