Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## Fixed

- Fixed Fix replacements mispairing when second sequence is longer
- Fixed puget printer for futures

## Changed
Expand Down
16 changes: 12 additions & 4 deletions src/lambdaisland/deep_diff2/diff_impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,18 @@
(dissoc (dec d))
(assoc d (seq (concat (next i)
(get ins d))))))
(recur rep
del
(next del-rest)
ins)))
(if-let [nearest (first (sort (filter #(> % d) (keys ins))))]
(if (and (every? del (range (inc d) (inc nearest)))
(seq (get ins nearest)))
(let [i (get ins nearest)]
(recur (assoc rep d (first i))
(disj del d)
(next del-rest)
(-> ins
(dissoc nearest)
(assoc d (seq (concat (next i) (get ins d)))))))
(recur rep del (next del-rest) ins))
(recur rep del (next del-rest) ins))))
[rep del (into {}
(remove (comp nil? val))
(shift-insertions ins))])))
Expand Down
9 changes: 7 additions & 2 deletions test/lambdaisland/deep_diff2/diff_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
(is (= [(diff/->Insertion []) {} (diff/->Insertion [])]
(diff/diff [{}] [[] {} []])))

(is (= [0 (diff/->Deletion 1) (diff/->Mismatch 2 :x) (diff/->Insertion :y) (diff/->Insertion :z)]
(is (= [0 (diff/->Mismatch 1 :x) (diff/->Mismatch 2 :y) (diff/->Insertion :z)]
(diff/diff [0 1 2] [0 :x :y :z]))))

(testing "sets"
Expand Down Expand Up @@ -167,7 +167,7 @@
(is (= [#{1 2} {2 [:x :y :z]}]
(diff/del+ins [0 1 2] [0 :x :y :z])))

(is (= [{2 :x} #{1} {2 '(:y :z)}]
(is (= [{1 :x, 2 :y} #{} {2 '(:z)}]
(diff/replacements [#{1 2} {2 [:x :y :z]}])))

(is (= [{} #{} {-1 [1], 1 [3], 3 [5]}]
Expand Down Expand Up @@ -304,6 +304,11 @@
(is (= [:a (diff/->Deletion :b) :c (diff/->Insertion :d)]
(diff/diff-seq [:a :b :c] [:a :c :d]))))

(deftest diff-longer-second-seq-test
(testing "when second sequence is longer and has no common elements, replacements are paired correctly"
(is (= [{:a (diff/->Mismatch 1 2)} {:b (diff/->Mismatch 1 2)} (diff/->Insertion {:c 2})]
(diff/diff '({:a 1} {:b 1}) '({:a 2} {:b 2} {:c 2}))))))


(comment
(use 'kaocha.repl)
Expand Down