Skip to content

Commit 05fb93a

Browse files
Test mapcat (#763)
* test `mapcat` (#354) * Address comments * cljs the problem child * Whoops * into [] -> doall --------- Co-authored-by: christos chatzifountas <[email protected]>
1 parent 77b4b83 commit 05fb93a

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
:url "https://github.com/jank-lang/clojure-test-suite"
44
:license {:name "MPL 2.0"
55
:url "https://www.mozilla.org/en-US/MPL/2.0/"}
6-
:dependencies [[org.clojure/clojure "1.12.0"]]
6+
:dependencies [[org.clojure/clojure "1.12.0"]
7+
[org.clojure/clojurescript "1.12.42"]]
78
:plugins [[com.jakemccrary/lein-test-refresh "0.25.0"]])

test/clojure/core_test/mapcat.cljc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
(ns clojure.core-test.mapcat
2+
(:require [clojure.test :as t :refer [deftest testing is]]
3+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
4+
5+
6+
(when-var-exists mapcat
7+
(deftest test-mapcat
8+
(testing "nil input"
9+
(is (nil? (seq (mapcat identity nil)))))
10+
(testing "concatenation"
11+
(is (= [1 2 3 4] (mapcat identity [[1 2] '(3 4)]))))
12+
(testing "function producing seqs"
13+
(is (= [0 0 1 0 1 2] (mapcat #(range %) [1 2 3]))))
14+
(testing "empty results contribute nothing"
15+
(is (= [2] (mapcat (fn [x] (if (odd? x) [] [x])) [1 2 3]))))
16+
(testing "strings"
17+
(is (= [\a \b \c] (mapcat identity ["ab" "" "c"]))))
18+
(testing "as transducer"
19+
(is (= [1 1 2 2 3 3] (transduce (mapcat #(repeat 2 %)) conj [] [1 2 3]))))
20+
(testing "into with transducer"
21+
(is (= [0 0 1 1 2 2] (into [] (mapcat #(repeat 2 %)) (range 3)))))
22+
(testing "infinite input laziness"
23+
(is (= [0 0 1 1 2] (take 5 (mapcat #(repeat 2 %) (range))))))
24+
(testing "empty collection input"
25+
(is (= [] (mapcat identity []))))
26+
(testing "single element producing empty sequence"
27+
(is (= [] (mapcat (constantly []) [42]))))
28+
(testing "single element producing seq"
29+
(is (= [99] (mapcat list [99]))))
30+
(testing "function returns a string (seqable)"
31+
(is (= [\h \i] (mapcat identity ["hi"]))))
32+
(testing "flatten key/value pairs"
33+
(is (= [:a 1 :b 2 :c 3] (mapcat identity {:a 1 :b 2 :c 3}))))
34+
(testing "function returns nil"
35+
(is (= [] (mapcat (constantly nil) [1 2 3]))))
36+
(testing "two collections zipped, function applied to pairs"
37+
(is (= [2 2 4 4 6 6] (mapcat (fn [x y] [(* 2 x) (* 2 y)]) [1 2 3] [1 2 3]))))
38+
(testing "one collection shorter than the other"
39+
(is (= [2 4] (mapcat (fn [x y] [(* x y)]) [1 2] [2 2 2]))))
40+
(testing "works lazily on infinite input"
41+
(is (= [0 1 2 3 4 5] (->> (mapcat (fn [x] [x]) (range)) (take 6)))))
42+
(testing "function sometimes returns []"
43+
(is (= [1 3 5] (mapcat #(if (odd? %) [%] []) (range 1 6)))))
44+
(testing "function sometimes returns nil"
45+
(is (= [2 4] (mapcat #(if (even? %) [%] nil) (range 1 5)))))
46+
(testing "multiple collections"
47+
(is (= '(:a 1 :b 2 :c 3) (mapcat list [:a :b :c] [1 2 3]))))
48+
(testing "incorrect shape"
49+
(is (thrown? #?(:cljs :default :default Exception)
50+
(mapcat identity 5))))
51+
(testing "non-seqable second arg"
52+
(is (thrown? #?(:cljs :default :default Exception)
53+
(mapcat identity 5))))
54+
(testing "non-function first arg"
55+
(is (thrown? #?(:cljs :default :default Exception)
56+
(mapcat 42 [1 2]))))
57+
(testing "non-concatable return value"
58+
(is (thrown? #?(:cljs :default :default Exception)
59+
(doall (mapcat identity (range 2))))))))

0 commit comments

Comments
 (0)