Skip to content

Commit 65d46ec

Browse files
authored
Add tests for when-not (#847)
1 parent 2ccd794 commit 65d46ec

File tree

1 file changed

+39
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)