Skip to content

Commit 1d9fccc

Browse files
authored
test when (#754)
* test `when` * test body-less `when`
1 parent e75098a commit 1d9fccc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/clojure/core_test/when.cljc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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))))))

0 commit comments

Comments
 (0)