Add the parser to parse the string as an integer if it's needed.
This commit is contained in:
@@ -3,7 +3,21 @@
|
|||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(defn unsigned-int?
|
(defn unsigned-int?
|
||||||
|
"Return true if the string is an unsigned integer."
|
||||||
[str]
|
[str]
|
||||||
(if (re-matches #"\d+" str)
|
(if (re-matches #"\d+" str)
|
||||||
true false))
|
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))
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
[cipher-analytical-machine.parsers.parsers :refer :all]))
|
[cipher-analytical-machine.parsers.parsers :refer :all]))
|
||||||
|
|
||||||
(deftest unsigned-int?-test
|
(deftest unsigned-int?-test
|
||||||
(testing ""
|
(testing "The function return true only for an unsingned integer as a string."
|
||||||
(are [str expected]
|
(are [str expected]
|
||||||
(= expected
|
(= expected
|
||||||
(unsigned-int? str))
|
(unsigned-int? str))
|
||||||
@@ -13,3 +13,13 @@
|
|||||||
"-10" false
|
"-10" false
|
||||||
"abc" 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)))
|
||||||
|
|||||||
Reference in New Issue
Block a user