Skip to content

Commit

Permalink
Merge branch 'patch-0.9.2' into develop
Browse files Browse the repository at this point in the history
Conflicts:
	CHANGELOG.md
  • Loading branch information
greglook committed Oct 21, 2015
2 parents d49ea07 + bdb19f4 commit fa480e3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ This page documents the high-level changes in each release of Puget.

## 0.10.0 (...)


## 0.9.2 (2015-10-20)

- Add `printer/unknown-handler` and improve rendering of unknown types.
- Add print handlers for class and function values.
- Line-break tagged literals when the form is a collection.

## 0.9.1 (2015-10-17)

- Improve `chained-lookup` logic to remove nil dispatch entries.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Puget

[![Build Status](https://travis-ci.org/greglook/puget.svg?branch=master)](https://travis-ci.org/greglook/puget)
[![Coverage Status](https://coveralls.io/repos/greglook/puget/badge.png?branch=master)](https://coveralls.io/r/greglook/puget?branch=master)
[![Dependency Status](https://www.versioneye.com/user/projects/53718bfb14c1589a89000144/badge.png?style=flat)](https://www.versioneye.com/clojure/mvxcvi:puget)
[![Dependency Status](https://www.versioneye.com/clojure/mvxcvi:puget/badge.svg)](https://www.versioneye.com/clojure/mvxcvi:puget)
[![API codox](http://b.repl.ca/v1/doc-API-blue.png)](https://greglook.github.io/puget/api/)
[![marginalia docs](http://b.repl.ca/v1/doc-marginalia-blue.png)](https://greglook.github.io/puget/marginalia/uberdoc.html)

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject mvxcvi/puget "0.10.0-SNAPSHOT"
(defproject mvxcvi/puget "0.9.2"
:description "Colorizing canonical Clojure printer for EDN values."
:url "https://github.com/greglook/puget"
:license {:name "Public Domain"
Expand Down
40 changes: 26 additions & 14 deletions src/puget/printer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@
([printer value repr]
(format-unknown printer value (.getName (class value)) repr))
([printer value tag repr]
[:span
(color/document printer :class-delimiter "#<")
(color/document printer :class-name tag)
(color/document printer :class-delimiter "@")
(Integer/toHexString (System/identityHashCode value))
" "
repr
(color/document printer :class-delimiter ">")]))
(let [sys-id (Integer/toHexString (System/identityHashCode value))]
[:span
(color/document printer :class-delimiter "#<")
(color/document printer :class-name tag)
(color/document printer :class-delimiter "@")
sys-id
(when (not= repr (str tag "@" sys-id))
(list " " repr))
(color/document printer :class-delimiter ">")])))


(defn format-doc*
Expand Down Expand Up @@ -210,9 +211,15 @@
(pr-str value))


(defn unknown-handler
"Print handler which renders the value using the printer's unknown type logic."
[printer value]
(fv/visit-unknown printer value))


(defn tagged-handler
"Generates a handler function which renders a tagged-literal with the given
tag and a value produced by calling the function."
"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]
(fn handler
[printer value]
Expand All @@ -222,7 +229,12 @@
(def java-handlers
"Map of print handlers for Java types. This supports syntax for regular
expressions, dates, UUIDs, and futures."
{java.util.regex.Pattern
{java.lang.Class
(fn class-handler
[printer value]
(format-unknown printer value "Class" (.getName ^Class value)))

java.util.regex.Pattern
(fn pattern-handler
[printer value]
[:span
Expand All @@ -235,7 +247,7 @@
(let [doc (if (future-done? value)
(format-doc printer @value)
(color/document printer :nil "pending"))]
(format-unknown printer value "Future" doc)))
(format-unknown printer value "Future" doc)))

java.util.Date
(tagged-handler 'inst
Expand Down Expand Up @@ -540,9 +552,9 @@
(visit-tagged
[this value]
(let [{:keys [tag form]} value]
[:span
[:group
(color/document this :tag (str "#" (:tag value)))
" "
(if (coll? form) :line " ")
(format-doc this (:form value))]))

(visit-unknown
Expand Down
19 changes: 15 additions & 4 deletions test/puget/printer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@
(deftest formatting-records
(testing "Records"
(let [r (->TestRecord \x \y)]
(is (= "#puget.printer_test.TestRecord {:bar \\y, :foo \\x}\n"
(with-out-str (pprint r)))))))
(is (= "#puget.printer_test.TestRecord\n{:bar \\y, :foo \\x}"
(pprint-str r {:width 30})))
(is (= "#puget.printer_test.TestRecord {:bar \\y, :foo \\x}"
(pprint-str r {:width 200}))))))


(deftype APending [is-realized]
Expand Down Expand Up @@ -104,6 +106,9 @@
(pprint-str v)))
(is (thrown? IllegalArgumentException
(render-str canonical v)))))
(testing "functions"
(is (re-seq #"#<Fn@[0-9a-z]+ puget\.printer/pretty_printer>"
(pprint-str pretty-printer))))
(testing "atom"
(let [v (atom :foo)]
(should-fail-when-strict v)
Expand All @@ -123,12 +128,12 @@
(testing "custom IPending, realized"
(let [v (->APending true)]
(should-fail-when-strict v)
(is (re-seq #"#<puget.printer_test.APending@[0-9a-f]+ 1"
(is (re-seq #"#<puget\.printer_test\.APending@[0-9a-f]+ 1"
(pprint-str v)))))
(testing "custom IPending, not realized"
(let [v (->APending false)]
(should-fail-when-strict v)
(is (re-seq #"#<puget.printer_test.APending@[0-9a-f]+ pending"
(is (re-seq #"#<puget\.printer_test\.APending@[0-9a-f]+ pending"
(pprint-str v))))))


Expand Down Expand Up @@ -162,6 +167,12 @@

(deftest handled-types
(is (= "\"foo\"" (pr-handler {} "foo")))
(is (= "{{ complex value print }}"
(pprint-str (ComplexValue.)
{:print-handlers {ComplexValue unknown-handler}
:print-fallback :print})))
(is (re-seq #"#<Class@[0-9a-f]+ java\.util\.Date>"
(pprint-str java.util.Date)))
(is (= "#inst \"2015-10-12T05:23:08.000-00:00\""
(render-str (canonical-printer java-handlers)
(java.util.Date. 1444627388000)))))
Expand Down

0 comments on commit fa480e3

Please sign in to comment.