diff --git a/src/cipher_analytical_machine/parsers/parsers.clj b/src/cipher_analytical_machine/parsers/parsers.clj index e0ef1dd..e932f2e 100644 --- a/src/cipher_analytical_machine/parsers/parsers.clj +++ b/src/cipher_analytical_machine/parsers/parsers.clj @@ -3,7 +3,21 @@ (:gen-class)) (defn unsigned-int? + "Return true if the string is an unsigned integer." [str] (if (re-matches #"\d+" str) true false)) +(defn parse-unsigned-int + "Return an integer if the argument is an integer." + [str-or-int] + (cond + (int? str-or-int) + str-or-int + + (unsigned-int? str-or-int) + (Integer/parseInt str-or-int) + + :else + nil)) + diff --git a/test/cipher_analytical_machine/parsers/parsers_test.clj b/test/cipher_analytical_machine/parsers/parsers_test.clj index ab723ef..75556e0 100644 --- a/test/cipher_analytical_machine/parsers/parsers_test.clj +++ b/test/cipher_analytical_machine/parsers/parsers_test.clj @@ -4,7 +4,7 @@ [cipher-analytical-machine.parsers.parsers :refer :all])) (deftest unsigned-int?-test - (testing "" + (testing "The function return true only for an unsingned integer as a string." (are [str expected] (= expected (unsigned-int? str)) @@ -13,3 +13,13 @@ "-10" false "abc" false))) +(deftest parse-unsigned-int-test + (testing "The function parse the integer if the string is an integer." + (are [str-or-int expected] + (= expected + (parse-unsigned-int str-or-int)) + "9" 9 + 9 9 + "10" 10 + "-10" nil + "abc" nil)))