diff --git a/exercises/practice/wordy/.meta/config.json b/exercises/practice/wordy/.meta/config.json index 972ea282d..075758a35 100644 --- a/exercises/practice/wordy/.meta/config.json +++ b/exercises/practice/wordy/.meta/config.json @@ -6,7 +6,8 @@ "AndreaCrotti", "haus", "sjwarner-bp", - "yurrriq" + "yurrriq", + "erikschierboom" ], "files": { "solution": [ diff --git a/exercises/practice/wordy/.meta/example.clj b/exercises/practice/wordy/.meta/example.clj index 4d94a71f2..e5bf3fbaf 100644 --- a/exercises/practice/wordy/.meta/example.clj +++ b/exercises/practice/wordy/.meta/example.clj @@ -11,13 +11,22 @@ (defn- parse-op [op-str] (or (ops op-str) - (throw (IllegalArgumentException. (str "unknown operator " op-str))))) + (if (every? Character/isDigit op-str) + (throw (IllegalArgumentException. "syntax error")) + (throw (IllegalArgumentException. "unknown operation"))))) + +(defn- parse-operand [operand-str] + (try + (Integer/parseInt operand-str) + (catch java.lang.NumberFormatException _ (throw (IllegalArgumentException. "syntax error"))))) (defn evaluate [expr] - (if-let [[_ exprs] (re-matches #"What is (.+)\?" expr)] - (if-let [[token & tokens] (re-seq tokens-pattern exprs)] - (reduce (fn [acc [op x]] - ((parse-op op) acc (Integer/parseInt x))) - (Integer/parseInt token) (partition-all 2 tokens)) - (throw (IllegalArgumentException. "no arithmetic expression found"))) - (throw (IllegalArgumentException. "cannot recognize question")))) + (if-let [[_ exprs] (re-matches #"What is(.*)\?" expr)] + (if (empty? exprs) + (throw (IllegalArgumentException. "syntax error")) + (if-let [[token & tokens] (re-seq tokens-pattern exprs)] + (reduce (fn [acc [op x]] + ((parse-op op) acc (parse-operand x))) + (parse-operand token) (partition-all 2 tokens)) + (throw (IllegalArgumentException. "unknown operation")))) + (throw (IllegalArgumentException. "syntax error")))) diff --git a/exercises/practice/wordy/.meta/generator.clj b/exercises/practice/wordy/.meta/generator.clj new file mode 100644 index 000000000..af53699bb --- /dev/null +++ b/exercises/practice/wordy/.meta/generator.clj @@ -0,0 +1,11 @@ +(ns wordy-generator) + +(defn normalize-error [test-case error] + (if (= "Non math question" (:description test-case)) + "^syntax error$" + (str "^" error "$"))) + +(defn transform-test-case [test-case] + (if-let [error (get-in test-case [:expected :error])] + (assoc-in test-case [:expected :error] (normalize-error test-case error)) + test-case)) diff --git a/exercises/practice/wordy/.meta/generator.tpl b/exercises/practice/wordy/.meta/generator.tpl new file mode 100644 index 000000000..83f3836c6 --- /dev/null +++ b/exercises/practice/wordy/.meta/generator.tpl @@ -0,0 +1,12 @@ +(ns wordy-test + (:require [clojure.test :refer [deftest testing is]] + wordy)) +{{#test_cases.answer}} +(deftest evaluate_test_{{idx}} + (testing {{description}} + {{~#if error}} + (is (thrown-with-msg? IllegalArgumentException #{{error}} (wordy/evaluate {{input.question}}))))) + {{else}} + (is (= {{expected}} (wordy/evaluate {{input.question}}))))) + {{/if~}} +{{/test_cases.answer~}} diff --git a/exercises/practice/wordy/src/wordy.clj b/exercises/practice/wordy/src/wordy.clj index f1de41250..4de9e44a9 100644 --- a/exercises/practice/wordy/src/wordy.clj +++ b/exercises/practice/wordy/src/wordy.clj @@ -1,5 +1,7 @@ (ns wordy) -(defn evaluate [] ;; <- arglist goes here - ;; your code goes here -) +(defn evaluate + "Evaluates a simple math problem" + [question] + ;; function body + ) diff --git a/exercises/practice/wordy/test/wordy_test.clj b/exercises/practice/wordy/test/wordy_test.clj index 75964c46c..159fe3b39 100644 --- a/exercises/practice/wordy/test/wordy_test.clj +++ b/exercises/practice/wordy/test/wordy_test.clj @@ -1,56 +1,96 @@ (ns wordy-test - (:require [clojure.test :refer [deftest is]] + (:require [clojure.test :refer [deftest testing is]] wordy)) -(deftest addition - (is (= (wordy/evaluate "What is 1 plus 1?") 2))) - -(deftest more-addition - (is (= (wordy/evaluate "What is 53 plus 2?") 55))) - -(deftest addition-with-negative-numbers - (is (= (wordy/evaluate "What is -1 plus -10?") -11))) - -(deftest large-addition - (is (= (wordy/evaluate "What is 123 plus 45678?") 45801))) - -(deftest subtraction - (is (= (wordy/evaluate "What is 4 minus -12?") 16))) - -(deftest multiplication - (is (= (wordy/evaluate "What is -3 multiplied by 25?") -75))) - -(deftest division - (is (= (wordy/evaluate "What is 33 divided by -3?") -11))) - -(deftest multiple-additions - (is (= (wordy/evaluate "What is 1 plus 1 plus 1?") 3))) - -(deftest addition-and-subtraction - (is (= (wordy/evaluate "What is 1 plus 5 minus -2?") 8))) - -(deftest multiple-subtraction - (is (= (wordy/evaluate "What is 20 minus 4 minus 13?") 3))) - -(deftest subtraction-then-addition - (is (= (wordy/evaluate "What is 17 minus 6 plus 3?") 14))) - -(deftest multiple-multiplication - (is (= (wordy/evaluate "What is 2 multiplied by -2 multiplied by 3?") -12))) - -(deftest addition-and-multiplication - (is (= (wordy/evaluate "What is -3 plus 7 multiplied by -2?") -8))) - -(deftest multiple-division - (is (= (wordy/evaluate "What is -12 divided by 2 divided by -3?") 2))) - -(deftest unknown-operation - (is (thrown? - IllegalArgumentException - (wordy/evaluate "What is 52 cubed?")))) - -(deftest Non-math-question - (is (thrown? - IllegalArgumentException - (wordy/evaluate "Who is the President of the United States?")))) - +(deftest evaluate_test_1 + (testing "just a number" + (is (= 5 (wordy/evaluate "What is 5?"))))) + +(deftest evaluate_test_2 + (testing "addition" + (is (= 2 (wordy/evaluate "What is 1 plus 1?"))))) + +(deftest evaluate_test_3 + (testing "more addition" + (is (= 55 (wordy/evaluate "What is 53 plus 2?"))))) + +(deftest evaluate_test_4 + (testing "addition with negative numbers" + (is (= -11 (wordy/evaluate "What is -1 plus -10?"))))) + +(deftest evaluate_test_5 + (testing "large addition" + (is (= 45801 (wordy/evaluate "What is 123 plus 45678?"))))) + +(deftest evaluate_test_6 + (testing "subtraction" + (is (= 16 (wordy/evaluate "What is 4 minus -12?"))))) + +(deftest evaluate_test_7 + (testing "multiplication" + (is (= -75 (wordy/evaluate "What is -3 multiplied by 25?"))))) + +(deftest evaluate_test_8 + (testing "division" + (is (= -11 (wordy/evaluate "What is 33 divided by -3?"))))) + +(deftest evaluate_test_9 + (testing "multiple additions" + (is (= 3 (wordy/evaluate "What is 1 plus 1 plus 1?"))))) + +(deftest evaluate_test_10 + (testing "addition and subtraction" + (is (= 8 (wordy/evaluate "What is 1 plus 5 minus -2?"))))) + +(deftest evaluate_test_11 + (testing "multiple subtraction" + (is (= 3 (wordy/evaluate "What is 20 minus 4 minus 13?"))))) + +(deftest evaluate_test_12 + (testing "subtraction then addition" + (is (= 14 (wordy/evaluate "What is 17 minus 6 plus 3?"))))) + +(deftest evaluate_test_13 + (testing "multiple multiplication" + (is (= -12 (wordy/evaluate "What is 2 multiplied by -2 multiplied by 3?"))))) + +(deftest evaluate_test_14 + (testing "addition and multiplication" + (is (= -8 (wordy/evaluate "What is -3 plus 7 multiplied by -2?"))))) + +(deftest evaluate_test_15 + (testing "multiple division" + (is (= 2 (wordy/evaluate "What is -12 divided by 2 divided by -3?"))))) + +(deftest evaluate_test_16 + (testing "unknown operation" + (is (thrown-with-msg? IllegalArgumentException #"^unknown operation$" (wordy/evaluate "What is 52 cubed?"))))) + +(deftest evaluate_test_17 + (testing "Non math question" + (is (thrown-with-msg? IllegalArgumentException #"^syntax error$" (wordy/evaluate "Who is the President of the United States?"))))) + +(deftest evaluate_test_18 + (testing "reject problem missing an operand" + (is (thrown-with-msg? IllegalArgumentException #"^syntax error$" (wordy/evaluate "What is 1 plus?"))))) + +(deftest evaluate_test_19 + (testing "reject problem with no operands or operators" + (is (thrown-with-msg? IllegalArgumentException #"^syntax error$" (wordy/evaluate "What is?"))))) + +(deftest evaluate_test_20 + (testing "reject two operations in a row" + (is (thrown-with-msg? IllegalArgumentException #"^syntax error$" (wordy/evaluate "What is 1 plus plus 2?"))))) + +(deftest evaluate_test_21 + (testing "reject two numbers in a row" + (is (thrown-with-msg? IllegalArgumentException #"^syntax error$" (wordy/evaluate "What is 1 plus 2 1?"))))) + +(deftest evaluate_test_22 + (testing "reject postfix notation" + (is (thrown-with-msg? IllegalArgumentException #"^syntax error$" (wordy/evaluate "What is 1 2 plus?"))))) + +(deftest evaluate_test_23 + (testing "reject prefix notation" + (is (thrown-with-msg? IllegalArgumentException #"^syntax error$" (wordy/evaluate "What is plus 1 2?"))))) + \ No newline at end of file