File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ (ns clojure.core-test.when
2
+ (:require [clojure.test :as t :refer [deftest testing is are]]
3
+ [clojure.core-test.portability #?(:cljs :refer-macros :default :refer ) [when-var-exists ]]))
4
+
5
+ (when-var-exists when
6
+ (deftest test-when
7
+ (testing " `when` checks logical truth"
8
+ (is (nil? (when nil :foo )))
9
+ (is (nil? (when false :foo )))
10
+ (is (nil? (when ((constantly nil )) :foo )))
11
+
12
+ (testing " without a body, truth doesn't matter"
13
+ (is (nil? (when false )))
14
+ (is (nil? (when true ))))
15
+
16
+ (testing " things which are false in other languages but not false in Clojure"
17
+ (is (= :foo (when 0 :foo )))
18
+ (is (= :foo (when " " :foo )))
19
+ (is (= :foo (when (list ) :foo )))
20
+ (is (= :foo (when '() :foo ))))
21
+
22
+ (is (= :foo (when true :foo )))
23
+ (is (= :foo (when (constantly nil ) :foo )))
24
+ (is (= :foo (when " false" :foo )))
25
+ (is (= :foo (when [] :foo )))
26
+ (is (= :foo (when {} :foo )))
27
+ (is (= :foo (when #{} :foo )))
28
+ (is (= :foo (when :haberdashery :foo ))))
29
+
30
+ (testing " `when` has implicit `do`"
31
+ (is (= :bar (when true :foo :bar )))
32
+ (let [foo (atom 0 )]
33
+ (is (= :bar (when true
34
+ (swap! foo inc)
35
+ (swap! foo inc)
36
+ (swap! foo inc)
37
+ :bar )))
38
+ (is (= 3 @foo))))))
You can’t perform that action at this time.
0 commit comments