|
1 | 1 | (ns cider.nrepl.middleware.out-test
|
2 | 2 | (:require
|
3 | 3 | [cider.nrepl.middleware.out :as o]
|
4 |
| - [clojure.test :refer :all])) |
| 4 | + [clojure.test :refer :all]) |
| 5 | + (:import |
| 6 | + [java.io PrintWriter StringWriter])) |
5 | 7 |
|
6 | 8 | (defn random-str []
|
7 | 9 | (->> #(format "%x" (rand-int 15))
|
|
32 | 34 | (testing "The mapping is computed once; not doing so would defeat is point and create issues."
|
33 | 35 | (is (map? o/original-output))
|
34 | 36 | (is (not (fn? o/original-output)))))
|
| 37 | + |
| 38 | +(defmacro with-original-output |
| 39 | + [[m] & body] |
| 40 | + `(let [orig# o/original-output] |
| 41 | + (try |
| 42 | + (alter-var-root #'o/original-output (constantly ~m)) |
| 43 | + ~@body |
| 44 | + (finally |
| 45 | + (alter-var-root #'o/original-output (constantly orig#)))))) |
| 46 | + |
| 47 | +(defn- forking-printer-test-streams |
| 48 | + [] |
| 49 | + (let [out-writer (StringWriter.) |
| 50 | + message-writer (StringWriter.) |
| 51 | + message {:session (atom {#'*out* message-writer})} |
| 52 | + printer (o/forking-printer [message] :out)] |
| 53 | + {:out-writer out-writer |
| 54 | + :message-writer message-writer |
| 55 | + :printer printer})) |
| 56 | + |
| 57 | +(deftest forking-printer-test |
| 58 | + (testing "forking-printer prints to all message streams and original stream" |
| 59 | + (testing "with String argument " |
| 60 | + (let [{:keys [^StringWriter out-writer |
| 61 | + ^StringWriter message-writer |
| 62 | + ^PrintWriter printer]} |
| 63 | + (forking-printer-test-streams)] |
| 64 | + (with-original-output [{:out out-writer}] |
| 65 | + (.write printer "Hello") |
| 66 | + (is (= "Hello" (.toString out-writer))) |
| 67 | + (is (= "Hello" (.toString message-writer)))))) |
| 68 | + (testing "with int" |
| 69 | + (let [{:keys [^StringWriter out-writer |
| 70 | + ^StringWriter message-writer |
| 71 | + ^PrintWriter printer]} |
| 72 | + (forking-printer-test-streams) |
| 73 | + an-int (int 32)] |
| 74 | + (with-original-output [{:out out-writer}] |
| 75 | + (.write printer an-int) |
| 76 | + (is (= " " (.toString out-writer))) |
| 77 | + (is (= " " (.toString message-writer)))))) |
| 78 | + (testing "with char array" |
| 79 | + (let [{:keys [^StringWriter out-writer |
| 80 | + ^StringWriter message-writer |
| 81 | + ^PrintWriter printer]} |
| 82 | + (forking-printer-test-streams)] |
| 83 | + (with-original-output [{:out out-writer}] |
| 84 | + (.write printer (char-array "and")) |
| 85 | + (is (= "and" (.toString out-writer))) |
| 86 | + (is (= "and" (.toString message-writer)))))) |
| 87 | + (testing "with String with offsets" |
| 88 | + (let [{:keys [^StringWriter out-writer |
| 89 | + ^StringWriter message-writer |
| 90 | + ^PrintWriter printer]} |
| 91 | + (forking-printer-test-streams)] |
| 92 | + (with-original-output [{:out out-writer}] |
| 93 | + (.write printer "12 good34" 3 4) |
| 94 | + (is (= "good" (.toString out-writer))) |
| 95 | + (is (= "good" (.toString message-writer)))))) |
| 96 | + (testing "with char array with offsets" |
| 97 | + (let [{:keys [^StringWriter out-writer |
| 98 | + ^StringWriter message-writer |
| 99 | + ^PrintWriter printer]} |
| 100 | + (forking-printer-test-streams)] |
| 101 | + (with-original-output [{:out out-writer}] |
| 102 | + (.write printer (char-array " bye67") 1 3) |
| 103 | + (is (= "bye" (.toString out-writer))) |
| 104 | + (is (= "bye" (.toString message-writer)))))))) |
0 commit comments