Merge printing records

This commit is contained in:
Miloslav Nenadal 2016-02-21 17:37:53 +01:00
parent d8d72e3e3b
commit 81b3188ce7
3 changed files with 35 additions and 37 deletions

View file

@ -1,5 +1,6 @@
(ns pjstadig.humane-test-output
(:use [clojure.test])
(:use [clojure.test]
[pjstadig.util])
(:require [clojure.data :as data]
[clojure.pprint :as pp]))
@ -55,22 +56,7 @@
(print-expected actual))))))
;; this code is just yanked from clojure.pprint
(defmethod pp/simple-dispatch clojure.lang.IRecord [arec]
(pp/pprint-logical-block
:prefix (str "#" (.getName (class arec)) "{") :suffix "}"
(pp/print-length-loop
[aseq (seq arec)]
(when aseq
(pp/pprint-logical-block
(pp/write-out (ffirst aseq))
(.write ^java.io.Writer *out* " ")
(pp/pprint-newline :linear)
;; [pjs] this is kind of ugly, but it is a private var :(
(.set #'pp/*current-length* 0) ; always print both parts of the [k v] pair
(pp/write-out (fnext (first aseq))))
(when (next aseq)
(.write ^java.io.Writer *out* ", ")
(pp/pprint-newline :linear)
(recur (next aseq)))))))
(pprint-record arec))
(prefer-method pp/simple-dispatch
clojure.lang.IRecord
clojure.lang.IPersistentMap))))

29
src/pjstadig/util.cljc Normal file
View file

@ -0,0 +1,29 @@
(ns pjstadig.util
(:require #?(:clj [clojure.pprint :as pp]
:cljs [cljs.pprint :as pp :include-macros true])))
(defn- print-seq [aseq]
(pp/pprint-logical-block
(pp/write-out (ffirst aseq))
(print " ")
(pp/pprint-newline :linear)
;; [pjs] this is kind of ugly, but it is a private var :(
;; always print both parts of the [k v] pair
#?(:clj (.set #'pp/*current-length* 0)
:cljs (set! pp/*current-length* 0))
(pp/write-out (fnext (first aseq)))))
(defn pprint-record [arec]
(pp/pprint-logical-block
#?@(:clj [:prefix (str "#" (.getName (class arec)) "{") :suffix "}"]
:cljs [:prefix (re-find #".*?\{" (with-out-str (print arec))) :suffix "}"])
(pp/print-length-loop
[aseq (seq arec)]
(when aseq
(print-seq aseq)
(when (next aseq)
(print ", ")
(pp/pprint-newline :linear)
(recur (next aseq)))))))

View file

@ -6,34 +6,17 @@
[pjstadig.humane-test-output.records-test]
[clojure.data :refer [diff]]
[cljs.pprint :as pp]
[pjstadig.macro :refer [do-report]])
[pjstadig.macro :refer [do-report]]
[pjstadig.util :as util])
(:require-macros [pjstadig.assert-expr]))
(enable-console-print!)
(def pprint-map (get-method pp/simple-dispatch :map))
(defn pprint-record [arec]
(pp/pprint-logical-block
:prefix (re-find #".*?\{" (with-out-str (print arec))) :suffix "}"
(pp/print-length-loop
[aseq (seq arec)]
(when aseq
(pp/pprint-logical-block
(pp/write-out (ffirst aseq))
(print " ")
(pp/pprint-newline :linear)
;; [pjs] this is kind of ugly, but it is a private var :(
(set! pp/*current-length* 0) ; always print both parts of the [k v] pair
(pp/write-out (fnext (first aseq))))
(when (next aseq)
(print ", ")
(pp/pprint-newline :linear)
(recur (next aseq)))))))
(defmethod pp/simple-dispatch :map [amap]
(if (record? amap)
(pprint-record amap)
(util/pprint-record amap)
(pprint-map amap)))
(defmethod cljs.test/report [:cljs.test/default :fail]