* add XML edn reading and make *data-readers* closer to Clojure - initial value of *data-readers*: empty map - add XML readers to feature-flagged edn-readers for parsing *input* - add edn-readers to *data-readers* to emulate data_readers.clj handling * add YAML round-trip testing * set root val of *data-readers* at top level
This commit is contained in:
parent
58cbfd987b
commit
407bd74a00
4 changed files with 43 additions and 2 deletions
|
|
@ -26,7 +26,7 @@
|
|||
([sym] (core-dynamic-var sym nil))
|
||||
([sym init-val] (sci/new-dynamic-var sym init-val {:ns clojure-core-ns})))
|
||||
|
||||
(def data-readers (core-dynamic-var '*data-readers*))
|
||||
(def data-readers (core-dynamic-var '*data-readers* {}))
|
||||
(def command-line-args (core-dynamic-var '*command-line-args*))
|
||||
(def warn-on-reflection (core-dynamic-var '*warn-on-reflection* false))
|
||||
(def math-context (core-dynamic-var '*math-context*))
|
||||
|
|
|
|||
|
|
@ -425,7 +425,13 @@ Use bb run --help to show this help output.
|
|||
|
||||
(def edn-readers (cond-> {}
|
||||
features/yaml?
|
||||
(assoc 'ordered/map @(resolve 'flatland.ordered.map/ordered-map))))
|
||||
(assoc 'ordered/map @(resolve 'flatland.ordered.map/ordered-map))
|
||||
features/xml?
|
||||
(assoc 'xml/ns @(resolve 'clojure.data.xml.name/uri-symbol)
|
||||
'xml/element @(resolve 'clojure.data.xml.node/tagged-element))))
|
||||
|
||||
;; also put the edn readers into *data-readers*
|
||||
(sci/alter-var-root core/data-readers into edn-readers)
|
||||
|
||||
(defn edn-seq*
|
||||
[^java.io.BufferedReader rdr]
|
||||
|
|
|
|||
17
test/babashka/xml_test.clj
Normal file
17
test/babashka/xml_test.clj
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(ns babashka.xml-test
|
||||
(:require [babashka.test-utils :as test-utils]
|
||||
[clojure.string :as str]
|
||||
[clojure.test :refer [deftest is testing]]))
|
||||
|
||||
(def simple-xml-str "<a><b>data</b></a>")
|
||||
|
||||
(deftest xml-edn-read-test
|
||||
(let [parsed-edn (test-utils/bb nil (str "(xml/parse-str \"" simple-xml-str "\")"))
|
||||
emitted-xml (test-utils/bb parsed-edn "(xml/emit-str *input*)")]
|
||||
(is (str/includes? emitted-xml simple-xml-str))))
|
||||
|
||||
(def round-trip-prog
|
||||
(str "(xml/emit-str (read-string (pr-str (xml/parse-str \"" simple-xml-str "\"))))"))
|
||||
|
||||
(deftest xml-data-readers-test
|
||||
(is (str/includes? (test-utils/bb nil round-trip-prog) simple-xml-str)))
|
||||
18
test/babashka/yaml_test.clj
Normal file
18
test/babashka/yaml_test.clj
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(ns babashka.yaml-test
|
||||
(:require [babashka.test-utils :as test-utils]
|
||||
[clojure.string :as str]
|
||||
[clojure.test :refer [deftest is testing]]))
|
||||
|
||||
(def simple-yaml-str "topic: [point 1, point 2]")
|
||||
|
||||
(deftest yaml-edn-read-test
|
||||
(let [parsed-edn (test-utils/bb nil (str "(yaml/parse-string \"" simple-yaml-str "\")"))
|
||||
emitted-yaml (test-utils/bb parsed-edn "(yaml/generate-string *input*)")]
|
||||
(is (every? #(str/includes? emitted-yaml %) ["topic:" "point 1" "point 2"]))))
|
||||
|
||||
(def round-trip-prog
|
||||
(str "(yaml/generate-string (read-string (pr-str (yaml/parse-string \"" simple-yaml-str "\"))))"))
|
||||
|
||||
(deftest yaml-data-readers-test
|
||||
(let [emitted-yaml (test-utils/bb nil round-trip-prog)]
|
||||
(is (every? #(str/includes? emitted-yaml %) ["topic:" "point 1" "point 2"]))))
|
||||
Loading…
Reference in a new issue