Skip to content

Commit e8027e5

Browse files
committed
improve remove-unchanged
1 parent 337924d commit e8027e5

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
lines changed

src/lambdaisland/deep_diff2/manipulate.cljc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@
2828
(defn remove-unchanged
2929
"Postwalk diff, removing values that are unchanged"
3030
[diff]
31-
(postwalk
32-
(fn [x]
33-
(cond
34-
(map-entry? x) (when ;; Either k or v of a map-entry contains/is? diff-item,
35-
;; keep the map-entry. Otherwise, remove it.
36-
(or (diff-item? (key x))
37-
(has-diff-item? (val x)))
38-
x)
39-
:else x))
40-
diff))
31+
(let [y (postwalk
32+
(fn [x]
33+
(cond
34+
(map-entry? x) (when
35+
;; Either k or v of a map-entry contains/is? diff-item,
36+
;; keep the map-entry. Otherwise, remove it.
37+
(or (diff-item? (key x))
38+
(has-diff-item? (val x)))
39+
x)
40+
(or (set? x) (sequential? x)) (into (empty x) (filter #(or (diff-item? %)
41+
(has-diff-item? %)) x))
42+
:else (do
43+
x)))
44+
diff)]
45+
(cond
46+
(coll? y) y
47+
:else nil)))
4148

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
(ns lambdaisland.deep-diff2.manipulate-test
22
(:require [clojure.test :refer [deftest testing is are]]
3+
[lambdaisland.deep-diff2.diff-test :as diff-test]
4+
[clojure.test.check.clojure-test :refer [defspec]]
5+
[clojure.test.check.generators :as gen]
6+
[clojure.test.check.properties :as prop]
37
[lambdaisland.deep-diff2.diff-impl :as diff]
48
[lambdaisland.deep-diff2.manipulate :as manipulate]
59
[lambdaisland.deep-diff2 :as ddiff]))
610

7-
(deftest strip-test
8-
(testing "removing the same items"
11+
(deftest basic-strip-test
12+
(testing "diff without remove-unchanged"
913
(let [x {:a 1 :b 2 :d {:e 1} :g [:e [:k 14 :g 15]]}
1014
y {:a 1 :c 3 :d {:e 15} :g [:e [:k 14 :g 15]]}]
1115
(is (= (ddiff/diff x y)
@@ -14,10 +18,63 @@
1418
:d {:e (diff/->Mismatch 1 15)}
1519
:g [:e [:k 14 :g 15]]
1620
(diff/->Insertion :c) 3}))))
17-
(testing "removing the same items"
21+
(testing "diff with remove-unchanged"
1822
(let [x {:a 1 :b 2 :d {:e 1} :g [:e [:k 14 :g 15]]}
1923
y {:a 1 :c 3 :d {:e 15} :g [:e [:k 14 :g 15]]}]
2024
(is (= (manipulate/remove-unchanged (ddiff/diff x y))
2125
{(diff/->Deletion :b) 2
2226
:d {:e (diff/->Mismatch 1 15)}
2327
(diff/->Insertion :c) 3})))))
28+
29+
(deftest remove-unchanged-on-diff-test
30+
(testing "diffing atoms"
31+
(testing "when different"
32+
(is (= (manipulate/remove-unchanged
33+
(ddiff/diff :a :b))
34+
(diff/->Mismatch :a :b))))
35+
36+
(testing "when equal"
37+
(is (= (manipulate/remove-unchanged
38+
(ddiff/diff :a :a))
39+
nil))))
40+
41+
(testing "diffing collections"
42+
(testing "when different collection types"
43+
(is (= (manipulate/remove-unchanged
44+
(ddiff/diff [:a :b] #{:a :b}))
45+
(diff/->Mismatch [:a :b] #{:a :b}))))
46+
47+
(testing "when equal with clojure set"
48+
(is (= (manipulate/remove-unchanged
49+
(ddiff/diff #{:a :b} #{:a :b}))
50+
#{})))
51+
52+
(testing "when different with clojure set"
53+
(is (= (manipulate/remove-unchanged
54+
(ddiff/diff #{:a :b :c} #{:a :b :d}))
55+
#{(diff/->Insertion :d) (diff/->Deletion :c)})))
56+
57+
(testing "when equal with clojure vector"
58+
(is (= (manipulate/remove-unchanged
59+
(ddiff/diff [:a :b] [:a :b]))
60+
[])))
61+
62+
(testing "when equal with clojure hashmap"
63+
(is (= (manipulate/remove-unchanged
64+
(ddiff/diff {:a 1} {:a 1}))
65+
{})))
66+
67+
(testing "when equal with clojure nesting vector"
68+
(is (= (manipulate/remove-unchanged
69+
(ddiff/diff [:a [:b :c :d]] [:a [:b :c :d]]))
70+
[])))))
71+
72+
;; "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)))))

0 commit comments

Comments
 (0)