Created
January 17, 2014 20:27
-
-
Save senior/8480800 to your computer and use it in GitHub Desktop.
simple-check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(sm/defn basic-diff | |
"Basic diffing that returns only the keys/values of `right` whose values don't match those of `left`. | |
This is different from clojure.data/diff in that it treats non-equal sets as completely different | |
(rather than returning only the differing items of the set) and only returns differences from `right`." | |
[left right] | |
(reduce-kv (fn [acc k right-value] | |
(let [left-value (get left k)] | |
(if (= left-value right-value) | |
acc | |
(assoc acc k right-value)))) | |
{} right)) | |
;;original | |
(deftest test-resource-metadata-diff | |
(are [expected left right] (= expected (basic-diff left right)) | |
{} | |
{:type "foo" :title "bar"} | |
{:type "foo" :title "bar"} | |
{:line 20} | |
{:type "File" | |
:title "/etc/foobar/baz" | |
:exported false | |
:file "/tmp/bar" | |
:line 10 | |
:tags #{"file" "class" "foobar"}} | |
{:type "File" | |
:title "/etc/foobar/baz" | |
:exported false | |
:file "/tmp/bar" | |
:line 20 | |
:tags #{"file" "class" "foobar"}} | |
{:exported true | |
:file "/tmp/bar/baz" | |
:line 30 | |
:tags #{"file" "class" "foobar" "baz"}} | |
{:type "File" | |
:title "/etc/foobar/baz" | |
:exported false | |
:file "/tmp/bar" | |
:line 20 | |
:tags #{"file" "class" "foobar"}} | |
{:type "File" | |
:title "/etc/foobar/baz" | |
:exported true | |
:file "/tmp/bar/baz" | |
:line 30 | |
:tags #{"file" "class" "foobar" "baz"}})) | |
;;simple-check | |
(defspec diff-test | |
50 | |
(prop/for-all [ks (gen/such-that gen/not-empty (gen/vector gen/keyword 1000)) | |
v1 (gen/such-that gen/not-empty (gen/vector gen/any-printable 500)) | |
v2 (gen/such-that gen/not-empty (gen/vector gen/any-printable 500)) | |
v3 (gen/such-that gen/not-empty (gen/vector gen/any-printable 500))] | |
(let [left (zipmap ks (concat v1 v2)) | |
right (zipmap ks (concat v1 v3)) | |
result (basic-diff left right)] | |
(every? (fn [k] | |
(let [l (get left k) | |
r (get right k) | |
res (get result k)] | |
(or (and (= r res) | |
(not= res l)) | |
(= r l)))) | |
(keys right))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment