|
1 | 1 | (ns ^{:doc "Tests for EDN parser."
|
2 | 2 | :author "Yannick Scherer"}
|
3 | 3 | rewrite-clj.parser-test
|
4 |
| - (:refer-clojure :exclude [read-string]) |
5 | 4 | (: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] |
8 | 7 | [rewrite-clj.node :as node]
|
9 | 8 | [rewrite-clj.parser :as p])
|
10 | 9 | #?(:clj (:import [clojure.lang ExceptionInfo]
|
|
130 | 129 | :default (is (Double/isNaN e)))))
|
131 | 130 |
|
132 | 131 | (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 | + (let [n (p/parse-string s) |
| 150 | + children (node/children n) |
| 151 | + c (map node/tag children)] |
| 152 | + (is (= t (node/tag n)) "tag") |
| 153 | + (is (= ltag (last c)) "ltag") |
| 154 | + (is (= sexpr (node/sexpr n)) "sexpr") |
| 155 | + (is (= s (node/string n)) "string") |
| 156 | + ;; ` and #= return different sexpr's than via clojure.core/read-string |
| 157 | + (when-not (#{:syntax-quote :eval} t) |
| 158 | + (is (= sexpr |
| 159 | + #?(:cljs (rdr/read-string s) |
| 160 | + :default (binding [rdr/*read-eval* false] (rdr/read-string s))) |
| 161 | + #?@(:cljs [] |
| 162 | + :default [(binding [*read-eval* false] (read-string s))])) |
| 163 | + "read-string")) |
| 164 | + (is (= lcld (node/sexpr (last children))) "lcld") |
| 165 | + (is (= ws (vec (butlast c))) "ws"))))) |
156 | 166 |
|
157 | 167 | (deftest t-eval
|
158 | 168 | (let [n (p/parse-string "#=(+ 1 2)")]
|
|
223 | 233 | fq (frequencies (map node/tag children))]
|
224 | 234 | (is (= t (node/tag n)))
|
225 | 235 | (is (= (string/trim s) (node/string n)))
|
226 |
| - (is (= (node/sexpr n) (read-string s))) |
| 236 | + (is (= (node/sexpr n) #?(:cljs (rdr/read-string s) |
| 237 | + :default (binding [rdr/*read-eval* false] (rdr/read-string s))))) |
227 | 238 | (is (= w (:whitespace fq 0)))
|
228 | 239 | (is (= c (:token fq 0))))))
|
229 | 240 |
|
|
0 commit comments