Skip to content

Commit

Permalink
fixed docs
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeJahad committed Jun 1, 2010
1 parent 1a5958e commit fc1eb08
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
1 change: 0 additions & 1 deletion #README.muse#

This file was deleted.

11 changes: 5 additions & 6 deletions README
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
Diffs two clojure forms, eg: Diffs two clojure forms, eg:


(use 'com.georgejahad.difform) (use 'com.georgejahad.difform)
(difform { 1 2 3 4 6 5} { 6 7 3 4 1 2}) (difform { 1 2 3 4 5 6} { 5 6 1 2 3 7})

{1 2, 3
{1 2, 3 4, 6 - 4
- 5
+ 7 + 7
} , 5 6}


(difform (difform


Expand Down Expand Up @@ -55,4 +54,4 @@ A very common use is with clojure.test. For example, if clojure test reports yo
FAIL in (xyz-test) (xyz_spec.clj:7) FAIL in (xyz-test) (xyz_spec.clj:7)
expected: (= form1 form2) expected: (= form1 form2)


Run (form-diff form1 form2) to see what the exact failure was. Run (diff form1 form2) to see what the exact failure was.
90 changes: 90 additions & 0 deletions difform.muse
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,90 @@
#title difform - June 1, 2010
** Diffing Clojure Forms

Start with Stuart Sierra's excellent clojure.walk, (to sort the
maps/sets,) add Tom Faulhauber wonderful Clojure pretty print, and
finish it off with Neil Fraser's magnificent Java diff library:
http://code.google.com/p/google-diff-match-patch/wiki/API, and you'll
find it's pretty easy to create a function to diff Clojure forms.
Mine's here:

http://github.com/GeorgeJahad/difform

http://clojars.org/difform

Works like this:

<example>
(use 'com.georgejahad.difform)
(difform { 1 2 3 4 5 6} { 5 6 1 2 3 7})
{1 2, 3
- 4
+ 7
, 5 6}
</example>

As you can see, the two forms differ only in the value of key 3, where
it is 4 in the first form and 7 in the second. For a more interesting
example,

<example>
(difform
{:cart {:items [{:new-delivery {:ids ["-1"]}, :old-unit-price "700.00", :product-id "prod-1", :quantity "1", :unit-price "700.00"}], :old-total-dollars "700.00", :purchase? false, :total-dollars "700.00"}, :day "2010-05-18", :event-ids ["1274166000010:some-session-id-1"], :old-id "14", :delivery {:ids [nil]}, :session "some-session-id-1"}
{:day "2010-05-18", :old-id "14", :session "some-session-id-1", :delivery nil, :cart {:purchase? false, :old-total-dollars "1400.00", :total-dollars "1400.00", :items [{:product-id "prod-1", :old-unit-price "700.00", :unit-price "700.00", :quantity "2", :new-delivery {:ids ["-1"]}}]}, :event-ids ["1274166000000:some-session-id-1"]})
</example>

produces the following. Here the values of the quantity,
total-dollars, and a few other fields have changed:



<example>
{:cart
{:items
[{:new-delivery {:ids ["-1"]},
:old-unit-price "700.00",
:product-id "prod-1",
:quantity "
- 1
+ 2
",
:unit-price "700.00"}],
:old-total-dollars "
- 7
+ 14
00.00",
:purchase? false,
:total-dollars "
- 7
+ 14
00.00"},
:day "2010-05-18",
:delivery
- {:ids [
nil
- ]}
,
:event-ids ["1274166
+ 0
0000
- 1
0:some-session-id-1"],
:old-id "14",
:session "some-session-id-1"}
</example>

A very common use is with clojure.test. For example, if clojure test reports you have failed a test like this:

FAIL in (xyz-test) (xyz_spec.clj:7)
expected: (= form1 form2)

Run (difform form1 form2) to see what the exact failure was.

** Comments/Suggestions

Send any comments/suggestions to George Jahad at "george-clojure at blackbirdsystems.net" or to the main clojure mailing list: http://groups.google.com/group/clojure

5 changes: 3 additions & 2 deletions src/com/georgejahad/difform.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
(defn- sort-form [f] (defn- sort-form [f]
(postwalk s-form f)) (postwalk s-form f))


(def diff-markers (def diff-markers
{diff_match_patch$Operation/EQUAL " " {diff_match_patch$Operation/EQUAL " "
diff_match_patch$Operation/INSERT "+" diff_match_patch$Operation/INSERT "+"
diff_match_patch$Operation/DELETE "-"}) diff_match_patch$Operation/DELETE "-"})


(defn- print-diff [d] (defn- print-diff [d]
(let [m (diff-markers (.operation d)) ] (let [m (diff-markers (.operation d)) ]
(println (str " " m) (s/replace (.trim (.text d)) #"\n" (str "\n " m " "))))) (println (str " " m) (s/replace (.trim (.text d))
#"\n" (str "\n " m " ")))))


(defn difform [x y] (defn difform [x y]
(let [diffs (.diff_main (diff_match_patch.) (let [diffs (.diff_main (diff_match_patch.)
Expand Down

0 comments on commit fc1eb08

Please sign in to comment.