-
Notifications
You must be signed in to change notification settings - Fork 780
/
analyzer_tests.clj
337 lines (300 loc) · 11.6 KB
/
analyzer_tests.clj
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
(ns cljs.analyzer-tests
(:require [clojure.java.io :as io]
[cljs.analyzer :as a]
[cljs.env :as e]
[cljs.env :as env]
[cljs.analyzer.api :as ana-api])
(:use clojure.test))
;;******************************************************************************
;; cljs-warnings tests
;;******************************************************************************
(def warning-forms
{:undeclared-var (let [v (gensym)] `(~v 1 2 3))
:fn-arity '(do (defn x [a b] (+ a b))
(x 1 2 3 4))
:keyword-arity '(do (:argumentless-keyword-invocation))})
(defn warn-count [form]
(let [counter (atom 0)
tracker (fn [warning-type env & [extra]]
(when (warning-type a/*cljs-warnings*)
(swap! counter inc)))]
(a/with-warning-handlers [tracker]
(a/analyze (a/empty-env) form))
@counter))
(deftest no-warn
(is (every? zero? (map (fn [[name form]] (a/no-warn (warn-count form))) warning-forms))))
(deftest all-warn
(is (every? #(= 1 %) (map (fn [[name form]] (a/all-warn (warn-count form))) warning-forms))))
;; =============================================================================
;; NS parsing
(def ns-env (assoc-in (a/empty-env) [:ns :name] 'cljs.user))
(deftest spec-validation
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:require {:foo :bar})))
(catch Exception e
(.getMessage e)))
"Only [lib.ns & options] and lib.ns specs supported in :require / :require-macros"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:require [:foo :bar])))
(catch Exception e
(.getMessage e)))
"Library name must be specified as a symbol in :require / :require-macros"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:require [baz.woz :as woz :refer [] :plop])))
(catch Exception e
(.getMessage e)))
"Only :as alias and :refer (names) options supported in :require"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:require [baz.woz :as woz :refer [] :plop true])))
(catch Exception e
(.getMessage e)))
"Only :as and :refer options supported in :require / :require-macros"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:require [baz.woz :as woz :refer [] :as boz :refer []])))
(catch Exception e
(.getMessage e)))
"Each of :as and :refer options may only be specified once in :require / :require-macros"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:refer-clojure :refer [])))
(catch Exception e
(.getMessage e)))
"Only [:refer-clojure :exclude (names)] form supported"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:use [baz.woz :exclude []])))
(catch Exception e
(.getMessage e)))
"Only [lib.ns :only (names)] specs supported in :use / :use-macros"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:require [baz.woz :as []])))
(catch Exception e
(.getMessage e)))
":as must be followed by a symbol in :require / :require-macros"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:require [baz.woz :as woz] [noz.goz :as woz])))
(catch Exception e
(.getMessage e)))
":as alias must be unique"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:unless [])))
(catch Exception e
(.getMessage e)))
"Only :refer-clojure, :require, :require-macros, :use, :use-macros, and :import libspecs supported"))
(is (.startsWith
(try
(a/analyze ns-env '(ns foo.bar (:require baz.woz) (:require noz.goz)))
(catch Exception e
(.getMessage e)))
"Only one ")))
;; =============================================================================
;; Inference tests
(def test-cenv (atom {}))
(def test-env (assoc-in (a/empty-env) [:ns :name] 'cljs.core))
(a/no-warn
(e/with-compiler-env test-cenv
(binding [a/*analyze-deps* false]
(a/analyze-file (io/file "src/main/cljs/cljs/core.cljs")))))
(deftest basic-inference
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '1)))
'number))
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '"foo")))
'string))
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '(make-array 10))))
'array))
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '(js-obj))))
'object))
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '[])))
'cljs.core/IVector))
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '{})))
'cljs.core/IMap))
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '#{})))
'cljs.core/ISet))
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env ())))
'cljs.core/IList))
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '(fn [x] x))))
'function)))
(deftest if-inference
(is (= (a/no-warn
(e/with-compiler-env test-cenv
(:tag (a/analyze test-env '(if x "foo" 1)))))
'#{number string})))
(deftest method-inference
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '(.foo js/bar))))
'any)))
(deftest fn-inference
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env
; '(let [x (fn ([a] 1) ([a b] "foo") ([a b & r] ()))]
; (x :one)))))
; 'number))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env
; '(let [x (fn ([a] 1) ([a b] "foo") ([a b & r] ()))]
; (x :one :two)))))
; 'string))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env
; '(let [x (fn ([a] 1) ([a b] "foo") ([a b & r] ()))]
; (x :one :two :three)))))
; 'cljs.core/IList))
)
(deftest lib-inference
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '(+ 1 2))))
'number))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(alength (array)))))
; 'number))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(aclone (array)))))
; 'array))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(-count [1 2 3]))))
; 'number))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(count [1 2 3]))))
; 'number))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(into-array [1 2 3]))))
; 'array))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(js-obj))))
; 'object))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(-conj [] 1))))
; 'clj))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(conj [] 1))))
; 'clj))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(assoc nil :foo :bar))))
; 'clj))
;(is (= (e/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(dissoc {:foo :bar} :foo))))
; '#{clj clj-nil}))
)
(deftest test-always-true-if
(is (= (e/with-compiler-env test-cenv
(:tag (a/analyze test-env '(if 1 2 "foo"))))
'number)))
;; will only work if the previous test works
(deftest test-count
;(is (= (cljs.env/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(count []))))
; 'number))
)
(deftest test-numeric
;(is (= (a/no-warn
; (cljs.env/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(dec x)))))
; 'number))
;(is (= (a/no-warn
; (cljs.env/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(int x)))))
; 'number))
;(is (= (a/no-warn
; (cljs.env/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(unchecked-int x)))))
; 'number))
;(is (= (a/no-warn
; (cljs.env/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(mod x y)))))
; 'number))
;(is (= (a/no-warn
; (cljs.env/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(quot x y)))))
; 'number))
;(is (= (a/no-warn
; (cljs.env/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(rem x y)))))
; 'number))
;(is (= (a/no-warn
; (cljs.env/with-compiler-env test-cenv
; (:tag (a/analyze test-env '(bit-count n)))))
; 'number))
)
;; =============================================================================
;; Catching errors during macroexpansion
(deftest test-defn-error
(is (.startsWith
(try
(a/analyze test-env '(defn foo 123))
(catch Exception e
(.getMessage e)))
"Parameter declaration \"123\" should be a vector")))
;; =============================================================================
;; ns desugaring
(deftest test-cljs-975
(let [spec '((:require [bar :refer [baz] :refer-macros [quux]] :reload))]
(is (= (set (a/desugar-ns-specs spec))
(set '((:require-macros (bar :refer [quux]) :reload)
(:require (bar :refer [baz]) :reload)))))))
;; =============================================================================
;; Namespace metadata
(deftest test-namespace-metadata
(binding [a/*cljs-ns* a/*cljs-ns*]
(is (= (do (a/analyze ns-env '(ns weeble {:foo bar}))
(meta a/*cljs-ns*))
{:foo 'bar}))
(is (= (do (a/analyze ns-env '(ns ^{:foo bar} weeble))
(meta a/*cljs-ns*))
{:foo 'bar}))
(is (= (do (a/analyze ns-env '(ns ^{:foo bar} weeble {:baz quux}))
(meta a/*cljs-ns*))
{:foo 'bar :baz 'quux}))
(is (= (do (a/analyze ns-env '(ns ^{:foo bar} weeble {:foo baz}))
(meta a/*cljs-ns*))
{:foo 'baz}))
(is (= (meta (:name (a/analyze ns-env '(ns weeble {:foo bar}))))
{:foo 'bar}))
(is (= (meta (:name (a/analyze ns-env '(ns ^{:foo bar} weeble))))
{:foo 'bar}))
(is (= (meta (:name (a/analyze ns-env '(ns ^{:foo bar} weeble {:baz quux}))))
{:foo 'bar :baz 'quux}))
(is (= (meta (:name (a/analyze ns-env '(ns ^{:foo bar} weeble {:foo baz}))))
{:foo 'baz}))))
(deftest test-cljs-1105
;; munge turns - into _, must preserve the dash first
(is (not= (a/gen-constant-id :test-kw)
(a/gen-constant-id :test_kw))))
(deftest test-symbols-munge-cljs-1432
(is (not= (a/gen-constant-id :$)
(a/gen-constant-id :.)))
(is (not= (a/gen-constant-id '$)
(a/gen-constant-id '.))))
(deftest test-unicode-munging-cljs-1457
(is (= (a/gen-constant-id :C♯) 'cst$kw$C_u266f_)
(= (a/gen-constant-id 'C♯) 'cst$sym$C_u266f_)))
;; Constants
(deftest test-constants
(is (.startsWith
(try
(a/analyze test-env '(do (def ^:const foo 123) (def foo 246)))
(catch Exception e
(.getMessage e)))
"Can't redefine a constant"))
(is (.startsWith
(try
(a/analyze test-env '(do (def ^:const foo 123) (set! foo 246)))
(catch Exception e
(.getMessage e)))
"Can't set! a constant")))