|
180 | 180 | (.getMessage (.getCause e))))
|
181 | 181 | "Only one ")))
|
182 | 182 |
|
183 |
| -;; ============================================================================= |
184 |
| -;; Inference tests |
185 |
| - |
186 | 183 | (def test-cenv (atom {}))
|
187 | 184 | (def test-env (assoc-in (ana/empty-env) [:ns :name] 'cljs.core))
|
188 | 185 | (def test-core-env (atom {}))
|
|
207 | 204 | (binding [ana/*analyze-deps* false]
|
208 | 205 | (ana/analyze-file (io/file "src/main/cljs/cljs/core.cljs")))))
|
209 | 206 |
|
210 |
| -(deftest basic-inference |
211 |
| - (is (= (env/with-compiler-env test-cenv |
212 |
| - (:tag (analyze test-env '1))) |
213 |
| - 'number)) |
214 |
| - (is (= (env/with-compiler-env test-cenv |
215 |
| - (:tag (analyze test-env '"foo"))) |
216 |
| - 'string)) |
217 |
| - (is (= (env/with-compiler-env test-cenv |
218 |
| - (:tag (analyze test-env '\a))) |
219 |
| - 'string)) |
220 |
| - (is (= (env/with-compiler-env test-cenv |
221 |
| - (:tag (analyze test-env '(make-array 10)))) |
222 |
| - 'array)) |
223 |
| - (is (= (env/with-compiler-env test-cenv |
224 |
| - (:tag (analyze test-env '(js-obj)))) |
225 |
| - 'object)) |
226 |
| - (is (= (env/with-compiler-env test-cenv |
227 |
| - (:tag (analyze test-env '[]))) |
228 |
| - 'cljs.core/IVector)) |
229 |
| - (is (= (env/with-compiler-env test-cenv |
230 |
| - (:tag (analyze test-env '{}))) |
231 |
| - 'cljs.core/IMap)) |
232 |
| - (is (= (env/with-compiler-env test-cenv |
233 |
| - (:tag (analyze test-env '#{}))) |
234 |
| - 'cljs.core/ISet)) |
235 |
| - (is (= (env/with-compiler-env test-cenv |
236 |
| - (:tag (analyze test-env ()))) |
237 |
| - 'cljs.core/IList)) |
238 |
| - (is (= (env/with-compiler-env test-cenv |
239 |
| - (:tag (analyze test-env '(fn [x] x)))) |
240 |
| - 'function))) |
241 |
| - |
242 |
| -(deftest if-inference |
243 |
| - (is (= (ana/no-warn |
244 |
| - (env/with-compiler-env test-cenv |
245 |
| - (:tag (analyze test-env '(if x "foo" 1))))) |
246 |
| - '#{number string}))) |
247 |
| - |
248 |
| -(deftest if-induced-inference |
249 |
| - (is (= (ana/no-warn |
250 |
| - (env/with-compiler-env test-cenv |
251 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (nil? x) x :kw)))))) |
252 |
| - '#{clj-nil cljs.core/Keyword})) |
253 |
| - (is (= (ana/no-warn |
254 |
| - (env/with-compiler-env test-cenv |
255 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (boolean? x) x :kw)))))) |
256 |
| - '#{boolean cljs.core/Keyword})) |
257 |
| - (is (= (ana/no-warn |
258 |
| - (env/with-compiler-env test-cenv |
259 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (number? x) x :kw)))))) |
260 |
| - '#{number cljs.core/Keyword})) |
261 |
| - (is (= (ana/no-warn |
262 |
| - (env/with-compiler-env test-cenv |
263 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (double? x) x :kw)))))) |
264 |
| - '#{number cljs.core/Keyword})) |
265 |
| - (is (= (ana/no-warn |
266 |
| - (env/with-compiler-env test-cenv |
267 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (float? x) x :kw)))))) |
268 |
| - '#{number cljs.core/Keyword})) |
269 |
| - (is (= (ana/no-warn |
270 |
| - (env/with-compiler-env test-cenv |
271 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (integer? x) x :kw)))))) |
272 |
| - '#{number cljs.core/Keyword})) |
273 |
| - (is (= (ana/no-warn |
274 |
| - (env/with-compiler-env test-cenv |
275 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (seq? x) x :kw)))))) |
276 |
| - '#{seq cljs.core/Keyword})) |
277 |
| - (is (= (ana/no-warn |
278 |
| - (env/with-compiler-env test-cenv |
279 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (array? x) x :kw)))))) |
280 |
| - '#{array cljs.core/Keyword})) |
281 |
| - (is (= (ana/no-warn |
282 |
| - (env/with-compiler-env test-cenv |
283 |
| - (:tag (ana/analyze test-env '(let [x ^any []] (if (seqable? x) x :kw)))))) |
284 |
| - '#{cljs.core/ISeqable array string cljs.core/Keyword})) |
285 |
| - (is (= (ana/no-warn |
286 |
| - (env/with-compiler-env test-cenv |
287 |
| - (:tag (ana/analyze test-env '(let [x (namespace :x)] (if x x :kw)))))) |
288 |
| - '#{string cljs.core/Keyword}))) |
289 |
| - |
290 |
| -(deftest loop-recur-inference |
291 |
| - (is (= (ana/no-warn |
292 |
| - (env/with-compiler-env test-cenv |
293 |
| - (:tag (analyze test-env '(loop [x "a"] x))))) |
294 |
| - 'string)) |
295 |
| - (is (= (ana/no-warn |
296 |
| - (env/with-compiler-env test-cenv |
297 |
| - (:tag (analyze test-env '(loop [x 10] |
298 |
| - (if (pos? x) |
299 |
| - (dec x) |
300 |
| - x)))))) |
301 |
| - 'number)) |
302 |
| - (is (= (ana/no-warn |
303 |
| - (env/with-compiler-env test-cenv |
304 |
| - (:tag (analyze test-env '((fn [p?] |
305 |
| - (loop [x nil] |
306 |
| - (if (p? x) |
307 |
| - x |
308 |
| - (recur (str x))))) |
309 |
| - 11))))) |
310 |
| - '#{string clj-nil})) |
311 |
| - (is (= (ana/no-warn |
312 |
| - (env/with-compiler-env test-cenv |
313 |
| - (:tag (analyze test-env '((fn [^string x] |
314 |
| - (loop [y x] |
315 |
| - (if (= "x" y) |
316 |
| - y |
317 |
| - (recur 1)))) |
318 |
| - "a"))))) |
319 |
| - '#{number string}))) |
320 |
| - |
321 |
| -(deftest method-inference |
322 |
| - (is (= (env/with-compiler-env test-cenv |
323 |
| - (:tag (analyze test-env '(.foo js/bar)))) |
324 |
| - 'js))) |
325 |
| - |
326 |
| -(deftest fn-method-inference |
327 |
| - ;; should always infer 'function as tag |
328 |
| - (is (= 'function |
329 |
| - (:tag |
330 |
| - (env/with-compiler-env test-cenv |
331 |
| - (analyze test-env |
332 |
| - '(fn ([a] 1) ([a b] "foo") ([a b & r] ()))))))) |
333 |
| - (is (nil? |
334 |
| - (:ret-tag |
335 |
| - (env/with-compiler-env test-cenv |
336 |
| - (analyze test-env |
337 |
| - '(fn ([a] 1) ([a b] "foo") ([a b & r] ())))))) ) |
338 |
| - ;; methods should have inferred types |
339 |
| - (is (= '(number string cljs.core/IList) |
340 |
| - (map :tag |
341 |
| - (:methods |
342 |
| - (env/with-compiler-env test-cenv |
343 |
| - (analyze test-env |
344 |
| - '(fn ([a] 1) ([a b] "foo") ([a b & r] ()))))))))) |
345 |
| - |
346 |
| -(deftest fn-inference |
347 |
| - (is (= 'number |
348 |
| - (env/with-compiler-env test-cenv |
349 |
| - (:tag (analyze test-env |
350 |
| - '(let [x (fn ([a] 1) ([a b] "foo") ([a b & r] ()))] |
351 |
| - (x :one))))))) |
352 |
| - (is (= 'string |
353 |
| - (env/with-compiler-env test-cenv |
354 |
| - (:tag (analyze test-env |
355 |
| - '(let [x (fn ([a] 1) ([a b] "foo") ([a b & r] ()))] |
356 |
| - (x :one :two))))))) |
357 |
| - (is (= 'cljs.core/IList |
358 |
| - (env/with-compiler-env test-cenv |
359 |
| - (:tag (analyze test-env |
360 |
| - '(let [x (fn ([a] 1) ([a b] "foo") ([a b & r] ()))] |
361 |
| - (x :one :two :three))))))) |
362 |
| - (is (= 'cljs.core/IList |
363 |
| - (env/with-compiler-env test-cenv |
364 |
| - (:tag (analyze test-env |
365 |
| - '(let [x (fn ([a] 1) ([a b] "foo") ([a b & r] ()))] |
366 |
| - (x :one :two :three :four)))))))) |
367 |
| - |
368 |
| -(deftest top-fn-inference |
369 |
| - (env/with-compiler-env test-cenv |
370 |
| - (ana/analyze-form-seq |
371 |
| - '[(ns test.cljs-2901) |
372 |
| - (defn foo |
373 |
| - ([a] 1) |
374 |
| - ([a b] "foo") |
375 |
| - ([a b & r] ())) |
376 |
| - (foo :one)])) |
377 |
| - (is (= '[number string cljs.core/IList] |
378 |
| - (map :tag |
379 |
| - (get-in @test-cenv [::ana/namespaces 'test.cljs-2901 :defs 'foo :methods])))) |
380 |
| - (is (= 'number |
381 |
| - (:tag |
382 |
| - (env/with-compiler-env test-cenv |
383 |
| - (ana/analyze-form-seq |
384 |
| - '[(ns test.cljs-2901) |
385 |
| - (defn foo |
386 |
| - ([a] 1) |
387 |
| - ([a b] "foo") |
388 |
| - ([a b & r] ())) |
389 |
| - (foo :one)] |
390 |
| - nil true))))) |
391 |
| - (is (= 'string |
392 |
| - (:tag |
393 |
| - (env/with-compiler-env test-cenv |
394 |
| - (ana/analyze-form-seq |
395 |
| - '[(ns test.cljs-2901) |
396 |
| - (defn foo |
397 |
| - ([a] 1) |
398 |
| - ([a b] "foo") |
399 |
| - ([a b & r] ())) |
400 |
| - (foo :one :two)] |
401 |
| - nil true))))) |
402 |
| - (is (= 'cljs.core/IList |
403 |
| - (:tag |
404 |
| - (env/with-compiler-env test-cenv |
405 |
| - (ana/analyze-form-seq |
406 |
| - '[(ns test.cljs-2901) |
407 |
| - (defn foo |
408 |
| - ([a] 1) |
409 |
| - ([a b] "foo") |
410 |
| - ([a b & r] ())) |
411 |
| - (foo :one :two :three)] |
412 |
| - nil true)))))) |
413 |
| - |
414 |
| -(deftest variadic-fn-inference |
415 |
| - (is (= '(cljs.core/IList) |
416 |
| - (map :tag |
417 |
| - (:methods |
418 |
| - (env/with-compiler-env test-cenv |
419 |
| - (analyze test-env |
420 |
| - '(fn ([a b & r] ())))))))) |
421 |
| - (is (= 'cljs.core/IList |
422 |
| - (env/with-compiler-env test-cenv |
423 |
| - (:tag (analyze test-env |
424 |
| - '(let [x (fn ([a b & r] ()))] |
425 |
| - (x :one :two))))))) |
426 |
| - |
427 |
| - (is (= 'cljs.core/IList |
428 |
| - (env/with-compiler-env test-cenv |
429 |
| - (:tag (analyze test-env |
430 |
| - '(let [x (fn ([a b & r] ()))] |
431 |
| - (x :one :two :three))))))) |
432 |
| - |
433 |
| - (is (= 'cljs.core/IList |
434 |
| - (env/with-compiler-env test-cenv |
435 |
| - (:tag (analyze test-env |
436 |
| - '(let [x (fn ([a b & r] ()))] |
437 |
| - (x :one :two :three :four))))))) |
438 |
| - ) |
439 |
| - |
440 |
| -(deftest top-variadic-fn-inference |
441 |
| - (env/with-compiler-env test-cenv |
442 |
| - (ana/analyze-form-seq |
443 |
| - '[(ns test.cljs-2901-b) |
444 |
| - (defn foo ([a b & r] ())) |
445 |
| - (foo :one :two :three :four)] |
446 |
| - nil false)) |
447 |
| - (is (= '[cljs.core/IList] |
448 |
| - (map :tag |
449 |
| - (get-in @test-cenv |
450 |
| - [::ana/namespaces 'test.cljs-2901-b :defs 'foo :methods])))) |
451 |
| - (is (= 'cljs.core/IList |
452 |
| - (:tag |
453 |
| - (env/with-compiler-env test-cenv |
454 |
| - (ana/analyze-form-seq |
455 |
| - '[(ns test.cljs-2901-b) |
456 |
| - (defn foo ([a b & r] ())) |
457 |
| - (foo :one :two)] |
458 |
| - nil true))))) |
459 |
| - (is (= 'cljs.core/IList |
460 |
| - (:tag |
461 |
| - (env/with-compiler-env test-cenv |
462 |
| - (ana/analyze-form-seq |
463 |
| - '[(ns test.cljs-2901-b) |
464 |
| - (defn foo ([a b & r] ())) |
465 |
| - (foo :one :two :three)] |
466 |
| - nil true))))) |
467 |
| - (is (= 'cljs.core/IList |
468 |
| - (:tag |
469 |
| - (env/with-compiler-env test-cenv |
470 |
| - (ana/analyze-form-seq |
471 |
| - '[(ns test.cljs-2901-b) |
472 |
| - (defn foo ([a b & r] ())) |
473 |
| - (foo :one :two :three :four)] |
474 |
| - nil true)))))) |
475 |
| - |
476 |
| -(deftest lib-inference |
477 |
| - (is (= (env/with-compiler-env test-cenv |
478 |
| - (:tag (analyze test-env '(+ 1 2)))) |
479 |
| - 'number)) |
480 |
| - (is (= (env/with-compiler-env test-cenv |
481 |
| - (:tag (analyze test-env '(alength (array))))) |
482 |
| - 'number)) |
483 |
| - (is (= (env/with-compiler-env test-cenv |
484 |
| - (:tag (analyze test-env '(aclone (array))))) |
485 |
| - 'array)) |
486 |
| - (is (= (env/with-compiler-env test-cenv |
487 |
| - (:tag (analyze test-env '(-count [1 2 3])))) |
488 |
| - 'number)) |
489 |
| - (is (= (env/with-compiler-env test-cenv |
490 |
| - (:tag (analyze test-env '(count [1 2 3])))) |
491 |
| - 'number)) |
492 |
| - (is (= (env/with-compiler-env test-cenv |
493 |
| - (:tag (analyze test-env '(into-array [1 2 3])))) |
494 |
| - 'array)) |
495 |
| - (is (= (env/with-compiler-env test-cenv |
496 |
| - (:tag (analyze test-env '(js-obj)))) |
497 |
| - 'object)) |
498 |
| - (is (= (env/with-compiler-env test-cenv |
499 |
| - (:tag (analyze test-env '(-conj [] 1)))) |
500 |
| - 'clj)) |
501 |
| - (is (= (env/with-compiler-env test-cenv |
502 |
| - (:tag (analyze test-env '(conj [] 1)))) |
503 |
| - 'clj)) |
504 |
| - (is (= (env/with-compiler-env test-cenv |
505 |
| - (:tag (analyze test-env '(dissoc {:foo :bar} :foo)))) |
506 |
| - '#{clj clj-nil})) |
507 |
| - ;; has changed, why does this return #{clj any} ? |
508 |
| - ;(is (= (env/with-compiler-env test-cenv |
509 |
| - ; (:tag (analyze test-env '(assoc nil :foo :bar)))) |
510 |
| - ; 'clj)) |
511 |
| - ) |
512 |
| - |
513 |
| -(deftest test-always-true-if |
514 |
| - (is (= (env/with-compiler-env test-cenv |
515 |
| - (:tag (analyze test-env '(if 1 2 "foo")))) |
516 |
| - 'number))) |
517 |
| - |
518 |
| -;; will only work if the previous test works |
519 |
| -(deftest test-count |
520 |
| - (is (= (cljs.env/with-compiler-env test-cenv |
521 |
| - (:tag (analyze test-env '(count [])))) |
522 |
| - 'number)) |
523 |
| - ) |
524 |
| - |
525 |
| -(deftest test-numeric |
526 |
| - (is (= (ana/no-warn |
527 |
| - (cljs.env/with-compiler-env test-cenv |
528 |
| - (:tag (analyze test-env '(dec x))))) |
529 |
| - 'number)) |
530 |
| - (is (= (ana/no-warn |
531 |
| - (cljs.env/with-compiler-env test-cenv |
532 |
| - (:tag (analyze test-env '(int x))))) |
533 |
| - 'number)) |
534 |
| - (is (= (ana/no-warn |
535 |
| - (cljs.env/with-compiler-env test-cenv |
536 |
| - (:tag (analyze test-env '(unchecked-int x))))) |
537 |
| - 'number)) |
538 |
| - (is (= (ana/no-warn |
539 |
| - (cljs.env/with-compiler-env test-cenv |
540 |
| - (:tag (analyze test-env '(mod x y))))) |
541 |
| - 'number)) |
542 |
| - (is (= (ana/no-warn |
543 |
| - (cljs.env/with-compiler-env test-cenv |
544 |
| - (:tag (analyze test-env '(quot x y))))) |
545 |
| - 'number)) |
546 |
| - (is (= (ana/no-warn |
547 |
| - (cljs.env/with-compiler-env test-cenv |
548 |
| - (:tag (analyze test-env '(rem x y))))) |
549 |
| - 'number)) |
550 |
| - (is (= (ana/no-warn |
551 |
| - (cljs.env/with-compiler-env test-cenv |
552 |
| - (:tag (analyze test-env '(bit-count n))))) |
553 |
| - 'number)) |
554 |
| - ) |
555 |
| - |
556 | 207 | ;; =============================================================================
|
557 | 208 | ;; Catching errors during macroexpansion
|
558 | 209 |
|
|
0 commit comments