-
Notifications
You must be signed in to change notification settings - Fork 211
/
runner_tests.cljs
265 lines (238 loc) · 7.21 KB
/
runner_tests.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
;; Copyright (c) Rich Hickey and contributors. 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.async.runner-tests
(:require [cljs.core.async.impl.protocols :refer [full? add! remove!]]
[cljs.core.async.impl.ioc-helpers :as ioch])
(:require-macros [cljs.core.async.test-helpers :refer [is= is deftest testing runner locals-test]]
[cljs.core.async.macros :refer [go]]
[cljs.core.async.impl.ioc-macros :as ioc]))
(defn pause [state blk val]
(ioc/aset-all! state ioch/STATE-IDX blk ioch/VALUE-IDX val)
:recur)
(deftest runner-tests
(testing "macros add locals to the env"
(is= :pass
(runner (let [x 42]
(pause (locals-test))))))
(testing "do blocks"
(is= 42
(runner (do (pause 42))))
(is= 42
(runner (do (pause 44)
(pause 42)))))
(testing "if expressions"
(is= true
(runner (if (pause true)
(pause true)
(pause false))))
(is= false
(runner (if (pause false)
(pause true)
(pause false))))
(is= true
(runner (when (pause true)
(pause true))))
(is= nil
(runner (when (pause false)
(pause true)))))
(testing "loop expressions"
(is= 100
(runner (loop [x 0]
(if (< x 100)
(recur (inc (pause x)))
(pause x)))))
(is= [:b :a]
(runner (loop [a :a b :b n 1]
(if (pos? n)
(recur b a (dec n)) ;; swap bindings
[a b]))))
(is= 1
(runner (loop [x 0
y (inc x)]
y))))
(testing "let expressions"
(is= 3
(runner (let [x 1 y 2]
(+ x y)))))
(testing "vector destructuring"
(is= 3
(runner (let [[x y] [1 2]]
(+ x y)))))
(testing "hash-map destructuring"
(is= 3
(runner (let [{:keys [x y] x2 :x y2 :y :as foo} {:x 1 :y 2}]
(assert (and foo (pause x) y x2 y2 foo))
(+ x y)))))
(testing "hash-map literals"
(is= {:1 1 :2 2 :3 3}
(runner {:1 (pause 1)
:2 (pause 2)
:3 (pause 3)})))
(testing "hash-set literals"
(is= #{1 2 3}
(runner #{(pause 1)
(pause 2)
(pause 3)})))
(testing "vector literals"
(is= [1 2 3]
(runner [(pause 1)
(pause 2)
(pause 3)])))
(testing "dotimes"
(is= 42 (runner
(dotimes [x 10]
(pause x))
42)))
(testing "set! with field"
(let [x (js-obj)]
(runner (set! (.-foo x) "bar")
(is= (.-foo x) "bar"))
(is= (.-foo x) "bar")))
(testing "set! with var"
(def test-target 0)
(runner (set! test-target 42))
(is= test-target 42))
(testing "keywords as functions"
(is (= :bar
(runner (:foo (pause {:foo :bar}))))))
(testing "vectors as functions"
(is (= 2
(runner ([1 2] 1)))))
(testing "dot forms"
(is (= 8 (runner (. js/Math (pow 2 3)))))
(is (= 8 (runner (. js/Math pow 2 3)))))
(testing "quote"
(is= '(1 2 3)
(runner (pause '(1 2 3)))))
(testing "fn closures"
(is= 42
(runner
(let [x 42
_ (pause x)
f (fn [] x)]
(f)))))
(testing "case"
(is= 43
(runner
(let [value :bar]
(case value
:foo (pause 42)
:bar (pause 43)
:baz (pause 44)))))
(is= :default
(runner
(case :baz
:foo 44
:default)))
(is= nil
(runner
(case true
false false
nil)))
(is= 42
(runner
(loop [x 0]
(case (int x)
0 (recur (inc x))
1 42)))))
(testing "try"
(is= 42
(runner
(try 42
(catch js/Error ex ex))))
(is= 42
(runner
(try
(assert false)
(catch js/Error ex 42))))
(is= 42
(runner
(try
(assert false)
(catch :default ex 42))))
(let [a (atom false)
v (runner
(try
true
(catch js/Error ex false)
(finally (pause (reset! a true)))))]
(is (and @a v)))
(let [a (atom false)
v (runner
(try
(assert false)
(catch js/Error ex true)
(finally (reset! a true))))]
(is (and @a v)))
(testing "https://clojure.atlassian.net/browse/ASYNC-73"
(let [a (atom false)]
(runner
(try
(throw (js/Error. "asdf"))
(catch ExceptionInfo e)
(catch js/Error e)
(finally
(reset! a true))))
(is @a))
(is (runner
(try
(throw (new js/TypeError "unexpected"))
(catch ExceptionInfo e
false)
(catch :default e
true)))))
(testing "https://clojure.atlassian.net/browse/ASYNC-172"
(is (= 123 (runner
(try (throw 123)
(catch :default e 123))))))
(let [a (atom false)
v (try (runner
(try
(assert false)
(finally (reset! a true))))
(catch js/Error ex ex))]
(is (and @a v)))
(let [a (atom 0)
v (runner
(try
(try
42
(finally (swap! a inc)))
(finally (swap! a inc))))]
(is (= @a 2)))
(let [a (atom 0)
v (try (runner
(try
(try
(assert false)
(finally (swap! a inc)))
(finally (swap! a inc))))
(catch js/Error ex ex))]
(is (= @a 2)))
(let [a (atom 0)
v (try (runner
(try
(try
(assert false)
(catch js/Error ex (throw ex))
(finally (swap! a inc)))
(catch js/Error ex (throw ex))
(finally (swap! a inc))))
(catch js/Error ex ex))]
(is (= @a 2)))
(let [a (atom 0)
v (try (runner
(try
(try
(assert false)
(catch js/Error ex (pause (throw ex)))
(finally (pause (swap! a inc))))
(catch js/Error ex (pause (throw ex)))
(finally (pause (swap! a inc)))))
(catch js/Error ex ex))]
(is (= @a 2)))))