Merge release Beta 1 into stable #21
2 changed files with 44 additions and 10 deletions
|
@ -62,9 +62,9 @@
|
||||||
[conf defaults]
|
[conf defaults]
|
||||||
(into conf
|
(into conf
|
||||||
(for [k (keys defaults)]
|
(for [k (keys defaults)]
|
||||||
(let [val1 (get conf k)
|
(let [conf-val (get conf k)
|
||||||
val2 (get defaults k)]
|
default-val (get defaults k)]
|
||||||
(if (and (map? val1)
|
(if (and (map? conf-val) ; both are maps, we have to go level deeper
|
||||||
(map? val2))
|
(map? default-val)) ; If only one is, we don't care cus then it's just assigment
|
||||||
{k (map-apply-defaults val1 val2)}
|
{k (map-apply-defaults conf-val default-val)}
|
||||||
{k (nil?-else val2 val1)})))))
|
{k (nil?-else conf-val default-val)})))))
|
||||||
|
|
|
@ -16,13 +16,47 @@
|
||||||
(:require [clojure.test :refer :all]
|
(:require [clojure.test :refer :all]
|
||||||
[rss-thread-watch.utils :refer :all]))
|
[rss-thread-watch.utils :refer :all]))
|
||||||
|
|
||||||
(def first-map {:a :b
|
(def first-map
|
||||||
:c "c"})
|
"Example config map with two keys"
|
||||||
|
{:a :b
|
||||||
|
:c "c"
|
||||||
|
:nested {:fst 1 :scnd {:super :nested}}})
|
||||||
|
|
||||||
|
(def pony-map
|
||||||
|
"Map containing none of the items in map 1"
|
||||||
|
{:best-pony "Twilight Sparkle"})
|
||||||
|
|
||||||
|
(def conflicting-basic-merge (conj pony-map {:a 17 :c 15}))
|
||||||
|
|
||||||
|
(def deep-pony-map {:a "x"
|
||||||
|
:c :something-else
|
||||||
|
:nested {:ponies "everywhere"
|
||||||
|
:fst 69}})
|
||||||
|
|
||||||
(def empty-map {})
|
(def empty-map {})
|
||||||
|
|
||||||
(deftest map-deep-merge-missing-test
|
(deftest map-deep-merge-missing-test
|
||||||
(testing "Default values in place of missing keys"
|
(testing "Full and no-replace"
|
||||||
(is (= first-map (map-apply-defaults first-map empty-map))
|
(is (= first-map (map-apply-defaults first-map empty-map))
|
||||||
"No defaults should return conf map unchanged")
|
"No defaults should return conf map unchanged")
|
||||||
(is (= first-map (map-apply-defaults empty-map first-map))
|
(is (= first-map (map-apply-defaults empty-map first-map))
|
||||||
"Empty map should be completely replaced by defaults")))
|
"Empty map should be completely replaced by defaults"))
|
||||||
|
|
||||||
|
(testing "Basic merge"
|
||||||
|
(is (= (conj pony-map first-map) (map-apply-defaults first-map pony-map))
|
||||||
|
"When all keys unique, maps should be conjd")
|
||||||
|
(is (= (conj first-map pony-map) (map-apply-defaults first-map pony-map))
|
||||||
|
"When all keys unique, maps should be conjd, order matters")
|
||||||
|
(is (= (conj first-map pony-map) (map-apply-defaults pony-map first-map))
|
||||||
|
"When all keys unique, maps should be conjd, more order that matters")
|
||||||
|
(is (= (conj first-map pony-map) (map-apply-defaults first-map pony-map))
|
||||||
|
"Conflicting basic merge"))
|
||||||
|
;; Most important part, this is the reason we have the function in the first place
|
||||||
|
;; Conj wont merge deep
|
||||||
|
(testing "Nested merge"
|
||||||
|
(is (= {:a :b
|
||||||
|
:c "c"
|
||||||
|
:nested {:ponies "everywhere"
|
||||||
|
:fst 1
|
||||||
|
:scnd {:super :nested}}}
|
||||||
|
(map-apply-defaults first-map deep-pony-map)))))
|
||||||
|
|
Loading…
Reference in a new issue