-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(ns me.flowthing.pp-flush-test | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[me.flowthing.pp :as sut]) | ||
(:import (java.io BufferedWriter StringWriter))) | ||
|
||
(defn ^:private flush-observing-writer | ||
[writer] | ||
(let [a (atom 0)] | ||
[(proxy [BufferedWriter] [writer] | ||
(flush [] | ||
(proxy-super flush) | ||
(swap! a inc))) | ||
a])) | ||
|
||
(deftest flush-on-newline | ||
(testing "Does not flush after printing if *flush-on-newline* is nil" | ||
(let [sw (StringWriter.) | ||
[writer a] (flush-observing-writer sw)] | ||
(binding [*flush-on-newline* nil] | ||
(sut/pprint writer {:a 1 :b 2 :c 3 :d 4} {:max-width 10})) | ||
(is (= [0 ""] [@a (str sw)])))) | ||
|
||
(testing "Only flushes after printing the entire top-level form if *flush-on-newline* is true" | ||
;; So when pretty-printing, *flush-on-newline* doesn't actually flush on | ||
;; newline, only after printing the entire top-level form. | ||
(let [sw (StringWriter.) | ||
[writer a] (flush-observing-writer sw)] | ||
(binding [*flush-on-newline* true] | ||
(sut/pprint writer {:a 1 :b 2 :c 3 :d 4} {:max-width 10}) | ||
(sut/pprint writer {:a 1 :b 2 :c 3 :d 4} {:max-width 10})) | ||
(is (= [2 "{:a 1,\n :b 2,\n :c 3,\n :d 4}\n{:a 1,\n :b 2,\n :c 3,\n :d 4}\n"] | ||
[@a (str sw)]))))) |