Skip to content

Migrate codebase to cljc #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ classes
*.png
files
docs/
target/

.lein-failures
.lein-repl-history
.nrepl-port
*-init.clj
38 changes: 25 additions & 13 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
(defproject clj-diff "1.0.0-SNAPSHOT"
:description "Sequential diff in Clojure."
:url "http://github.com/brentonashworth/clj-diff"
:source-path "src/clj"
:java-source-path "src/jvm"
:java-fork "true"
:java-debug "true"
:hooks [leiningen.hooks.javac
leiningen.hooks.difftest]
:dev-dependencies [[org.clojure/clojure "1.2.0"]
[lein-javac "1.2.1-SNAPSHOT"]
[marginalia "0.5.0"]
[lein-difftest "1.3.2-SNAPSHOT"]])
(defproject clj-diff "1.1.0-SNAPSHOT"
:description "Sequential diff in Clojure/Script."
:url "http://github.com/rymndhng/clj-diff"
:auto-clean false
:java-source-paths ["src/jvm"]
:jar-exclusions [#"\.swp|\.swo|\.DS_Store|\.class"]

:dependencies [[org.clojure/clojure "1.7.0-RC1"]]

:profiles {:dev {:dependencies [[org.clojure/clojurescript "0.0-3308"]
[marginalia "0.8.0"]]
:plugins [[lein-difftest "2.0.0"]
[com.cemerick/clojurescript.test "0.3.3"]
[lein-cljsbuild "1.0.6"]]
:aliases {"cleantest" ["do" "clean," "once," "test," "cljsbuild" "test"]
"jar" ["do" "clean," "once," "jar"]
"deploy" ["do" "clean," "deploy" "clojars"]}}}

:cljsbuild {:builds
{:test {:source-paths ["target/classes" "target/test-classes"]
:compiler {:output-to "target/testable.js"
:optimizations :simple
:pretty-print true}}}
:test-commands {"test" ["node" :node-runner "target/testable.js"]}}
)
7 changes: 4 additions & 3 deletions src/clj/clj_diff/core.clj → src/clj_diff/core.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns clj-diff.core
"Diff, patch and related functions for Clojure sequences."
(:require [clj-diff [miller :as miller]]))
(:require [clj-diff.miller :as miller]))

(defn diff
"Create the edit script for transforming sequance a into sequence b.
Expand Down Expand Up @@ -49,13 +49,14 @@
patch will use the edit script to transform a into b.

(diff a b) -> x, (patch a x) -> b."
(fn [s _] (class s)))
#?(:clj (fn [s _] (class s))
:cljs (fn [s _] (when (string? s) :string))))

(defmethod patch :default
[s edit-script]
(patch* s edit-script))

(defmethod patch String
(defmethod patch #?(:clj String :cljs :string)
[s edit-script]
(apply str (patch* s edit-script)))

Expand Down
5 changes: 3 additions & 2 deletions src/clj/clj_diff/miller.clj → src/clj_diff/miller.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Sun Wu, Udi Manber, Gene Myers and Web Miller.

Please refer to the above paper while reading this code."
(:require [clj-diff [optimizations :as opt]]))
#?(:clj (:require [clj-diff [optimizations :as opt]])))

(defn- next-x
"Get the next farthest x value by looking at previous farthest values on the
Expand Down Expand Up @@ -223,9 +223,10 @@
[a b]
(seq-diff a b))

#?(:clj
(defmethod diff :string
[a b]
(opt/diff a b seq-diff))
(opt/diff a b seq-diff)))

(defn seq-edit-dist
[a b]
Expand Down
File renamed without changes.
78 changes: 41 additions & 37 deletions test/clj_diff/test/core.clj → test/clj_diff/test/core.cljc
Original file line number Diff line number Diff line change
@@ -1,59 +1,63 @@
(ns clj-diff.test.core
(:use [clj-diff.core] :reload)
(:use [clojure.test]))
(:use [clj-diff.core :only (diff patch edit-distance
levenshtein-distance longest-common-subseq)])

(deftest diff-test
#?@(:clj [(:require [clojure.test :as t])]
:cljs [(:require [cemerick.cljs.test :as t])
(:require-macros [cemerick.cljs.test :as t])]))

(t/deftest diff-test
(let [t (fn [a b] (edit-distance (diff a b)))]
(is (= (t [1 2 3 4 3 2 3 2 1 2 3] [2 3 1 2 3 4 5 4 3])
(t/is (= (t [1 2 3 4 3 2 3 2 1 2 3] [2 3 1 2 3 4 5 4 3])
10))
(is (= (t "abcab" "cbab")
(t/is (= (t "abcab" "cbab")
3))
(is (= (t "abcabba" "cbabac")
(t/is (= (t "abcabba" "cbabac")
5))
(is (= (t [{:a 1} {:a 2} {:a 3} {:a 4} {:a 5} {:a 6} {:a 7}]
(t/is (= (t [{:a 1} {:a 2} {:a 3} {:a 4} {:a 5} {:a 6} {:a 7}]
[{:a 2} {:a 3} {:a 4} {:a 5} {:a 6} {:a 7} {:a 1}])
2))
(is (= (t "FWdRgevf43" "T5C7U3Lz5v")
(t/is (= (t "FWdRgevf43" "T5C7U3Lz5v")
18))
(is (= (t "s2dI8h9aK1FWdRgevf43"
(t/is (= (t "s2dI8h9aK1FWdRgevf43"
"5hs8L9T3K2T5C7U3Lz5v")
30))
(is (= (t "nBP8GaFHVls2dI8h9aK1FWdRgevf43"
(t/is (= (t "nBP8GaFHVls2dI8h9aK1FWdRgevf43"
"925BCPcYhT5hs8L9T3K2T5C7U3Lz5v")
46))
(is (= (t "aba" "aca")
(t/is (= (t "aba" "aca")
2))))

(deftest patch-test
(is (= (patch "aba"
(t/deftest patch-test
(t/is (= (patch "aba"
{:+ [[1 \c]], :- [1]})
"aca"))
(is (= (patch [1 2 3 4 3 2 3 2 1 2 3]
(t/is (= (patch [1 2 3 4 3 2 3 2 1 2 3]
{:+ [[10 4 5 4 3]], :- [0 3 4 5 6 7]})
[2 3 1 2 3 4 5 4 3]))
(is (= (patch "abcab"
(t/is (= (patch "abcab"
{:+ [[2 \b]], :- [0 1]})
"cbab"))
(is (= (patch "abcabba"
(t/is (= (patch "abcabba"
{:+ [[2 \b] [6 \c]], :- [0 1 5]})
"cbabac"))
(is (= (patch "we1otec29q"
(t/is (= (patch "we1otec29q"
{:+ [[9 \g \B \n \B \L \N \C \U \D \B]]
:- [0 1 2 3 4 5 6 7 8 9]})
"gBnBLNCUDB"))
(is (= (patch [{:a 1} {:a 2} {:a 3} {:a 4} {:a 5} {:a 6} {:a 7}]
(t/is (= (patch [{:a 1} {:a 2} {:a 3} {:a 4} {:a 5} {:a 6} {:a 7}]
{:+ [[6 {:a 1}]], :- [0]})
[{:a 2} {:a 3} {:a 4} {:a 5} {:a 6} {:a 7} {:a 1}]))
(is (= (patch "FWdRgevf43"
(t/is (= (patch "FWdRgevf43"
{:+ [[8 \T \5 \C \7 \U] [9 \L \z \5 \v]]
:- [0 1 2 3 4 5 6 7 8]})
"T5C7U3Lz5v"))
(is (= (patch "s2dI8h9aK1FWdRgevf43"
(t/is (= (patch "s2dI8h9aK1FWdRgevf43"
{:+ [[-1 \5 \h] [5 \L] [7 \T \3]
[18 \2 \T \5 \C \7 \U] [19 \L \z \5 \v]]
:- [1 2 3 5 7 9 10 11 12 13 14 15 16 17 18]})
"5hs8L9T3K2T5C7U3Lz5v"))
(is (= (patch
(t/is (= (patch
"nBP8GaFHVls2dI8h9aK1FWdRgevf43"
{:+ [[0 \9 \2 \5]
[1 \C]
Expand All @@ -65,8 +69,8 @@
:- [0 3 4 5 6 7 8 9 11 12 13 15 17 19 20 21 22 23 24 25 26 27 28]})
"925BCPcYhT5hs8L9T3K2T5C7U3Lz5v")))

(deftest roundtrip
(are [a b]
(t/deftest roundtrip
(t/are [a b]
(= b (patch a (diff a b)))

"aba" "aca"
Expand All @@ -76,21 +80,21 @@
"nBP8GaFHVls2dI8h9aK1FWdRgevf43" "925BCPcYhT5hs8L9T3K2T5C7U3Lz5v"
"aba" "aca"))

(deftest edit-distance-test
(is (= (edit-distance (diff "aba" "aca"))
(t/deftest edit-distance-test
(t/is (= (edit-distance (diff "aba" "aca"))
2))
(is (= (edit-distance "aba" "aca")
(t/is (= (edit-distance "aba" "aca")
2))
(is (= (edit-distance (diff "abcabba" "cbabac"))
(t/is (= (edit-distance (diff "abcabba" "cbabac"))
5))
(is (= (edit-distance "abcabba" "cbabac")
(t/is (= (edit-distance "abcabba" "cbabac")
5))
(is (= (edit-distance (diff "nBP8GaFHVls2dI8h9aK1FWdRgevf43"
(t/is (= (edit-distance (diff "nBP8GaFHVls2dI8h9aK1FWdRgevf43"
"925BCPcYhT5hs8L9T3K2T5C7U3Lz5v"))
46)))

(deftest levenshtein-distance-test
(are [a b d] (= (levenshtein-distance a b) d)
(t/deftest levenshtein-distance-test
(t/are [a b d] (= (levenshtein-distance a b) d)
"aba" "aba" 0
"aba" "ada" 1
"abca" "aca" 1
Expand All @@ -101,8 +105,8 @@
"nBP8GaFHVls2dI8h9aK1FWdRgevf43" "925BCPcYhT5hs8L9T3K2T5C7U3Lz5v" 28
[1 2 4] [1 2 4 3] 1))

(deftest longest-common-subseq-test
(are [a b _ d] (= (longest-common-subseq a b) d)
(t/deftest longest-common-subseq-test
(t/are [a b _ d] (= (longest-common-subseq a b) d)
"aba" "aba" => "aba"
"aba" "ada" => "aa"
"abca" "aca" => "aca"
Expand All @@ -113,14 +117,14 @@
"nBP8GaFHVls2dI8h9aK1FWdRgevf43" "925BCPcYhT5hs8L9T3K2T5C7U3Lz5v" =>
"BPs89Kv"))

(deftest longest-common-subseq-seq-test
(are [a b _ d] (= (longest-common-subseq (seq a) (seq b)) d)
(t/deftest longest-common-subseq-seq-test
(t/are [a b _ d] (= (longest-common-subseq (seq a) (seq b)) d)
"kitten" "sitting" => [\i \t \t \n]
"Saturday" "Sunday" => [\S \u \d \a \y]
"gumbo" "gambol" => [\g \m \b \o]))

(deftest longest-common-subseq-clojure-test
(are [a b _ d] (= (longest-common-subseq (seq a) (seq b)) d)
(t/deftest longest-common-subseq-clojure-test
(t/are [a b _ d] (= (longest-common-subseq (seq a) (seq b)) d)
[:k :i :t :t :e :n] [:s :i :t :t :i :n :g] => [:i :t :t :n]
[:s :a :t :u :r :d :a :y] [:s :u :n :d :a :y] => [:s :u :d :a :y]
[{:x 1 :y 3} {:x 2 :y 7} {:x 3 :y 2} {:x 8 :y 3}]
Expand Down
Loading