|
| 1 | + |
| 2 | +(ns clojure.core-test.when-some |
| 3 | + (:require [clojure.test :as t :refer [deftest is testing]] |
| 4 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 5 | + |
| 6 | + |
| 7 | +(when-var-exists when-some |
| 8 | + (deftest test-when-some |
| 9 | + (testing "basic single-binding tests using vectors or nil" |
| 10 | + (is (= [0 1 2 3 4] (when-some [x [0 1 2 3 4]] x))) |
| 11 | + (is (some? (when-some [x [nil]] x))) |
| 12 | + (is (= [] (when-some [x []] x)))) |
| 13 | + (testing "side effects don't get evaluated" |
| 14 | + (let [test-atom (atom nil)] |
| 15 | + (is (nil? (when-some [x nil] (reset! test-atom 1) x))) |
| 16 | + (is (nil? @test-atom)))) |
| 17 | + (testing "basic single-binding tests using seqs" |
| 18 | + (is (= '(0 1 2 3 4) (when-some [x (range 5)] x)))) |
| 19 | + (testing "unlike when-let, we're looking for not-nil specifically, so false evaluates" |
| 20 | + (is (= false (when-some [x false] x)))) |
| 21 | + (testing "seq is only called once" |
| 22 | + (let [calls (atom 0) |
| 23 | + seq-fn (fn s [] (lazy-seq |
| 24 | + (swap! calls inc) |
| 25 | + (cons 1 (s)))) |
| 26 | + s (take 5 (seq-fn))] |
| 27 | + (is (= '(1 1 1 1 1) (when-some [x s] x))) |
| 28 | + (is (= @calls 5)))) |
| 29 | + (testing "without a body, truth doesn't matter" |
| 30 | + (is (nil? (when-some [x nil]))) |
| 31 | + (is (nil? (when-some [x [false]]))) |
| 32 | + (is (nil? (when-some [x [true]])))))) |
0 commit comments