From 4f3068b8b9a99ad36641cf84b59a8633cb2f845f Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Sun, 18 Feb 2024 15:57:12 +0100 Subject: [PATCH] Test: Too short input --- src/main.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index b692b5e..0a6240d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,13 +71,16 @@ fn parse_playfield(field: &String, field_size: usize) -> Playfield { let chars = field.split(" ").collect::>(); + if chars.len() != field_size * field_size { + panic!( + "Input must have dimension of size {size}x{size}", + size = field_size + ); + } for row in 0..field_size { for col in 0..field_size { let field = &mut playfield.fields[row][col]; - let char_field = chars.get(row * field_size + col).expect(&format!( - "Input must have dimension of size {size}x{size}", - size = field_size - )); + let char_field = chars.get(row * field_size + col).unwrap(); if *char_field == "0" { continue; @@ -115,11 +118,20 @@ mod tests { } #[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() { let input = "1 2 3 0 0 0 3 2 1"; let field_size = 2; let _ = parse_playfield(&input.to_string(), field_size); } + + #[test] + #[should_panic(expected = "Input must have dimension of size 2x2")] + fn test_parse_field_too_short_input() { + let input = "1 2 3"; + let field_size = 2; + + let _ = parse_playfield(&input.to_string(), field_size); + } }