Skip to content

Commit 2e491a3

Browse files
committed
convert the eval tests into compile time tests that check for warnings about undeclared vars
1 parent f9c8e19 commit 2e491a3

2 files changed

Lines changed: 63 additions & 37 deletions

File tree

src/test/cljs/cljs/destructuring_test.cljs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,7 @@
254254
(is (thrown? js/Error (let [{:keys! [b & :foo/c]} (dissoc sample-map :foo/c)] b)))
255255
(is (= 1 (let [{:foo/keys! [aa & :bb]} sample-map2] aa)))
256256
(is (= aa 1))
257-
(is (= 1 (let [{:keys! [::a & ::b]} {::a 1 , ::b 2}] a)))))
258-
#_(testing "that right of & is unbound (compile-time errors)"
259-
(is (thrown? js/Error (eval '(let [{:keys! [a & :b]} sample-map] b))))
260-
(is (thrown? js/Error (eval '(let [{a :a {aa :a :as m :keys [b c & :e]} :b} sample-map] e))))
261-
(is (thrown? js/Error (eval '(let [{:keys! [foo/a & :foo/c]} sample-map] c))))
262-
(let [sample-map2 {:foo/aa 1 :bb 2 :foo/cc 3}]
263-
(is (thrown? js/Error (eval '(let [{:foo/keys! [foo/aa & :foo/cc]} sample-map2] cc))))))))
257+
(is (= 1 (let [{:keys! [::a & ::b]} {::a 1 , ::b 2}] a)))))))
264258

265259
(deftest syms-bang
266260
(let [sample-map '{a 1 b 2}]
@@ -291,13 +285,7 @@
291285
(is (thrown? js/Error (let [{:syms! [b & 'foo/c]} (dissoc sample-map 'b)] b)))
292286
(is (thrown? js/Error (let [{:syms! [b & 'foo/c]} (dissoc sample-map 'foo/c)] b)))
293287
(is (= aa 1))
294-
(is (= 1 (let [{:foo/syms! [aa & 'bb]} sample-map2] aa)))))
295-
#_(testing "that right of & is unbound (compile-time errors)"
296-
(is (thrown? js/Error (eval '(let [{:syms! [a & 'b]} sample-map] b))))
297-
(is (thrown? js/Error (eval '(let [{a a {aa a :as m :syms [b c & 'e]} :b} sample-map] e))))
298-
(is (thrown? js/Error (eval '(let [{:syms! [foo/a & 'foo/c]} sample-map] c))))
299-
(let [sample-map2 '{foo/aa 1 bb 2 foo/cc 3}]
300-
(is (thrown? js/Error (eval '(let [{:foo/syms! [foo/aa & 'foo/cc]} sample-map2] cc))))))))
288+
(is (= 1 (let [{:foo/syms! [aa & 'bb]} sample-map2] aa)))))))
301289

302290
(deftest strs-bang
303291
(let [sample-map {"a" 1 "b" 2}]
@@ -316,10 +304,7 @@
316304
(is (= b 3))
317305
(is (= c 4))
318306
(is (thrown? js/Error (let [{a "a" {aa "a" :as m :strs! [b c & "d" "e"]} "b"} sample-map] a)))
319-
(is (thrown? js/Error (let [{a "a" {aa "a" :as m :strs! [b c & "d"]} "b"} (update sample-map "b" dissoc "c")] a)))))
320-
#_(testing "that right of & is unbound (compile-time errors)"
321-
(is (thrown? js/Error (eval '(let [{:strs! [a & "b"]} sample-map] b))))
322-
(is (thrown? js/Error (eval '(let [{a "a" {aa "a" :as m :keys [b c & "e"]} "b"} sample-map] e)))))))
307+
(is (thrown? js/Error (let [{a "a" {aa "a" :as m :strs! [b c & "d"]} "b"} (update sample-map "b" dissoc "c")] a)))))))
323308

324309
(deftest select-directive
325310
(let [m {:a 1 :b 2 :c 3 :d 4
@@ -397,7 +382,7 @@
397382
(testing "happy path"
398383
(testing ":defaults"
399384
(is (empty? (let [{:defaults d :or {}} {}] d)))
400-
#_(is (thrown? js/Error (eval '(let [{:defaults d :or {:a 1}} {}] d))))
385+
401386
(is (= {:a 1} (let [{:keys [a] :defaults d :or {:a 1}} {}] d)))
402387
(is (= {:a 1} (let [{:keys [a] :defaults d :or {a 1}} {}] d)))
403388
(is (= {:a 1, 'b 2, "c" 3} (let [{b 'b, c "c", :keys [a] :defaults d :or {:a 1, 'b 2, "c" 3}} {}] d))))
@@ -437,11 +422,7 @@
437422

438423
(testing "mixed things after &"
439424
(is (= 1 (let [{:keys [a & 'b]} {:a 1}] a)))
440-
(is (= 1 (let [{:keys! [a & 'b "c"]} {:a 1, 'b 2, "c" 3}] a))))
441-
442-
#_(testing "known compile-time errors"
443-
(is (thrown? js/Error (eval '(let [{:keys [a] :defaults d :or {:a 1, a 1}} {}] d))))
444-
(is (thrown? js/Error (eval '(let [{:defaults d} {}] d))))))))
425+
(is (= 1 (let [{:keys! [a & 'b "c"]} {:a 1, 'b 2, "c" 3}] a)))))))
445426

446427
(comment
447428

src/test/clojure/cljs/compiler_tests.clj

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,27 @@
4444
(doseq [form forms]
4545
(comp/emit (ana/analyze (ana/empty-env) form)))))))
4646

47+
(defn compile-simple-form
48+
[form]
49+
(env/with-compiler-env (env/default-compiler-env)
50+
(comp/with-core-cljs {}
51+
(fn []
52+
(compile-form-seq [form])))))
53+
4754
#_(deftest should-recompile
48-
(let [src (File. "test/hello.cljs")
49-
dst (File/createTempFile "compilertest" ".cljs")
50-
opt {:optimize-constants true}
51-
optmod {:optimize-constants true :elide-asserts false}]
52-
(with-redefs [util/*clojurescript-version* {:major 0 :minor 0 :qualifier 42}]
53-
(env/with-compiler-env (env/default-compiler-env)
54-
(.setLastModified dst (- (.lastModified src) 100))
55-
(is (comp/requires-compilation? src dst opt))
56-
(comp/compile-file src dst opt)
57-
(is (not (comp/requires-compilation? src dst opt)))
58-
(is (comp/requires-compilation? src dst optmod))
59-
(comp/compile-file src dst optmod)
60-
(is (not (comp/requires-compilation? src dst optmod)))))))
55+
(let [src (File. "test/hello.cljs")
56+
dst (File/createTempFile "compilertest" ".cljs")
57+
opt {:optimize-constants true}
58+
optmod {:optimize-constants true :elide-asserts false}]
59+
(with-redefs [util/*clojurescript-version* {:major 0 :minor 0 :qualifier 42}]
60+
(env/with-compiler-env (env/default-compiler-env)
61+
(.setLastModified dst (- (.lastModified src) 100))
62+
(is (comp/requires-compilation? src dst opt))
63+
(comp/compile-file src dst opt)
64+
(is (not (comp/requires-compilation? src dst opt)))
65+
(is (comp/requires-compilation? src dst optmod))
66+
(comp/compile-file src dst optmod)
67+
(is (not (comp/requires-compilation? src dst optmod)))))))
6168

6269
(deftest fn-scope-munge
6370
(is (= (comp/munge
@@ -425,6 +432,44 @@
425432
(is (instance? clojure.lang.ExceptionInfo t))
426433
(is (.startsWith (-> t ex-cause ex-message) "Can't use & as a local binding"))))))
427434

435+
;; -----------------------------------------------------------------------------
436+
;; 1.13 Compile Time Destructuring Tests
437+
438+
;; The following only emits warnings in ClojureScript
439+
440+
(defn undeclared-warning? [form sym]
441+
(let [[[warn-type warn-mesg] :as warns] (capture-warnings (compile-simple-form form))]
442+
(and (= warn-type :undeclared-var)
443+
(.startsWith warn-mesg (str "WARNING: Use of undeclared Var cljs.user/" (name sym))))))
444+
445+
(deftest keys-bang
446+
(testing "that right of & is unbound (compile-time errors)"
447+
(is (undeclared-warning? '(let [{:keys! [a & :b]} {:a 1 :b 2}] b) 'b))
448+
(is (undeclared-warning? '(let [{a :a {aa :a :as m :keys [b c & :e]} :b} {:a 1 :b 2}] e) 'e))
449+
(is (undeclared-warning? '(let [{:keys! [foo/a & :foo/c]} {:a 1 :b 2}] c) 'c))
450+
(is (undeclared-warning? '(let [{:foo/keys! [foo/aa & :foo/cc]} {:foo/aa 1 :bb 2 :foo/cc 3}] cc) 'cc))))
451+
452+
(deftest syms-bang
453+
(testing "that right of & is unbound (compile-time errors)"
454+
(is (undeclared-warning? '(let [{:syms! [a & 'b]} (quote {a 1 b 2})] b) 'b))
455+
#_(is (undeclared-warning? '(let [{a a {aa a :as m :syms [b c & 'e]} :b} (quote {a 1 b 2})] e) 'e))
456+
(is (undeclared-warning? '(let [{:syms! [foo/a & 'foo/c]} (quote {a 1 b 2})] c) 'c))
457+
(is (undeclared-warning? '(let [{:foo/syms! [foo/aa & 'foo/cc]} (quote {foo/aa 1 bb 2 foo/cc 3})] cc) 'cc))))
458+
459+
(deftest strs-bang
460+
(testing "that right of & is unbound (compile-time errors)"
461+
(is (undeclared-warning? '(let [{:strs! [a & "b"]} {"a" 1 "b" 2}] b) 'b))
462+
(is (undeclared-warning? '(let [{a "a" {aa "a" :as m :keys [b c & "e"]} "b"} {"a" 1 "b" 2}] e) 'e))))
463+
464+
(deftest select-or-defaults
465+
(testing ":defaults"
466+
(is (thrown? Exception
467+
(compile-simple-form
468+
'(let [{:defaults d :or {:a 1}} {}] d)))))
469+
(testing "known compile-time errors"
470+
(is (thrown? Exception (compile-simple-form '(let [{:keys [a] :defaults d :or {:a 1, a 1}} {}] d))))
471+
(is (thrown? Exception (compile-simple-form '(let [{:defaults d} {}] d))))))
472+
428473
;; CLJS-1225
429474

430475
(comment

0 commit comments

Comments
 (0)