Skip to content

Commit d126c9c

Browse files
committed
test merge
1 parent 2f8b271 commit d126c9c

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

test/clojure/core_test/merge.cljc

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
(ns clojure.core-test.merge
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+
(when-var-exists clojure.core/merge
6+
(deftest test-merge
7+
(testing "`merge`"
8+
(testing "`nil` and empty map behavior"
9+
(is (nil? (merge)))
10+
(is (nil? (merge nil)))
11+
(is (nil? (merge nil nil)))
12+
(is (nil? (merge nil nil nil)))
13+
14+
(is (= {} (merge {})))
15+
(is (= {} (merge {} nil)))
16+
(is (= {} (merge nil {})))
17+
(is (= {} (merge nil {} nil)))
18+
19+
(is (= {[2 3] :foo} (merge {[2 3] :foo} nil {})))
20+
(is (= {1 11} (merge {1 11} {} nil))))
21+
22+
(testing "lattermost mapping wins"
23+
(is (= {:a "aaaaa"} (merge {:a "a"} {:a "aaaaa"})))
24+
(is (= {:a "a" :b "b"} (merge {:a "aaaa"} {:a "a" :b "b"})))
25+
(is (= {:a "a" :b "b"} (merge {:a "aaaa"}
26+
{:a "a" :b "bbbb"}
27+
{:a "a" :b "b"})))
28+
(is (= {:a nil :b "b" :c "c"} (merge {:a "aaaa"}
29+
{:a "a" :b "bbbb" :c "c"}
30+
{:a nil :b "b"})))
31+
(is (= {:x 1 :y 10 :z 100} (merge {:x 1 :y 5555}
32+
{:y 10 :z 100}))))
33+
34+
(testing "map entries are accepted in position 2+, per `conj`"
35+
(is (= {:a "a", :b "b"} (merge {:a nil}
36+
(first {:a "a"})
37+
{:b "b"}))))
38+
39+
(testing "vectors in position 2+ are treated as map-entries, per `conj`"
40+
(is (thrown? #?(:cljs :default, :clj java.lang.IllegalArgumentException, :clr Exception)
41+
(merge {} [])))
42+
(is (thrown? #?(:cljs :default, :clj java.lang.IllegalArgumentException, :clr Exception)
43+
(merge {} [:foo])))
44+
(is (= {:foo "foo"} (merge {} [:foo "foo"])))
45+
(is (= {"x" 1} (merge {} ["x" 1])))
46+
(is (= {'x 10, 'y 10} (merge {'x 10} ['y 10])))
47+
(testing "In CLJS (unlike other dialects) vectors with >2 arguments are treated as map-entries (where the latter values are ignored)"
48+
#?(:cljs (is (= {:foo :bar} (merge {} [:foo :bar :baz]))),
49+
:clj (is (thrown? java.lang.IllegalArgumentException (merge {} [:foo :bar :baz]))),
50+
:clr (is (thrown? Exception (merge {} [:foo :bar :baz])))))
51+
52+
(is (= {:foo "foo", :bar "bar"} (merge {} [:foo "foo"] [:bar "bar"])))
53+
(is (= {'x 10, 'y 10, 'z 10} (merge {'x 10} ['y 10] ['z 10])))
54+
(testing "In CLJS (unlike other dialects) vectors with >2 arguments are treated as map-entries (where the latter values are ignored)"
55+
#?(:cljs (is (= {:foo :bar} (merge {} [:foo :bar :baz :bar]))),
56+
:clj (is (thrown? java.lang.IllegalArgumentException (merge {} [:foo :bar :baz :bar]))),
57+
:clr (is (thrown? Exception (merge {} [:foo :bar :baz :bar]))))))
58+
59+
(testing "atomic values in position 2+ throw"
60+
(is (thrown? #?(:cljs :default, :clj Exception, :clr Exception)
61+
(merge {} 1)))
62+
(is (thrown? #?(:cljs :default, :clj Exception, :clr Exception)
63+
(merge {} 1 2)))
64+
(is (thrown? #?(:cljs :default, :clj Exception, :clr Exception)
65+
(merge {} :foo)))
66+
(is (thrown? #?(:cljs :default, :clj Exception, :clr Exception)
67+
(merge {} "str"))))
68+
69+
;; Behavior for non-maps is undefined and intentionally not tested.
70+
(comment
71+
(merge '(1 2 3) 1)
72+
(merge [1 2] 3 4 5)
73+
(merge [] nil {} 1 {:a "c"})
74+
(merge (first {:a "a"}) {:b "b"} {:c "c"})
75+
(merge :foo)
76+
(merge :foo :bar)
77+
(merge {} (range))
78+
(merge {} '(1 2))
79+
(merge {} 1 2)))))

0 commit comments

Comments
 (0)