|
| 1 | +;; Copyright (c) Rich Hickey. All rights reserved. |
| 2 | +;; The use and distribution terms for this software are covered by the |
| 3 | +;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
| 4 | +;; which can be found in the file epl-v10.html at the root of this distribution. |
| 5 | +;; By using this software in any fashion, you are agreeing to be bound by |
| 6 | +;; the terms of this license. |
| 7 | +;; You must not remove this notice, or any other, from this software. |
| 8 | + |
| 9 | +(ns cljs.analyzer-pass-tests |
| 10 | + (:require [cljs.analyzer :as ana] |
| 11 | + [cljs.analyzer.passes.and-or :as and-or] |
| 12 | + [cljs.analyzer-tests :as ana-tests :refer [analyze]] |
| 13 | + [cljs.compiler :as comp] |
| 14 | + [cljs.compiler-tests :as comp-tests :refer [compile-form-seq emit]] |
| 15 | + [cljs.env :as env] |
| 16 | + [clojure.string :as string] |
| 17 | + [clojure.test :as test :refer [deftest is testing]])) |
| 18 | + |
| 19 | +(deftest test-and-or-code-gen-pass |
| 20 | + (testing "and/or optimization code gen pass" |
| 21 | + (let [expr-env (assoc (ana/empty-env) :context :expr) |
| 22 | + ast (->> `(and true false) |
| 23 | + (analyze expr-env)) |
| 24 | + code (with-out-str (emit ast))] |
| 25 | + (is (= code "((true) && (false))"))) |
| 26 | + (let [expr-env (assoc (ana/empty-env) :context :expr) |
| 27 | + ast (analyze expr-env |
| 28 | + `(and true (or true false))) |
| 29 | + code (with-out-str (emit ast))] |
| 30 | + (is (= code "((true) && (((true) || (false))))"))) |
| 31 | + (let [expr-env (assoc (ana/empty-env) :context :expr) |
| 32 | + ast (analyze expr-env |
| 33 | + `(or true (and false true))) |
| 34 | + code (with-out-str (emit ast))] |
| 35 | + (is (= code "((true) || (((false) && (true))))"))) |
| 36 | + (let [expr-env (assoc (ana/empty-env) :context :expr) |
| 37 | + local (gensym) |
| 38 | + ast (analyze expr-env |
| 39 | + `(let [~local true] |
| 40 | + (and true (or ~local false)))) |
| 41 | + code (with-out-str (emit ast))] |
| 42 | + (is (= code |
| 43 | + (string/replace |
| 44 | + "(function (){var $SYM = true;\nreturn ((true) && ((($SYM) || (false))));\n})()" |
| 45 | + "$SYM" (str local))))))) |
| 46 | + |
| 47 | +(deftest test-and-or-local |
| 48 | + (testing "and/or optimizable with boolean local" |
| 49 | + (let [expr-env (assoc (ana/empty-env) :context :expr) |
| 50 | + ast (->> `(let [x# true] |
| 51 | + (and x# true false)) |
| 52 | + (analyze expr-env)) |
| 53 | + code (with-out-str (emit ast))] |
| 54 | + (is (= 2 (count (re-seq #"&&" code))))))) |
| 55 | + |
| 56 | +(deftest test-and-or-boolean-fn-arg |
| 57 | + (testing "and/or optimizable with boolean fn arg" |
| 58 | + (let [arg (with-meta 'x {:tag 'boolean}) |
| 59 | + ast (analyze (assoc (ana/empty-env) :context :expr) |
| 60 | + `(fn [~arg] |
| 61 | + (and ~arg false false))) |
| 62 | + code (with-out-str (emit ast))] |
| 63 | + (is (= 2 (count (re-seq #"&&" code))))))) |
| 64 | + |
| 65 | +(deftest test-and-or-boolean-var |
| 66 | + (testing "and/or optimizable with boolean var" |
| 67 | + (let [code (env/with-compiler-env (env/default-compiler-env) |
| 68 | + (compile-form-seq |
| 69 | + '[(ns foo.bar) |
| 70 | + (def baz true) |
| 71 | + (defn woz [] |
| 72 | + (and baz false))]))] |
| 73 | + (is (= 1 (count (re-seq #"&&" code))))))) |
| 74 | + |
| 75 | +(deftest test-and-or-js-boolean-var |
| 76 | + (testing "and/or optimizable with js boolean var" |
| 77 | + (let [code (env/with-compiler-env (env/default-compiler-env) |
| 78 | + (compile-form-seq |
| 79 | + '[(ns foo.bar) |
| 80 | + (defn baz [] |
| 81 | + (and ^boolean js/woz false))]))] |
| 82 | + (is (= 1 (count (re-seq #"&&" code))))))) |
| 83 | + |
| 84 | +(deftest test-and-or-host-call |
| 85 | + (testing "and/or optimizable with host call" |
| 86 | + (let [code (env/with-compiler-env (env/default-compiler-env) |
| 87 | + (compile-form-seq |
| 88 | + '[(ns foo.bar) |
| 89 | + (defn bar [x] |
| 90 | + (and ^boolean (.woz x) false))]))] |
| 91 | + (is (= 1 (count (re-seq #"&&" code))))))) |
| 92 | + |
| 93 | +(deftest test-and-or-host-field |
| 94 | + (testing "and/or optimizable with host field" |
| 95 | + (let [code (env/with-compiler-env (env/default-compiler-env) |
| 96 | + (compile-form-seq |
| 97 | + '[(ns foo.bar) |
| 98 | + (defn bar [x] |
| 99 | + (and ^boolean (.-woz x) false))]))] |
| 100 | + (is (= 1 (count (re-seq #"&&" code))))))) |
| 101 | + |
| 102 | +(deftest test-core-predicates |
| 103 | + (testing "and/or optimizable with core predicates" |
| 104 | + (let [code (env/with-compiler-env (env/default-compiler-env) |
| 105 | + (comp/with-core-cljs {} |
| 106 | + (fn [] |
| 107 | + (compile-form-seq |
| 108 | + '[(ns foo.bar) |
| 109 | + (defn bar [] |
| 110 | + (and (even? 1) false))]))))] |
| 111 | + (is (= 1 (count (re-seq #"&&" code))))))) |
| 112 | + |
| 113 | +(comment |
| 114 | + (test/run-tests) |
| 115 | + |
| 116 | + (require '[clojure.pprint :refer [pprint]]) |
| 117 | + |
| 118 | + ) |
0 commit comments