/
core_test.cljs
1545 lines (1327 loc) · 52 KB
/
core_test.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns cljs.core-test
(:refer-clojure :exclude [iter])
(:require [cljs.test :refer-macros [deftest testing is are]]
[clojure.test.check :as tc]
[clojure.test.check.clojure-test :refer-macros [defspec]]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop :include-macros true]
[clojure.string :as s]
[clojure.set :as set]
[goog.object :as gobject]))
(deftest test-metadata
(testing "Testing metadata"
(is (= {"x" "y"} (meta ^{"x" "y"} [])))
))
(deftest test-fn-with-metadata
(let [f (fn [x] (* x 2))
m {:foo "bar"}
mf (with-meta f m)]
(testing "Testing functions with metadata"
(is (nil? (meta f)))
(is (fn? mf))
(is (= 4 (mf 2)))
(is (= 4 (apply mf [2])))
(is (= (meta mf) m)))))
(deftest test-atoms-and-volatile
(let [a (atom 0)]
(testing "Testing basic atom operations"
(is (= 0 (deref a)))
(is (= 1 (swap! a inc)))
(is (= false (compare-and-set! a 0 42)))
(is (= true (compare-and-set! a 1 7)))
(is (nil? (meta a)))
(is (nil? (get-validator a)))))
(let [a (atom 0)]
(testing "Testing swap!"
(is (= 1 (swap! a + 1)))
(is (= 4 (swap! a + 1 2)))
(is (= 10 (swap! a + 1 2 3)))
(is (= 20 (swap! a + 1 2 3 4)))))
(let [a (atom [1] :validator coll? :meta {:a 1})]
(testing "Testing atom validators"
(is (= coll? (get-validator a)))
(is (thrown? js/Error (reset! a 1)))
(is (thrown? js/Error (set-validator! a number?)))
(is (some? (get-validator a)))
(set-validator! a nil)
(is (nil? (get-validator a)))
(let [e1 (ex-info "" {})]
(try
(set-validator! a (fn [_] (throw e1)))
(catch :default e2
(is (identical? e1 e2)))))
(is (= {:a 1} (meta a)))
(alter-meta! a assoc :b 2)
(is (= {:a 1 :b 2} (meta a)))))
(let [v (volatile! 1)]
(testing "Testing volatile"
(is (volatile? v))
(is (not (volatile? (atom 1))))
(is (= 2 (vreset! v 2)))
(is (= 3 (vswap! v inc)))
(is (= 3 @v)))))
(deftest test-contains?
(testing "Testing contains?"
(is (contains? {:a 1 :b 2} :a))
(is (not (contains? {:a 1 :b 2} :z)))
(is (contains? [5 6 7] 1))
(is (contains? [5 6 7] 2))
(is (not (contains? [5 6 7] 3)))
(is (contains? (to-array [5 6 7]) 1))
(is (contains? (to-array [5 6 7]) 2))
(is (not (contains? (to-array [5 6 7]) 3)))
(is (not (contains? nil 42)))
(is (contains? "f" 0))
(is (not (contains? "f" 55)))))
(deftest test-run!
(testing "Testing run!"
(let [a (atom 0)]
(run! (fn [n]
(swap! a + n))
(range 5))
(is (= 10 @a)))
(is (nil? (run! identity [1])))
(is (nil? (run! reduced (range))))))
(deftest test-in-operations
(testing "Testing update-in"
(is (= {:foo {:bar {:baz 1}}}
(update-in {:foo {:bar {:baz 0}}} [:foo :bar :baz] inc)))
(is (= {:foo 1 :bar 2 :baz 10}
(update-in {:foo 1 :bar 2 :baz 3} [:baz] + 7)))
(is (= [{:foo 1, :bar 2} {:foo 1, :bar 3}]
(update-in [{:foo 1 :bar 2}, {:foo 1 :bar 2}] [1 :bar] inc)))
(is (= [{:foo {:bar 2}} {:foo {:bar 3}}]
(update-in [{:foo {:bar 2}}, {:foo {:bar 2}}] [1 :foo :bar] inc))))
(testing "Testing assoc-in"
(is (= {:foo {:bar {:baz 100}}}
(assoc-in {:foo {:bar {:baz 0}}} [:foo :bar :baz] 100)))
(is (= {:foo 1 :bar 2 :baz 100}
(assoc-in {:foo 1 :bar 2 :baz 3} [:baz] 100)))
(is (= [{:foo [{:bar 2} {:baz 3}]} {:foo [{:bar 2} {:baz 100}]}]
(assoc-in [{:foo [{:bar 2} {:baz 3}]}, {:foo [{:bar 2} {:baz 3}]}]
[1 :foo 1 :baz] 100)))
(is (= [{:foo 1, :bar 2} {:foo 1, :bar 100}]
(assoc-in [{:foo 1 :bar 2}, {:foo 1 :bar 2}] [1 :bar] 100))))
(testing "Testing get-in"
(is (= 1 (get-in {:foo 1 :bar 2} [:foo])))
(is (= 2 (get-in {:foo {:bar 2}} [:foo :bar])))
(is (= 1 (get-in [{:foo 1}, {:foo 2}] [0 :foo])))
(is (= 4 (get-in [{:foo 1 :bar [{:baz 1}, {:buzz 2}]}, {:foo 3 :bar [{:baz 3}, {:buzz 4}]}]
[1 :bar 1 :buzz]))))
)
(deftest test-js-clj-conversions
(testing "Testing JS / CLJS data conversions"
(testing "js->clj"
(is (= {"a" 1, "b" 2} (js->clj (js* "{\"a\":1,\"b\":2}"))))
(is (= {"a" nil} (js->clj (js* "{\"a\":null}"))))
(is (= {} (js->clj (js* "{}"))))
(is (= {"a" true, "b" false} (js->clj (js* "{\"a\":true,\"b\":false}"))))
(is (= {:a 1, :b 2} (js->clj (js* "{\"a\":1,\"b\":2}") :keywordize-keys true)))
(is (= [[{:a 1, :b 2} {:a 1, :b 2}]]
(js->clj (js* "[[{\"a\":1,\"b\":2}, {\"a\":1,\"b\":2}]]") :keywordize-keys true)))
(is (= [[{:a 1, :b 2} {:a 1, :b 2}]]
(js->clj [[{:a 1, :b 2} {:a 1, :b 2}]])))
(is (= (js->clj nil) nil)))
(testing "clj->js"
(is (= (clj->js 'a) "a"))
(is (= (clj->js :a) "a"))
(is (= (clj->js "a") "a"))
(is (= (clj->js 1) 1))
(is (= (clj->js nil) (js* "null")))
(is (= (clj->js true) (js* "true")))
(is (goog/isArray (clj->js [])))
(is (goog/isArray (clj->js #{})))
(is (goog/isArray (clj->js '())))
(is (goog/isObject (clj->js {})))
(is (= (gobject/get (clj->js {:a 1}) "a") 1))
(is (= (-> (clj->js {:a {:b {{:k :ey} :d}}})
(gobject/get "a")
(gobject/get "b")
(gobject/get "{:k :ey}"))
"d")))
(is (= (-> (clj->js {:foo/bar "a"})
(gobject/get "bar"))
"a"))
(is (= (-> (clj->js {:foo/bar "a"} :keyword-fn namespace)
(gobject/get "foo"))
"a"))))
(deftest test-delay
(let [a (atom 0)
d (delay (swap! a inc))]
(testing "Testing delay"
(is (false? (realized? d)))
(is (zero? @a)) ;; delay hasn't triggered yet
(is (= 1 @d)) ;; trigger it
(is (= 1 @a)) ;; make sure side effect has happened
(is (true? (realized? d)))
(is (= 1 @d)) ;; body doesn't happen again
(is (= 1 @a)) ;; atom hasn't changed either
(is (= (force d) @d))
(is (= 1 (force 1))))) ;; you can safely force non-delays
)
(deftest test-hierarchy
(testing "Testing hierarchy operations"
(derive ::rect ::shape)
(derive ::square ::rect)
(is (= #{:cljs.core-test/shape} (parents ::rect)))
(is (= #{:cljs.core-test/rect :cljs.core-test/shape} (ancestors ::square)))
(is (= #{:cljs.core-test/rect :cljs.core-test/square} (descendants ::shape)))
(is (true? (isa? 42 42)))
(is (true? (isa? ::square ::shape)))
(derive cljs.core.ObjMap ::collection)
(derive cljs.core.PersistentHashSet ::collection)
(is (true? (isa? cljs.core.ObjMap ::collection)))
(is (true? (isa? cljs.core.PersistentHashSet ::collection)))
(is (false? (isa? cljs.core.IndexedSeq ::collection)))
;; ?? (isa? String Object)
(is (true? (isa? [::square ::rect] [::shape ::shape])))
;; ?? (ancestors java.util.ArrayList)
;; ?? isa? based dispatch tests
))
(defmulti bar (fn [x y] [x y]))
(defmethod bar [::rect ::shape] [x y] :rect-shape)
(defmethod bar [::shape ::rect] [x y] :shape-rect)
;;(bar ::rect ::rect)
;; -> java.lang.IllegalArgumentException:
;; Multiple methods match dispatch value:
;; [:cljs.core-test/rect :cljs.core-test/rect] -> [:cljs.core-test/rect :cljs.core-test/shape]
;; and [:cljs.core-test/shape :cljs.core-test/rect],
;; and neither is preferred
(deftest test-multimethods-1
(testing "Testing basic multimethod usage"
(is (zero? (count (prefers bar))))
(prefer-method bar [::rect ::shape] [::shape ::rect])
(is (= 1 (count (prefers bar))))
(is (= :rect-shape (bar ::rect ::rect)))
(is (= :rect-shape (apply (-get-method bar [::rect ::shape]) [::rect ::shape])))
))
(defmulti nested-dispatch (fn [m] (-> m :a :b)))
(defmethod nested-dispatch :c [m] :nested-a)
(defmethod nested-dispatch :default [m] :nested-default)
(defmulti nested-dispatch2 ffirst)
(defmethod nested-dispatch2 :a [m] :nested-a)
(defmethod nested-dispatch2 :default [m] :nested-default)
(defmulti foo1 (fn [& args] (first args)))
(defmethod foo1 :a [& args] :a-return)
(defmethod foo1 :default [& args] :default-return)
(defmulti area :Shape)
(defn rect [wd ht] {:Shape :Rect :wd wd :ht ht})
(defn circle [radius] {:Shape :Circle :radius radius})
(defmethod area :Rect [r]
(* (:wd r) (:ht r)))
(defmethod area :Circle [c]
(* Math/PI (* (:radius c) (:radius c))))
(defmethod area :default [x] :oops)
(defmulti foo2 (fn []))
(defmethod foo2 :default [] :foo)
(defmulti apply-multi-test (fn ([_] 0) ([_ _] 0) ([_ _ _] 0)))
(defmethod apply-multi-test 0
([x] :one)
([x y] :two)
([x y & r] [:three r]))
;; CLJS-469, helpful exception message on bad dispatch
(defmulti no-dispatch-value :test)
;; custom hierarchy
(def my-map-hierarchy
(atom (-> (make-hierarchy)
(derive (type (obj-map)) ::map)
(derive (type (array-map)) ::map)
(derive (type (hash-map)) ::map)
(derive (type (sorted-map)) ::map))))
(defmulti my-map? type :hierarchy my-map-hierarchy)
(defmethod my-map? ::map [_] true)
(defmethod my-map? :default [_] false)
(defmulti foo2' identity)
(defmethod foo2' 0 [x] x)
(def three-levels-h (-> (make-hierarchy)
(derive :parent :gparent)
(derive :child :parent)))
(defmulti multi-with-h (fn [v] v) :hierarchy #'three-levels-h)
(defmethod multi-with-h :gparent [_] :gparent)
(defmethod multi-with-h :parent [_] :parent)
(deftest test-multimethods-2
(let [r (rect 4 13)
c (circle 12)]
(testing "Testing multimethod edge cases"
(is (= :nested-a (nested-dispatch {:a {:b :c}})))
(is (= :nested-a (nested-dispatch2 [[:a :b]])))
(is (= :a-return (foo1 :a)))
(is (= :default-return (foo1 1)))
(is (= 52 (area r)))
(is (= :oops (area {})))
;; CLJS-863
(is (= :foo (foo2)))
;; remove method tests
(is (= 2 (count (methods bar))))
(remove-method bar [::rect ::shape])
(is (= 1 (count (methods bar))))
(remove-all-methods bar)
(is (zero? (count (methods bar))))
(is (= [:three '(2)] (apply apply-multi-test [0 1 2])))
(is (try
(no-dispatch-value {:test :test})
(catch js/Error e
(not= -1 (.indexOf (.-message e) "cljs.core-test/no-dispatch-value")))))
(doseq [m [(obj-map) (array-map) (hash-map) (sorted-map)]]
(is (my-map? m)))
(doseq [not-m [[] 1 "asdf" :foo]]
(is (not (my-map? not-m))))
;; multimethod hashing
(is (= foo2' (ffirst {foo2' 1})))
(is (= :parent (multi-with-h :child)))
)))
(deftest test-transducers
(testing "Testing transducers"
(is (= (sequence (map inc) (array 1 2 3)) '(2 3 4)))
(is (= (apply str (sequence (map #(.toUpperCase %)) "foo")) "FOO"))
(is (== (hash [1 2 3]) (hash (sequence (map inc) (range 3)))))
(is (= [1 2 3] (sequence (map inc) (range 3))))
(is (= (sequence (map inc) (range 3)) [1 2 3]))
(is (= (sequence (remove even?) (range 10)) '(1 3 5 7 9)))
(is (= (sequence (take 5) (range 10))
'(0 1 2 3 4)))
(is (= (sequence (take-while #(< % 5)) (range 10))
'(0 1 2 3 4)))
(is (= (sequence (drop 5) (range 10))
'(5 6 7 8 9)))
(is (= (sequence (drop-while #(< % 5)) (range 10))
'(5 6 7 8 9)))
(is (= (sequence (take-nth 2) (range 10))
'(0 2 4 6 8)))
(is (= (sequence (replace {:foo :bar}) '(:foo 1 :foo 2))
'(:bar 1 :bar 2)))
(let [ret (into [] (map inc) (range 3))]
(is (and (vector? ret) (= ret '(1 2 3)))))
(let [ret (into [] (filter even?) (range 10))]
(is (and (vector? ret) (= ret '(0 2 4 6 8)))))
(is (= (map inc (sequence (map inc) (range 3)))
'(2 3 4)))
(is (= (sequence (dedupe) [1 1 2 2 3 3])
'(1 2 3)))
(is (= (mapcat reverse [[3 2 1 0] [6 5 4] [9 8 7]])
(range 10)))
(is (= (sequence (mapcat reverse) [[3 2 1 0] [6 5 4] [9 8 7]])
(range 10)))
(is (= (seq (eduction (map inc) [1 2 3])) '(2 3 4)))
(is (= (seq (eduction (map inc) (map inc) [1 2 3])) '(3 4 5)))
(is (= (sequence (partition-by #{:split}) [1 2 3 :split 4 5 6])
'([1 2 3] [:split] [4 5 6])))
(is (= (sequence (partition-all 3) '(1 2 3 4 5))
'([1 2 3] [4 5])))
(is (= (sequence (keep identity) [1 nil 2 nil 3])
'(1 2 3)))
(is (= (keep-indexed identity [:foo nil :bar nil :baz])
(sequence (keep-indexed identity) [:foo nil :bar nil :baz])))
(let [xform (comp (map inc)
(filter even?)
(dedupe)
(mapcat range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(mapcat flatten)
(random-sample 1.0)
(take-nth 1)
(keep #(when (odd? %) (* % %)))
(keep-indexed #(when (even? %1) (* %1 %2)))
(replace {2 "two" 6 "six" 18 "eighteen"})
(take 11)
(take-while #(not= 300 %))
(drop 1)
(drop-while string?)
(remove string?))
data (vec (interleave (range 18) (range 20)))]
(is (= (sequence xform data) '(36 200 10))))
(let [xf (map #(+ %1 %2))]
(is (= (sequence xf [0 0] [1 2]) [1 2])))
(is (= (-> (sequence (map inc) [1 2 3])
(with-meta {:a 1})
meta) {:a 1}))
(let [xf (fn [rf]
(fn
([] (rf))
([result] (rf result :foo))
([result input] (rf result input))))]
(is (= (sequence xf [1 2 3]) [1 2 3 :foo]))))
(testing "CLJS-2258"
(is (= ["1"] (sequence (map str) (eduction [1]))))))
(deftest test-obj-equiv
(testing "Object equiv method"
(is (.equiv :foo :foo))
(is (.equiv 'foo 'foo))
(is (.equiv {:foo 1 :bar 2} {:foo 1 :bar 2}))
(is (.equiv [1 2 3] [1 2 3]))
(is (.equiv '(1 2 3) '(1 2 3)))
(is (.equiv (map inc [1 2 3]) (map inc [1 2 3])))
(is (.equiv #{:cat :dog :bird} #{:cat :dog :bird}))
))
(defn seq-iter-match
[coll]
(let [i (-iterator coll)]
(loop [s (seq coll)
n 0]
(if (seq s)
(do
(when-not (.hasNext i)
(throw
(js/Error.
(str "Iterator exhausted before seq at(" n ")" ))))
(let [iv (.next i)
sv (first s)]
(when-not (= iv sv)
(throw
(js/Error.
(str "Iterator value " iv " and seq value " sv " did not match at ( " n ")")))))
(recur (rest s) (inc n)))
(if (.hasNext i)
(throw
(js/Error.
(str "Seq exhausted before iterator at (" n ")")))
true)))))
(defrecord TestIterRec [a b])
(deftest coll-iter-seq-match
(testing "Direct iterators match sequences"
(let [test-map (apply hash-map (range 200))
test-set (apply hash-set (range 200))
test-queue (into cljs.core.PersistentQueue.EMPTY (vec (range 100)))
test-record (into (TestIterRec. 1 2) {:c 3 :d 4})]
(is (= true (seq-iter-match test-map)))
(is (= true (seq-iter-match test-set)))
(is (= true (seq-iter-match test-queue)))
(is (= true (seq-iter-match test-record))))))
(deftest test-es6-interfaces
(testing "ES6 collection interfaces"
(let [iter (es6-iterator [1 2 3])]
(testing "basic iterations"
(is (= (.-value (.next iter)) 1))
(is (= (.-value (.next iter)) 2))
(is (= (.-value (.next iter)) 3))
(is (.-done (.next iter)))))
(is (.has {:foo "bar"} :foo))
(is (= (.get {:foo "bar"} :foo) "bar"))
(is (= (.get {:foo "bar"} :bar :default) :default))
(let [iter (.keys {:foo "bar" :baz "woz"})]
(testing "map key iteration"
(is (#{:foo :baz} (.-value (.next iter))))
(is (#{:foo :baz} (.-value (.next iter))))
(is (.-done (.next iter)))))
(let [eiter (.entries {:foo "bar" :baz "woz"})]
(testing "map entry iteration"
(let [entries #{(seq #js [:foo "bar"]) (seq #js [:baz "woz"])}]
(is (entries (seq (.-value (.next eiter)))))
(is (entries (seq (.-value (.next eiter))))))
(is (.-done (.next eiter)))))
(let [iter (.values {:foo "bar" :baz "woz"})]
(testing "map value iteration"
(is (#{"bar" "woz"} (.-value (.next iter))))
(is (#{"bar" "woz"} (.-value (.next iter))))
(is (.-done (.next iter)))))
(is (.has #{:cat :bird :dog} :bird))
(let [iter (.keys #{:cat :bird :dog})]
(testing "set key iteration"
(is (#{:cat :bird :dog} (.-value (.next iter))))
(is (#{:cat :bird :dog} (.-value (.next iter))))
(is (#{:cat :bird :dog} (.-value (.next iter))))
(is (.-done (.next iter)))))
(let [iter (.entries #{:cat :bird :dog})]
(testing "set entry iteration"
(is (#{[:cat :cat] [:bird :bird] [:dog :dog]} (seq (.-value (.next iter)))))
(is (#{[:cat :cat] [:bird :bird] [:dog :dog]} (seq (.-value (.next iter)))))
(is (#{[:cat :cat] [:bird :bird] [:dog :dog]} (seq (.-value (.next iter)))))
(is (.-done (.next iter)))))
(let [iter (.values #{:cat :bird :dog})]
(testing "set value iteration"
(is (#{:cat :bird :dog} (.-value (.next iter))))
(is (#{:cat :bird :dog} (.-value (.next iter))))
(is (#{:cat :bird :dog} (.-value (.next iter))))
(is (.-done (.next iter)))))
))
(deftest test-reader-literals
(testing "Testing reader literals"
(is (= #queue [1] (into cljs.core.PersistentQueue.EMPTY [1])))
(is (not= #queue [1 2] (into cljs.core.PersistentQueue.EMPTY [1])))
(is (= #inst "2010-11-12T18:14:15.666-00:00"
#inst "2010-11-12T13:14:15.666-05:00"))
(is (= #uuid "550e8400-e29b-41d4-a716-446655440000"
#uuid "550e8400-e29b-41d4-a716-446655440000"))
(is (= 42
(get {#uuid "550e8400-e29b-41d4-a716-446655440000" 42}
#uuid "550e8400-e29b-41d4-a716-446655440000")))
))
(deftest test-uuid
(testing "Testing UUID"
(is (= (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")))
(is (not (identical? (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000"))))
(is (= 42 (get {(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000") 42}
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
:not-at-all-found)))
(is (= :not-at-all-found
(get {(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000") 42}
(cljs.core/uuid "666e8400-e29b-41d4-a716-446655440000")
:not-at-all-found)))
(is (= -1 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "666e8400-e29b-41d4-a716-446655440000"))))
(is (= 1 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "550e8400-a29b-41d4-a716-446655440000"))))
(is (= 0 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")))))
(testing "UUID hashing"
(let [id "550e8400-e29b-41d4-a716-446655440000"
uuid (cljs.core/uuid id)
expected (hash id)]
(is (= expected (hash uuid)))
(is (= expected (.-__hash uuid))))))
(def constantly-nil (constantly nil))
(deftest some->test
(is (nil? (some-> nil)))
(is (= 0 (some-> 0)))
(is (= -1 (some-> 1 (- 2))))
(is (nil? (some-> 1 constantly-nil (- 2)))))
(deftest some->>test
(is (nil? (some->> nil)))
(is (= 0 (some->> 0)))
(is (= 1 (some->> 1 (- 2))))
(is (nil? (some->> 1 constantly-nil (- 2)))))
(deftest cond->test
(is (= 0 (cond-> 0)))
(is (= -1 (cond-> 0 true inc true (- 2))))
(is (= 0 (cond-> 0 false inc)))
(is (= -1 (cond-> 1 true (- 2) false inc))))
(deftest cond->>test
(is (= 0 (cond->> 0)))
(is (= 1 (cond->> 0 true inc true (- 2))))
(is (= 0 (cond->> 0 false inc)))
(is (= 1 (cond->> 1 true (- 2) false inc))))
(deftest as->test
(is (= 0 (as-> 0 x)))
(is (= 1 (as-> 0 x (inc x))))
(is (= 2 (as-> [0 1] x
(map inc x)
(reverse x)
(first x)))))
(deftest threading-loop-recur
(is (nil? (loop []
(as-> 0 x
(when-not (zero? x)
(recur))))))
(is (nil? (loop [x nil] (some-> x recur))))
(is (nil? (loop [x nil] (some->> x recur))))
(is (= 0 (loop [x 0] (cond-> x false recur))))
(is (= 0 (loop [x 0] (cond->> x false recur)))))
(defspec boolean-test 10
(prop/for-all [b gen/boolean]
(boolean? b)))
(deftest aget-test
(is (= 11 (aget #js [10 11 12] 1)))
(is (= 11 (apply aget [#js [10 11 12] 1])))
(is (= 3 (aget #js [1 2 #js [3 4]] 2 0)))
(is (= 3 (apply aget [#js [1 2 #js [3 4]] 2 0]))))
(deftest aset-test
(let [array #js [10 11 12]]
(is (= 13 (aset array 1 13)))
(is (= 13 (aget array 1))))
(let [array #js [10 11 12]]
(is (= 13 (apply aset [array 1 13])))
(is (= 13 (aget array 1))))
(let [array #js [1 2 #js [3 4]]]
(is (= 13 (aset array 2 0 13)))
(is (= 13 (aget array 2 0))))
(let [array #js [1 2 #js [3 4]]]
(is (= 13 (apply aset [array 2 0 13])))
(is (= 13 (aget array 2 0)))))
(deftest unchecked-get-test
(is (= 1 (unchecked-get #js {:a 1} "a")))
(is (nil? (unchecked-get #js {:a 1} "b")))
(is (nil? (unchecked-get #js {:a 1} nil))))
(deftest js-invoke-test
(let [o (doto (js-obj) (gobject/set "my sum" (fn [a b] (+ a b))))]
(is (= 5 (js-invoke o "my sum" 2 3)))))
(deftest memfn-test
(let [substr (memfn substr start length)]
(is (= "cde" (substr "abcdefg" 2 3))))
(let [trim (memfn trim)]
(is (= ["abc" "def"] (map trim [" abc " " def "])))))
;; =============================================================================
;; Tickets
(deftest test-383
(testing "Testing CLJS-383"
(let [f1 (fn f1 ([] 0) ([a] 1) ([a b] 2) ([a b c & more] 3))
f2 (fn f2 ([x] :foo) ([x y & more] (apply f1 y more)))]
(is (= 1 (f2 1 2))))
(let [f (fn ([]) ([a & more] more))]
(is (nil? (f :foo))))
(is (nil? (array-seq (array 1) 1)))) )
(deftest test-513
(testing "Testing CLJS-513"
(let [sentinel (js-obj)]
(is (identical? sentinel (try ([] 0) (catch js/Error _ sentinel)))))))
(defprotocol IFoo (foo [this]))
(deftest test-reify-meta
(is (= (meta (with-meta (reify IFoo (foo [this] :foo)) {:foo :bar}))
{:foo :bar})))
(let [x "original"]
(defn original-closure-stmt [] x))
(deftest test-401-411
(let [x "overwritten"]
(is (= "original" (original-closure-stmt))))
(is (= "original" (let [x "original"
oce (fn [] x)
x "overwritten"]
(oce)))))
(deftest test-letfn-shadowing
(letfn [(x [] "original")
(y [] (x))]
(let [x (fn [] "overwritten")]
(is (= "original" (y))))))
(deftest test-459
(is (= (reduce-kv conj [] (sorted-map :foo 1 :bar 2))
[:bar 2 :foo 1])))
(deftest test-kv-reduce
(letfn [(kvr-test [data expect]
(and
(= :reduced
(reduce-kv
(fn [_ _ _] (reduced :reduced))
[] data))
(= (sort expect)
(sort
(reduce-kv
(fn [r k v] (-> r (conj [k v])))
[] data)))))]
(testing "Testing IKVReduce"
(doseq [[data expect] [[(obj-map :k0 :v0 :k1 :v1) [[:k0 :v0] [:k1 :v1]]]
[(hash-map :k0 :v0 :k1 :v1) [[:k0 :v0] [:k1 :v1]]]
[(array-map :k0 :v0 :k1 :v1) [[:k0 :v0] [:k1 :v1]]]
[[:v0 :v1] [[0 :v0] [1 :v1]]]]]
(is (kvr-test data expect)))
(is (= {:init :val} (reduce-kv assoc {:init :val} nil))))))
(deftest test-data-conveying-exceptions
(is (= {:foo 1}
(try (throw (ex-info "asdf" {:foo 1}))
(catch ExceptionInfo e
(ex-data e)))))
(is (instance? js/Error (ex-info "asdf" {:foo 1})))
(is (= (pr-str (ex-info "abc" {:x 1})) "#error {:message \"abc\", :data {:x 1}}"))
(is (= (pr-str (ex-info "abc" {:x 1} "def")) "#error {:message \"abc\", :data {:x 1}, :cause \"def\"}"))
(is (= (.toString (ex-info "abc" {:x 1} "def")) "#error {:message \"abc\", :data {:x 1}, :cause \"def\"}"))
(is (= (str (ex-info "abc" {:x 1} "def")) "#error {:message \"abc\", :data {:x 1}, :cause \"def\"}"))
(is (not (instance? cljs.core.ExceptionInfo (js/Error.)))))
(deftest test-2067
(is (= 0 (reduce-kv
(fn [x k _]
(when (zero? k)
(reduced k)))
nil (zipmap (range 17) (repeat 0))))))
(deftest test-435
(is (= (assoc {} 154618822656 1 261993005056 1)
{154618822656 1 261993005056 1})))
(deftest test-458
(is (= (get-in {:a {:b 1}} [:a :b :c] :nothing-there)
:nothing-there)))
(deftest test-464
(is (nil? (get-in {:foo {:bar 2}} [:foo :bar :baz]))))
(deftest test-symbol-meta
(is (= (meta (with-meta 'foo {:tag 'int})) {:tag 'int}))
(is (= (meta (quote ^{:bar true} foo)) {:bar true}))
(is (= (meta (quote ^:bar foo)) {:bar true}))
(is (= (meta (first '[^:bar x])) {:bar true})))
(deftest test-467
(is (= (reduce-kv + 0 (apply hash-map (range 1000)))
(reduce + (range 1000)))))
(deftest test-477
(is (= [js/undefined 1 2] ((fn [& more] more) js/undefined 1 2)))
(is (= [js/undefined 4 5] ((fn [a b & more] more) 1 2 js/undefined 4 5))))
(deftest test-493
(is (nil? (get 42 :anything)))
(is (= (get 42 :anything :not-found) :not-found))
(is (nil? (first (map get [42] [:anything]))))
(is (= (first (map get [42] [:anything] [:not-found])) :not-found)))
(deftest test-481
(let [fs (atom [])]
(doseq [x (range 4)
:let [y (inc x)
f (fn [] y)]]
(swap! fs conj f))
(is (= (map #(%) @fs) '(1 2 3 4)))))
(def exists?-test-val 'foo)
(deftest test-495
(testing "Testing CLJS-495, exists?"
(is (false? (exists? js/jQuery)))
(is (exists? exists?-test-val))))
(deftest test-518
(is (nil? (:test "test"))))
(deftest test-541
(letfn [(f! [x] (print \f) x)
(g! [x] (print \g) x)]
(is (= "ffgfg"
(with-out-str
(instance? Symbol (f! 'foo))
(max (f! 5) (g! 10))
(min (f! 5) (g! 10)))))))
(deftest test-582
(is (= #{1 2} (set [1 2 2])))
(is (= #{1 2} (hash-set 1 2 2)))
(is (= #{1 2} (apply hash-set [1 2 2]))))
(deftest test-585
(is (= (last (map identity (into [] (range 32)))) 31))
(is (= (into #{} (range 32))
(set (map identity (into [] (range 32)))))))
(def foo580)
(def foo580 {:a (fn []) :b (fn [] (foo580 :a))})
(deftest test-580
(is (nil? (((:b foo580))))))
(deftest test-587
(is (== (first (filter #(== % 9999) (range))) 9999)))
(deftest test-604
(is (= () (concat nil [])))
(is (= () (concat [] []))))
(deftest test-600
(is (= "foobar" (apply str (concat "foo" "bar")))))
(deftest test-608
(is (= '("") (re-seq #"\s*" ""))))
(deftype KeywordTest []
ILookup
(-lookup [o k] :nothing)
(-lookup [o k not-found] not-found))
(deftest tset-638
(is (= (:a (KeywordTest.)) :nothing)))
(deftest test-648
(let [a (reify IHash (-hash [_] 42))
b (reify IHash (-hash [_] 42))
s (set (range 128))]
(testing "Testing CLJS-648 (CLJ-1285)"
(is (= (-> (conj s a b) transient (disj! a) persistent! (conj a))
(-> (conj s a b) transient (disj! a) persistent! (conj a)))))))
(deftest test-660
(testing "Testing CLJS-660, namespace handling"
(is (= (-> 'a.b keyword ((juxt namespace name))) [nil "a.b"]))
(is (= (-> 'a.b/c keyword ((juxt namespace name))) ["a.b" "c"]))
(is (= (-> "a.b" keyword ((juxt namespace name))) [nil "a.b"]))
(is (= (-> "a.b/c" keyword ((juxt namespace name))) ["a.b" "c"]))))
(deftest test-663
(testing "Testing CLJS-663, invalid keywords"
(is (= (keyword 123) nil))
(is (= (keyword (js/Date.)) nil))))
(deftest test-647
(let [keys #(vec (js-keys %))
z "x"]
(testing "Testing CLJS-647, js-keys"
(assert (= ["x"]
(keys (js-obj "x" "y"))
(keys (js-obj (identity "x") "y"))
(keys (js-obj z "y")))))))
(def some-x 1)
(def some-y 1)
(deftest test-583
(is (= (count #{some-x some-y}) 1)))
(deftest test-584
(is (= (count {some-x :foo some-y :bar}) 1)))
(deftest test-725
(testing "Testing CLJS-725, drop"
(is (= (apply vector (drop-while (partial = 1) [1 2 3])) [2 3]))
(is (= (apply list (drop-while (partial = 1) [1 2 3])) '(2 3)))
(is (= (set (drop 1 #js [1 2 3])) #{2 3}))))
(deftest test-724
(is (nil? (first (rest (rest (rest (range 3))))))))
(deftest test-730
(testing "Testing CLJS-730, object? predicate"
(is (true? (object? #js {})))
(is (false? (object? nil)))))
(deftest test-767
(testing "Testing CLJS-767, invalid assoc"
(doseq [n [nil "-1" "" "0" "1" false true (js-obj)]]
(is (= :fail (try (assoc [1 2] n 4)
(catch js/Error e :fail))))
(is (= :fail (try (assoc (subvec [1 2 3] 2) n 4)
(catch js/Error e :fail))))
(is (= :fail (try (assoc (range 1 3) n 4)
(catch js/Error e :fail)))))))
(deftest test-768
(testing "Testing CLJS-768, invalid assoc!"
(doseq [n [nil "-1" "" "0" "1" false true (js-obj)]]
(is (= :fail (try (assoc! (transient [1 2]) n 4)
(catch js/Error e :fail)))))))
(defn cljs-739 [arr names]
(let [name (first names)]
(if name
(recur (conj arr (fn [] (println name)))
(rest names))
arr)))
(deftest test-739
(testing "Testing CLJS-739, with-out-str"
(binding [*print-newline* true]
(is (= (with-out-str (doseq [fn (cljs-739 [] [:a :b :c :d])] (fn)))
":a\n:b\n:c\n:d\n")))))
(deftest print-ns-maps
(testing "Testing CLJS-1786, *print-namespace-maps*"
(is (= "#:user{:a 1}" (binding [*print-namespace-maps* true] (pr-str {:user/a 1}))))
(is (= "{:user/a 1}" (binding [*print-namespace-maps* false] (pr-str {:user/a 1}))))))
(deftest test-728
(testing "Testing CLJS-728, lookup with default"
(doseq [n [nil "-1" "" "0" "1" false true (js-obj)]]
(is (nil? (get [1 2] n)))
(is (= :fail (try (nth [1 2] n) (catch js/Error e :fail))))
(is (= 4 (get [1 2] n 4)))
(is (= :fail (try (nth [1 2] n 4) (catch js/Error e :fail))))
(is (nil? (get (subvec [1 2] 1) n)))
(is (= :fail (try (nth (subvec [1 2] 1) n) (catch js/Error e :fail))))
(is (= 4 (get (subvec [1 2] 1) n 4)))
(is (= :fail (try (nth (subvec [1 2] 1) n 4) (catch js/Error e :fail))))
(is (nil? (get (transient [1 2]) n)))
(is (= :fail (try (nth (transient [1 2]) n) (catch js/Error e :fail))))
(is (= 4 (get (transient [1 2]) n 4)))
(is (= :fail (try (nth (transient [1 2]) n 4) (catch js/Error e :fail))))
(is (nil? (get (range 1 3) n)))
(is (= :fail (try (nth (range 1 3) n) (catch js/Error e :fail))))
(is (= 4 (get (range 1 3) n 4)))
(is (= :fail (try (nth (range 1 3) n 4) (catch js/Error e :fail))))))
)
(def cljs-780 (atom {:foo (with-meta [] {:bar '(1 2 3)})}))
(deftest test-780
(let [_ (swap! cljs-780 update-in [:foo] vary-meta update-in [:bar] vec)
x (-> @cljs-780 :foo meta :bar)]
(testing "Testing CLJS-780, update-in + vary-meta"
(is (vector? x))
(is (= x [1 2 3])))) )
(deftest test-782
(testing "Testing CLJS-782, UUID toString"
(is (= (.toString #uuid "550e8400-e29b-41d4-a716-446655440000")
"550e8400-e29b-41d4-a716-446655440000"))))
(deftest test-case-keyword
(is (= (let [x "a"] (case x :a 1 "a")) "a")))
(deftest test-801
(testing "Testing CLJS-801, str"
(is (= "0atrue:key/wordsymb/olfalse[1 2 3 4]1234.56789"
(str 0 "a" true nil :key/word 'symb/ol false [1 2 3 4] 1234.5678 0x09)))))
(defn case-recur [value]
(case value
:a (recur :b)
:b 0))
(deftest test-812
(testing "Testing CLJS-812, case with recur"
(is (= (case-recur :a) 0))))
(deftest test-816
(testing "Testing CLJS-816, rename-keys"
(is (= (set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}))
(is (= (set/rename-keys {:a "one" :b "two"} {:a :z :c :y}) {:z "one" :b "two"}))
(is (= (set/rename-keys {:a "one" :b "two" :c "three"} {:a :b :b :a})
{:a "two" :b "one" :c "three"}))) )
(deftest test-881
(testing "Testing CLJS-881, duplicate keys in array maps"
(is (= [:foo] (keys (apply array-map [:foo 1 :foo 2]))))))
(deftest test-810
(let [not-strings [true false nil 1 (fn [])]]
(testing "Testing CLJS-810, exception on bad input to regex fns"
(is (every? #(= :failed (try (re-find #"." %)
(catch js/TypeError _ :failed))) not-strings))
(is (every? #(= :failed (try (re-matches #"." %)
(catch js/TypeError _ :failed))) not-strings))
(is (every? #(= :failed (try (re-find #"nomatch" %)
(catch js/TypeError _ :failed))) not-strings))
(is (every? #(= :failed (try (re-matches #"nomatch" %)
(catch js/TypeError _ :failed))) not-strings)))))
(deftest test-853
(testing "Testing CLJS-853, function metadata"
(is (= {:foo true} (meta ^:foo (fn []))))))
(deftest test-807
(testing "Testing CLJS-807, big int, float, big dec literals"
(is (= -1 -1N))
(is (= 9.007199254740996E15 9007199254740995N))
(is (= 1.5 1.5M))
(is (= 4.9E-324 5E-324M))))
(deftest test-921-var-meta-name
(testing "testing CLJS-921, :name var metadata should be unqualified"
(is (= (-> (var first) meta :name) 'first))))
(deftype MyWatchable []
IWatchable
(-notify-watches [this oldval newval])
(-add-watch [this key f])
(-remove-watch [this key]))
(deftest test-920-watch-ops-return-ref
(testing "tesing CLJS-920, add-watch/return-watch should return reference"
(let [w (MyWatchable.)]
(is (identical? (add-watch w :foo (fn [])) w))
(is (identical? (remove-watch w :foo) w)))))
(deftype MyCustomAtom [^:mutable state]
IDeref
(-deref [_] state)
IReset
(-reset! [_ newval]
(set! state newval)))
(deftest test-919-generic-cas
(testing "testing CLJS-919, CAS should on custom atom types"
(let [a0 (MyCustomAtom. 10)
a1 (MyCustomAtom. 0)]
(compare-and-set! a0 0 20)
(compare-and-set! a1 0 20)
(is (== @a0 10))
(is (== @a1 20)))))
(deftest test-map-new-transducers
(testing "Test distinct, interpose, map-indexed transducers"
(is (= [1 2 3]
(transduce (distinct) conj [] [1 1 2 2 3 3])))
(is (= [1 :foo 2 :foo 3]
(transduce (interpose :foo) conj [] [1 2 3])))
(is (= [[0 1] [1 2] [2 3]]
(transduce (map-indexed (fn [i x] [i x])) conj [] [1 2 3])))))
(defn foo-var [f]
(fn [x]
(f x)))
(defn foo-set [x]
(first x))