Skip to content

Commit 4f5e6c6

Browse files
authored
Use *default-data-reader-fn* when calling core read functions #924 (#1001)
This is an amendment for #979. This was missed when merging back #986 after it was separated out.
1 parent 5c8304b commit 4f5e6c6

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/basilisp/core.lpy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4446,7 +4446,8 @@
44464446
(:eof opts)
44474447
(= (:eof opts) :eofthrow)
44484448
(:features opts)
4449-
(not= :preserve (:read-cond opts)))))
4449+
(not= :preserve (:read-cond opts))
4450+
*default-data-reader-fn*)))
44504451

44514452
(defn read-string
44524453
"Read a string of Basilisp code.

tests/basilisp/test_data_readers.lpy

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@
3838
path-files-fixture
3939
reset-data-readers-fixture)
4040

41+
(deftest data-readers-test
42+
(alter-var-root #'*data-readers* assoc
43+
'test/test vector
44+
'test/test2 list)
45+
46+
(testing "use var root value"
47+
(is (= '[x] (read-string "#test/test x"))))
48+
49+
(testing "use bound value"
50+
(binding [*data-readers* {'test/test (partial vector :pass)}]
51+
(is (= '[:pass x] (read-string "#test/test x")))
52+
(is (thrown? reader/SyntaxError
53+
#"No data reader found for tag #test/test2"
54+
(read-string "#test/test2 x")))))
55+
56+
(testing "fallback to defaults"
57+
(is (= #py {} (read-string "#py {}"))))
58+
59+
(testing "override defaults"
60+
(binding [*data-readers* {'py identity}]
61+
(is (= {} (read-string "#py {}"))))))
62+
4163
(def custom-data-reader list)
4264

4365
(deftest load-data-readers-from-path-test
@@ -161,3 +183,13 @@
161183
(is (thrown-with-msg? basilisp.lang.exception/ExceptionInfo
162184
#"Conflicting data-reader mapping"
163185
(#'basilisp.core/load-data-readers)))))
186+
187+
(deftest default-data-readers-fn-test
188+
(testing "raise syntax exception by default"
189+
(is (thrown-with-msg? reader/SyntaxError
190+
#"No data reader found for tag #default"
191+
(read-string "#default nil"))))
192+
193+
(testing "use *default-data-reader-fn* binding"
194+
(binding [*default-data-reader-fn* vector]
195+
(is (= ['default nil] (read-string "#default nil"))))))

0 commit comments

Comments
 (0)