Skip to content

Commit dc849ed

Browse files
authored
Merge pull request #48 from dgr/dgr-cleanup-abs-to-intern
Clean up tests for CLJS `abs` to `intern`
2 parents e91d053 + 0d3d679 commit dc849ed

File tree

15 files changed

+374
-281
lines changed

15 files changed

+374
-281
lines changed

test/clojure/core_test/abs.cljc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns clojure.core-test.abs
22
(:require [clojure.test :as t :refer [deftest testing is are]]
3+
[clojure.core-test.number-range :as r]
34
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
45

56
(when-var-exists clojure.core/abs
@@ -8,7 +9,8 @@
89
(are [in ex] (= ex (abs in))
910
-1 1
1011
1 1
11-
Long/MIN_VALUE Long/MIN_VALUE ; Special case!
12+
(inc r/min-int) (- (inc r/min-int))
13+
#?@(:clj [r/min-int r/min-int]) ; fixed int 2's complement oddity
1214
-1.0 1.0
1315
-0.0 0.0
1416
##-Inf ##Inf
@@ -19,7 +21,8 @@
1921
:default
2022
[-1/5 1/5]))
2123
(is (NaN? (abs ##NaN)))
22-
(is (thrown? #?(:cljs :default :clj Exception) (abs nil))))
24+
#?(:cljs (is (zero? (abs nil)))
25+
:default(is (thrown? Exception (abs nil)))))
2326

2427
(testing "unboxed"
2528
(let [a 42

test/clojure/core_test/binding.cljc

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,60 @@
1010
(defn test-fn [] *x*)
1111

1212
(t/deftest test-binding
13-
;; base-case with no overrides
14-
(t/is (= *x* :unset) "Unset is :unset")
15-
(t/is (= (*f* 1) 2) "fn call")
13+
;; base-case with no overrides
14+
(t/is (= *x* :unset) "Unset is :unset")
15+
(t/is (= (*f* 1) 2) "fn call")
1616

17-
;; common cases
18-
(t/is (binding [*x* :set] (= *x* :set)) "Can bind dynamic var.")
19-
(t/is (binding [*x* :set] (= (test-fn) :set)) "Binding for indirect reference.")
20-
(t/is (binding [*x* nil] (= (test-fn) nil)) "Dynamic vars are nullable.")
21-
(t/is (binding [*f* dec] (= (*f* 1) 0)) "Can bind functions.")
17+
;; common cases
18+
(t/is (binding [*x* :set] (= *x* :set)) "Can bind dynamic var.")
19+
(t/is (binding [*x* :set] (= (test-fn) :set)) "Binding for indirect reference.")
20+
(t/is (binding [*x* nil] (= (test-fn) nil)) "Dynamic vars are nullable.")
21+
(t/is (binding [*f* dec] (= (*f* 1) 0)) "Can bind functions.")
2222

23-
;; infinite seqs
24-
(binding [*x* (range)]
25-
(t/is (= '(0 1 2 3) (take 4 (test-fn))) "Infinite range")
26-
(t/is (= '(0 1 2 3) (take 4 (test-fn))) "Immutability"))
23+
;; infinite seqs
24+
(binding [*x* (range)]
25+
(t/is (= '(0 1 2 3) (take 4 (test-fn))) "Infinite range")
26+
(t/is (= '(0 1 2 3) (take 4 (test-fn))) "Immutability"))
2727

28-
;; Nested cases
29-
(binding [*x* :first!]
30-
(let [layer-1 (fn [] (test-fn))]
31-
(binding [*x* :second!]
32-
(t/is (= :second! (layer-1) (test-fn)) "Value is determined at call-site"))))
33-
(binding [*y* *x*]
34-
(t/is (= *y* :unset) "Dynamic reference is by value at binding.")
35-
(binding [*x* :layer-2]
36-
(t/is (= *y* :unset) "Dynamic reference does not update."))
37-
(binding [*y* *x*
38-
*x* :set-later]
39-
(t/is (= *y* :unset) "Bind vars are applied in sequence.")))
40-
(let [f (fn [] (binding [*x* :inside-f] (test-fn)))]
41-
(binding [*x* :outside-f]
42-
(t/is (= (test-fn) :outside-f))
43-
(t/is (= (f) :inside-f) "Nested in func-call")))
44-
(binding [*y* (binding [*x* :bad] (test-fn))]
45-
(t/is (= *y* :bad) "Binding in a binding vector"))
28+
;; Nested cases
29+
(binding [*x* :first!]
30+
(let [layer-1 (fn [] (test-fn))]
31+
(binding [*x* :second!]
32+
(t/is (= :second! (layer-1) (test-fn)) "Value is determined at call-site"))))
33+
(binding [*y* *x*]
34+
(t/is (= *y* :unset) "Dynamic reference is by value at binding.")
35+
(binding [*x* :layer-2]
36+
(t/is (= *y* :unset) "Dynamic reference does not update."))
37+
(binding [*y* *x*
38+
*x* :set-later]
39+
(t/is (= *y* :unset) "Bind vars are applied in sequence.")))
40+
(let [f (fn [] (binding [*x* :inside-f] (test-fn)))]
41+
(binding [*x* :outside-f]
42+
(t/is (= (test-fn) :outside-f))
43+
(t/is (= (f) :inside-f) "Nested in func-call")))
44+
(binding [*y* (binding [*x* :bad] (test-fn))]
45+
(t/is (= *y* :bad) "Binding in a binding vector"))
4646

47-
;; Threading/future/delay cases
48-
(let [f (delay (test-fn))]
49-
(binding [*x* :here]
50-
(t/is (= @f :here) "Delayed functions inherit there bindings when forced"))
51-
(t/is (= @f :here) "And value persists outside binding expression"))
52-
(let [f (future (test-fn))]
53-
(binding [*x* :now-here]
54-
(t/is (= @f :unset) "Thread context is separate from joining thread")))
55-
(binding [*x* :outer]
56-
(let [f (future (test-fn))]
57-
(binding [*x* :inner]
58-
(t/is (= @f :outer) "Thread context preserves binding context."))))
59-
(binding [*x* :caller]
60-
(let [f (future
61-
(binding [*x* :callee]
62-
(future (test-fn))))]
63-
(binding [*x* :derefer]
64-
(let [derefed-f @f]
65-
(t/is (= :callee @derefed-f) "Binding in futures preserved.")))))))
47+
;; Threading/future/delay cases
48+
(let [f (delay (test-fn))]
49+
(binding [*x* :here]
50+
(t/is (= @f :here) "Delayed functions inherit there bindings when forced"))
51+
(t/is (= @f :here) "And value persists outside binding expression"))
52+
53+
;; CLJS doesn't have futures
54+
#?@(:cljs []
55+
:default
56+
[(let [f (future (test-fn))]
57+
(binding [*x* :now-here]
58+
(t/is (= @f :unset) "Thread context is separate from joining thread")))
59+
(binding [*x* :outer]
60+
(let [f (future (test-fn))]
61+
(binding [*x* :inner]
62+
(t/is (= @f :outer) "Thread context preserves binding context."))))
63+
(binding [*x* :caller]
64+
(let [f (future
65+
(binding [*x* :callee]
66+
(future (test-fn))))]
67+
(binding [*x* :derefer]
68+
(let [derefed-f @f]
69+
(t/is (= :callee @derefed-f) "Binding in futures preserved.")))))])))

test/clojure/core_test/bit_and.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
8 8 0xff
1616
0 r/all-ones-int 0
1717
0 0 r/all-ones-int
18-
r/all-ones-int r/all-ones-int r/all-ones-int
18+
#?(:cljs -1 :default r/all-ones-int) r/all-ones-int r/all-ones-int
1919
0 r/full-width-checker-pos 0
2020
r/full-width-checker-pos r/full-width-checker-pos r/full-width-checker-pos
2121
r/full-width-checker-pos r/full-width-checker-pos r/all-ones-int

test/clojure/core_test/bit_and_not.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
8 12 4
1616
0xff 0xff 0
1717
0x80 0xff 0x7f
18-
r/all-ones-int r/all-ones-int 0
18+
#?(:cljs -1 :default r/all-ones-int) r/all-ones-int 0
1919
0 0 r/all-ones-int
2020
0 r/all-ones-int r/all-ones-int
2121
r/full-width-checker-pos r/full-width-checker-pos 0

test/clojure/core_test/bit_or.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
(are [ex a b] (= ex (bit-or a b))
1414
2r1101 2r1100 2r1001
1515
1 1 0
16-
r/all-ones-int r/all-ones-int 0
17-
r/all-ones-int 0 r/all-ones-int
18-
r/all-ones-int r/all-ones-int r/all-ones-int
16+
#?(:cljs -1 :default r/all-ones-int) r/all-ones-int 0
17+
#?(:cljs -1 :default r/all-ones-int) 0 r/all-ones-int
18+
#?(:cljs -1 :default r/all-ones-int) r/all-ones-int r/all-ones-int
1919
r/full-width-checker-pos r/full-width-checker-pos 0
2020
r/full-width-checker-pos r/full-width-checker-pos r/full-width-checker-pos
21-
r/all-ones-int r/full-width-checker-pos r/all-ones-int
22-
r/all-ones-int r/full-width-checker-pos r/full-width-checker-neg)))
21+
#?(:cljs -1 :default r/all-ones-int) r/full-width-checker-pos r/all-ones-int
22+
#?(:cljs -1 :default r/all-ones-int) r/full-width-checker-pos r/full-width-checker-neg)))
2323

test/clojure/core_test/bit_set.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
(are [ex a b] (= ex (bit-set a b))
1313
2r1111 2r1011 2
14-
-9223372036854775808 0 63
15-
4294967296 0 32
14+
#?@(:cljs [] :default [-9223372036854775808 0 63])
15+
#?@(:cljs [] :default [4294967296 0 32])
1616
65536 0 16
1717
256 0 8
1818
16 0 4)))

test/clojure/core_test/bit_test.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
(when-var-exists clojure.core/bit-test
66
(deftest test-bit-test
77
#?(:clj (is (thrown? Exception (bit-test nil 1)))
8-
:cljs (is (bit-test nil 1)))
8+
:cljs (is (= false (bit-test nil 1))))
99
#?(:clj (is (thrown? Exception (bit-test 1 nil)))
10-
:cljs (is (bit-test 1 nil)))
10+
:cljs (is (= true (bit-test 1 nil))))
1111

1212
(are [ex a b] (= ex (bit-test a b))
1313
true 2r1001 0

test/clojure/core_test/bit_xor.cljc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
(are [ex a b] (= ex (bit-xor a b))
1414
2r0101 2r1100 2r1001
15-
r/all-ones-int r/all-ones-int 0
16-
r/all-ones-int 0 r/all-ones-int
15+
#?(:cljs -1 :default r/all-ones-int) r/all-ones-int 0
16+
#?(:cljs -1 :default r/all-ones-int) 0 r/all-ones-int
1717
0 r/all-ones-int r/all-ones-int
1818
r/full-width-checker-pos r/full-width-checker-pos 0
1919
0 r/full-width-checker-pos r/full-width-checker-pos
20-
r/full-width-checker-neg r/full-width-checker-pos r/all-ones-int
21-
r/all-ones-int r/full-width-checker-pos r/full-width-checker-neg)))
20+
#?(:cljs -1431655766 :default r/full-width-checker-neg) r/full-width-checker-pos r/all-ones-int
21+
#?(:cljs -1 :default r/all-ones-int) r/full-width-checker-pos r/full-width-checker-neg)))

test/clojure/core_test/byte.cljc

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,65 @@
33
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
44

55
(when-var-exists clojure.core/byte
6-
(deftest test-byte
7-
;; There is no platform independent predicate to test for a
8-
;; byte (e.g., `byte?`). In ClojureJVM, it's an instance of
9-
;; `java.lang.Byte`, but there is no predicate for it. Here, we just
10-
;; test whether it's a fixed-length integer of some sort.
11-
(is (int? (byte 0)))
12-
#?@(:cljs []
13-
:default
14-
[(is (instance? java.lang.Byte (byte 0)))])
6+
(deftest test-byte
7+
;; There is no platform independent predicate to test for a
8+
;; byte (e.g., `byte?`). In ClojureJVM, it's an instance of
9+
;; `java.lang.Byte`, but there is no predicate for it. Here, we just
10+
;; test whether it's a fixed-length integer of some sort.
11+
(is (int? (byte 0)))
12+
#?@(:cljs []
13+
:default
14+
[(is (instance? java.lang.Byte (byte 0)))])
1515

16-
;; Check conversions and rounding from other numeric types
17-
(are [expected x] (= expected (byte x))
18-
-128 -128
19-
0 0
20-
127 127
21-
1 1N
22-
0 0N
23-
-1 -1N
24-
1 1.0M
25-
0 0.0M
26-
-1 -1.0M
27-
1 1.1
28-
-1 -1.1
29-
1 1.9
30-
#?@(:cljs []
31-
:default
32-
[1 3/2
33-
-1 -3/2
34-
0 1/10
35-
0 -1/10])
36-
1 1.1M
37-
-1 -1.1M)
16+
;; Check conversions and rounding from other numeric types
17+
(are [expected x] (= expected (byte x))
18+
-128 -128
19+
0 0
20+
127 127
21+
1 1N
22+
0 0N
23+
-1 -1N
24+
1 1.0M
25+
0 0.0M
26+
-1 -1.0M
27+
;; Clojurescript `byte` is a "dummy cast" which doesn't do
28+
;; anything (no-op). Thus, there is no conversion, no truncation
29+
;; of decimal values, etc.
30+
#?@(:cljs
31+
[1.1 1.1
32+
-1.1 -1.1
33+
1.9 1.9
34+
1.1 1.1M
35+
-1.1 -1.1M]
36+
:default
37+
[1 1.1
38+
-1 -1.1
39+
1 1.9
40+
1 3/2
41+
-1 -3/2
42+
0 1/10
43+
0 -1/10
44+
1 1.1M
45+
-1 -1.1M]))
3846

39-
;; `byte` throws outside the range of 127 ... -128.
40-
(is (thrown? #?(:cljs :default :clj Exception) (byte -128.000001)))
41-
(is (thrown? #?(:cljs :default :clj Exception) (byte -129)))
42-
(is (thrown? #?(:cljs :default :clj Exception) (byte 128)))
43-
(is (thrown? #?(:cljs :default :clj Exception) (byte 127.000001)))
44-
45-
;; Check handling of other types
46-
(is (thrown? #?(:cljs :default :clj Exception) (byte "0")))
47-
(is (thrown? #?(:cljs :default :clj Exception) (byte :0)))
48-
(is (thrown? #?(:cljs :default :clj Exception) (byte [0])))
49-
(is (thrown? #?(:cljs :default :clj Exception) (byte nil)))))
47+
#?@(:cljs
48+
[ ;; ClojureScript `byte` just returns its argument
49+
(is (= -128.5 (byte -128.5)))
50+
(is (= -129 (byte -129)))
51+
(is (= 128(byte 128)))
52+
(is (= 127.5 (byte 127.5)))
53+
(is (= "0" (byte "0")))
54+
(is (= :0 (byte :0)))
55+
(is (= [0] (byte [0])))
56+
(is (= nil (byte nil)))]
57+
:default
58+
[ ;; `byte` throws outside the range of 127 ... -128.
59+
(is (thrown? Exception (byte -128.000001)))
60+
(is (thrown? Exception (byte -129)))
61+
(is (thrown? Exception (byte 128)))
62+
(is (thrown? Exception (byte 127.000001)))
63+
;; Check handling of other types
64+
(is (thrown? Exception (byte "0")))
65+
(is (thrown? Exception (byte :0)))
66+
(is (thrown? Exception (byte [0])))
67+
(is (thrown? Exception (byte nil)))])))

test/clojure/core_test/char.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
;; TODO: Add Unicode tests
1414
)
1515

16-
#?(:default (is (thrown? Exception (char -1)))) ; TODO: include CLJS test. CLJS returns string with just NULL byte
16+
#?(:cljs nil :default (is (thrown? Exception (char -1))))
1717
(is (thrown? #?(:cljs :default :default Exception) (char nil)))))

0 commit comments

Comments
 (0)