Skip to content

Commit 748fa3d

Browse files
committed
Add basic tests for when-first.
Includes a test ensuring the seq is run only once, since that's a core promise when-first makes.
1 parent 9b38590 commit 748fa3d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(ns clojure.core-test.when-first
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+
6+
(when-var-exists when-first
7+
(deftest test-when-first
8+
(testing "Basic single-binding tests using vectors or nil"
9+
(is (= 0 (when-first [x [0 1 2 3 4] ] x)))
10+
(is (nil? (when-first [x [nil]] x)))
11+
(is (nil? (when-first [x []] x)))
12+
(is (nil? (when-first [x nil] x))))
13+
(testing "Basic single-binding tests using seqs"
14+
(is (= 0 (when-first [x (range 5) ] x)))
15+
(is (nil? (when-first [x (repeat nil)] x))))
16+
(testing "Seq only called once"
17+
(let [calls (atom 0)
18+
seq-fn (fn s [] (lazy-seq
19+
(swap! calls inc)
20+
(cons 1 (s))))
21+
s (seq-fn)]
22+
(is (= 1 (when-first [x s] x)))
23+
(is (= @calls 1))))))

0 commit comments

Comments
 (0)