Skip to content

Commit 5793228

Browse files
committed
Improve handling for sets and maps
Make sure we don't report a difference when there is none. For sets the issue came from handling them as ordered collections when they aren't, with maps we had a case of using sets as predicates, which breaks when one of the elements is false or null. Closes #43
1 parent e8027e5 commit 5793228

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

src/lambdaisland/deep_diff2/diff_impl.cljc

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,31 @@
9797
(diff-seq-insertions ins)
9898
(into []))))
9999

100+
(defn diff-set [exp act]
101+
(into
102+
(into #{}
103+
(map (fn [e]
104+
(if (contains? act e)
105+
e
106+
(->Deletion e))))
107+
exp)
108+
(map ->Insertion)
109+
(remove #(contains? exp %) act)))
110+
111+
(let [exp {false 0, 0 0}
112+
act {false 0, 0 0}
113+
exp-ks (keys exp)
114+
act-ks (concat (filter #(contains? (set (keys act)) %) exp-ks)
115+
(remove #(contains? (set exp-ks) %) (keys act)))
116+
[del ins] (del+ins exp-ks act-ks)]
117+
[del ins])
118+
(del+ins [0 false] [0 false])
119+
100120
(defn diff-map [exp act]
101121
(first
102122
(let [exp-ks (keys exp)
103-
act-ks (concat (filter (set (keys act)) exp-ks)
104-
(remove (set exp-ks) (keys act)))
123+
act-ks (concat (filter #(contains? (set (keys act)) %) exp-ks)
124+
(remove #(contains? (set exp-ks) %) (keys act)))
105125
[del ins] (del+ins exp-ks act-ks)]
106126
(reduce
107127
(fn [[m idx] k]
@@ -162,10 +182,7 @@
162182
(extend-protocol Diff
163183
#?(:clj java.util.Set :cljs cljs.core/PersistentHashSet)
164184
(-diff-similar [exp act]
165-
(let [exp-seq (seq exp)
166-
act-seq (seq act)]
167-
(set (diff-seq exp-seq (concat (filter act exp-seq)
168-
(remove exp act-seq))))))
185+
(diff-set exp act))
169186
#?@(:clj
170187
[java.util.List
171188
(-diff-similar [exp act] (diff-seq exp act))

test/lambdaisland/deep_diff2/diff_test.cljc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,16 @@
212212
(gen/such-that (complement NaN?) gen/simple-type)))
213213

214214
(defspec round-trip-diff 100
215-
(prop/for-all [x gen-any-except-NaN
216-
y gen-any-except-NaN]
217-
(let [diff (diff/diff x y)]
218-
(= [x y] [(diff/left-undiff diff) (diff/right-undiff diff)]))))
215+
(prop/for-all
216+
[x gen-any-except-NaN
217+
y gen-any-except-NaN]
218+
(let [diff (diff/diff x y)]
219+
(= [x y] [(diff/left-undiff diff) (diff/right-undiff diff)]))))
220+
221+
(defspec diff-same-is-same 100
222+
(prop/for-all
223+
[x gen-any-except-NaN]
224+
(= x (diff/diff x x))))
219225

220226
(deftest diff-seq-test
221227
(is (= [(diff/->Insertion 1) 2 (diff/->Insertion 3)]

test/lambdaisland/deep_diff2/manipulate_test.cljc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@
7070
[])))))
7171

7272
;; "diff itself and remove-unchanged yields empty"
73-
(comment
74-
(defspec diff-itself 100
75-
(prop/for-all [x diff-test/gen-any-except-NaN]
76-
(if (coll? x)
77-
(= (manipulate/remove-unchanged (ddiff/diff x x))
78-
(empty x))
79-
(= (manipulate/remove-unchanged (ddiff/diff x x))
80-
nil)))))
73+
(defspec diff-itself 100
74+
(prop/for-all
75+
[x diff-test/gen-any-except-NaN]
76+
(if (coll? x)
77+
(= (manipulate/remove-unchanged (ddiff/diff x x))
78+
(empty x))
79+
(nil? (manipulate/remove-unchanged (ddiff/diff x x))))))

0 commit comments

Comments
 (0)