Skip to content

Commit 5b4abd3

Browse files
committed
wip: add if-let
1 parent c9f662d commit 5b4abd3

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

test/clojure/core_test/if_let.cljc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 logical false specifically, so false doesn't evaluate"
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+
:default (testing "if-let accepts exactly two bindings"
30+
(is (thrown? Exception
31+
(macroexpand
32+
'(if-let [x (range 5) y (range 5)]))))))
33+
#?(:cljs nil ; Skipped due to ClojureScript's atypical macro expansion.
34+
:default (testing "if-let accepts exactly two bindings"
35+
(is (thrown? Exception
36+
(macroexpand
37+
'(if-let [x (range 5) y (range 5)]))))))))

0 commit comments

Comments
 (0)