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