forked from clojure/clojurescript
/
quick.cljs
158 lines (118 loc) · 5.26 KB
/
quick.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
(ns cljs.test.quick-tests)
(defmacro quick-mac [x] x)
(defmacro quick-unless [pred a b] `(if (not ~pred) ~a ~b))
(defn run-tests []
;; Function reader
(assert (= '(1) (#(list %1) 1)))
(assert (= '(:a) (#(list %1) :a)))
(assert (= 11 (#(+ 1 %) 10)))
;; Keywords
(assert (= ':foo (keyword "foo")))
(assert (= ':bar/foo (keyword "bar" "foo")))
(assert (= *ns-sym* (symbol (namespace ::foo))))
(assert (= (namespace :foo) nil) "ns isn't nil")
(assert (= (name :foo) "foo") "name isn't foo")
(assert (= (name ::foo) "foo") "name isn't foo")
(assert (= (namespace ::foo) (str *ns-sym*)) "wrong ns")
(assert (= (namespace :1) nil) "ns isn't nil")
(assert (= (name :1) \1) "name isn't 1")
(assert (= (namespace ::2) (str *ns-sym*)) "wrong ns")
(assert (= (name ::2) \2) "name isn't 2")
;; symbols
(assert (= 'mysym (symbol "mysym")))
(assert (= 'bar/mysym (symbol "bar" "mysym")))
(assert (= true (symbol? 'blah)))
(assert (= true (symbol? (symbol "blah"))))
(assert (= false (symbol? :blah)))
(assert (= 'blah (with-meta 'blah {:a 1})))
(assert (= {:a 1} (meta (with-meta 'blah {:a 1}))))
(assert (= "cljs.user" (namespace (with-meta 'cljs.user/foo {:a 1}))))
(assert (= "cljs.user" (namespace (with-meta (with-meta 'cljs.user/foo {:a 1}) {:b 2}))))
;; hexs
(assert (= 0x7e7 2023) "hexs are read and parsed")
;; regex
(assert (= '("/a" "/d" "/g") (re-seq #"/." "/abc/def/ghi")))
(assert (= 1 (count (take 2 (re-seq #"^$" "")))))
;; arrays
(assert (= '(3 6 9 12) (seq (let [a (array 1 2 3 4)] (amap a i ret (* 3 (aget a i)))))))
(assert (= 25 (let [a (array 1 2 3 4 5)] (areduce a i ret 10 (+ ret (aget a i))))))
;; unicode
(assert (= "ü" "\u00fc") "unicodes are read properly")
;; Macros
(assert (= 5 (quick-mac 5)))
(assert (= :yep (quick-unless false :yep :nope)))
(assert (= :nope (quick-unless true :yep :nope)))
;; destructuring
(assert (= [4 3 2 1] (let [[a b] [1 2] {:keys [c d]} {:c 3 :d 4}] [d c b a])))
(assert (= [2 1] ((fn [a b] [b a]) 1 2)))
(assert (= [2 1] ((fn [[a b]] [b a]) [1 2])))
(assert (= [20 10] ((fn [{:keys [a b]}] [b a]) {:a 10 :b 20})))
;; with-out-str, time, regex, for
(assert (= "01234" (with-out-str (doseq [n (range 5)] (print n)))))
;; try, throw
(assert (= 3 (try (+ 1 2) (catch js/Error e :exc))))
(assert (= :exc (try (throw (js/Error. "err")) (catch js/Error e :exc))))
;; ..
(assert (= "6" (.. (array 4 5 6) (pop) (toString))))
;; doto
(assert (= "b" (aget (doto (new js/Array) (.push "a") (.push "b")) 1)))
;; memfn
(assert (= '(:a "1" "[object Object]") (map (memfn toString) [:a 1 (new js/Object)])))
;; letfn
(assert (= [20 300] (letfn [(twice [x] (* 2 x)) (thrice [x] (* 3 x))] [(twice 10) (thrice 100)])))
;; for
(assert (= '([1 2] [3 6]) (for [x [1 2 3 4] :let [y (* x 2)] :when (odd? x)] [x y])))
;; while
(def w-a (atom 5))
(assert (= "5\n4\n3\n2\n1\n" (with-out-str (while (pos? @w-a) (do (println @w-a) (swap! w-a dec))))))
;; doseq
(assert (= "[1 2]\n[3 6]\n" (with-out-str (doseq [x [1 2 3 4] :let [y (* x 2)] :when (odd? x)] (println [x y])))))
;; dotimes
(assert (= "01234" (with-out-str (dotimes [n 5] (print n)))))
;; delay
(assert (= 50 @(delay (* 10 5))))
;; lazy-seq
(defn positive-numbers ([] (positive-numbers 1)) ([n] (cons n (lazy-seq (positive-numbers (inc n))))))
(assert (= '(1 2 3 4 5) (take 5 (positive-numbers))))
;; condp
(defn tcp [v] (condp = v 1 "one" 2 "two" 3 "three" (str v " ???")))
(assert (= "three" (tcp 3)))
(assert (= "4 ???" (tcp 4)))
;; case
(defn tc [v] (case v "" "empty" "hello" "hello world" (str (count v) " chars")))
(assert (= "empty" (tc "")))
(assert (= "hello world" (tc "hello")))
(assert (= "4 chars" (tc "blah")))
(defn tc2 [v] (case v (1 3 5 7 9) "odd" (0 2 4 6 8) "even"))
(assert (= "odd" (tc2 1)))
(assert (= "even" (tc2 2)))
(assert (= :exc (try (tc2 10) (catch js/Error e :exc))))
;; deftype, defrecord, defprotocol
(deftype MyType [] INext (-next [_] "mytype next"))
(assert (= "mytype next" (next (MyType.))))
(deftype Fn-ish [a] IFn (-invoke [_] a))
(assert (= 1 ((Fn-ish. 1))))
(defrecord Banana [qty])
(defprotocol Fruit (-subtotal [item]))
(extend-type Banana Fruit (-subtotal [item] (* 158 (:qty item))))
(def myb (Banana. 10))
(assert (= 10 (:qty myb)))
(assert (= true (satisfies? Fruit myb)))
(assert (= 1580 (-subtotal myb)))
;; extend-protocol
(defprotocol Wonderful (-wonder [x]))
(extend-protocol Wonderful
nil (-wonder [x] "What a wonderful protocol!"))
(assert (= "What a wonderful protocol!" (-wonder nil)))
;; reify
(assert (= "foo-reified" (str (let [f "foo"] (reify Object (toString [this] (str f "-reified")))))))
(assert (= "reified ISeqable" (seq (reify ISeqable (-seq [_] "reified ISeqable")))))
;; multimethods
(defmulti bar (fn [x y] (type x)))
(defmethod bar cljs.core/Symbol [x y] (str "got a symbol and " y))
(defmethod bar cljs.core/PersistentVector[x y] (str "got a vector and " y))
(assert (= "got a symbol and 1" (bar 'blah 1)))
(assert (= "got a vector and 2" (bar [] 2)))
;; with-out-str, time, regex, for
(re-find #"Elapsed time: \d* msecs" (with-out-str (time (apply str (for [n (range 100)] (with-out-str (print (format "%d/" n))))))))
(println "All tests finished successfully"))