Skip to content

Commit fdc2d8d

Browse files
committed
cleanup infer tests w/ helper
1 parent f12bce5 commit fdc2d8d

1 file changed

Lines changed: 100 additions & 170 deletions

File tree

src/test/clojure/cljs/analyzer_tests.clj

Lines changed: 100 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -856,190 +856,120 @@
856856
))))
857857
)
858858

859+
(defn infer-test-helper [{:keys [forms externs warnings]}]
860+
(let [test-cenv (atom {::a/externs
861+
(externs/externs-map
862+
(closure/load-externs {:externs (or externs [])}))})]
863+
(a/with-warning-handlers [(collecting-warning-handler (or warnings (atom [])))]
864+
(binding [a/*cljs-ns* a/*cljs-ns*
865+
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
866+
(e/with-compiler-env test-cenv
867+
(a/analyze-form-seq forms)
868+
(with-out-str
869+
(comp/emit-externs
870+
(reduce util/map-merge {}
871+
(map (comp :externs second)
872+
(get @test-cenv ::a/namespaces))))))))))
873+
859874
(deftest test-basic-infer
860-
(let [test-cenv (atom {::a/externs (externs/externs-map)})]
861-
(binding [a/*cljs-ns* a/*cljs-ns*
862-
a/*cljs-warnings* (assoc a/*cljs-warnings*
863-
:infer-warning true
864-
:undeclared-var false)]
865-
(e/with-compiler-env test-cenv
866-
(a/analyze-form-seq
867-
'[(ns foo.core)
868-
(defn bar [a] (js/parseInt a))
869-
(def c js/React.Component)
870-
(js/console.log "Hello world!")
871-
(fn [& args]
872-
(.apply (.-log js/console) js/console (into-array args)))
873-
(js/console.log js/Number.MAX_VALUE)
874-
(js/console.log js/Symbol.iterator)])
875-
(is (= "var React;\nReact.Component;\n"
876-
(with-out-str
877-
(comp/emit-externs
878-
(reduce util/map-merge {}
879-
(map (comp :externs second)
880-
(get @test-cenv ::a/namespaces)))))))))))
875+
(let [res (infer-test-helper
876+
{:forms '[(ns foo.core)
877+
(defn bar [a] (js/parseInt a))
878+
(def c js/React.Component)
879+
(js/console.log "Hello world!")
880+
(fn [& args]
881+
(.apply (.-log js/console) js/console (into-array args)))
882+
(js/console.log js/Number.MAX_VALUE)
883+
(js/console.log js/Symbol.iterator)]})]
884+
(is (= "var React;\nReact.Component;\n" res))))
881885

882886
(deftest test-method-infer
883-
(let [test-cenv (atom {::a/externs (externs/externs-map)})]
884-
(binding [a/*cljs-ns* a/*cljs-ns*
885-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
886-
(e/with-compiler-env test-cenv
887-
(a/analyze-form-seq
888-
'[(defn foo [^js/React.Component c]
889-
(.render c))])
890-
(is (= "var React;\nReact.Component;\nReact.Component.prototype.render;\n"
891-
(with-out-str
892-
(comp/emit-externs
893-
(reduce util/map-merge {}
894-
(map (comp :externs second)
895-
(get @test-cenv ::a/namespaces)))))))))))
887+
(let [res (infer-test-helper
888+
{:forms '[(defn foo [^js/React.Component c]
889+
(.render c))]})]
890+
(is (= "var React;\nReact.Component;\nReact.Component.prototype.render;\n"
891+
res))))
896892

897893
(deftest test-minimal-infer
898-
(let [test-cenv (atom {::a/externs (externs/externs-map
899-
(closure/load-externs
900-
{:externs ["src/test/externs/test.js"]}))})]
901-
(binding [a/*cljs-ns* a/*cljs-ns*
902-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
903-
(e/with-compiler-env test-cenv
904-
(a/analyze-form-seq
905-
'[(js/console.log (.wozMethod (js/baz)))])
906-
(is (= ""
907-
(with-out-str
908-
(comp/emit-externs
909-
(reduce util/map-merge {}
910-
(map (comp :externs second)
911-
(get @test-cenv ::a/namespaces)))))))))))
894+
(let [res (infer-test-helper
895+
{:forms '[(js/console.log (.wozMethod (js/baz)))]
896+
:externs ["src/test/externs/test.js"]})]
897+
(is (string/blank? res))))
912898

913899
(deftest test-type-hint-minimal-infer
914-
(let [test-cenv (atom {::a/externs (externs/externs-map
915-
(closure/load-externs
916-
{:externs ["src/test/externs/test.js"]}))})]
917-
(binding [a/*cljs-ns* a/*cljs-ns*
918-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
919-
(e/with-compiler-env test-cenv
920-
(a/analyze-form-seq
921-
'[(defn afun [^js/Foo x]
922-
(.wozMethod x))])
923-
(is (= ""
924-
(with-out-str
925-
(comp/emit-externs
926-
(reduce util/map-merge {}
927-
(map (comp :externs second)
928-
(get @test-cenv ::a/namespaces)))))))))))
900+
(let [res (infer-test-helper
901+
{:forms ''[(defn afun [^js/Foo x]
902+
(.wozMethod x))]
903+
:externs ["src/test/externs/test.js"]})]
904+
(is (string/blank? res))))
929905

930906
(deftest test-type-hint-infer-unknown-method-in-chain
931-
(let [ws (atom [])
932-
test-cenv (atom {::a/externs (externs/externs-map
933-
(closure/load-externs
934-
{:externs ["src/test/externs/test.js"]}))})]
935-
(a/with-warning-handlers [(collecting-warning-handler ws)]
936-
(binding [a/*cljs-ns* a/*cljs-ns*
937-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
938-
(e/with-compiler-env test-cenv
939-
(a/analyze-form-seq
940-
'[(defn afun [^js/Foo.Bar x]
941-
(let [z (.baz x)]
942-
(.wozz z)))])
943-
(is (= "Foo.Boo.prototype.wozz;\n"
944-
(with-out-str
945-
(comp/emit-externs
946-
(reduce util/map-merge {}
947-
(map (comp :externs second)
948-
(get @test-cenv ::a/namespaces)))))))
949-
(is (= 1 (count @ws)))
950-
(is (string/starts-with?
951-
(first @ws)
952-
"Cannot resolve property wozz for inferred type js/Foo.Boo")))))))
907+
(let [ws (atom [])
908+
res (infer-test-helper
909+
{:forms '[(defn afun [^js/Foo.Bar x]
910+
(let [z (.baz x)]
911+
(.wozz z)))]
912+
:externs ["src/test/externs/test.js"]
913+
:warnings ws})]
914+
(is (= "Foo.Boo.prototype.wozz;\n" res))
915+
(is (= 1 (count @ws)))
916+
(is (string/starts-with?
917+
(first @ws)
918+
"Cannot resolve property wozz for inferred type js/Foo.Boo"))))
953919

954920
(deftest test-type-hint-infer-unknown-property-in-chain
955-
(let [ws (atom [])
956-
test-cenv (atom {::a/externs (externs/externs-map
957-
(closure/load-externs
958-
{:externs ["src/test/externs/test.js"]}))})]
959-
(a/with-warning-handlers [(collecting-warning-handler ws)]
960-
(binding [a/*cljs-ns* a/*cljs-ns*
961-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
962-
(e/with-compiler-env test-cenv
963-
(a/analyze-form-seq
964-
'[(defn afun [^js/Foo.Bar x]
965-
(let [z (.baz x)]
966-
(.-wozz z)))])
967-
(is (= "Foo.Boo.prototype.wozz;\n"
968-
(with-out-str
969-
(comp/emit-externs
970-
(reduce util/map-merge {}
971-
(map (comp :externs second)
972-
(get @test-cenv ::a/namespaces)))))))
973-
(is (= 1 (count @ws)))
974-
(is (string/starts-with?
975-
(first @ws)
976-
"Cannot resolve property wozz for inferred type js/Foo.Boo")))))))
921+
(let [ws (atom [])
922+
res (infer-test-helper
923+
{:forms '[(defn afun [^js/Foo.Bar x]
924+
(let [z (.baz x)]
925+
(.-wozz z)))]
926+
:externs ["src/test/externs/test.js"]
927+
:warnings ws})]
928+
(is (= "Foo.Boo.prototype.wozz;\n" res))
929+
(is (= 1 (count @ws)))
930+
(is (string/starts-with?
931+
(first @ws)
932+
"Cannot resolve property wozz for inferred type js/Foo.Boo"))))
977933

978934
(deftest test-type-hint-infer-unknown-method
979-
(let [ws (atom [])
980-
test-cenv (atom {::a/externs (externs/externs-map
981-
(closure/load-externs
982-
{:externs ["src/test/externs/test.js"]}))})]
983-
(a/with-warning-handlers [(collecting-warning-handler ws)]
984-
(binding [a/*cljs-ns* a/*cljs-ns*
985-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
986-
(e/with-compiler-env test-cenv
987-
(a/analyze-form-seq
988-
'[(defn baz [^js/Foo a]
989-
(.gozMethod a))])
990-
(is (= "Foo.prototype.gozMethod;\n"
991-
(with-out-str
992-
(comp/emit-externs
993-
(reduce util/map-merge {}
994-
(map (comp :externs second)
995-
(get @test-cenv ::a/namespaces)))))))
996-
(is (= 1 (count @ws)))
997-
(is (string/starts-with?
998-
(first @ws)
999-
"Cannot resolve property gozMethod for inferred type js/Foo")))))))
935+
(let [ws (atom [])
936+
res (infer-test-helper
937+
{:forms '[(defn baz [^js/Foo a]
938+
(.gozMethod a))]
939+
:externs ["src/test/externs/test.js"]
940+
:warnings ws})]
941+
(is (= "Foo.prototype.gozMethod;\n" res))
942+
(is (= 1 (count @ws)))
943+
(is (string/starts-with?
944+
(first @ws)
945+
"Cannot resolve property gozMethod for inferred type js/Foo"))))
1000946

1001947
(deftest test-infer-unknown-method-from-externs
1002-
(let [ws (atom [])
1003-
test-cenv (atom {::a/externs (externs/externs-map
1004-
(closure/load-externs
1005-
{:externs ["src/test/externs/test.js"]}))})]
1006-
(a/with-warning-handlers [(collecting-warning-handler ws)]
1007-
(binding [a/*cljs-ns* a/*cljs-ns*
1008-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
1009-
(e/with-compiler-env test-cenv
1010-
(a/analyze-form-seq
1011-
'[(.gozMethod (js/baz))])
1012-
(is (= "Foo.prototype.gozMethod;\n"
1013-
(with-out-str
1014-
(comp/emit-externs
1015-
(reduce util/map-merge {}
1016-
(map (comp :externs second)
1017-
(get @test-cenv ::a/namespaces)))))))
1018-
(is (= 1 (count @ws)))
1019-
(is (string/starts-with?
1020-
(first @ws)
1021-
"Cannot resolve property gozMethod for inferred type js/Foo")))))))
948+
(let [ws (atom [])
949+
res (infer-test-helper
950+
{:forms '[(.gozMethod (js/baz))]
951+
:externs ["src/test/externs/test.js"]
952+
:warnings ws})]
953+
(is (= "Foo.prototype.gozMethod;\n" res))
954+
(is (= 1 (count @ws)))
955+
(is (string/starts-with?
956+
(first @ws)
957+
"Cannot resolve property gozMethod for inferred type js/Foo"))))
1022958

1023959
(deftest test-infer-js-require
1024-
(let [ws (atom [])
1025-
test-cenv (atom {::a/externs (externs/externs-map
1026-
(closure/load-externs
1027-
{:externs ["src/test/externs/test.js"]}))})]
1028-
(a/with-warning-handlers [(collecting-warning-handler ws)]
1029-
(binding [a/*cljs-ns* a/*cljs-ns*
1030-
a/*cljs-warnings* (assoc a/*cljs-warnings* :infer-warning true)]
1031-
(e/with-compiler-env test-cenv
1032-
(a/analyze-form-seq
1033-
'[(ns foo.core)
1034-
(def React (js/require "react"))
1035-
(.log js/console (.-Component React))])
1036-
(is (= "var require;\nObject.Component;\n"
1037-
(with-out-str
1038-
(comp/emit-externs
1039-
(reduce util/map-merge {}
1040-
(map (comp :externs second)
1041-
(get @test-cenv ::a/namespaces)))))))
1042-
(is (= 1 (count @ws)))
1043-
(is (string/starts-with?
1044-
(first @ws)
1045-
"Adding extern to Object for property Component")))))))
960+
(let [ws (atom [])
961+
res (infer-test-helper
962+
{:forms '[(ns foo.core)
963+
(def React (js/require "react"))
964+
(.log js/console (.-Component React))]
965+
:externs ["src/test/externs/test.js"]
966+
:warnings ws})]
967+
(is (= "var require;\nObject.Component;\n" res))
968+
(is (= 1 (count @ws)))
969+
(is (string/starts-with?
970+
(first @ws)
971+
"Adding extern to Object for property Component"))))
972+
973+
(comment
974+
975+
)

0 commit comments

Comments
 (0)