File tree Expand file tree Collapse file tree
test/clojure/test_clojure Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5858 (let [fields (ref {:max max-columns, :cur 0 , :line 0 :base writer})]
5959 (proxy [Writer IDeref] []
6060 (deref [] fields)
61+ (flush []
62+ (.flush writer))
6163 (write
6264 ([^chars cbuf ^Integer off ^Integer len]
6365 (let [^Writer writer (get-field this :base )]
Original file line number Diff line number Diff line change @@ -164,7 +164,7 @@ radix specifier is in the form #XXr where XX is the decimal value of *print-base
164164 (make-pretty-writer base-writer# *print-right-margin* *print-miser-width*)
165165 base-writer#)]
166166 ~@body
167- (.flush *out*))))
167+ (.ppflush *out*))))
168168
169169
170170; ;;TODO: if pretty print is not set, don't use pr but rather something that respects *print-base*, etc.
Original file line number Diff line number Diff line change 380380 :miser-width miser-width
381381 :trailing-white-space nil
382382 :pos 0 })]
383- (proxy [Writer IDeref] []
383+ (proxy [Writer IDeref PrettyFlush ] []
384384 (deref [] fields)
385385
386386 (write
408408 Long
409409 (p-write-char this x))))
410410
411- (flush []
411+ (ppflush []
412412 (if (= (getf :mode ) :buffering )
413- (dosync
413+ (dosync
414414 (write-tokens this (getf :buffer ) true )
415415 (setf :buffer []))
416416 (write-white-space this)))
417417
418+ (flush []
419+ (.ppflush this)
420+ (.flush (getf :base )))
421+
418422 (close []
419423 (.flush this)))))
420424
Original file line number Diff line number Diff line change @@ -102,3 +102,6 @@ beginning of aseq"
102102 `(prerr ~@(cons (list 'quote prefix) (mapcat #(list (list 'quote %) " =" %)
103103 (cons arg (seq more-args))))))
104104
105+ ; ; Flush the pretty-print buffer without flushing the underlying stream
106+ (definterface PrettyFlush
107+ (^void ppflush []))
Original file line number Diff line number Diff line change 1010
1111(ns clojure.test-clojure.pprint
1212 (:refer-clojure :exclude [format])
13- (:use [clojure.test :only (deftest are run-tests )]
13+ (:use [clojure.test :only (deftest is are run-tests )]
1414 [clojure.test-helper :only [platform-newlines]]
1515 clojure.test-clojure.pprint.test-helper
1616 clojure.pprint))
Original file line number Diff line number Diff line change @@ -326,3 +326,49 @@ It is implemented with a number of custom enlive templates.\"
326326 " [1, 2, 3, 4, 5, 6]\n "
327327 )
328328
329+ (defn- flush-alerting-writer
330+ [o]
331+ (let [flush-count-atom (atom 0 )]
332+ [
333+ (proxy [java.io.BufferedWriter] [o]
334+ (flush []
335+ (proxy-super flush)
336+ (swap! flush-count-atom inc)))
337+ flush-count-atom]))
338+
339+ (deftest test-flush-underlying-prn
340+ []
341+ (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter. ))]
342+ (binding [*out* out
343+ *flush-on-newline* true ]
344+ (prn (range 50 ))
345+ (prn (range 50 )))
346+ (is (= @flush-count-atom 2 ) " println flushes on newline" )))
347+
348+ (deftest test-flush-underlying-pprint
349+ []
350+ (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter. ))]
351+ (binding [*out* out
352+ *flush-on-newline* true ]
353+ (pprint (range 50 ))
354+ (pprint (range 50 )))
355+ (is (= @flush-count-atom 2 ) " pprint flushes on newline" )))
356+
357+ (deftest test-noflush-underlying-prn
358+ []
359+ (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter. ))]
360+ (binding [*out* out
361+ *flush-on-newline* nil ]
362+ (prn (range 50 ))
363+ (prn (range 50 )))
364+ (is (= @flush-count-atom 0 ) " println flushes on newline" )))
365+
366+ (deftest test-noflush-underlying-pprint
367+ []
368+ (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter. ))]
369+ (binding [*out* out
370+ *flush-on-newline* nil ]
371+ (pprint (range 50 ))
372+ (pprint (range 50 )))
373+ (is (= @flush-count-atom 0 ) " pprint flushes on newline" )))
374+
You can’t perform that action at this time.
0 commit comments