From 67268acbc513921b12239e5fbd3256c2f600889c Mon Sep 17 00:00:00 2001 From: Felisp Date: Mon, 8 Jan 2024 03:01:42 +0100 Subject: [PATCH] Finish tests for ``map-apply-defaults`` and refactor internal names --- src/rss_thread_watch/utils.clj | 12 ++++---- test/rss_thread_watch/utils_test.clj | 42 +++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/rss_thread_watch/utils.clj b/src/rss_thread_watch/utils.clj index 5e28a87..0de1280 100644 --- a/src/rss_thread_watch/utils.clj +++ b/src/rss_thread_watch/utils.clj @@ -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)}))))) diff --git a/test/rss_thread_watch/utils_test.clj b/test/rss_thread_watch/utils_test.clj index e90a305..0115dce 100644 --- a/test/rss_thread_watch/utils_test.clj +++ b/test/rss_thread_watch/utils_test.clj @@ -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)))))