|
| 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 | + |
1 | 9 | (ns cljs.core-test
|
2 | 10 | (:refer-clojure :exclude [iter])
|
3 | 11 | (:require [cljs.test :refer-macros [deftest testing is]]
|
4 | 12 | [clojure.string :as s]
|
5 | 13 | [clojure.set :as set]))
|
6 | 14 |
|
7 |
| -(deftest test-js-primitives |
8 |
| - ;; js primitives |
9 |
| - (let [keys #(vec (js-keys %))] |
10 |
| - (testing "Testing js primitives" |
11 |
| - (is (= [] (keys (js-obj)) (keys (apply js-obj [])))) |
12 |
| - (is (= ["x"] (keys (js-obj "x" "y")) (keys (apply js-obj ["x" "y"]))))))) |
13 |
| - |
14 |
| -(deftest test-equiv |
15 |
| - (testing "Testing -equiv" |
16 |
| - (is (= 1)) |
17 |
| - (is (= 1 1)) |
18 |
| - (is (= 1 1 1)) |
19 |
| - (is (= 1 1 1 1)) |
20 |
| - (is (not (= 1 2))) |
21 |
| - (is (not (= 1 2 1))) |
22 |
| - (is (not (= 1 1 2))) |
23 |
| - (is (not (= 1 1 2 1))) |
24 |
| - (is (not (= 1 1 1 2))))) |
25 |
| - |
26 |
| -(deftest test-arithmetic |
27 |
| - (testing "Testing addition" |
28 |
| - (is (= (+) 0)) |
29 |
| - (is (= (apply + []) 0)) |
30 |
| - (is (= (+ 1) 1)) |
31 |
| - (is (= (apply + [1]) 1)) |
32 |
| - (is (= (+ 1 1) 2)) |
33 |
| - (is (= (apply + [1 1]) 2)) |
34 |
| - (is (= (+ 1 2 3) 6)) |
35 |
| - (is (= (apply + [1 2 3]) 6))) |
36 |
| - |
37 |
| - (testing "Testing subtraction" |
38 |
| - (is (= (- 1) -1)) |
39 |
| - (is (= (apply - [1]) -1)) |
40 |
| - (is (= (- 1 1) 0)) |
41 |
| - (is (= (apply - [1 1]) 0)) |
42 |
| - (is (= (- 3 2 1) 0)) |
43 |
| - (is (= (apply - [3 2 1]) 0))) |
44 |
| - |
45 |
| - (testing "Testing multiplication" |
46 |
| - (is (= (*) 1)) |
47 |
| - (is (= (apply * []) 1)) |
48 |
| - (is (= (* 2) 2)) |
49 |
| - (is (= (apply * [2]) 2)) |
50 |
| - (is (= (* 2 3) 6)) |
51 |
| - (is (= (apply * [2 3]) 6))) |
52 |
| - |
53 |
| - (testing "Testing division" |
54 |
| - (is (= (/ 2) 0.5)) |
55 |
| - (is (= (apply / [2]) 0.5)) |
56 |
| - (is (= (/ 6 2) 3)) |
57 |
| - (is (= (apply / [6 2]) 3)) |
58 |
| - (is (= (/ 6 3 2) 1)) |
59 |
| - (is (= (apply / [6 3 2]) 1))) |
60 |
| - |
61 |
| - (testing "Testing less than" |
62 |
| - (is (= (< 1) true)) |
63 |
| - (is (= (apply < [1]) true)) |
64 |
| - (is (= (< 1 2) true)) |
65 |
| - (is (= (apply < [1 2]) true)) |
66 |
| - (is (= (< 1 1) false)) |
67 |
| - (is (= (apply < [1 1]) false)) |
68 |
| - (is (= (< 2 1) false)) |
69 |
| - (is (= (apply < [2 1]) false)) |
70 |
| - (is (= (< 1 2 3) true)) |
71 |
| - (is (= (apply < [1 2 3]) true)) |
72 |
| - (is (= (< 1 1 3) false)) |
73 |
| - (is (= (apply < [1 1 3]) false)) |
74 |
| - (is (= (< 3 1 1) false)) |
75 |
| - (is (= (apply < [3 1 1]) false))) |
76 |
| - |
77 |
| - (testing "Testing less than or equal to" |
78 |
| - (is (= (<= 1) true)) |
79 |
| - (is (= (apply <= [1]) true)) |
80 |
| - (is (= (<= 1 1) true)) |
81 |
| - (is (= (apply <= [1 1]) true)) |
82 |
| - (is (= (<= 1 2) true)) |
83 |
| - (is (= (apply <= [1 2]) true)) |
84 |
| - (is (= (<= 2 1) false)) |
85 |
| - (is (= (apply <= [2 1]) false)) |
86 |
| - (is (= (<= 1 2 3) true)) |
87 |
| - (is (= (apply <= [1 2 3]) true)) |
88 |
| - (is (= (<= 1 1 3) true)) |
89 |
| - (is (= (apply <= [1 1 3]) true)) |
90 |
| - (is (= (<= 3 1 1) false)) |
91 |
| - (is (= (apply <= [3 1 1]) false))) |
92 |
| - |
93 |
| - (testing "Testing greater than" |
94 |
| - (is (= (> 1) true)) |
95 |
| - (is (= (apply > [1]) true)) |
96 |
| - (is (= (> 2 1) true)) |
97 |
| - (is (= (apply > [2 1]) true)) |
98 |
| - (is (= (> 1 1) false)) |
99 |
| - (is (= (apply > [1 1]) false)) |
100 |
| - (is (= (> 1 2) false)) |
101 |
| - (is (= (apply > [1 2]) false)) |
102 |
| - (is (= (> 3 2 1) true)) |
103 |
| - (is (= (apply > [3 2 1]) true)) |
104 |
| - (is (= (> 3 1 1) false)) |
105 |
| - (is (= (apply > [3 1 1]) false)) |
106 |
| - (is (= (> 1 1 3) false)) |
107 |
| - (is (= (apply > [1 1 3]) false))) |
108 |
| - |
109 |
| - (testing "Testing greater than or equal to" |
110 |
| - (is (= (>= 1) true)) |
111 |
| - (is (= (apply >= [1]) true)) |
112 |
| - (is (= (>= 2 1) true)) |
113 |
| - (is (= (apply >= [2 1]) true)) |
114 |
| - (is (= (>= 1 1) true)) |
115 |
| - (is (= (apply >= [1 1]) true)) |
116 |
| - (is (= (>= 1 2) false)) |
117 |
| - (is (= (apply >= [1 2]) false)) |
118 |
| - (is (= (>= 3 2 1) true)) |
119 |
| - (is (= (apply >= [3 2 1]) true)) |
120 |
| - (is (= (>= 3 1 1) true)) |
121 |
| - (is (= (apply >= [3 1 1]) true)) |
122 |
| - (is (= (>= 3 1 2) false)) |
123 |
| - (is (= (apply >= [3 1 2]) false)) |
124 |
| - (is (= (>= 1 1 3) false)) |
125 |
| - (is (= (apply >= [1 1 3]) false))) |
126 |
| - |
127 |
| - (testing "Testing dec/inc" |
128 |
| - (is (= (dec 1) 0)) |
129 |
| - (is (= (apply dec [1]) 0)) |
130 |
| - (is (= (inc 0) 1)) |
131 |
| - (is (= (apply inc [0]) 1))) |
132 |
| - |
133 |
| - (testing "Testing zero? pos? neg? even? odd?" |
134 |
| - (is (= (zero? 0) true)) |
135 |
| - (is (= (apply zero? [0]) true)) |
136 |
| - (is (= (zero? 1) false)) |
137 |
| - (is (= (apply zero? [1]) false)) |
138 |
| - (is (= (zero? -11) false)) |
139 |
| - (is (= (apply zero? [-11]) false)) |
140 |
| - (is (= (pos? 0) false)) |
141 |
| - (is (= (apply pos? [0]) false)) |
142 |
| - (is (= (pos? 1) true)) |
143 |
| - (is (= (apply pos? [1]) true)) |
144 |
| - (is (= (pos? -1) false)) |
145 |
| - (is (= (apply pos? [-1]) false)) |
146 |
| - (is (= (neg? -1) true)) |
147 |
| - (is (= (apply neg? [-1]) true)) |
148 |
| - (is (neg? -1)) |
149 |
| - (is (not (neg? 1))) |
150 |
| - (is (neg? -1.765)) |
151 |
| - (is (not (neg? 0))) |
152 |
| - (is (= [true false true false true false true false] |
153 |
| - (map integer? |
154 |
| - [1 1.00001 0x7e7 [] (- 88 1001991881) :foo 0 "0"]))) |
155 |
| - (is (= [true false true false true false] |
156 |
| - (map odd? [1 2 3 4 -1 0]))) |
157 |
| - (is (= [true false true false true true] |
158 |
| - (map even? [2 3 4 5 -2 0])))) |
159 |
| - |
160 |
| - (testing "Testing max / min" |
161 |
| - (is (= (max 1) 1)) |
162 |
| - (is (= (apply max [1]) 1)) |
163 |
| - (is (= (max 1 2) 2)) |
164 |
| - (is (= (apply max [1 2]) 2)) |
165 |
| - (is (= (max 2 1) 2)) |
166 |
| - (is (= (apply max [2 1]) 2)) |
167 |
| - (is (= (max 1 2 3) 3)) |
168 |
| - (is (= (apply max [1 2 3]) 3)) |
169 |
| - (is (= (max 1 3 2) 3)) |
170 |
| - (is (= (apply max [1 3 2]) 3)) |
171 |
| - |
172 |
| - (is (= (min 1) 1)) |
173 |
| - (is (= (apply min [1]) 1)) |
174 |
| - (is (= (min 1 2) 1)) |
175 |
| - (is (= (apply min [1 2]) 1)) |
176 |
| - (is (= (min 2 1) 1)) |
177 |
| - (is (= (apply min [2 1]) 1)) |
178 |
| - (is (= (min 1 2 3) 1)) |
179 |
| - (is (= (apply min [1 2 3]) 1)) |
180 |
| - (is (= (min 2 1 3) 1)) |
181 |
| - (is (= (apply min [3 1 3]) 1))) |
182 |
| - |
183 |
| - (testing "Testing mod" |
184 |
| - (is (= (mod 4 2) 0)) |
185 |
| - (is (= (apply mod [4 2]) 0)) |
186 |
| - (is (= (mod 3 2) 1)) |
187 |
| - (is (= (apply mod [3 2]) 1)) |
188 |
| - (is (= (mod -2 5) 3))) |
189 |
| - |
190 |
| - (testing "Testing numeric equality in collections" |
191 |
| - (is (= [4 3 2 1 0] |
192 |
| - (loop [i 0 j ()] |
193 |
| - (if (< i 5) |
194 |
| - (recur (inc i) (conj j (fn [] i))) |
195 |
| - (map #(%) j))))) |
196 |
| - (is (= [[1 1] [1 2] [1 3] [2 1] [2 2] [2 3]] |
197 |
| - (map #(%) (for [i [1 2] j [1 2 3]] (fn [] [i j])))))) |
198 |
| - |
199 |
| - (testing "Testing integer? predicate" |
200 |
| - (is (integer? 0)) |
201 |
| - (is (integer? 42)) |
202 |
| - (is (integer? -42)) |
203 |
| - (is (not (integer? ""))) |
204 |
| - (is (not (integer? 1e308))) |
205 |
| - (is (not (integer? js/Infinity))) |
206 |
| - (is (not (integer? (- js/Infinity)))) |
207 |
| - (is (not (integer? js/NaN)))) |
208 |
| - |
209 |
| - (testing "Testing integer coercions" |
210 |
| - (is (= 42 (int 42.5))) |
211 |
| - (is (integer? (int 42.5))) |
212 |
| - (is (= 42 (long 42.5))) |
213 |
| - (is (integer? (long 42.5))) |
214 |
| - (is (= -1 (int -1.5))) |
215 |
| - (is (= -9 (long -9.8)))) |
216 |
| - |
217 |
| - (testing "Testing numeric equality from collection" |
218 |
| - (is (= 2 (:b {:a 1 :b 2}))) |
219 |
| - (is (= 2 ('b '{:a 1 b 2}))) |
220 |
| - (is (= 2 ({:a 1 :b 2} :b))) |
221 |
| - (is (= 2 ({1 1 2 2} 2))) |
222 |
| - (is (= 2 (:a {:b 1} 2))) |
223 |
| - (is (= 2 (:a {} 2))) |
224 |
| - (is (= 2 ({:b 1} :a 2))) |
225 |
| - (is (= 2 ({} :a 2))) |
226 |
| - (is (= nil (:a {}))) |
227 |
| - (is (= nil (:a ""))) |
228 |
| - (is (= 2 (:a "" 2))) |
229 |
| - (is (= 2 (#{1 2 3} 2))) |
230 |
| - (is (= 1 (apply :a '[{:a 1 a 2}]))) |
231 |
| - (is (= 1 (apply 'a '[{a 1 :b 2}]))) |
232 |
| - (is (= 1 (apply {:a 1} [:a]))) |
233 |
| - (is (= 2 (apply {:a 1} [:b 2])))) |
234 |
| - |
235 |
| - (testing "Testing quot" |
236 |
| - (is (= (quot 4 2) 2)) |
237 |
| - (is (= (quot 3 2) 1)) |
238 |
| - (is (= (quot 6 4) 1)) |
239 |
| - (is (= (quot 0 5) 0)) |
240 |
| - (is (= (quot 42 5) 8)) |
241 |
| - (is (= (quot 42 -5) -8)) |
242 |
| - (is (= (quot -42 -5) 8)) |
243 |
| - (is (= (quot 9 3) 3)) |
244 |
| - (is (= (quot 9 -3) -3)) |
245 |
| - (is (= (quot -9 3) -3)) |
246 |
| - (is (= (quot 2 -5) 0)) |
247 |
| - (is (= (quot -2 5) 0)) |
248 |
| - (is (= (quot 0 3) 0)) |
249 |
| - (is (= (quot 0 -3) 0))) |
250 |
| - |
251 |
| - (testing "Testing mod" |
252 |
| - (is (= (mod 4 2) 0)) |
253 |
| - (is (= (mod 3 2) 1)) |
254 |
| - (is (= (mod 6 4) 2)) |
255 |
| - (is (= (mod 0 5) 0)) |
256 |
| - (is (= (mod 4.5 2.0) 0.5)) |
257 |
| - (is (= (mod 42 5) 2)) |
258 |
| - (is (= (mod 9 3) 0)) |
259 |
| - (is (= (mod 9 -3) 0)) |
260 |
| - (is (= (mod -9 3) 0)) |
261 |
| - (is (= (mod -9 -3) 0)) |
262 |
| - (is (= (mod 0 3) 0)) |
263 |
| - (is (= (mod 3216478362187432 432143214) 120355456))) |
264 |
| - |
265 |
| - (testing "Testing rem" |
266 |
| - (is (= (rem 4 2) 0)) |
267 |
| - (is (= (rem 0 5) 0)) |
268 |
| - (is (= (rem 4.5 2.0) 0.5)) |
269 |
| - (is (= (rem 42 5) 2)) |
270 |
| - (is (= (rem 2 5) 2)) |
271 |
| - (is (= (rem 2 -5) 2)) |
272 |
| - (is (= (rem 0 3) 0))) |
273 |
| -) |
274 |
| - |
275 | 15 | (deftest test-hash-null
|
276 | 16 | (is (zero? (hash (aget (js-obj) "foo")))))
|
277 | 17 |
|
278 |
| -;; See |
279 |
| -;; https://github.com/clojure/tools.reader#differences-from-lispreaderjava |
280 |
| -;; about why these tests won't pass. Not clear if we should change the reader |
281 |
| -;; or the test |
282 |
| -;; (assert (= "baz" (name 'foo/bar/baz))) |
283 |
| -;; (assert (= "foo/bar" (namespace 'foo/bar/baz))) |
284 |
| -;; (assert (= "baz" (name :foo/bar/baz))) |
285 |
| -;; (assert (= "foo/bar" (namespace :foo/bar/baz))) |
286 |
| -;; TODO: These next two tests need Clojure 1.5 |
287 |
| -;; (assert (= "foo" (namespace 'foo//))) |
288 |
| -;; (assert (= "/" (name 'foo//))) |
289 |
| - |
290 |
| -(deftest test-symbols-and-keywords |
291 |
| - (testing "Testing name / namespace" |
292 |
| - (is (nil? (namespace '/))) |
293 |
| - (is (= "/" (name '/))) |
294 |
| - (is (= "keyword" (name :keyword)))) |
295 |
| - |
296 |
| - (testing "Testing str on keywords / symbols" |
297 |
| - (is (= ":hello" (str :hello))) |
298 |
| - (is (= "hello" (str 'hello))) |
299 |
| - (is (= "hello:world" (str "hello" :world))) |
300 |
| - (is (= ":helloworld" (str :hello 'world)))) |
301 |
| - |
302 |
| - (testing "Testing symbol ctor is idempotent" |
303 |
| - (is (= 'a (symbol 'a)))) |
304 |
| - |
305 |
| - (testing "Testing constructed division symbol" |
306 |
| - (is (= '/ (symbol "/"))) |
307 |
| - (is (= (namespace '/) (namespace (symbol "/")))) |
308 |
| - (is (= (hash '/) (hash (symbol "/"))))) |
309 |
| - |
310 |
| - (testing "Testing keyword ctor" |
311 |
| - (is (= :a (keyword "a"))) |
312 |
| - (is (= :a (keyword 'a))) |
313 |
| - (is (= :a/b (keyword 'a 'b))) |
314 |
| - (is (= :a (keyword :a)))) |
315 |
| - |
316 |
| - (testing "Testing name munging CLJS-1432" |
317 |
| - (is (not= :$ :.)) |
318 |
| - (is (not= '$ '.)))) |
319 |
| - |
320 | 18 | (deftest test-map-operations
|
321 | 19 | (testing "Test basic map collection operations"
|
322 | 20 | (is (= {:a :b} (get {[1 2 3] {:a :b}, 4 5} [1 2 3])))
|
|
0 commit comments