From 56dcbe53739350d89e166934d316eddbf4fcef44 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Wed, 14 Aug 2024 11:06:37 -0500 Subject: [PATCH] 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 #=. --- src/rewrite_clj/node/quote.cljc | 4 +- src/rewrite_clj/node/reader_macro.cljc | 2 +- test/rewrite_clj/parser_test.cljc | 67 +++++++++++++++----------- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/rewrite_clj/node/quote.cljc b/src/rewrite_clj/node/quote.cljc index 75456dae..b2a6431a 100644 --- a/src/rewrite_clj/node/quote.cljc +++ b/src/rewrite_clj/node/quote.cljc @@ -110,7 +110,7 @@ [children] (if (sequential? children) (->node - :unquote "~" 'unquote + :unquote "~" 'clojure.core/unquote children) (recur [children]))) @@ -135,6 +135,6 @@ [children] (if (sequential? children) (->node - :unquote-splicing "~@" 'unquote-splicing + :unquote-splicing "~@" 'clojure.core/unquote-splicing children) (recur [children]))) diff --git a/src/rewrite_clj/node/reader_macro.cljc b/src/rewrite_clj/node/reader_macro.cljc index 06f34f3d..5a0e944e 100644 --- a/src/rewrite_clj/node/reader_macro.cljc +++ b/src/rewrite_clj/node/reader_macro.cljc @@ -68,7 +68,7 @@ (node-type [_node] :deref) (printable-only?[_node] false) (sexpr* [_node opts] - (list* 'deref (node/sexprs children opts))) + (list* 'clojure.core/deref (node/sexprs children opts))) (length [_node] (inc (node/sum-lengths children))) (string [_node] diff --git a/test/rewrite_clj/parser_test.cljc b/test/rewrite_clj/parser_test.cljc index a0eaffe3..5c3950b4 100644 --- a/test/rewrite_clj/parser_test.cljc +++ b/test/rewrite_clj/parser_test.cljc @@ -1,10 +1,9 @@ (ns ^{:doc "Tests for EDN parser." :author "Yannick Scherer"} rewrite-clj.parser-test - (:refer-clojure :exclude [read-string]) (:require [clojure.string :as string] - [clojure.test :refer [deftest is]] - [clojure.tools.reader.edn :refer [read-string]] + [clojure.test :refer [deftest is testing]] + [clojure.tools.reader :as rdr] [rewrite-clj.node :as node] [rewrite-clj.parser :as p]) #?(:clj (:import [clojure.lang ExceptionInfo] @@ -130,29 +129,42 @@ :default (is (Double/isNaN e))))) (deftest t-parsing-reader-prefixed-data - (doseq [[s t ws sexpr] - [["@sym" :deref [] '(deref sym)] - ["@ sym" :deref [:whitespace] '(deref sym)] - ["'sym" :quote [] '(quote sym)] - ["' sym" :quote [:whitespace] '(quote sym)] - ["`sym" :syntax-quote [] '(quote sym)] - ["` sym" :syntax-quote [:whitespace] '(quote sym)] - ["~sym" :unquote [] '(unquote sym)] - ["~ sym" :unquote [:whitespace] '(unquote sym)] - ["~@sym" :unquote-splicing [] '(unquote-splicing sym)] - ["~@ sym" :unquote-splicing [:whitespace] '(unquote-splicing sym)] - ["#=sym" :eval [] '(eval 'sym)] - ["#= sym" :eval [:whitespace] '(eval 'sym)] - ["#'sym" :var [] '(var sym)] - ["#'\nsym" :var [:newline] '(var sym)]]] - (let [n (p/parse-string s) - children (node/children n) - c (map node/tag children)] - (is (= t (node/tag n))) - (is (= :token (last c))) - (is (= sexpr (node/sexpr n))) - (is (= 'sym (node/sexpr (last children)))) - (is (= ws (vec (butlast c))))))) + (doseq [[ s t ws sexpr ltag lcld] + [["@sym" :deref [] '@sym :token 'sym] + ["@ sym" :deref [:whitespace] '@sym :token 'sym] + ["'sym" :quote [] ''sym :token 'sym] + ["' sym" :quote [:whitespace] ''sym :token 'sym] + ["`sym" :syntax-quote [] ''sym :token 'sym] + ["` sym" :syntax-quote [:whitespace] ''sym :token 'sym] + ["~sym" :unquote [] '~sym :token 'sym] + ["~ sym" :unquote [:whitespace] '~sym :token 'sym] + ["~@sym" :unquote-splicing [] '~@sym :token 'sym] + ["~@ sym" :unquote-splicing [:whitespace] '~@sym :token 'sym] + ["~ @sym" :unquote [:whitespace] '~ @sym :deref '@sym] + ["#=sym" :eval [] '(eval 'sym) :token 'sym] + ["#= sym" :eval [:whitespace] '(eval 'sym) :token 'sym] + ["#'sym" :var [] '#'sym :token 'sym] + ["#'\nsym" :var [:newline] '#'sym :token 'sym]]] + (testing (pr-str s) + (binding [*ns* #?(:cljs 'rewrite-clj.parser-test + :clj (the-ns 'rewrite-clj.parser-test))] + (let [n (p/parse-string s) + children (node/children n) + c (map node/tag children)] + (is (= t (node/tag n)) "tag") + (is (= ltag (last c)) "ltag") + (is (= sexpr (node/sexpr n)) "sexpr") + (is (= s (node/string n)) "string") + ;; ` and #= return different sexpr's than via clojure.core/read-string + (when-not (#{:syntax-quote :eval} t) + (is (= sexpr + #?(:cljs (rdr/read-string s) + :default (binding [rdr/*read-eval* false] (rdr/read-string s))) + #?@(:cljs [] + :default [(binding [*read-eval* false] (read-string s))])) + "read-string")) + (is (= lcld (node/sexpr (last children))) "lcld") + (is (= ws (vec (butlast c))) "ws")))))) (deftest t-eval (let [n (p/parse-string "#=(+ 1 2)")] @@ -223,7 +235,8 @@ fq (frequencies (map node/tag children))] (is (= t (node/tag n))) (is (= (string/trim s) (node/string n))) - (is (= (node/sexpr n) (read-string s))) + (is (= (node/sexpr n) #?(:cljs (rdr/read-string s) + :default (binding [rdr/*read-eval* false] (rdr/read-string s))))) (is (= w (:whitespace fq 0))) (is (= c (:token fq 0))))))