|
44 | 44 | (doseq [form forms] |
45 | 45 | (comp/emit (ana/analyze (ana/empty-env) form))))))) |
46 | 46 |
|
| 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 | + |
47 | 54 | #_(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))))))) |
61 | 68 |
|
62 | 69 | (deftest fn-scope-munge |
63 | 70 | (is (= (comp/munge |
|
425 | 432 | (is (instance? clojure.lang.ExceptionInfo t)) |
426 | 433 | (is (.startsWith (-> t ex-cause ex-message) "Can't use & as a local binding")))))) |
427 | 434 |
|
| 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 | + |
428 | 473 | ;; CLJS-1225 |
429 | 474 |
|
430 | 475 | (comment |
|
0 commit comments