|
2 | 2 | :author "Yannick Scherer"}
|
3 | 3 | rewrite-clj.parser-test
|
4 | 4 | (:refer-clojure :exclude [read-string])
|
5 |
| - (:require [clojure.string :as string] |
6 |
| - [clojure.test :refer [deftest is]] |
| 5 | + (:require [clojure.core :as cc] |
| 6 | + [clojure.string :as string] |
| 7 | + [clojure.test :refer [deftest is testing]] |
7 | 8 | [clojure.tools.reader.edn :refer [read-string]]
|
8 | 9 | [rewrite-clj.node :as node]
|
9 | 10 | [rewrite-clj.parser :as p])
|
10 | 11 | #?(:clj (:import [clojure.lang ExceptionInfo]
|
11 | 12 | [java.io File])))
|
12 | 13 |
|
| 14 | +(def this-nsym (ns-name *ns*)) |
| 15 | + |
13 | 16 | (deftest t-parsing-the-first-few-whitespaces
|
14 | 17 | (doseq [[ws parsed]
|
15 | 18 | [[" " " "]
|
|
130 | 133 | :default (is (Double/isNaN e)))))
|
131 | 134 |
|
132 | 135 | (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))))))) |
| 136 | + (doseq [[ s t ws sexpr ltag lcld] |
| 137 | + [["@sym" :deref [] '@sym :token 'sym] |
| 138 | + ["@ sym" :deref [:whitespace] '@sym :token 'sym] |
| 139 | + ["'sym" :quote [] ''sym :token 'sym] |
| 140 | + ["' sym" :quote [:whitespace] ''sym :token 'sym] |
| 141 | + ["`sym" :syntax-quote [] ''sym :token 'sym] |
| 142 | + ["` sym" :syntax-quote [:whitespace] ''sym :token 'sym] |
| 143 | + ["~sym" :unquote [] '~sym :token 'sym] |
| 144 | + ["~ sym" :unquote [:whitespace] '~sym :token 'sym] |
| 145 | + ["~@sym" :unquote-splicing [] '~@sym :token 'sym] |
| 146 | + ["~@ sym" :unquote-splicing [:whitespace] '~@sym :token 'sym] |
| 147 | + ["~ @sym" :unquote [:whitespace] '~ @sym :deref '@sym] |
| 148 | + ["#=sym" :eval [] '(eval 'sym) :token 'sym] |
| 149 | + ["#= sym" :eval [:whitespace] '(eval 'sym) :token 'sym] |
| 150 | + ["#'sym" :var [] '#'sym :token 'sym] |
| 151 | + ["#'\nsym" :var [:newline] '#'sym :token 'sym]]] |
| 152 | + (testing (pr-str s) |
| 153 | + (binding [*ns* (the-ns this-nsym)] |
| 154 | + (let [n (p/parse-string s) |
| 155 | + children (node/children n) |
| 156 | + c (map node/tag children)] |
| 157 | + (is (= t (node/tag n)) "tag") |
| 158 | + (is (= ltag (last c)) "ltag") |
| 159 | + (is (= sexpr (node/sexpr n)) "sexpr") |
| 160 | + (is (= s (node/string n)) "string") |
| 161 | + ;; ` and #= return different sexpr's than via clojure.core/read-string |
| 162 | + (when-not (#{:syntax-quote :eval} t) |
| 163 | + (is (= sexpr (binding [cc/*read-eval* false] (cc/read-string s))) |
| 164 | + "read-string")) |
| 165 | + (is (= lcld (node/sexpr (last children))) "lcld") |
| 166 | + (is (= ws (vec (butlast c))) "ws")))))) |
156 | 167 |
|
157 | 168 | (deftest t-eval
|
158 | 169 | (let [n (p/parse-string "#=(+ 1 2)")]
|
|
0 commit comments