| 
 | 1 | +(ns clojure.core-test.mapcat  | 
 | 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 mapcat  | 
 | 7 | +  (deftest common-cases  | 
 | 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 "incorrect shape"  | 
 | 23 | +      (is (thrown? Throwable (do (mapcat identity 5) true))))  | 
 | 24 | +    (testing "infinite input laziness"  | 
 | 25 | +      (is (= [0 0 1 1 2]  (take 5  (mapcat #(repeat 2 %) (range))))))  | 
 | 26 | +    (testing "empty collection input"  | 
 | 27 | +      (is (= [] (mapcat identity []))))  | 
 | 28 | +    (testing "single element producing empty sequence"  | 
 | 29 | +      (is (= [] (mapcat (constantly []) [42]))))  | 
 | 30 | +    (testing "single element producing seq"  | 
 | 31 | +      (is (= [99] (mapcat list [99]))))  | 
 | 32 | +    (testing "function returns a vector"  | 
 | 33 | +      (is (vector? (vec (mapcat (fn [x] [x x x]) [1])))))  | 
 | 34 | +    (testing "function returns a string (seqable)"  | 
 | 35 | +      (is (= [\h \i] (mapcat identity ["hi"]))))  | 
 | 36 | +    (testing "flatten key/value pairs"  | 
 | 37 | +        (is (= [:a 1 :b 2 :c 3]  | 
 | 38 | +               (mapcat identity {:a 1 :b 2 :c 3}))))  | 
 | 39 | +    (testing "function returns nil"  | 
 | 40 | +      (is (= [] (mapcat (constantly nil) [1 2 3]))))  | 
 | 41 | +    (testing "two collections zipped, function applied to pairs"  | 
 | 42 | +      (is (= [2 2 4 4 6 6] (mapcat (fn [x y] [(* 2 x) (* 2 y)]) [1 2 3] [1 2 3]))))  | 
 | 43 | +    (testing "one collection shorter than the other"  | 
 | 44 | +      (is (= [2 4] (mapcat (fn [x y] [(* x y)]) [1 2] [2 2 2]))))  | 
 | 45 | +    (testing "works lazily on infinite input"  | 
 | 46 | +      (is (= [0 1 2 3 4 5] (->> (mapcat (fn [x] [x]) (range)) (take 6)))))  | 
 | 47 | +    (testing "function sometimes returns []"  | 
 | 48 | +      (is (= [1 3 5] (mapcat #(if (odd? %) [%] []) (range 1 6)))))  | 
 | 49 | +    (testing "function sometimes returns nil"  | 
 | 50 | +      (is (= [2 4] (mapcat #(if (even? %) [%] nil) (range 1 5)))))  | 
 | 51 | +    (testing "throws on non-seqable arg"  | 
 | 52 | +      (is (thrown? Throwable (do (mapcat identity 5) true))))  | 
 | 53 | +    (testing "throws on non-fn first arg"  | 
 | 54 | +      (is (thrown? Throwable (do (mapcat 42 [1 2]) true))))))  | 
 | 55 | + | 
0 commit comments