Skip to content

Commit 780d69f

Browse files
committed
Invoke rewrite-clj only when tests are run
While diagnosing a reader behaviour, I noticed that many tests namespaces were effecting calls to rewrite-clj at namespace load time. Moved to always scope vars within tests so that rewrite-clj is only invoked when tests are actually run.
1 parent f78856e commit 780d69f

File tree

8 files changed

+394
-398
lines changed

8 files changed

+394
-398
lines changed

test/rewrite_clj/examples/cljx_test.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@
101101
(z/root-string
102102
(cljx-walk zloc active-profiles))))
103103

104-
(let [data (str "(defn debug-inc\n"
105-
" [x]\n"
106-
" #+debug (println #-compact :debug 'inc x)\n"
107-
" (inc x))")]
108-
(deftest t-cljx
104+
(deftest t-cljx
105+
(let [data (str "(defn debug-inc\n"
106+
" [x]\n"
107+
" #+debug (println #-compact :debug 'inc x)\n"
108+
" (inc x))")]
109109
(are [?profiles ?result]
110110
(is (= ?result (cljx-string data ?profiles)))
111111
#{}

test/rewrite_clj/regression_test.cljc

Lines changed: 229 additions & 235 deletions
Large diffs are not rendered by default.

test/rewrite_clj/transform_test.cljc

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
11
(ns rewrite-clj.transform-test
22
(:require [clojure.string :as string]
3-
[clojure.test :refer [deftest is]]
3+
[clojure.test :refer [deftest testing is]]
44
[rewrite-clj.zip :as z]))
55

6-
(def data-string
7-
(str ";; This is a Project File.\n"
8-
"(defproject my-project \"0.1.0-SNAPSHOT\"\n"
9-
" :description \"A project.\n"
10-
" Multiline!\"\n"
11-
" :dependencies [[a \"0.1.0\"]\n"
12-
" [b \"1.2.3\"]]\n"
13-
" :repositories { \"private\" \"http://private.com/repo\" })"))
6+
(deftest t-transform
7+
(let [data-string (str ";; This is a Project File.\n"
8+
"(defproject my-project \"0.1.0-SNAPSHOT\"\n"
9+
" :description \"A project.\n"
10+
" Multiline!\"\n"
11+
" :dependencies [[a \"0.1.0\"]\n"
12+
" [b \"1.2.3\"]]\n"
13+
" :repositories { \"private\" \"http://private.com/repo\" })")
14+
data (z/of-string data-string)]
1415

15-
(def data (z/of-string data-string))
16+
(testing "simple transformation"
17+
(is (= (string/replace data-string "SNAPSHOT" "SNAPSHOT-1")
18+
(-> data
19+
(z/find-value z/next 'defproject)
20+
z/right z/right
21+
(z/edit #(str % "-1"))
22+
z/root-string))))
1623

17-
(deftest t-a-simple-transformation
18-
(is (= (string/replace data-string "SNAPSHOT" "SNAPSHOT-1")
19-
(-> data
20-
(z/find-value z/next 'defproject)
21-
z/right z/right
22-
(z/edit #(str % "-1"))
23-
z/root-string))))
24+
(testing "seq transformation prefix"
25+
(is (= (-> data-string
26+
(string/replace "[a " "[prefix-a ")
27+
(string/replace "[b " "[prefix-b "))
28+
(-> data
29+
(z/find-value z/next 'defproject)
30+
(z/find-value :dependencies) z/right
31+
(->>
32+
(z/map
33+
(fn [loc]
34+
(-> loc z/down
35+
(z/prefix "prefix-")
36+
z/up))))
37+
z/root-string))))
2438

25-
(deftest t-a-seq-transformation-prefix
26-
(is (= (-> data-string
27-
(string/replace "[a " "[prefix-a ")
28-
(string/replace "[b " "[prefix-b "))
29-
(-> data
30-
(z/find-value z/next 'defproject)
31-
(z/find-value :dependencies) z/right
32-
(->>
33-
(z/map
34-
(fn [loc]
35-
(-> loc z/down
36-
(z/prefix "prefix-")
37-
z/up))))
38-
z/root-string))))
39+
(testing "seq transformation suffix"
40+
(is (= (-> data-string
41+
(string/replace "[a " "[a-suffix ")
42+
(string/replace "[b " "[b-suffix "))
43+
(-> data
44+
(z/find-value z/next 'defproject)
45+
(z/find-value :dependencies) z/right
46+
(->>
47+
(z/map
48+
(fn [loc]
49+
(-> loc z/down
50+
(z/suffix "-suffix")
51+
z/up))))
52+
z/root-string))))
3953

40-
(deftest t-a-seq-transformation-suffix
41-
(is (= (-> data-string
42-
(string/replace "[a " "[a-suffix ")
43-
(string/replace "[b " "[b-suffix "))
44-
(-> data
45-
(z/find-value z/next 'defproject)
46-
(z/find-value :dependencies) z/right
47-
(->>
48-
(z/map
49-
(fn [loc]
50-
(-> loc z/down
51-
(z/suffix "-suffix")
52-
z/up))))
53-
z/root-string))))
54-
55-
(deftest t-whitespace-handling-in-removal
56-
(is (= (string/replace data-string #"\[a [^\s]+\]\s+" "")
57-
(-> data
58-
(z/find-value z/next 'defproject)
59-
(z/find-value :dependencies)
60-
z/right z/down z/remove
61-
z/root-string))))
54+
(testing "whitespace handling in removal"
55+
(is (= (string/replace data-string #"\[a [^\s]+\]\s+" "")
56+
(-> data
57+
(z/find-value z/next 'defproject)
58+
(z/find-value :dependencies)
59+
z/right z/down z/remove
60+
z/root-string))))))

test/rewrite_clj/zip/base_test.cljc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
[rewrite-clj.node :as node]
44
[rewrite-clj.zip :as z]))
55

6-
(let [n (node/forms-node
7-
[(node/spaces 3)
8-
(node/coerce [[1 2] 3])])
9-
s " [[1 2] 3]"]
10-
(deftest t-edn-for-zipper-creation-from-node
11-
(let [loc (z/edn* n)
12-
[_ a b c d] (iterate z/next* loc)]
6+
(deftest t-edn-for-zipper-creation-from-node
7+
(let [n (node/forms-node
8+
[(node/spaces 3)
9+
(node/coerce [[1 2] 3])])
10+
s " [[1 2] 3]"
11+
loc (z/edn* n)
12+
[_ a b c d] (iterate z/next* loc)]
1313
(is (= :forms (z/tag loc)))
1414
(is (= [[1 2] 3] (z/sexpr loc)))
1515
(is (= :whitespace (z/tag a)))
@@ -20,13 +20,13 @@
2020
(is (= "[1 2]" (z/string c)))
2121
(is (= s (with-out-str (z/print loc))))
2222
(is (= s (with-out-str (z/print-root loc))))
23-
(is (every? #{s} (map z/root-string [loc a b c d]))))))
23+
(is (every? #{s} (map z/root-string [loc a b c d])))))
2424

2525
#?(:clj
26-
(let [f (java.io.File/createTempFile "rewrite" ".clj")
27-
s " [[1 2] 3]"]
28-
(spit f s)
29-
(deftest t-zipper-creation-with-movement-to-first-non-ws-node
26+
(deftest t-zipper-creation-with-movement-to-first-non-ws-node
27+
(let [f (java.io.File/createTempFile "rewrite" ".clj")
28+
s " [[1 2] 3]"]
29+
(spit f s)
3030
(are [?loc]
3131
(let [loc ?loc
3232
[_ a b c d] (iterate z/next* loc)]

test/rewrite_clj/zip/editz_test.cljc

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
(ns rewrite-clj.zip.editz-test
2-
(:require [clojure.test :refer [deftest is are]]
2+
(:require [clojure.test :refer [deftest testing is are]]
33
[rewrite-clj.node :as node]
44
[rewrite-clj.zip :as z]))
55

6-
(let [root (z/of-string "[1 \"2\" :3]")
7-
elements (iterate z/next root)]
8-
(deftest t-edit-operations
9-
(are [?n ?f ?s]
10-
(let [loc (nth elements ?n)
11-
loc' (z/edit loc ?f)]
12-
(is (= ?s (z/root-string loc'))))
13-
0 #(subvec % 1) "[\"2\" :3]"
14-
1 #(str % "_x") "[\"1_x\" \"2\" :3]"
15-
2 #(keyword % "k") "[1 :2/k :3]"))
16-
(deftest t-replace-operations
17-
(are [?n ?v ?s]
18-
(let [loc (nth elements ?n)
19-
loc' (z/replace loc ?v)]
20-
(is (= ?s (z/root-string loc'))))
21-
0 [1] "[1]"
22-
1 #{0} "[#{0} \"2\" :3]")))
6+
(deftest t-edit-operations
7+
(let [root (z/of-string "[1 \"2\" :3]")
8+
elements (iterate z/next root)]
9+
(testing "edit"
10+
(are [?n ?f ?s]
11+
(let [loc (nth elements ?n)
12+
loc' (z/edit loc ?f)]
13+
(is (= ?s (z/root-string loc'))))
14+
0 #(subvec % 1) "[\"2\" :3]"
15+
1 #(str % "_x") "[\"1_x\" \"2\" :3]"
16+
2 #(keyword % "k") "[1 :2/k :3]"))
17+
(testing "replace"
18+
(are [?n ?v ?s]
19+
(let [loc (nth elements ?n)
20+
loc' (z/replace loc ?v)]
21+
(is (= ?s (z/root-string loc'))))
22+
0 [1] "[1]"
23+
1 #{0} "[#{0} \"2\" :3]"))))
2324

2425
(deftest t-edit-with-args
2526
(is (= "[1 102 3]" (-> "[1 2 3]"
@@ -49,9 +50,9 @@
4950
:unexpected))
5051
z/root-string)))))
5152

52-
(let [root (z/of-string "[1 [ ] [2 3] [ 4 ]]")
53-
elements (iterate z/next root)]
54-
(deftest t-splice-operations
53+
(deftest t-splice-operations
54+
(let [root (z/of-string "[1 [ ] [2 3] [ 4 ]]")
55+
elements (iterate z/next root)]
5556
(are [?n ?s ?e]
5657
(let [loc (nth elements ?n)
5758
loc' (z/splice loc)]

test/rewrite_clj/zip/move_test.cljc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
(:require [clojure.test :refer [deftest is are]]
33
[rewrite-clj.zip :as z]))
44

5-
(let [root (z/of-string "[ 1 [2 3] 4]")]
65
(deftest t-whitespace-aware-movement
7-
(are [?ops ?sexpr ?pred]
8-
(let [loc ((apply comp (reverse ?ops)) root)]
9-
(is (= ?sexpr (z/sexpr loc)))
10-
(is (?pred loc)))
11-
[z/down] 1 identity
12-
[z/next] 1 (complement z/end?)
13-
[z/next z/next] [2 3] (complement z/end?)
14-
[z/down z/right z/right] 4 (complement z/end?)
15-
[z/down z/rightmost] 4 z/rightmost?
16-
[z/down z/rightmost z/rightmost] 4 z/rightmost?
17-
[z/down z/leftmost] 1 z/leftmost?
18-
[z/down z/right z/leftmost] 1 z/leftmost?
19-
[z/down z/next z/next z/up] [2 3] identity
20-
[z/down z/next z/next z/up z/up] [1 [2 3] 4] #(and (z/leftmost? %) (z/rightmost? %))
21-
[z/down z/rightmost z/prev] 3 identity
22-
[z/down z/rightmost z/next] 4 z/end?)))
6+
(let [root (z/of-string "[ 1 [2 3] 4]")]
7+
(are [?ops ?sexpr ?pred]
8+
(let [loc ((apply comp (reverse ?ops)) root)]
9+
(is (= ?sexpr (z/sexpr loc)))
10+
(is (?pred loc)))
11+
[z/down] 1 identity
12+
[z/next] 1 (complement z/end?)
13+
[z/next z/next] [2 3] (complement z/end?)
14+
[z/down z/right z/right] 4 (complement z/end?)
15+
[z/down z/rightmost] 4 z/rightmost?
16+
[z/down z/rightmost z/rightmost] 4 z/rightmost?
17+
[z/down z/leftmost] 1 z/leftmost?
18+
[z/down z/right z/leftmost] 1 z/leftmost?
19+
[z/down z/next z/next z/up] [2 3] identity
20+
[z/down z/next z/next z/up z/up] [1 [2 3] 4] #(and (z/leftmost? %) (z/rightmost? %))
21+
[z/down z/rightmost z/prev] 3 identity
22+
[z/down z/rightmost z/next] 4 z/end?)))
2323

2424
(deftest t-moving-into-an-empty-inner-node
2525
(let [zloc (z/of-string "[]")]

test/rewrite_clj/zip/subedit_test.cljc

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
[rewrite-clj.zip :as z]
44
[rewrite-clj.zip.base :as zbase]))
55

6-
(let [root (z/of-string "[1 #{2 [3 4] 5} 6]")]
7-
(deftest t-modifying-subtrees
8-
(let [loc (z/subedit-> root
9-
z/next
10-
z/next
11-
z/next
12-
(z/replace* 'x))]
13-
(is (= :vector (z/tag loc)))
14-
(is (= "[1 #{x [3 4] 5} 6]" (z/string loc)))))
15-
(deftest t-modifying-the-whole-tree
16-
(let [loc (z/edit-> (-> root z/next z/next z/next)
17-
z/prev z/prev
18-
(z/replace* 'x))]
19-
(is (= :token (z/tag loc)))
20-
(is (= "2" (z/string loc)))
21-
(is (= "[x #{2 [3 4] 5} 6]" (z/root-string loc))))))
6+
(deftest t-trees
7+
(let [root (z/of-string "[1 #{2 [3 4] 5} 6]")]
8+
(testing "modifying subtrees"
9+
(let [loc (z/subedit-> root
10+
z/next
11+
z/next
12+
z/next
13+
(z/replace* 'x))]
14+
(is (= :vector (z/tag loc)))
15+
(is (= "[1 #{x [3 4] 5} 6]" (z/string loc)))))
16+
(testing "modifying the whole tree"
17+
(let [loc (z/edit-> (-> root z/next z/next z/next)
18+
z/prev z/prev
19+
(z/replace* 'x))]
20+
(is (= :token (z/tag loc)))
21+
(is (= "2" (z/string loc)))
22+
(is (= "[x #{2 [3 4] 5} 6]" (z/root-string loc)))))))
2223

2324
(deftest zipper-retains-options
2425
(let [zloc (z/of-string "(1 (2 (3 4 ::my-kw)))" {:auto-resolve (fn [_x] 'custom-resolved)})
Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
(ns rewrite-clj.zip.whitespace-test
2-
(:require [clojure.test :refer [deftest is are]]
2+
(:require [clojure.test :refer [deftest is testing are]]
33
[rewrite-clj.node :as node]
44
[rewrite-clj.zip :as z]))
55

6-
(let [space (node/spaces 1)
7-
linebreak (node/newlines 1)
8-
c0 (node/comment-node "comment")
9-
t0 (node/token-node 0)
10-
t1 (node/token-node 1)
11-
loc (z/edn* (node/forms-node [space linebreak t0 space t1 c0]))]
12-
(deftest t-predicates
13-
(is (z/whitespace? (-> loc z/down*)))
14-
(is (z/linebreak? (-> loc z/down* z/right*)))
15-
(is (z/whitespace-or-comment? (-> loc z/down* z/rightmost*))))
16-
(deftest t-skipping-whitespace-to-the-right
17-
(let [n (-> loc z/down* z/skip-whitespace z/node)]
18-
(is (= :token (node/tag n)))
19-
(is (= 0 (node/sexpr n)))))
20-
(deftest t-skipping-whitespace-to-the-left
21-
(let [n (-> loc z/down* z/rightmost* z/skip-whitespace-left z/node)]
22-
(is (= :token (node/tag n)))
23-
(is (= 1 (node/sexpr n)))))
24-
(deftest t-skipping-whitespace-with-a-movement-function
25-
(let [n (->> loc z/down* (z/skip-whitespace z/next*) z/node)]
26-
(is (= :token (node/tag n)))
27-
(is (= 0 (node/sexpr n)))))
28-
(deftest t-prepending=appending-spaces
29-
(are [?left-fn ?right-fn]
30-
(do (let [n (-> loc z/down* z/rightmost* (?left-fn 3))]
31-
(is (= " \n0 1 ;comment" (z/root-string n))))
32-
(let [n (-> loc z/down* z/rightmost* (?right-fn 3))]
33-
(is (= " \n0 1;comment " (z/root-string n)))))
34-
z/prepend-space z/append-space
35-
z/insert-space-left z/insert-space-right))
36-
(deftest t-prepending-appending-linebreaks
37-
(are [?left-fn ?right-fn]
38-
(do (let [n (-> loc z/down* z/rightmost* (?left-fn 3))]
39-
(is (= " \n0 1\n\n\n;comment" (z/root-string n))))
40-
(let [n (-> loc z/down* z/rightmost* (?right-fn 3))]
41-
(is (= " \n0 1;comment\n\n\n" (z/root-string n)))))
42-
z/prepend-newline z/append-newline
43-
z/insert-newline-left z/insert-newline-right)))
6+
(deftest whitespace
7+
(let [space (node/spaces 1)
8+
linebreak (node/newlines 1)
9+
c0 (node/comment-node "comment")
10+
t0 (node/token-node 0)
11+
t1 (node/token-node 1)
12+
loc (z/edn* (node/forms-node [space linebreak t0 space t1 c0]))]
13+
(testing "predicates"
14+
(is (z/whitespace? (-> loc z/down*)))
15+
(is (z/linebreak? (-> loc z/down* z/right*)))
16+
(is (z/whitespace-or-comment? (-> loc z/down* z/rightmost*))))
17+
(testing "skipping whitespace to the right"
18+
(let [n (-> loc z/down* z/skip-whitespace z/node)]
19+
(is (= :token (node/tag n)))
20+
(is (= 0 (node/sexpr n)))))
21+
(testing "skipping whitespace to the left"
22+
(let [n (-> loc z/down* z/rightmost* z/skip-whitespace-left z/node)]
23+
(is (= :token (node/tag n)))
24+
(is (= 1 (node/sexpr n)))))
25+
(testing "skipping whitespace with a movement function"
26+
(let [n (->> loc z/down* (z/skip-whitespace z/next*) z/node)]
27+
(is (= :token (node/tag n)))
28+
(is (= 0 (node/sexpr n)))))
29+
(testing "prepending appending spaces"
30+
(are [?left-fn ?right-fn]
31+
(do (let [n (-> loc z/down* z/rightmost* (?left-fn 3))]
32+
(is (= " \n0 1 ;comment" (z/root-string n))))
33+
(let [n (-> loc z/down* z/rightmost* (?right-fn 3))]
34+
(is (= " \n0 1;comment " (z/root-string n)))))
35+
z/prepend-space z/append-space
36+
z/insert-space-left z/insert-space-right))
37+
(testing "prepending appending linebreaks"
38+
(are [?left-fn ?right-fn]
39+
(do (let [n (-> loc z/down* z/rightmost* (?left-fn 3))]
40+
(is (= " \n0 1\n\n\n;comment" (z/root-string n))))
41+
(let [n (-> loc z/down* z/rightmost* (?right-fn 3))]
42+
(is (= " \n0 1;comment\n\n\n" (z/root-string n)))))
43+
z/prepend-newline z/append-newline
44+
z/insert-newline-left z/insert-newline-right))))

0 commit comments

Comments
 (0)