Skip to content

Commit de4f7da

Browse files
authored
Merge pull request #39 from dgr/dgr-cljs-fix-compare
Make tests in `compare` more resilient and fix CLJS crash
2 parents 0dbba45 + 92c95e4 commit de4f7da

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

test/clojure/core_test/compare.cljc

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,52 @@
55
(when-var-exists clojure.core/compare
66
(deftest test-compare
77
(testing "numeric-types"
8-
(are [r args] (= r (compare (first args) (second args)))
9-
-1 [0 10]
10-
0 [0 0]
11-
1 [0 -100N]
12-
0 [1 1.0]
8+
(are [pred args] (pred (compare (first args) (second args)))
9+
neg? [0 10]
10+
zero? [0 0]
11+
pos? [0 -100N]
12+
zero? [1 1.0]
1313
#?@(:cljs []
1414
:default
15-
[-1 [1 100/3]])
16-
-1 [0 0x01]
17-
-1 [0 2r01]
18-
1 [1 nil])
15+
[neg? [1 100/3]])
16+
neg? [0 0x01]
17+
neg? [0 2r01]
18+
pos? [1 nil])
1919

2020
(is (thrown? #?(:cljs :default :clj Exception) (compare 1 []))))
2121

2222
(testing "lexical-types"
23-
(are [r args] (= r (compare (first args) (second args)))
24-
-1 [\a \b]
25-
0 [\0 \0]
26-
25 [\z \a]
27-
-1 ["cat" "dog"]
28-
-1 ['cat 'dog]
29-
-1 [:cat :dog]
30-
0 [:dog :dog]
31-
-1 [:cat :animal/cat]
32-
1 ['a nil])
23+
(are [pred args] (pred (compare (first args) (second args)))
24+
neg? [\a \b]
25+
zero? [\0 \0]
26+
pos? [\z \a]
27+
neg? ["cat" "dog"]
28+
neg? ['cat 'dog]
29+
neg? [:cat :dog]
30+
zero? [:dog :dog]
31+
neg? [:cat :animal/cat]
32+
pos? ['a nil])
3333

3434
(is (thrown? #?(:cljs :default :clj Exception) (compare "a" [])))
3535
(is (thrown? #?(:cljs :default :clj Exception) (compare "cat" '(\c \a \t)))))
3636

3737
(testing "collection-types"
38-
(are [r args] (= r (compare (first args) (second args)))
39-
0 [[] []]
40-
1 [[3] [1]]
41-
-1 [[] [1 2]]
42-
-1 [[] [[]]]
43-
0 [#{} #{}]
44-
0 [{} {}]
45-
0 [(array-map) (array-map)]
46-
0 [(hash-map) (hash-map)]
47-
0 [{} (hash-map)]
48-
0 [{} (array-map)]
49-
0 ['() '()]
50-
1 [[] nil])
38+
(are [pred args] (pred (compare (first args) (second args)))
39+
zero? [[] []]
40+
pos? [[3] [1]]
41+
neg? [[] [1 2]]
42+
neg? [[] [[]]]
43+
;; Sets, maps, and lists don't implement java.lang.Comparable,
44+
;; so just comment these out for now. TODO: make decision as to
45+
;; what we want to do with these tests.
46+
;; zero? [#{} #{}]
47+
;; zero? [{} {}]
48+
;; zero? [(array-map) (array-map)]
49+
;; zero? [(hash-map) (hash-map)]
50+
;; zero? [{} (hash-map)]
51+
;; zero? [{} (array-map)]
52+
;; zero? ['() '()]
53+
pos? [[] nil])
5154

5255
(is (thrown? #?(:cljs :default :clj Exception) (compare [] '())))
5356
(is (thrown? #?(:cljs :default :clj Exception) (compare [1] [[]])))
@@ -57,4 +60,5 @@
5760
(is (thrown? #?(:cljs :default :clj Exception) (compare #{1} #{1})))
5861
(is (thrown? #?(:cljs :default :clj Exception) (compare {1 2} {1 2})))
5962
(is (thrown? #?(:cljs :default :clj Exception) (compare (range 5) (range 5))))
60-
(is (thrown? #?(:cljs :default :clj Exception) (compare (range 5) (range)))))))
63+
;; Clojurescript goes into an infinite loop of some sort when compiling this.
64+
#_(is (thrown? #?(:cljs :default :clj Exception) (compare (range 5) (range)))))))

0 commit comments

Comments
 (0)