|
| 1 | +(ns clojure.core-test.if-let |
| 2 | +(:require [clojure.test :as t :refer [are deftest is testing]] |
| 3 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 4 | + |
| 5 | +(when-var-exists if-let |
| 6 | + (deftest test-if-let |
| 7 | + (testing "basic single-binding tests using vectors or nil" |
| 8 | + (is (= [0 1 2 3 4] (if-let [x [0 1 2 3 4] ] x :else))) |
| 9 | + (is (not (nil? (if-let [x [nil]] x :else)))) |
| 10 | + (is (= [] (if-let [x []] x :else))) |
| 11 | + (is (= :else (if-let [x nil] x :else)))) |
| 12 | + (testing "basic single-binding tests using seqs" |
| 13 | + (is (= '(0 1 2 3 4) (if-let [x (range 5)] x :else)))) |
| 14 | + (testing "unlike, when-some, we're looking for not-nil specifically, so false evaluates" |
| 15 | + (is (= :else (if-let [x false] x :else)))) |
| 16 | + (testing "seq is only called once" |
| 17 | + (let [calls (atom 0) |
| 18 | + seq-fn (fn s [] (lazy-seq |
| 19 | + (swap! calls inc) |
| 20 | + (cons 1 (s)))) |
| 21 | + s (take 5 (seq-fn))] |
| 22 | + (is (= '(1 1 1 1 1) (if-let [x s] x :else))) |
| 23 | + (is (= @calls 5)))) |
| 24 | + (comment (testing "without a body, truth doesn't matter" |
| 25 | + (is (nil? (if-let [x nil]))) |
| 26 | + (is (nil? (if-let [x [false]]))) |
| 27 | + (is (nil? (if-let [x [true]]))))) |
| 28 | + #?(:cljs nil ; Skipped due to ClojureScript's atypical macro expansion. |
| 29 | + ;; :bb nil ; Skipped because of Babashka issue https://github.com/babashka/babashka/issues/1894 |
| 30 | + :default (testing "if-let accepts exactly two bindings" |
| 31 | + (is (thrown? Exception |
| 32 | + (macroexpand |
| 33 | + '(if-let [x (range 5) y (range 5)])))))) |
| 34 | + #?(:cljs nil ; Skipped due to ClojureScript's atypical macro expansion. |
| 35 | + ;; :bb nil ; Skipped because of Babashka issue https://github.com/babashka/babashka/issues/1894 |
| 36 | + :default (testing "if-let accepts exactly two bindings" |
| 37 | + (is (thrown? Exception |
| 38 | + (macroexpand |
| 39 | + '(if-let [x (range 5) y (range 5)])))))) |
| 40 | + )) |
0 commit comments