Skip to content

Commit

Permalink
Add checks to tagged-handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
greglook committed Feb 11, 2016
1 parent c26697b commit 2b47847
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

...
### Added
- Add checks to `printer/tagged-handler` to ensure argumenst are a symbol and
function, respectively.

## [1.0.0] - 2015-10-29

Expand Down
8 changes: 8 additions & 0 deletions src/puget/printer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@
"Generates a print handler function which renders a tagged-literal with the
given tag and a value produced by calling the function."
[tag value-fn]
(when-not (symbol? tag)
(throw (ex-info (str "Cannot create tagged handler with non-symbol tag "
(pr-str tag))
{:tag tag, :value-fn value-fn})))
(when-not (ifn? value-fn)
(throw (ex-info (str "Cannot create tagged handler for " tag
" with non-function value transform")
{:tag tag, :value-fn value-fn})))
(fn handler
[printer value]
(format-doc printer (tagged-literal tag (value-fn value)))))
Expand Down
4 changes: 4 additions & 0 deletions test/puget/printer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@


(deftest canonical-extensions
(testing "tagged-handler construction"
(is (thrown? clojure.lang.ExceptionInfo (tagged-handler "foo" str)))
(is (thrown? clojure.lang.ExceptionInfo (tagged-handler 'foo "abcd")))
(is (ifn? (tagged-handler 'foo str))))
(let [handlers {java.util.UUID (tagged-handler 'uuid str)}
printer (canonical-printer handlers)
uuid-str "31f7dd72-c7f7-4a15-a98b-0f9248d3aaa6"]
Expand Down

0 comments on commit 2b47847

Please sign in to comment.