diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a7afa5..2c5d574 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ## Fixed +- Fixed Fix replacements mispairing when second sequence is longer - Fixed puget printer for futures ## Changed diff --git a/src/lambdaisland/deep_diff2/diff_impl.cljc b/src/lambdaisland/deep_diff2/diff_impl.cljc index e158c95..68f072b 100644 --- a/src/lambdaisland/deep_diff2/diff_impl.cljc +++ b/src/lambdaisland/deep_diff2/diff_impl.cljc @@ -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))]))) diff --git a/test/lambdaisland/deep_diff2/diff_test.cljc b/test/lambdaisland/deep_diff2/diff_test.cljc index 4e0cff2..3c1b726 100644 --- a/test/lambdaisland/deep_diff2/diff_test.cljc +++ b/test/lambdaisland/deep_diff2/diff_test.cljc @@ -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" @@ -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]}] @@ -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)