Skip to content

Commit 8e1f6f2

Browse files
committed
start converting the externs inference stuff into real tests
1 parent fe021c1 commit 8e1f6f2

1 file changed

Lines changed: 56 additions & 26 deletions

File tree

src/test/clojure/cljs/analyzer_tests.clj

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[cljs.env :as env]
1414
[cljs.analyzer :as a]
1515
[cljs.analyzer.api :as ana-api]
16+
[cljs.compiler :as comp]
1617
[cljs.closure :as closure]
1718
[cljs.externs :as externs]
1819
[cljs.analyzer :as ana])
@@ -854,13 +855,12 @@
854855
))))
855856
)
856857

857-
(comment
858-
(require '[cljs.compiler :as cc])
859-
(require '[cljs.closure :as closure])
860-
858+
(deftest test-basic-infer
861859
(let [test-cenv (atom {::a/externs (externs/externs-map)})]
862860
(binding [a/*cljs-ns* a/*cljs-ns*
863-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
861+
a/*cljs-warnings* (assoc a/*cljs-warnings*
862+
:infer-warning true
863+
:undeclared-var false)]
864864
(e/with-compiler-env test-cenv
865865
(a/analyze-form-seq
866866
'[(ns foo.core)
@@ -871,24 +871,29 @@
871871
(.apply (.-log js/console) js/console (into-array args)))
872872
(js/console.log js/Number.MAX_VALUE)
873873
(js/console.log js/Symbol.iterator)])
874-
(cc/emit-externs
875-
(reduce util/map-merge {}
876-
(map (comp :externs second)
877-
(get @test-cenv ::a/namespaces)))))))
878-
874+
(is (= "var React;\nReact.Component;\n"
875+
(with-out-str
876+
(comp/emit-externs
877+
(reduce util/map-merge {}
878+
(map (comp :externs second)
879+
(get @test-cenv ::a/namespaces)))))))))))
880+
881+
(deftest test-method-infer
879882
(let [test-cenv (atom {::a/externs (externs/externs-map)})]
880883
(binding [a/*cljs-ns* a/*cljs-ns*
881884
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
882885
(e/with-compiler-env test-cenv
883886
(a/analyze-form-seq
884887
'[(defn foo [^js/React.Component c]
885888
(.render c))])
886-
(cc/emit-externs
887-
(reduce util/map-merge {}
888-
(map (comp :externs second)
889-
(get @test-cenv ::a/namespaces)))))))
890-
891-
;; works, does not generate extern
889+
(is (= "var React;\nReact.Component;\nReact.Component.prototype.render;\n"
890+
(with-out-str
891+
(comp/emit-externs
892+
(reduce util/map-merge {}
893+
(map (comp :externs second)
894+
(get @test-cenv ::a/namespaces)))))))))))
895+
896+
(deftest test-minimal-infer
892897
(let [test-cenv (atom {::a/externs (externs/externs-map
893898
(closure/load-externs
894899
{:externs ["src/test/externs/test.js"]}))})]
@@ -897,12 +902,14 @@
897902
(e/with-compiler-env test-cenv
898903
(a/analyze-form-seq
899904
'[(js/console.log (.wozMethod (js/baz)))])
900-
(cc/emit-externs
901-
(reduce util/map-merge {}
902-
(map (comp :externs second)
903-
(get @test-cenv ::a/namespaces)))))))
904-
905-
;; works, does not generate extern
905+
(is (= ""
906+
(with-out-str
907+
(comp/emit-externs
908+
(reduce util/map-merge {}
909+
(map (comp :externs second)
910+
(get @test-cenv ::a/namespaces)))))))))))
911+
912+
(deftest test-type-hint-minimal-infer
906913
(let [test-cenv (atom {::a/externs (externs/externs-map
907914
(closure/load-externs
908915
{:externs ["src/test/externs/test.js"]}))})]
@@ -912,10 +919,33 @@
912919
(a/analyze-form-seq
913920
'[(defn afun [^js/Foo x]
914921
(.wozMethod x))])
915-
(cc/emit-externs
916-
(reduce util/map-merge {}
917-
(map (comp :externs second)
918-
(get @test-cenv ::a/namespaces)))))))
922+
(is (= ""
923+
(with-out-str
924+
(comp/emit-externs
925+
(reduce util/map-merge {}
926+
(map (comp :externs second)
927+
(get @test-cenv ::a/namespaces)))))))))))
928+
929+
(deftest test-type-hint-minimal-infer-unknown-method
930+
(let [test-cenv (atom {::a/externs (externs/externs-map
931+
(closure/load-externs
932+
{:externs ["src/test/externs/test.js"]}))})]
933+
(binding [a/*cljs-ns* a/*cljs-ns*
934+
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
935+
(e/with-compiler-env test-cenv
936+
(a/analyze-form-seq
937+
'[(defn baz [^js/Foo a]
938+
(.gozMethod a))])
939+
(is (= "Foo.prototype.gozMethod;\n"
940+
(with-out-str
941+
(comp/emit-externs
942+
(reduce util/map-merge {}
943+
(map (comp :externs second)
944+
(get @test-cenv ::a/namespaces)))))))))))
945+
946+
(comment
947+
(require '[cljs.compiler :as cc])
948+
(require '[cljs.closure :as closure])
919949

920950
;; FIXME: generates externs we know about including the one we don't
921951
(let [test-cenv (atom {::a/externs (externs/externs-map

0 commit comments

Comments
 (0)