Skip to content
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
164 changes: 88 additions & 76 deletions test/clojure/core_test/keyword.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,98 @@
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/keyword
(deftest test-keyword
;; "Symbols begin with a non-numeric character and can contain
;; alphanumeric characters and *, +, !, -, _, ', ?, <, > and =
;; (other characters may be allowed eventually)."
;;
;; "Keywords are like symbols, except: * They can and must begin with a colon, e.g. :fred."
;;
;; (see http://clojure.org/reader for details)
;;
;; From https://clojuredocs.org/clojure.core/keyword
;; keyword does not validate input strings for ns and name, and may
;; return improper keywords with undefined behavior for non-conformant
;; ns and name.
(deftest test-keyword
;; "Symbols begin with a non-numeric character and can contain
;; alphanumeric characters and *, +, !, -, _, ', ?, <, > and =
;; (other characters may be allowed eventually)."
;;
;; "Keywords are like symbols, except: * They can and must begin with a colon, e.g. :fred."
;;
;; (see http://clojure.org/reader for details)
;;
;; From https://clojuredocs.org/clojure.core/keyword
;; keyword does not validate input strings for ns and name, and may
;; return improper keywords with undefined behavior for non-conformant
;; ns and name.

(are [expected name] (= expected (keyword name))
:abc "abc"
:abc 'abc
:abc :abc
:* "*"
:* '*
:* :*
:+ "+"
:+ '+
:+ :+
:! "!"
:! '!
:! :!
:- "-"
:- '-
:- :-
:_ "_"
:_ '_
:_ :_
:? "?"
:? '?
:? :?
:< "<"
:< '<
:< :<
:> ">"
:> '>
:> :>
:= "="
:= '=
:= :=
:abc*+!-_'?<>= "abc*+!-_'?<>=")
(are [expected name] (= expected (keyword name))
:abc "abc"
:abc 'abc
:abc :abc
:* "*"
:* '*
:* :*
:+ "+"
:+ '+
:+ :+
:! "!"
:! '!
:! :!
:- "-"
:- '-
:- :-
:_ "_"
:_ '_
:_ :_
:? "?"
:? '?
:? :?
:< "<"
:< '<
:< :<
:> ">"
:> '>
:> :>
:= "="
:= '=
:= :=
:abc*+!-_'?<>= "abc*+!-_'?<>=")

(are [expected ns name] (= expected (keyword ns name))
:abc/abc "abc" "abc"
:abc.def/abc "abc.def" "abc"
(are [expected ns name] (= expected (keyword ns name))
:abc/abc "abc" "abc"
:abc.def/abc "abc.def" "abc"

:*/abc "*" "abc"
:+/abc "+" "abc"
:!/abc "!" "abc"
:-/abc "-" "abc"
:_/abc "_" "abc"
:?/abc "?" "abc"
:</abc "<" "abc"
:>/abc ">" "abc"
:=/abc "=" "abc"
:*/abc "*" "abc"
:+/abc "+" "abc"
:!/abc "!" "abc"
:-/abc "-" "abc"
:_/abc "_" "abc"
:?/abc "?" "abc"
:</abc "<" "abc"
:>/abc ">" "abc"
:=/abc "=" "abc"

:abc.def/* "abc.def" "*"
:abc.def/+ "abc.def" "+"
:abc.def/! "abc.def" "!"
:abc.def/- "abc.def" "-"
:abc.def/_ "abc.def" "_"
:abc.def/? "abc.def" "?"
:abc.def/< "abc.def" "<"
:abc.def/> "abc.def" ">"
:abc.def/= "abc.def" "="
:abc.def/* "abc.def" "*"
:abc.def/+ "abc.def" "+"
:abc.def/! "abc.def" "!"
:abc.def/- "abc.def" "-"
:abc.def/_ "abc.def" "_"
:abc.def/? "abc.def" "?"
:abc.def/< "abc.def" "<"
:abc.def/> "abc.def" ">"
:abc.def/= "abc.def" "="

:abc*+!-_'?<>=/abc*+!-_'?<>= "abc*+!-_'?<>=" "abc*+!-_'?<>=")
:abc*+!-_'?<>=/abc*+!-_'?<>= "abc*+!-_'?<>=" "abc*+!-_'?<>=")

(is (nil? (keyword nil))) ; (keyword nil) => nil, surprisingly
(is (= :abc (keyword nil "abc"))) ; But if ns is nil, it just ignores it.
(is (thrown? #?(:cljs :default :default Exception) (keyword "abc" nil))) ; But if name is nil, then we throw.
(is (nil? (keyword nil))) ; (keyword nil) => nil, surprisingly
(is (= :abc (keyword nil "abc"))) ; If ns is nil, we just ignore it.
;; But if name is nil, then maybe we throw or maybe we don't
#?(:cljs nil ; CLJS creates a keyword that isn't
; readable (symbol part is null string: ":abc/")
:default
(is (thrown? Exception (keyword "abc" nil))))

;; Two arg version requires namespace and symbol to be a string, not
;; a symbol or keyword like the one arg version.
(is (thrown? #?(:cljs :default :default Exception) (keyword 'abc "abc")))
(is (thrown? #?(:cljs :default :default Exception) (keyword "abc" 'abc)))
(is (thrown? #?(:cljs :default :default Exception) (keyword :abc "abc")))
(is (thrown? #?(:cljs :default :default Exception) (keyword "abc" :abc)))))
#?@(:cljs
;; IMO, CLJS gets this right
[(is (= :abc/abc (keyword 'abc "abc")))
(is (= :abc/abc (keyword "abc" 'abc)))
(is (= :abc/abc (keyword :abc "abc")))
(is (= :abc/abc (keyword "abc" :abc)))]
:default
;; In Clojure JVM, two arg version requires namespace and
;; symbol to be a string, not a symbol or keyword like the one
;; arg version.
[(is (thrown? Exception (keyword 'abc "abc")))
(is (thrown? Exception (keyword "abc" 'abc)))
(is (thrown? Exception (keyword :abc "abc")))
(is (thrown? Exception (keyword "abc" :abc)))])))
84 changes: 46 additions & 38 deletions test/clojure/core_test/long.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,53 @@
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/long
(deftest test-long
;; There is no platform independent predicate to test specifically
;; for a long. In ClojureJVM, it's an instance of `java.lang.Long`,
;; but there is no predicate for it. Here, we just test whether it's
;; a fixed-length integer of some sort.
(is (int? (int 0)))
#?@(:cljs []
(deftest test-long
;; There is no platform independent predicate to test specifically
;; for a long. In ClojureJVM, it's an instance of `java.lang.Long`,
;; but there is no predicate for it. Here, we just test whether it's
;; a fixed-length integer of some sort.
(is (int? (int 0)))
#?(:cljs nil
:default
[(is (instance? java.lang.Long (long 0)))])
(is (instance? java.lang.Long (long 0))))

;; Check conversions and rounding from other numeric types
(are [expected x] (= expected (long x))
-9223372036854775808 -9223372036854775808
0 0
9223372036854775807 9223372036854775807
1 1N
0 0N
-1 -1N
1 1.0M
0 0.0M
-1 -1.0M
1 1.1
-1 -1.1
1 1.9
#?@(:cljs []
:default
[1 3/2
-1 -3/2
0 1/10
0 -1/10])
1 1.1M
-1 -1.1M)
;; Check conversions and rounding from other numeric types
(are [expected x] (= expected (long x))
-9223372036854775808 -9223372036854775808
0 0
9223372036854775807 9223372036854775807
1 1N
0 0N
-1 -1N
1 1.0M
0 0.0M
-1 -1.0M
1 1.1
-1 -1.1
1 1.9
#?@(:cljs []
:default
[1 3/2
-1 -3/2
0 1/10
0 -1/10])
1 1.1M
-1 -1.1M)

;; `long` throws outside the range of 9223372036854775807 ... -9223372036854775808
(is (thrown? #?(:cljs :default :clj Exception) (long -9223372036854775809)))
(is (thrown? #?(:cljs :default :clj Exception) (long 9223372036854775808)))
#?@(:cljs [] ; In CLJS all numbers are really doubles
:default
;; `long` throws outside the range of 9223372036854775807 ... -9223372036854775808
[(is (thrown? #?(:cljs :default :clj Exception) (long -9223372036854775809)))
(is (thrown? #?(:cljs :default :clj Exception) (long 9223372036854775808)))])

;; Check handling of other types
(is (thrown? #?(:cljs :default :clj Exception) (long "0")))
(is (thrown? #?(:cljs :default :clj Exception) (long :0)))
(is (thrown? #?(:cljs :default :clj Exception) (long [0])))
(is (thrown? #?(:cljs :default :clj Exception) (long nil)))))
;; Check handling of other types
#?@(:cljs
[(is (= 0 (long "0"))) ; JavaScript peeking through?
(is (NaN? (long :0)))
(is (NaN? (long [0])))
(is (= 0 (long nil)))] ; Hm. Interesting.
:default
[(is (thrown? Exception (long "0")))
(is (thrown? Exception (long :0)))
(is (thrown? Exception (long [0])))
(is (thrown? Exception (long nil)))])))
27 changes: 19 additions & 8 deletions test/clojure/core_test/max.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@
(is (= 5 (max 1 2 3 4 5 ##-Inf)))
(is (= ##Inf (max 1 2 3 4 5 ##Inf)))

(is (NaN? (max ##NaN 1)))
(is (NaN? (max 1 ##NaN)))
(is (NaN? (max 1 2 3 4 ##NaN)))
(is (NaN? (max ##-Inf ##NaN ##Inf)))
(is (NaN? (max ##NaN)))
#?@(:cljs
[(is (= 1 (max ##NaN 1))) ; Bug?
(is (NaN? (max 1 ##NaN))) ; Works as in JVM
(is (NaN? (max 1 2 3 4 ##NaN)))
(is (= ##Inf (max ##-Inf ##NaN ##Inf))) ; Bug?
(is (NaN? (max ##NaN)))

(is (thrown? #?(:cljs :default :clj Exception) (max "x" "y")))
(is (thrown? #?(:cljs :default :clj Exception) (max nil 1)))
(is (thrown? #?(:cljs :default :clj Exception) (max 1 nil)))))
(is (= "y" (max "x" "y"))) ; In CLJS "x" and "y" are characters
(is (= 1 (max nil 1)))
(is (= 1 (max 1 nil)))]
:default
[(is (NaN? (max ##NaN 1)))
(is (NaN? (max 1 ##NaN)))
(is (NaN? (max 1 2 3 4 ##NaN)))
(is (NaN? (max ##-Inf ##NaN ##Inf)))
(is (NaN? (max ##NaN)))

(is (thrown? Exception (max "x" "y")))
(is (thrown? Exception (max nil 1)))
(is (thrown? Exception (max 1 nil)))])))
27 changes: 19 additions & 8 deletions test/clojure/core_test/min.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@
(is (= ##-Inf (min 1 2 3 4 5 ##-Inf)))
(is (= 1 (min 1 2 3 4 5 ##Inf)))

(is (NaN? (min ##NaN 1)))
(is (NaN? (min 1 ##NaN)))
(is (NaN? (min 1 2 3 4 ##NaN)))
(is (NaN? (min ##-Inf ##NaN ##Inf)))
(is (NaN? (min ##NaN)))
#?@(:cljs
[(is (= 1 (min ##NaN 1))) ; Bug?
(is (NaN? (min 1 ##NaN)))
(is (NaN? (min 1 2 3 4 ##NaN)))
(is (= ##Inf (min ##-Inf ##NaN ##Inf))) ; Bug?
(is (NaN? (min ##NaN)))

(is (thrown? #?(:cljs :default :clj Exception) (min "x" "y")))
(is (thrown? #?(:cljs :default :clj Exception) (min nil 1)))
(is (thrown? #?(:cljs :default :clj Exception) (min 1 nil)))))
(is (= "x" (min "x" "y")))
(is (nil? (min nil 1))) ; nil acts like zero
(is (nil? (min 1 nil)))]
:default
[(is (NaN? (min ##NaN 1)))
(is (NaN? (min 1 ##NaN)))
(is (NaN? (min 1 2 3 4 ##NaN)))
(is (NaN? (min ##-Inf ##NaN ##Inf)))
(is (NaN? (min ##NaN)))

(is (thrown? #?(:cljs :default :clj Exception) (min "x" "y")))
(is (thrown? #?(:cljs :default :clj Exception) (min nil 1)))
(is (thrown? #?(:cljs :default :clj Exception) (min 1 nil)))])))
Loading
Loading