Skip to content

parse functions #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions test/clojure/core_test/parse_boolean.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(ns clojure.core-test.parse-boolean
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/parse-boolean
(deftest test-parse-boolean
(testing "common"
(are [expected x] (= expected (parse-boolean x))
false "false"
true "true"
nil "0"
nil "1"
nil ""
nil "foo"
nil "False"
nil "FALSE"
nil "True"
nil "TRUE"
nil "ttrue"
nil "truee"
nil "ffalse"
nil "falsee"
nil " true"
nil "tr ue"
nil "true "))

(testing "exceptions"
#?(:clj (are [x] (thrown? Exception (parse-boolean x))
nil
0
0.0
\a
:key
{}
'()
#{}
[]))
#?(:cljs (are [x] (thrown? js/Error (parse-boolean x))
nil
0
0.0
:key
{}
'()
#{}
[])))))
55 changes: 55 additions & 0 deletions test/clojure/core_test/parse_double.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(ns clojure.core-test.parse-double
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/parse-double
(deftest test-parse-double
(testing "common"
(are [expected x] (= expected (parse-double x))
nil ""
nil "foo"
Comment on lines +10 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clojure also supports scientific notation, like 1.5e10, which also supports uppercase E.

nil "f00"
nil "7oo"
nil "four"
nil "##Inf"
nil "-##Inf"
nil "+-5.6"
nil "-+5.6"
nil "7.9+6.4"
nil "7.9-6.4"
nil "Infinity7"
nil "-50Infinity7"
nil "8-Infinity7"
nil "InfinityE100"
nil "Infinitye5"
nil "2.6e8E5"
1.0 "1"
1.0 "1.0"
1.0 "1.000"
5.6 "+5.6"
-8.7 "-8.7"
90006.0 "9.0006e4"
-105.8 "-1.058e2"
568.51 "56851e-2"
568.51 "56851E-2"
##Inf "Infinity"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the edge case of two numbers next two each other, like Infinity7?

##-Inf "-Infinity"))
(testing "exceptions"
#?(:clj (are [x] (thrown? Exception (parse-double x))
{}
'()
[]
#{}
\a
:key
0.0
1000))
#?(:cljs (are [x] (thrown? js/Error (parse-double x))
{}
'()
[]
#{}
:key
0.0
1000)))))
51 changes: 51 additions & 0 deletions test/clojure/core_test/parse_long.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(ns clojure.core-test.parse-long
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/parse-long
(deftest test-parse-long
(testing "common"
(are [expected x] (= expected (parse-long x))
nil ""
nil "1L"
nil "foo"
nil "f00"
nil "7oo"
nil "four"
nil "0.0"
nil "+-5"
nil "-+5"
nil "##Inf"
nil "-##Inf"
nil "Infinity"
nil "Infinity7"
nil "-Infinity"
nil "-50Infinity"
nil "NaN"
0 "0"
42 "42"
12 "+12"
-1000 "-1000"
-100000000000 "-100000000000"
#?@(:clj [999999999999999999 "999999999999999999"]
:cljs [nil "999999999999999999"
999999999999999 "999999999999999"])))
(testing "exceptions"
#?(:clj (are [x] (thrown? Exception (parse-long x))
{}
'()
[]
#{}
\a
:key
0.0
1000))
#?(:cljs (are [x] (thrown? js/Error (parse-long x))
{}
'()
[]
#{}
:key
0.0
1000)))))
36 changes: 36 additions & 0 deletions test/clojure/core_test/parse_uuid.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(ns clojure.core-test.parse-uuid
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/parse-uuid
(deftest test-parse-uuid
(testing "common"
(are [expected x] (= expected (parse-uuid x))
nil ""
nil "0"
nil "df0993"
nil "b6883c0a-0342-4007-9966-bc2dfa6b109eb"
nil "ab6883c0a-0342-4007-9966-bc2dfa6b109e")
(is (= (parse-uuid "b6883c0a-0342-4007-9966-bc2dfa6b109e") (parse-uuid "B6883C0A-0342-4007-9966-BC2dfa6b109E")))
(when-var-exists clojure.core/instance?
#?(:clj (is (instance? java.util.UUID (parse-uuid "b6883c0a-0342-4007-9966-bc2dfa6b109e"))))
#?(:cljs (is (instance? cljs.core.UUID (parse-uuid "b6883c0a-0342-4007-9966-bc2dfa6b109e"))))))
(testing "exceptions"
#?(:clj (are [x] (thrown? Exception (parse-uuid x))
{}
'()
[]
#{}
\a
:key
0.0
1000))
#?(:cljs (are [x] (thrown? js/Error (parse-uuid x))
{}
'()
[]
#{}
:key
0.0
1000)))))