|
| 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