Finish tests for `map-apply-defaults` and refactor internal names

This commit is contained in:
Felisp 2024-01-08 03:01:42 +01:00
parent 2325b154d1
commit 67268acbc5
2 changed files with 44 additions and 10 deletions

View file

@ -62,9 +62,9 @@
[conf defaults]
(into conf
(for [k (keys defaults)]
(let [val1 (get conf k)
val2 (get defaults k)]
(if (and (map? val1)
(map? val2))
{k (map-apply-defaults val1 val2)}
{k (nil?-else val2 val1)})))))
(let [conf-val (get conf k)
default-val (get defaults k)]
(if (and (map? conf-val) ; both are maps, we have to go level deeper
(map? default-val)) ; If only one is, we don't care cus then it's just assigment
{k (map-apply-defaults conf-val default-val)}
{k (nil?-else conf-val default-val)})))))

View file

@ -16,13 +16,47 @@
(:require [clojure.test :refer :all]
[rss-thread-watch.utils :refer :all]))
(def first-map {:a :b
:c "c"})
(def first-map
"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 {})
(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))
"No defaults should return conf map unchanged")
(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)))))