From e1ca06de0a75a89be65ec5f3e484ea4688c2404b Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Fri, 16 Aug 2024 10:14:46 +0200 Subject: [PATCH] minesweeper: sync (#1980) [no important files changed] part of https://github.com/exercism/rust/issues/1824 --- .../minesweeper/.meta/test_template.tera | 33 +++++ .../practice/minesweeper/.meta/tests.toml | 19 ++- .../practice/minesweeper/tests/minesweeper.rs | 131 +++++++++++------- 3 files changed, 133 insertions(+), 50 deletions(-) create mode 100644 exercises/practice/minesweeper/.meta/test_template.tera diff --git a/exercises/practice/minesweeper/.meta/test_template.tera b/exercises/practice/minesweeper/.meta/test_template.tera new file mode 100644 index 000000000..451fbf988 --- /dev/null +++ b/exercises/practice/minesweeper/.meta/test_template.tera @@ -0,0 +1,33 @@ +use minesweeper::*; + +{% for test in cases %} +#[test] +#[ignore] +fn {{ test.description | make_ident }}() { + {% if test.input.minefield | length < 2 -%} + let input = &[ + {%- for line in test.input.minefield %} + {{ line | json_encode() }}, + {%- endfor %} + ]; + let expected{% if test.expected | length == 0 %}: &[&str]{% endif %} = &[ + {%- for line in test.expected %} + {{ line | json_encode() }}, + {%- endfor %} + ]; + {% else -%} + #[rustfmt::skip] + let (input, expected) = (&[ + {%- for line in test.input.minefield %} + {{ line | json_encode() }}, + {%- endfor %} + ], &[ + {%- for line in test.expected %} + {{ line | json_encode() }}, + {%- endfor %} + ]); + {% endif -%} + let actual = annotate(input); + assert_eq!(actual, expected); +} +{% endfor -%} diff --git a/exercises/practice/minesweeper/.meta/tests.toml b/exercises/practice/minesweeper/.meta/tests.toml index 83e6859da..2a1422224 100644 --- a/exercises/practice/minesweeper/.meta/tests.toml +++ b/exercises/practice/minesweeper/.meta/tests.toml @@ -1,6 +1,13 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. [0c5ec4bd-dea7-4138-8651-1203e1cb9f44] description = "no rows" @@ -11,6 +18,9 @@ description = "no columns" [6fbf8f6d-a03b-42c9-9a58-b489e9235478] description = "no mines" +[61aff1c4-fb31-4078-acad-cd5f1e635655] +description = "minefield with only mines" + [84167147-c504-4896-85d7-246b01dea7c5] description = "mine surrounded by spaces" @@ -31,3 +41,6 @@ description = "vertical line, mines at edges" [4b098563-b7f3-401c-97c6-79dd1b708f34] description = "cross" + +[04a260f1-b40a-4e89-839e-8dd8525abe0e] +description = "large minefield" diff --git a/exercises/practice/minesweeper/tests/minesweeper.rs b/exercises/practice/minesweeper/tests/minesweeper.rs index 807ede4ae..dfa647dda 100644 --- a/exercises/practice/minesweeper/tests/minesweeper.rs +++ b/exercises/practice/minesweeper/tests/minesweeper.rs @@ -1,148 +1,183 @@ -use minesweeper::annotate; - -fn remove_annotations(board: &[&str]) -> Vec { - board.iter().map(|r| remove_annotations_in_row(r)).collect() -} - -fn remove_annotations_in_row(row: &str) -> String { - row.as_bytes() - .iter() - .map(|&ch| match ch { - b'*' => '*', - _ => ' ', - }) - .collect() -} - -fn run_test(test_case: &[&str]) { - let cleaned = remove_annotations(test_case); - let cleaned_strs = cleaned.iter().map(|r| &r[..]).collect::>(); - let expected = test_case.iter().map(|&r| r.to_string()).collect::>(); - assert_eq!(expected, annotate(&cleaned_strs)); -} +use minesweeper::*; #[test] fn no_rows() { - #[rustfmt::skip] - run_test(&[ - ]); + let input = &[]; + let expected: &[&str] = &[]; + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn no_columns() { - #[rustfmt::skip] - run_test(&[ - "", - ]); + let input = &[""]; + let expected = &[""]; + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn no_mines() { #[rustfmt::skip] - run_test(&[ + let (input, expected) = (&[ + " ", + " ", + " ", + ], &[ " ", " ", " ", ]); + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] -fn board_with_only_mines() { +fn minefield_with_only_mines() { #[rustfmt::skip] - run_test(&[ + let (input, expected) = (&[ + "***", + "***", + "***", + ], &[ "***", "***", "***", ]); + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn mine_surrounded_by_spaces() { #[rustfmt::skip] - run_test(&[ + let (input, expected) = (&[ + " ", + " * ", + " ", + ], &[ "111", "1*1", "111", ]); + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn space_surrounded_by_mines() { #[rustfmt::skip] - run_test(&[ + let (input, expected) = (&[ + "***", + "* *", + "***", + ], &[ "***", "*8*", "***", ]); + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn horizontal_line() { - #[rustfmt::skip] - run_test(&[ - "1*2*1", - ]); + let input = &[" * * "]; + let expected = &["1*2*1"]; + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn horizontal_line_mines_at_edges() { - #[rustfmt::skip] - run_test(&[ - "*1 1*", - ]); + let input = &["* *"]; + let expected = &["*1 1*"]; + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn vertical_line() { #[rustfmt::skip] - run_test(&[ + let (input, expected) = (&[ + " ", + "*", + " ", + "*", + " ", + ], &[ "1", "*", "2", "*", "1", ]); + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn vertical_line_mines_at_edges() { #[rustfmt::skip] - run_test(&[ + let (input, expected) = (&[ + "*", + " ", + " ", + " ", + "*", + ], &[ "*", "1", " ", "1", "*", ]); + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] fn cross() { #[rustfmt::skip] - run_test(&[ + let (input, expected) = (&[ + " * ", + " * ", + "*****", + " * ", + " * ", + ], &[ " 2*2 ", "25*52", "*****", "25*52", " 2*2 ", ]); + let actual = annotate(input); + assert_eq!(actual, expected); } #[test] #[ignore] -fn large_board() { +fn large_minefield() { #[rustfmt::skip] - run_test(&[ + let (input, expected) = (&[ + " * * ", + " * ", + " * ", + " * *", + " * * ", + " ", + ], &[ "1*22*1", "12*322", " 123*2", @@ -150,4 +185,6 @@ fn large_board() { "1*22*2", "111111", ]); + let actual = annotate(input); + assert_eq!(actual, expected); }