Add tools reader tests
This commit is contained in:
parent
2ee7a0d12e
commit
0e1694f586
3 changed files with 26 additions and 3 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
(ns babashka.impl.clojure.tools.reader
|
(ns babashka.impl.clojure.tools.reader
|
||||||
(:refer-clojure :exclude [read])
|
(:refer-clojure :exclude [read read-string])
|
||||||
(:require
|
(:require
|
||||||
|
[clojure.tools.reader.reader-types :as rt]
|
||||||
[edamame.core :as e]
|
[edamame.core :as e]
|
||||||
[sci.core :as sci]
|
[sci.core :as sci]
|
||||||
[sci.ctx-store :as ctx]
|
[sci.ctx-store :as ctx]
|
||||||
|
|
@ -52,7 +53,13 @@
|
||||||
:readers (fn [sym]
|
:readers (fn [sym]
|
||||||
(resolve-tag sym))
|
(resolve-tag sym))
|
||||||
:auto-resolve (fn [alias]
|
:auto-resolve (fn [alias]
|
||||||
(@alias-map alias)))
|
(if (= :current alias)
|
||||||
|
(symbol (str @sci/ns))
|
||||||
|
(or (when-let [alias-map @alias-map]
|
||||||
|
(@alias-map alias))
|
||||||
|
(sci/eval-form (ctx/get-ctx)
|
||||||
|
(list 'get '(ns-aliases *ns*)
|
||||||
|
(list 'quote alias)))))))
|
||||||
v (e/parse-next reader opts)]
|
v (e/parse-next reader opts)]
|
||||||
(if (identical? ::e/eof v)
|
(if (identical? ::e/eof v)
|
||||||
(if (identical? :eofthrow eof)
|
(if (identical? :eofthrow eof)
|
||||||
|
|
@ -67,11 +74,18 @@
|
||||||
sentinel)
|
sentinel)
|
||||||
v))))
|
v))))
|
||||||
|
|
||||||
|
(defn read-string
|
||||||
|
([s] (read-string nil s))
|
||||||
|
([opts s]
|
||||||
|
(when (and s (not (identical? s "")))
|
||||||
|
(read opts (rt/string-push-back-reader s)))))
|
||||||
|
|
||||||
(defn resolve-symbol [sym]
|
(defn resolve-symbol [sym]
|
||||||
(p/fully-qualify (ctx/get-ctx) sym))
|
(p/fully-qualify (ctx/get-ctx) sym))
|
||||||
|
|
||||||
(def reader-namespace
|
(def reader-namespace
|
||||||
{'read (sci/copy-var read rns)
|
{'read (sci/copy-var read rns)
|
||||||
|
'read-string (sci/copy-var read-string rns)
|
||||||
'resolve-symbol (sci/copy-var resolve-symbol rns)
|
'resolve-symbol (sci/copy-var resolve-symbol rns)
|
||||||
'*default-data-reader-fn* default-data-reader-fn
|
'*default-data-reader-fn* default-data-reader-fn
|
||||||
'*alias-map* alias-map})
|
'*alias-map* alias-map})
|
||||||
|
|
|
||||||
|
|
@ -14,4 +14,5 @@
|
||||||
'read-char (sci/copy-var rt/read-char rtns)
|
'read-char (sci/copy-var rt/read-char rtns)
|
||||||
'unread (sci/copy-var rt/unread rtns)
|
'unread (sci/copy-var rt/unread rtns)
|
||||||
'source-logging-push-back-reader (sci/copy-var rt/source-logging-push-back-reader rtns)
|
'source-logging-push-back-reader (sci/copy-var rt/source-logging-push-back-reader rtns)
|
||||||
'source-logging-reader? (sci/copy-var rt/source-logging-reader? rtns)})
|
'source-logging-reader? (sci/copy-var rt/source-logging-reader? rtns)
|
||||||
|
'string-push-back-reader (sci/copy-var rt/string-push-back-reader rtns)})
|
||||||
|
|
|
||||||
|
|
@ -886,6 +886,14 @@ true")))
|
||||||
(deftest get-watches-test
|
(deftest get-watches-test
|
||||||
(is (true? (bb nil "(map? (.getWatches (doto (atom nil) (add-watch :foo (fn [k r o n])))))"))))
|
(is (true? (bb nil "(map? (.getWatches (doto (atom nil) (add-watch :foo (fn [k r o n])))))"))))
|
||||||
|
|
||||||
|
(deftest tools-reader-test
|
||||||
|
(is (= :user/foo (bb nil "(require '[clojure.tools.reader :as r]) (r/read-string \"::foo\")")))
|
||||||
|
(is (= :clojure.tools.reader/foo (bb nil "(require '[clojure.tools.reader :as r]) (r/read-string \"::r/foo\")")))
|
||||||
|
(is (= [1 2 3] (bb nil "
|
||||||
|
(require '[clojure.tools.reader :as r])
|
||||||
|
(binding [r/*default-data-reader-fn* (fn [sym] (fn [val] [1 2 3]))]
|
||||||
|
(r/read-string \"#dude []\"))"))))
|
||||||
|
|
||||||
;;;; Scratch
|
;;;; Scratch
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue