|
| 1 | +(ns lambdaisland.deep-diff2.minimize-test |
| 2 | + (: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] |
| 7 | + [lambdaisland.deep-diff2.diff-impl :as diff] |
| 8 | + [lambdaisland.deep-diff2 :as ddiff])) |
| 9 | + |
| 10 | +(deftest basic-strip-test |
| 11 | + (testing "diff without minimize" |
| 12 | + (let [x {:a 1 :b 2 :d {:e 1} :g [:e [:k 14 :g 15]]} |
| 13 | + y {:a 1 :c 3 :d {:e 15} :g [:e [:k 14 :g 15]]}] |
| 14 | + (is (= (ddiff/diff x y) |
| 15 | + {:a 1 |
| 16 | + (diff/->Deletion :b) 2 |
| 17 | + :d {:e (diff/->Mismatch 1 15)} |
| 18 | + :g [:e [:k 14 :g 15]] |
| 19 | + (diff/->Insertion :c) 3})))) |
| 20 | + (testing "diff with minimize" |
| 21 | + (let [x {:a 1 :b 2 :d {:e 1} :g [:e [:k 14 :g 15]]} |
| 22 | + y {:a 1 :c 3 :d {:e 15} :g [:e [:k 14 :g 15]]}] |
| 23 | + (is (= (ddiff/minimize (ddiff/diff x y)) |
| 24 | + {(diff/->Deletion :b) 2 |
| 25 | + :d {:e (diff/->Mismatch 1 15)} |
| 26 | + (diff/->Insertion :c) 3}))))) |
| 27 | + |
| 28 | +(deftest minimize-on-diff-test |
| 29 | + (testing "diffing atoms" |
| 30 | + (testing "when different" |
| 31 | + (is (= (ddiff/minimize |
| 32 | + (ddiff/diff :a :b)) |
| 33 | + (diff/->Mismatch :a :b)))) |
| 34 | + |
| 35 | + (testing "when equal" |
| 36 | + (is (= (ddiff/minimize |
| 37 | + (ddiff/diff :a :a)) |
| 38 | + nil)))) |
| 39 | + |
| 40 | + (testing "diffing collections" |
| 41 | + (testing "when different collection types" |
| 42 | + (is (= (ddiff/minimize |
| 43 | + (ddiff/diff [:a :b] #{:a :b})) |
| 44 | + (diff/->Mismatch [:a :b] #{:a :b})))) |
| 45 | + |
| 46 | + (testing "when equal with clojure set" |
| 47 | + (is (= (ddiff/minimize |
| 48 | + (ddiff/diff #{:a :b} #{:a :b})) |
| 49 | + #{}))) |
| 50 | + |
| 51 | + (testing "when different with clojure set" |
| 52 | + (is (= (ddiff/minimize |
| 53 | + (ddiff/diff #{:a :b :c} #{:a :b :d})) |
| 54 | + #{(diff/->Insertion :d) (diff/->Deletion :c)}))) |
| 55 | + |
| 56 | + (testing "when equal with clojure vector" |
| 57 | + (is (= (ddiff/minimize |
| 58 | + (ddiff/diff [:a :b] [:a :b])) |
| 59 | + []))) |
| 60 | + |
| 61 | + (testing "when equal with clojure hashmap" |
| 62 | + (is (= (ddiff/minimize |
| 63 | + (ddiff/diff {:a 1} {:a 1})) |
| 64 | + {}))) |
| 65 | + |
| 66 | + (testing "when equal with clojure nesting vector" |
| 67 | + (is (= (ddiff/minimize |
| 68 | + (ddiff/diff [:a [:b :c :d]] [:a [:b :c :d]])) |
| 69 | + []))))) |
| 70 | + |
| 71 | +;; "diff itself and minimize yields empty" |
| 72 | +(defspec diff-itself 100 |
| 73 | + (prop/for-all |
| 74 | + [x diff-test/gen-any-except-NaN] |
| 75 | + (if (coll? x) |
| 76 | + (= (ddiff/minimize (ddiff/diff x x)) |
| 77 | + (empty x)) |
| 78 | + (nil? (ddiff/minimize (ddiff/diff x x)))))) |
0 commit comments