Skip to content

Use *default-data-reader-fn* when calling core read functions #924 #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -4446,7 +4446,8 @@
(:eof opts)
(= (:eof opts) :eofthrow)
(:features opts)
(not= :preserve (:read-cond opts)))))
(not= :preserve (:read-cond opts))
*default-data-reader-fn*)))

(defn read-string
"Read a string of Basilisp code.
Expand Down
32 changes: 32 additions & 0 deletions tests/basilisp/test_data_readers.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@
path-files-fixture
reset-data-readers-fixture)

(deftest data-readers-test
(alter-var-root #'*data-readers* assoc
'test/test vector
'test/test2 list)

(testing "use var root value"
(is (= '[x] (read-string "#test/test x"))))

(testing "use bound value"
(binding [*data-readers* {'test/test (partial vector :pass)}]
(is (= '[:pass x] (read-string "#test/test x")))
(is (thrown? reader/SyntaxError
#"No data reader found for tag #test/test2"
(read-string "#test/test2 x")))))

(testing "fallback to defaults"
(is (= #py {} (read-string "#py {}"))))

(testing "override defaults"
(binding [*data-readers* {'py identity}]
(is (= {} (read-string "#py {}"))))))

(def custom-data-reader list)

(deftest load-data-readers-from-path-test
Expand Down Expand Up @@ -161,3 +183,13 @@
(is (thrown-with-msg? basilisp.lang.exception/ExceptionInfo
#"Conflicting data-reader mapping"
(#'basilisp.core/load-data-readers)))))

(deftest default-data-readers-fn-test
(testing "raise syntax exception by default"
(is (thrown-with-msg? reader/SyntaxError
#"No data reader found for tag #default"
(read-string "#default nil"))))

(testing "use *default-data-reader-fn* binding"
(binding [*default-data-reader-fn* vector]
(is (= ['default nil] (read-string "#default nil"))))))
Loading