Skip to content

Commit dba74da

Browse files
committed
add some basic inference tests
1 parent fc79102 commit dba74da

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

test/clj/cljs/analyzer_tests.clj

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(ns cljs.analyzer-tests
2-
(:require [cljs.analyzer :as a])
2+
(:require [clojure.java.io :as io]
3+
[cljs.analyzer :as a]
4+
[cljs.env :as e])
35
(:use clojure.test))
46

57
;;******************************************************************************
@@ -31,3 +33,60 @@
3133

3234
(deftest all-warn
3335
(is (every? #(= 1 %) (map (fn [[name form]] (a/all-warn (warn-count form))) warning-forms))))
36+
37+
;; =============================================================================
38+
;; Inference tests
39+
40+
(def test-cenv (atom {}))
41+
(def test-env (assoc-in (a/empty-env) [:ns :name] 'cljs.core))
42+
43+
(e/with-compiler-env test-cenv
44+
(a/analyze-file (io/file "src/cljs/cljs/core.cljs")))
45+
46+
(deftest basic-inference
47+
(is (= (e/with-compiler-env test-cenv
48+
(:tag (a/analyze test-env '1)))
49+
'number))
50+
(is (= (e/with-compiler-env test-cenv
51+
(:tag (a/analyze test-env '"foo")))
52+
'string))
53+
(is (= (e/with-compiler-env test-cenv
54+
(:tag (a/analyze test-env '(make-array 10))))
55+
'array))
56+
(is (= (e/with-compiler-env test-cenv
57+
(:tag (a/analyze test-env '(js-obj))))
58+
'object))
59+
(is (= (e/with-compiler-env test-cenv
60+
(:tag (a/analyze test-env '[])))
61+
'cljs.core/IVector))
62+
(is (= (e/with-compiler-env test-cenv
63+
(:tag (a/analyze test-env '{})))
64+
'cljs.core/IMap))
65+
(is (= (e/with-compiler-env test-cenv
66+
(:tag (a/analyze test-env '#{})))
67+
'cljs.core/ISet))
68+
(is (= (e/with-compiler-env test-cenv
69+
(:tag (a/analyze test-env ())))
70+
'cljs.core/IList))
71+
(is (= (e/with-compiler-env test-cenv
72+
(:tag (a/analyze test-env '(fn [x] x))))
73+
'function)))
74+
75+
(deftest if-inference
76+
(is (= (e/with-compiler-env test-cenv
77+
(:tag (a/analyze test-env '(if true "foo" 1))))
78+
'#{number string})))
79+
80+
(deftest lib-inference
81+
(is (= (e/with-compiler-env test-cenv
82+
(:tag (a/analyze test-env '(-conj [] 1))))
83+
'clj))
84+
(is (= (e/with-compiler-env test-cenv
85+
(:tag (a/analyze test-env '(conj [] 1))))
86+
'clj))
87+
(is (= (e/with-compiler-env test-cenv
88+
(:tag (a/analyze test-env '(assoc nil :foo :bar))))
89+
'clj))
90+
(is (= (e/with-compiler-env test-cenv
91+
(:tag (a/analyze test-env '(dissoc {:foo :bar} :foo))))
92+
'clj)))

0 commit comments

Comments
 (0)