Skip to content

Commit 5b5eaf5

Browse files
rauhsdnolen
authored and
dnolen
committed
CLJS-2147: apply test suit
1 parent f81c8bb commit 5b5eaf5

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

src/test/cljs/cljs/apply_test.cljs

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
9+
(ns cljs.apply-test
10+
(:require [clojure.test :refer [deftest is]]))
11+
12+
(defn fn-returning-this
13+
[x]
14+
(this-as this
15+
this))
16+
17+
(deftest js-fns-test
18+
(is (= 1 (apply js/parseInt ["1"])))
19+
(is (= 1 (apply js/parseInt "1" nil)))
20+
(is (= 1 (apply js/parseInt "1" [])))
21+
(is (= 15 (apply js/parseInt "F" [16])))
22+
(is (identical? fn-returning-this (apply fn-returning-this [0]))
23+
"apply should supply the this object to be the function itself"))
24+
25+
(deftest data-structures-test
26+
(is (= 1 (apply #{1} [1])))
27+
(is (= nil (apply #{1} [2])))
28+
(is (= 1 (apply #{1} 1 [2])))
29+
(is (= 2 (apply #{} 1 [2])))
30+
(is (thrown? js/Error (apply #{} []))
31+
"We should still get wrong arity errors"))
32+
33+
(def meta-f (with-meta (fn [& a] a) {}))
34+
35+
;; more data structure test:
36+
(deftest meta-fn-test
37+
(is (= nil (apply meta-f [])))
38+
(is (= '(1)) (apply meta-f [1]))
39+
(is (= '(1)) (apply meta-f 1 []))
40+
(is (= '(1 2)) (apply meta-f 1 2 []))
41+
(is (= '(1 2 3)) (apply meta-f 1 2 3 []))
42+
(is (= '(1 2 3 4)) (apply meta-f 1 2 3 4 []))
43+
(is (= '(1 2 3 4 5)) (apply meta-f 1 2 3 4 5 []))
44+
(is (= (range 1 8)) (apply meta-f 1 2 3 4 5 [6 7]))
45+
;; Currently: 20 is not seqable :(
46+
#_(is (= (range 21) (apply meta-f (range 21)))
47+
"Should properly call the last IFn arity with 20 args with last being a seq")
48+
;; Currently: Tries to call arity 21. Fault at .apply of the deftype proto
49+
;; Though, it could probably also be caught right by apply
50+
#_(is (= (range 22) (apply meta-f (range 22)))
51+
"Should properly call the last IFn arity with 20 args with last being a seq")
52+
#_(is (= (range 22) (.apply meta-f nil (to-array (range 22))))
53+
".apply should also handle >20 arguments"))
54+
55+
(deftest multi-arity-test
56+
(is (= 2 (apply (fn ([a] a) ([a b] b)) 1 [2])))
57+
(is (= 1 (apply (fn ([a] a) ([a b] b)) 1 [])))
58+
(is (= 1 (apply (fn ([a] a) ([a b] b)) 1 nil)))
59+
(is (thrown? js/Error (apply (fn ([a] a) ([a b] b)) 1 2 3 nil)))
60+
(is (thrown? js/Error (apply (fn ([a b] a)
61+
([a b c] a)) [1]))))
62+
63+
(deftest single-arity-variadic-test
64+
(doseq [f [(fn [& r] r)
65+
(fn [a & r] (list* a r))
66+
(fn [a b & r] (list* a b r))
67+
(fn [a b c & r] (list* a b c r))
68+
(fn [a b c d & r] (list* a b c d r))
69+
(fn [a b c d e & r] (list* a b c d e r))]]
70+
(is (= (range 10) (apply f (range 10))))
71+
(is (= (range 10) (apply f 0 (range 1 10))))
72+
(is (= (range 10) (apply f 0 1 (range 2 10))))
73+
(is (= (range 10) (apply f 0 1 2 (range 3 10))))
74+
(is (= (range 10) (apply f 0 1 2 3 (range 4 10))))
75+
(is (= (range 10) (apply f 0 1 2 3 4 (range 5 10))))
76+
(is (= (range 10) (apply f 0 1 2 3 4 5 (range 6 10)))))
77+
(is (nil? (apply (fn [a & b] b) [1]))
78+
"rest should stay nil")
79+
(is (nil? (apply (fn [a & b] b) 1 []))
80+
"rest should be nil'd")
81+
(is (= '(2) (apply (fn [a & b] b) 1 [2]))
82+
"rest should be nil'd")
83+
(is (= (range 30)
84+
(apply (fn [_ _ _ _ _ a b c d e _ _ _ _ _ f g h i j k _ _ _ _ _ & R]
85+
(let [a (array)]
86+
(copy-arguments a)
87+
(concat (.slice a 0 26) R)))
88+
(range 30)))
89+
"Variadic function are not limited to 20 params"))
90+
91+
(deftest multi-arity-variadic-test
92+
(doseq [f [(fn ([]) ([& r] r))
93+
(fn ([a]) ([a & r] (list* a r)))
94+
(fn ([a]) ([a b & r] (list* a b r)))
95+
(fn ([a]) ([a b c & r] (list* a b c r)))
96+
(fn ([a]) ([a b c d & r] (list* a b c d r)))
97+
(fn ([a]) ([a b c d e & r] (list* a b c d e r)))]]
98+
(is (= (range 10) (apply f (range 10))))
99+
(is (= (range 10) (apply f 0 (range 1 10))))
100+
(is (= (range 10) (apply f 0 1 (range 2 10))))
101+
(is (= (range 10) (apply f 0 1 2 (range 3 10))))
102+
(is (= (range 10) (apply f 0 1 2 3 (range 4 10))))
103+
(is (= (range 10) (apply f 0 1 2 3 4 (range 5 10))))
104+
(is (= (range 10) (apply f 0 1 2 3 4 5 (range 6 10)))))
105+
(is (= 1 (apply (fn ([a] a) ([a & b] b)) [1])))
106+
(is (= '(2) (apply (fn ([a] a) ([a & b] b)) 1 [2])))
107+
(is (= 1 (apply (fn ([a] a) ([a & b] b)) 1 [])))
108+
(is (= 1 (apply (fn ([a] a) ([a & b] b)) [1])))
109+
(is (= '(2 3 4 5 6) (apply (fn ([a] a) ([a & b] b)) 1 2 3 4 5 [6])))
110+
(is (= '(2 3 4 5) (apply (fn ([a] a) ([a & b] b)) 1 2 3 4 5 [])))
111+
(is (= (range 30)
112+
(apply (fn ([a])
113+
([_ _ _ _ _ a b c d e _ _ _ _ _ f g h i j k _ _ _ _ _ & R]
114+
(let [a (array)]
115+
(copy-arguments a)
116+
(concat (.slice a 0 26) R))))
117+
(range 30)))
118+
"Variadic function are not limited to 20 params"))
119+
120+
(deftest incorrect-invokes-m-arity-test
121+
;; This is the fault of .call currently not throwing. Can't be caught by apply:
122+
#_(is (thrown? js/Error
123+
(apply (fn ([a b] a)
124+
([a b & c] a)) [1]))
125+
"Apply should throw on wrong arity."))
126+
127+
(deftest incorrect-invokes-dispatcher-test
128+
;; The dispatcher needs to look at arguments.length:
129+
#_(is (thrown? js/Error (.call (fn [a b & b] a) nil 1))
130+
"Dispatcher should complain about wrong arity"))

src/test/cljs/test_runner.cljs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
(ns test-runner
1010
(:require [cljs.test :refer-macros [run-tests]]
11+
[cljs.apply-test]
1112
[cljs.primitives-test]
1213
[cljs.destructuring-test]
1314
[cljs.new-new-test]
@@ -51,6 +52,7 @@
5152
(set-print-fn! js/print)
5253

5354
(run-tests
55+
'cljs.apply-test
5456
'cljs.primitives-test
5557
'cljs.destructuring-test
5658
'cljs.new-new-test

0 commit comments

Comments
 (0)