|
| 1 | +(ns clojure.core-test.conj-bang |
| 2 | + (:require [clojure.test :refer [are deftest is testing]] |
| 3 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 4 | + |
| 5 | +(when-var-exists conj! |
| 6 | + (deftest test-conj! |
| 7 | + |
| 8 | + (testing "conj!" |
| 9 | + (is (= [] (persistent! (conj!))))) |
| 10 | + |
| 11 | + (testing "conj! coll" |
| 12 | + (testing "when coll is transient" |
| 13 | + (are [expected coll] (= expected (persistent! (conj! (transient coll)))) |
| 14 | + [] [] |
| 15 | + [1 2 3] [1 2 3] |
| 16 | + {} {} |
| 17 | + {:a 1 :b 2 :c 3} {:a 1 :b 2 :c 3} |
| 18 | + #{} #{} |
| 19 | + #{1} #{1})) |
| 20 | + (testing "when coll is anything else" |
| 21 | + (are [coll] (= coll (conj! coll)) |
| 22 | + nil |
| 23 | + [] |
| 24 | + [1 2 3] |
| 25 | + {} |
| 26 | + {:a 1 :b 2 :c 3} |
| 27 | + '() |
| 28 | + '(:a :b :c) |
| 29 | + (range 1 4) |
| 30 | + #{} |
| 31 | + #{1 2 3} |
| 32 | + "abc" |
| 33 | + 0 |
| 34 | + 0.0 |
| 35 | + :k |
| 36 | + true |
| 37 | + false))) |
| 38 | + |
| 39 | + (testing "conj! coll x" |
| 40 | + (testing "when x is nil or empty" |
| 41 | + (are [expected coll x] (= expected (persistent! (conj! coll x))) |
| 42 | + [nil] (transient []) nil |
| 43 | + [[]] (transient []) [] |
| 44 | + {} (transient {}) nil |
| 45 | + {} (transient {}) {} |
| 46 | + #{nil} (transient #{}) nil |
| 47 | + #{#{}} (transient #{}) #{})) |
| 48 | + (testing "when x is non-nil and non-empty" |
| 49 | + (are [expected coll x] (= expected (persistent! (conj! coll x))) |
| 50 | + [1] (transient []) 1 |
| 51 | + [1 [2]] (transient [1]) [2] |
| 52 | + [1 2 3 4] (conj! (transient [1 2]) 3) 4 |
| 53 | + {:a 1} (transient {}) {:a 1} |
| 54 | + {:a 1 :b 2} (transient {:a 1}) [:b 2] |
| 55 | + {:a 2 :b 1} (transient {:a 1 :b 2}) {:a 2 :b 1} |
| 56 | + {:a 1 :b 2 :c 3} (conj! (transient {:a 0}) {:b 2}) {:a 1 :c 3} |
| 57 | + #{1} (transient #{}) 1 |
| 58 | + #{1 #{2}} (transient #{1}) #{2} |
| 59 | + #{1 2 3 4} (conj! (transient #{1 2}) 3) 4))) |
| 60 | + |
| 61 | + (testing "cannot conj! after call to persistent!" |
| 62 | + (let [coll (transient []), _ (persistent! coll)] |
| 63 | + (is (thrown? #?(:cljs js/Error :cljr Exception :default Error) (conj! coll 0))))) |
| 64 | + |
| 65 | + (testing "bad shapes" |
| 66 | + (are [coll x] (thrown? #?(:cljs js/Error :default Exception) (conj! coll x)) |
| 67 | + (transient {}) '(:a 1) |
| 68 | + (transient {}) #{:a 1} |
| 69 | + (transient {}) (range 2) |
| 70 | + [] 1 |
| 71 | + {} {:a 1} |
| 72 | + '() true |
| 73 | + #{} :k |
| 74 | + "abc" \d |
| 75 | + true false |
| 76 | + 1 -1 |
| 77 | + (range 3) -1)))) |
0 commit comments