|
| 1 | +(ns clojure.core-test.dissoc |
| 2 | + (:require [clojure.test :refer [deftest testing is are]] |
| 3 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 4 | + |
| 5 | +(when-var-exists dissoc |
| 6 | + |
| 7 | + (defrecord TestDissocRecord [a b c]) |
| 8 | + |
| 9 | + (deftest test-dissoc |
| 10 | + |
| 11 | + (testing "non-nil" |
| 12 | + (are [expected m keys] (= expected (apply dissoc m keys)) |
| 13 | + {} {} [] |
| 14 | + {:a 1} {:a 1} [] |
| 15 | + {} {:a 1} [:a] |
| 16 | + {} {:a 1} [:a :a] |
| 17 | + {} {:a 1 :b 2} [:a :b] |
| 18 | + {:b 2} {:a 1 :b 2} [:a] |
| 19 | + {:b 2} {:a 1 :b 2} [:a :c])) |
| 20 | + |
| 21 | + (testing "nil" |
| 22 | + (are [expected m keys] (= expected (apply dissoc m keys)) |
| 23 | + nil nil [nil] |
| 24 | + {} {} [nil] |
| 25 | + {} {nil nil} [nil] |
| 26 | + {} {nil nil} [nil nil])) |
| 27 | + |
| 28 | + (testing "sorted preservation" |
| 29 | + (is (sorted? (dissoc (sorted-map :a 1 :b 2) :a)))) |
| 30 | + |
| 31 | + (testing "meta preservation" |
| 32 | + (let [test-meta {:me "ta"} |
| 33 | + with-test-meta #(with-meta % test-meta) |
| 34 | + with-test-meta? #(= test-meta (meta %))] |
| 35 | + (is (with-test-meta? (dissoc (with-test-meta {:a 1 :b 2}) :a))))) |
| 36 | + |
| 37 | + (testing "records" |
| 38 | + (let [r (TestDissocRecord. 1 2 nil)] |
| 39 | + (are [expected keys] #?(; bb preserves the record type even if a basis fields is dissociated |
| 40 | + ; https://github.com/babashka/babashka/issues/1886 |
| 41 | + :bb (= expected (into {} (apply dissoc r keys))) |
| 42 | + ; other implementations return a map |
| 43 | + :default (= expected (apply dissoc r keys))) |
| 44 | + {:b 2 :c nil} [:a] |
| 45 | + {:b 2 :c nil} [:a :d] |
| 46 | + {} [:a :b :c]) |
| 47 | + ; all implementations preserve the record type if no basis field is dissociated |
| 48 | + (is (= r (dissoc r :d))))) |
| 49 | + |
| 50 | + (testing "bad shape" |
| 51 | + (are [m keys] (thrown? #?(:cljs js/Error :default Exception) (apply dissoc m keys)) |
| 52 | + 42 [4] |
| 53 | + '() [0] |
| 54 | + [] [0] |
| 55 | + #{:a :b} [:a] |
| 56 | + "string" [\s \t])))) |
0 commit comments