Skip to content

Commit 56dcbe5

Browse files
committed
Qualify @ ~ ~@ sexpr's under clojure.core
z/sexpr should return the same value as clojure.core/read-string as per the documentation. The tests were moved from clojure.tools.reader.edn/read-string to clojure.tools.reader/read-string because the latter implements Clojure's reader. After this commit, the only special forms that return different values are ` and #=.
1 parent bf37e3d commit 56dcbe5

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

src/rewrite_clj/node/quote.cljc

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
[children]
111111
(if (sequential? children)
112112
(->node
113-
:unquote "~" 'unquote
113+
:unquote "~" 'clojure.core/unquote
114114
children)
115115
(recur [children])))
116116

@@ -135,6 +135,6 @@
135135
[children]
136136
(if (sequential? children)
137137
(->node
138-
:unquote-splicing "~@" 'unquote-splicing
138+
:unquote-splicing "~@" 'clojure.core/unquote-splicing
139139
children)
140140
(recur [children])))

src/rewrite_clj/node/reader_macro.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
(node-type [_node] :deref)
6969
(printable-only?[_node] false)
7070
(sexpr* [_node opts]
71-
(list* 'deref (node/sexprs children opts)))
71+
(list* 'clojure.core/deref (node/sexprs children opts)))
7272
(length [_node]
7373
(inc (node/sum-lengths children)))
7474
(string [_node]

test/rewrite_clj/parser_test.cljc

+40-27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
(ns ^{:doc "Tests for EDN parser."
22
:author "Yannick Scherer"}
33
rewrite-clj.parser-test
4-
(:refer-clojure :exclude [read-string])
54
(:require [clojure.string :as string]
6-
[clojure.test :refer [deftest is]]
7-
[clojure.tools.reader.edn :refer [read-string]]
5+
[clojure.test :refer [deftest is testing]]
6+
[clojure.tools.reader :as rdr]
87
[rewrite-clj.node :as node]
98
[rewrite-clj.parser :as p])
109
#?(:clj (:import [clojure.lang ExceptionInfo]
@@ -130,29 +129,42 @@
130129
:default (is (Double/isNaN e)))))
131130

132131
(deftest t-parsing-reader-prefixed-data
133-
(doseq [[s t ws sexpr]
134-
[["@sym" :deref [] '(deref sym)]
135-
["@ sym" :deref [:whitespace] '(deref sym)]
136-
["'sym" :quote [] '(quote sym)]
137-
["' sym" :quote [:whitespace] '(quote sym)]
138-
["`sym" :syntax-quote [] '(quote sym)]
139-
["` sym" :syntax-quote [:whitespace] '(quote sym)]
140-
["~sym" :unquote [] '(unquote sym)]
141-
["~ sym" :unquote [:whitespace] '(unquote sym)]
142-
["~@sym" :unquote-splicing [] '(unquote-splicing sym)]
143-
["~@ sym" :unquote-splicing [:whitespace] '(unquote-splicing sym)]
144-
["#=sym" :eval [] '(eval 'sym)]
145-
["#= sym" :eval [:whitespace] '(eval 'sym)]
146-
["#'sym" :var [] '(var sym)]
147-
["#'\nsym" :var [:newline] '(var sym)]]]
148-
(let [n (p/parse-string s)
149-
children (node/children n)
150-
c (map node/tag children)]
151-
(is (= t (node/tag n)))
152-
(is (= :token (last c)))
153-
(is (= sexpr (node/sexpr n)))
154-
(is (= 'sym (node/sexpr (last children))))
155-
(is (= ws (vec (butlast c)))))))
132+
(doseq [[ s t ws sexpr ltag lcld]
133+
[["@sym" :deref [] '@sym :token 'sym]
134+
["@ sym" :deref [:whitespace] '@sym :token 'sym]
135+
["'sym" :quote [] ''sym :token 'sym]
136+
["' sym" :quote [:whitespace] ''sym :token 'sym]
137+
["`sym" :syntax-quote [] ''sym :token 'sym]
138+
["` sym" :syntax-quote [:whitespace] ''sym :token 'sym]
139+
["~sym" :unquote [] '~sym :token 'sym]
140+
["~ sym" :unquote [:whitespace] '~sym :token 'sym]
141+
["~@sym" :unquote-splicing [] '~@sym :token 'sym]
142+
["~@ sym" :unquote-splicing [:whitespace] '~@sym :token 'sym]
143+
["~ @sym" :unquote [:whitespace] '~ @sym :deref '@sym]
144+
["#=sym" :eval [] '(eval 'sym) :token 'sym]
145+
["#= sym" :eval [:whitespace] '(eval 'sym) :token 'sym]
146+
["#'sym" :var [] '#'sym :token 'sym]
147+
["#'\nsym" :var [:newline] '#'sym :token 'sym]]]
148+
(testing (pr-str s)
149+
(binding [*ns* #?(:cljs 'rewrite-clj.parser-test
150+
:clj (the-ns 'rewrite-clj.parser-test))]
151+
(let [n (p/parse-string s)
152+
children (node/children n)
153+
c (map node/tag children)]
154+
(is (= t (node/tag n)) "tag")
155+
(is (= ltag (last c)) "ltag")
156+
(is (= sexpr (node/sexpr n)) "sexpr")
157+
(is (= s (node/string n)) "string")
158+
;; ` and #= return different sexpr's than via clojure.core/read-string
159+
(when-not (#{:syntax-quote :eval} t)
160+
(is (= sexpr
161+
#?(:cljs (rdr/read-string s)
162+
:default (binding [rdr/*read-eval* false] (rdr/read-string s)))
163+
#?@(:cljs []
164+
:default [(binding [*read-eval* false] (read-string s))]))
165+
"read-string"))
166+
(is (= lcld (node/sexpr (last children))) "lcld")
167+
(is (= ws (vec (butlast c))) "ws"))))))
156168

157169
(deftest t-eval
158170
(let [n (p/parse-string "#=(+ 1 2)")]
@@ -223,7 +235,8 @@
223235
fq (frequencies (map node/tag children))]
224236
(is (= t (node/tag n)))
225237
(is (= (string/trim s) (node/string n)))
226-
(is (= (node/sexpr n) (read-string s)))
238+
(is (= (node/sexpr n) #?(:cljs (rdr/read-string s)
239+
:default (binding [rdr/*read-eval* false] (rdr/read-string s)))))
227240
(is (= w (:whitespace fq 0)))
228241
(is (= c (:token fq 0))))))
229242

0 commit comments

Comments
 (0)