From 22cbd08ee30641afe5d6eab40bc1dc1d535f36d0 Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Mon, 11 Sep 2023 22:14:39 +0200 Subject: [PATCH] Sync exercise wordy with problem spec --- .../practice/wordy/.docs/instructions.md | 17 +- .../practice/wordy/.meta/test_template.tera | 16 ++ exercises/practice/wordy/.meta/tests.toml | 19 ++- exercises/practice/wordy/tests/wordy.rs | 156 ++++++++++++------ 4 files changed, 138 insertions(+), 70 deletions(-) create mode 100644 exercises/practice/wordy/.meta/test_template.tera diff --git a/exercises/practice/wordy/.docs/instructions.md b/exercises/practice/wordy/.docs/instructions.md index f65b05acf..0b9e67b6c 100644 --- a/exercises/practice/wordy/.docs/instructions.md +++ b/exercises/practice/wordy/.docs/instructions.md @@ -40,8 +40,7 @@ Now, perform the other three operations. Handle a set of operations, in sequence. -Since these are verbal word problems, evaluate the expression from -left-to-right, _ignoring the typical order of operations._ +Since these are verbal word problems, evaluate the expression from left-to-right, _ignoring the typical order of operations._ > What is 5 plus 13 plus 6? @@ -55,14 +54,6 @@ left-to-right, _ignoring the typical order of operations._ The parser should reject: -* Unsupported operations ("What is 52 cubed?") -* Non-math questions ("Who is the President of the United States") -* Word problems with invalid syntax ("What is 1 plus plus 2?") - -## Bonus — Exponentials - -If you'd like, handle exponentials. - -> What is 2 raised to the 5th power? - -32 +- Unsupported operations ("What is 52 cubed?") +- Non-math questions ("Who is the President of the United States") +- Word problems with invalid syntax ("What is 1 plus plus 2?") diff --git a/exercises/practice/wordy/.meta/test_template.tera b/exercises/practice/wordy/.meta/test_template.tera new file mode 100644 index 000000000..20f0191ba --- /dev/null +++ b/exercises/practice/wordy/.meta/test_template.tera @@ -0,0 +1,16 @@ +{% for test in cases %} +#[test] +{% if loop.index != 1 -%} +#[ignore] +{% endif -%} +fn {{ test.description | slugify | replace(from="-", to="_") }}() { + let input = {{ test.input | json_encode() }}; + let output = {{ crate_name }}::{{ fn_names[0] }}(input); + let expected = {% if test.expected is object -%} + None + {%- else -%} + Some({{ test.expected }}) + {%- endif %}; + assert_eq!(output, expected); +} +{% endfor -%} diff --git a/exercises/practice/wordy/.meta/tests.toml b/exercises/practice/wordy/.meta/tests.toml index b6a29d500..f812dfa98 100644 --- a/exercises/practice/wordy/.meta/tests.toml +++ b/exercises/practice/wordy/.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. [88bf4b28-0de3-4883-93c7-db1b14aa806e] description = "just a number" @@ -38,9 +45,15 @@ description = "multiple subtraction" [4f4a5749-ef0c-4f60-841f-abcfaf05d2ae] description = "subtraction then addition" +[312d908c-f68f-42c9-aa75-961623cc033f] +description = "multiple multiplication" + [38e33587-8940-4cc1-bc28-bfd7e3966276] description = "addition and multiplication" +[3c854f97-9311-46e8-b574-92b60d17d394] +description = "multiple division" + [3ad3e433-8af7-41ec-aa9b-97b42ab49357] description = "unknown operation" diff --git a/exercises/practice/wordy/tests/wordy.rs b/exercises/practice/wordy/tests/wordy.rs index 6550af76f..765011883 100644 --- a/exercises/practice/wordy/tests/wordy.rs +++ b/exercises/practice/wordy/tests/wordy.rs @@ -1,177 +1,225 @@ -use wordy::answer; - #[test] fn just_a_number() { - let command = "What is 5?"; - assert_eq!(Some(5), answer(command)); + let input = "What is 5?"; + let output = wordy::answer(input); + let expected = Some(5); + assert_eq!(output, expected); } #[test] #[ignore] fn addition() { - let command = "What is 1 plus 1?"; - assert_eq!(Some(2), answer(command)); + let input = "What is 1 plus 1?"; + let output = wordy::answer(input); + let expected = Some(2); + assert_eq!(output, expected); } #[test] #[ignore] fn more_addition() { - let command = "What is 53 plus 2?"; - assert_eq!(Some(55), answer(command)); + let input = "What is 53 plus 2?"; + let output = wordy::answer(input); + let expected = Some(55); + assert_eq!(output, expected); } #[test] #[ignore] fn addition_with_negative_numbers() { - let command = "What is -1 plus -10?"; - assert_eq!(Some(-11), answer(command)); + let input = "What is -1 plus -10?"; + let output = wordy::answer(input); + let expected = Some(-11); + assert_eq!(output, expected); } #[test] #[ignore] fn large_addition() { - let command = "What is 123 plus 45678?"; - assert_eq!(Some(45_801), answer(command)); + let input = "What is 123 plus 45678?"; + let output = wordy::answer(input); + let expected = Some(45801); + assert_eq!(output, expected); } #[test] #[ignore] fn subtraction() { - let command = "What is 4 minus -12?"; - assert_eq!(Some(16), answer(command)); + let input = "What is 4 minus -12?"; + let output = wordy::answer(input); + let expected = Some(16); + assert_eq!(output, expected); } #[test] #[ignore] fn multiplication() { - let command = "What is -3 multiplied by 25?"; - assert_eq!(Some(-75), answer(command)); + let input = "What is -3 multiplied by 25?"; + let output = wordy::answer(input); + let expected = Some(-75); + assert_eq!(output, expected); } #[test] #[ignore] fn division() { - let command = "What is 33 divided by -3?"; - assert_eq!(Some(-11), answer(command)); + let input = "What is 33 divided by -3?"; + let output = wordy::answer(input); + let expected = Some(-11); + assert_eq!(output, expected); } #[test] #[ignore] fn multiple_additions() { - let command = "What is 1 plus 1 plus 1?"; - assert_eq!(Some(3), answer(command)); + let input = "What is 1 plus 1 plus 1?"; + let output = wordy::answer(input); + let expected = Some(3); + assert_eq!(output, expected); } #[test] #[ignore] fn addition_and_subtraction() { - let command = "What is 1 plus 5 minus -2?"; - assert_eq!(Some(8), answer(command)); + let input = "What is 1 plus 5 minus -2?"; + let output = wordy::answer(input); + let expected = Some(8); + assert_eq!(output, expected); } #[test] #[ignore] fn multiple_subtraction() { - let command = "What is 20 minus 4 minus 13?"; - assert_eq!(Some(3), answer(command)); + let input = "What is 20 minus 4 minus 13?"; + let output = wordy::answer(input); + let expected = Some(3); + assert_eq!(output, expected); } #[test] #[ignore] fn subtraction_then_addition() { - let command = "What is 17 minus 6 plus 3?"; - assert_eq!(Some(14), answer(command)); + let input = "What is 17 minus 6 plus 3?"; + let output = wordy::answer(input); + let expected = Some(14); + assert_eq!(output, expected); } #[test] #[ignore] -fn multiple_multiplications() { - let command = "What is 2 multiplied by -2 multiplied by 3?"; - assert_eq!(Some(-12), answer(command)); +fn multiple_multiplication() { + let input = "What is 2 multiplied by -2 multiplied by 3?"; + let output = wordy::answer(input); + let expected = Some(-12); + assert_eq!(output, expected); } #[test] #[ignore] fn addition_and_multiplication() { - let command = "What is -3 plus 7 multiplied by -2?"; - assert_eq!(Some(-8), answer(command)); + let input = "What is -3 plus 7 multiplied by -2?"; + let output = wordy::answer(input); + let expected = Some(-8); + assert_eq!(output, expected); } #[test] #[ignore] -fn multiple_divisions() { - let command = "What is -12 divided by 2 divided by -3?"; - assert_eq!(Some(2), answer(command)); +fn multiple_division() { + let input = "What is -12 divided by 2 divided by -3?"; + let output = wordy::answer(input); + let expected = Some(2); + assert_eq!(output, expected); } #[test] #[ignore] fn unknown_operation() { - let command = "What is 52 cubed?"; - assert_eq!(None, answer(command)); + let input = "What is 52 cubed?"; + let output = wordy::answer(input); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] fn non_math_question() { - let command = "Who is the President of the United States?"; - assert_eq!(None, answer(command)); + let input = "Who is the President of the United States?"; + let output = wordy::answer(input); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] fn reject_problem_missing_an_operand() { - let command = "What is 1 plus?"; - assert_eq!(None, answer(command)); + let input = "What is 1 plus?"; + let output = wordy::answer(input); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] fn reject_problem_with_no_operands_or_operators() { - let command = "What is?"; - assert_eq!(None, answer(command)); + let input = "What is?"; + let output = wordy::answer(input); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] fn reject_two_operations_in_a_row() { - let command = "What is 1 plus plus 2?"; - assert_eq!(None, answer(command)); + let input = "What is 1 plus plus 2?"; + let output = wordy::answer(input); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] fn reject_two_numbers_in_a_row() { - let command = "What is 1 plus 2 1?"; - assert_eq!(None, answer(command)); + let input = "What is 1 plus 2 1?"; + let output = wordy::answer(input); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] fn reject_postfix_notation() { - let command = "What is 1 2 plus?"; - assert_eq!(None, answer(command)); + let input = "What is 1 2 plus?"; + let output = wordy::answer(input); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] fn reject_prefix_notation() { - let command = "What is plus 1 2?"; - assert_eq!(None, answer(command)); + let input = "What is plus 1 2?"; + let output = wordy::answer(input); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] #[cfg(feature = "exponentials")] fn exponential() { - let command = "What is 2 raised to the 5th power?"; - assert_eq!(Some(32), answer(command)); + let input = "What is 2 raised to the 5th power?"; + let output = wordy::answer(input); + let expected = Some(32); + assert_eq!(output, expected); } #[test] #[ignore] #[cfg(feature = "exponentials")] fn addition_and_exponential() { - let command = "What is 1 plus 2 raised to the 2nd power?"; - assert_eq!(Some(9), answer(command)); + let input = "What is 1 plus 2 raised to the 2nd power?"; + let output = wordy::answer(input); + let expected = Some(9); + assert_eq!(output, expected); }