Skip to content

Commit 5848b0a

Browse files
author
Yannick Scherer
committed
Testing multi-line Strings. Added prefix/suffix utility functions.
1 parent 4ac3fbe commit 5848b0a

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

src/rewrite_clj/zip/utils.clj

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(ns ^{ :doc "Utility operations for nodes."
2+
:author "Yannick Scherer" }
3+
rewrite-clj.zip.utils
4+
(:require [fast-zip.core :as z]))
5+
6+
;; ## Prefix
7+
8+
(defmulti prefix
9+
"Prefix the value of a zipper node with the given string. This supports multi-lined strings."
10+
(fn [zloc p]
11+
(when zloc
12+
(first (z/node zloc))))
13+
:default nil)
14+
15+
(defmethod prefix nil
16+
[zloc prefix]
17+
(throw (Exception. (str "Cannot prefix value of type: " (first (z/node zloc))))))
18+
19+
(defmethod prefix :token
20+
[zloc prefix]
21+
(let [v (second (z/node zloc))
22+
v1 (cond (string? v) (str prefix v)
23+
(symbol? v) (symbol (namespace v) (str prefix (name v)))
24+
(keyword? v) (keyword (namespace v) (str prefix (name v)))
25+
:else (throw (Exception. (str "Cannot prefix token: " v))))]
26+
(z/replace zloc [:token v1])))
27+
28+
(defmethod prefix :multi-line
29+
[zloc prefix]
30+
(let [[v & rst] (rest (z/node zloc))]
31+
(z/replace zloc (vec (list* :multi-line (str prefix v) rst)))))
32+
33+
;; ## Suffix
34+
35+
(defmulti suffix
36+
"Suffix the value of a zipper node with the given string. This supports multi-lined strings."
37+
(fn [zloc p]
38+
(when zloc
39+
(first (z/node zloc))))
40+
:default nil)
41+
42+
(defmethod suffix nil
43+
[zloc suffix]
44+
(throw (Exception. (str "Cannot suffix value of type: " (first (z/node zloc))))))
45+
46+
(defmethod suffix :token
47+
[zloc suffix]
48+
(let [v (second (z/node zloc))
49+
v1 (cond (string? v) (str v suffix)
50+
(symbol? v) (symbol (namespace v) (str (name v) suffix))
51+
(keyword? v) (keyword (namespace v) (str (name v) suffix))
52+
:else (throw (Exception. (str "Cannot suffix token: " v))))]
53+
(z/replace zloc [:token v1])))
54+
55+
(defmethod suffix :multi-line
56+
[zloc suffix]
57+
(let [[v & rst] (rest (z/node zloc))]
58+
(z/replace zloc (vec (list* :multi-line (str v suffix) rst)))))

test/rewrite_clj/convert_test.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
[:token 0] 0
1212
[:token 'id] 'id
1313
[:token :k] :k
14+
[:token "s"] "s"
15+
[:token "abc\ndef"] "abc\ndef"
16+
[:multi-line "abc" "def"] "abc\ndef"
1417
[:list [:token :k] [:token 5]] '(:k 5)
1518
[:vector [:token :k] [:token 5]] [:k 5]
1619
[:set [:token :k] [:token 5]] #{:k 5}
@@ -54,6 +57,7 @@
5457
0 [:token 0]
5558
:k [:token :k]
5659
'x [:token 'x]
60+
"s" [:token "s"]
5761
'(1 2) [:list [:token 1] [:whitespace " "] [:token 2]]
5862
[1 2] [:vector [:token 1] [:whitespace " "] [:token 2]]
5963
#{1 2} [:set [:token 1] [:whitespace " "] [:token 2]]

test/rewrite_clj/parser_test.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
"#\"regex\\.\"" "regex\\."
4040
"#\"[reg|k].x\"" "[reg|k].x")
4141

42+
(fact "about parsing strings"
43+
(p/parse-string "\"123\"") => [:token "123"]
44+
(p/parse-string "\"123\\n456\"") => [:token "123\n456"]
45+
(p/parse-string "\"123\n456\"") => [:multi-line "123" "456"])
46+
4247
(tabular
4348
(fact "about parsing seqs"
4449
(let [[k & rst] (p/parse-string ?s)]

test/rewrite_clj/transform_test.clj

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
:author "Yannick Scherer" }
33
rewrite-clj.transform-test
44
(:require [midje.sweet :refer :all]
5-
[rewrite-clj.zip :as z]))
5+
[rewrite-clj.zip :as z]
6+
[rewrite-clj.zip.utils :as zu]))
67

78
(def data-string
89
";; This is a Project File.
910
(defproject my-project \"0.1.0-SNAPSHOT\"
10-
:description \"A project.\"
11+
:description \"A project.
12+
Multiline!\"
1113
:dependencies [[a \"0.1.0\"]
1214
[b \"1.2.3\"]]
1315
:repositories { \"private\" \"http://private.com/repo\" })")
@@ -17,33 +19,55 @@
1719
(fact "about a simple transformation"
1820
(-> data
1921
(z/find-value z/next 'defproject)
20-
(z/find-value :description)
21-
z/right (z/edit #(str "DESCR: " %))
22+
z/right z/right
23+
(z/edit #(str % "-1"))
2224
z/->root-string)
2325
=>
2426
";; This is a Project File.
25-
(defproject my-project \"0.1.0-SNAPSHOT\"
26-
:description \"DESCR: A project.\"
27+
(defproject my-project \"0.1.0-SNAPSHOT-1\"
28+
:description \"A project.
29+
Multiline!\"
2730
:dependencies [[a \"0.1.0\"]
2831
[b \"1.2.3\"]]
2932
:repositories { \"private\" \"http://private.com/repo\" })")
3033

31-
(fact "about a seq transformation"
34+
(fact "about a seq transformation (prefix)"
3235
(-> data
3336
(z/find-value z/next 'defproject)
3437
(z/find-value :dependencies) z/right
3538
(->>
3639
(z/map
3740
(fn [loc]
3841
(-> loc z/down
39-
(z/edit (comp symbol #(str "prefix-" %)) )
42+
(zu/prefix "prefix-")
4043
z/up))))
4144
z/->root-string)
4245
=>
4346
";; This is a Project File.
4447
(defproject my-project \"0.1.0-SNAPSHOT\"
45-
:description \"A project.\"
48+
:description \"A project.
49+
Multiline!\"
4650
:dependencies [[prefix-a \"0.1.0\"]
4751
[prefix-b \"1.2.3\"]]
52+
:repositories { \"private\" \"http://private.com/repo\" })")
53+
54+
(fact "about a seq transformation (suffix)"
55+
(-> data
56+
(z/find-value z/next 'defproject)
57+
(z/find-value :dependencies) z/right
58+
(->>
59+
(z/map
60+
(fn [loc]
61+
(-> loc z/down
62+
(zu/suffix "-suffix")
63+
z/up))))
64+
z/->root-string)
65+
=>
66+
";; This is a Project File.
67+
(defproject my-project \"0.1.0-SNAPSHOT\"
68+
:description \"A project.
69+
Multiline!\"
70+
:dependencies [[a-suffix \"0.1.0\"]
71+
[b-suffix \"1.2.3\"]]
4872
:repositories { \"private\" \"http://private.com/repo\" })"
4973
)

0 commit comments

Comments
 (0)