|
7 | 7 | os
|
8 | 8 | textwrap))
|
9 | 9 |
|
| 10 | +(defn trim-and-replace-newlines |
| 11 | + [s] |
| 12 | + (-> (str/trim s) |
| 13 | + (str/replace "\n" os/linesep))) |
| 14 | + |
10 | 15 | (deftest column-writer-test
|
11 | 16 | (let [write (fn [s]
|
12 | 17 | (with [buf (io/StringIO)]
|
13 | 18 | (let [writer (@#'pprint/get-column-writer buf)]
|
14 | 19 | (.write writer s)
|
15 | 20 | (select-keys @writer [:col :line]))) )]
|
16 | 21 | (is (= {:line 0 :col 5} (write "hello")))
|
17 |
| - (is (= {:line 1 :col 15} (write "hello\nthere my friend"))) |
18 |
| - (is (= {:line 2 :col 0} (write "hello\nthere my friend\n"))) |
| 22 | + (is (= {:line 1 :col 15} (write (str "hello" os/linesep "there my friend")))) |
| 23 | + (is (= {:line 2 :col 0} (write (str "hello" os/linesep "there my friend" os/linesep)))) |
19 | 24 | (is (= {:line 0 :col 0} (write "")))))
|
20 | 25 |
|
21 | 26 | (deftest pprint-test
|
22 | 27 | (testing "scalars"
|
23 |
| - (are [res expr] (= res (str/rtrim (with-out-str (pprint/pprint expr)))) |
| 28 | + (are [res expr] (= res (trim-and-replace-newlines |
| 29 | + (with-out-str |
| 30 | + (pprint/pprint expr)))) |
24 | 31 | "nil" nil
|
25 | 32 | "true" true
|
26 | 33 | "false" false
|
|
41 | 48 | "long.ns/sym" 'long.ns/sym))
|
42 | 49 |
|
43 | 50 | (testing "collections"
|
44 |
| - (are [res expr] (= res (str/rtrim (with-out-str (pprint/pprint expr)))) |
| 51 | + (are [res expr] (= res (trim-and-replace-newlines |
| 52 | + (with-out-str |
| 53 | + (pprint/pprint expr)))) |
45 | 54 | "{}" {}
|
46 | 55 | "{:a 1}" {:a 1}
|
47 | 56 |
|
|
64 | 73 | "#{:a}" #{:a}))
|
65 | 74 |
|
66 | 75 | (testing "python collections"
|
67 |
| - (are [res expr] (= res (str/rtrim (with-out-str (pprint/pprint expr)))) |
| 76 | + (are [res expr] (= res (trim-and-replace-newlines |
| 77 | + (with-out-str |
| 78 | + (pprint/pprint expr)))) |
68 | 79 | "#py {}" (python/dict)
|
69 | 80 | "#py {:a 1}" (python/dict {:a 1})
|
70 | 81 |
|
|
80 | 91 | "#py #{:a}" (python/set [:a])))
|
81 | 92 |
|
82 | 93 | (testing "large collections"
|
83 |
| - (are [res expr] (= res (str/rtrim (with-out-str (pprint/pprint expr)))) |
| 94 | + (are [res expr] (= res (trim-and-replace-newlines |
| 95 | + (with-out-str |
| 96 | + (pprint/pprint expr)))) |
84 | 97 | "[(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)
|
85 | 98 | (21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39)
|
86 | 99 | :a
|
|
135 | 148 | ["abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"])))))
|
136 | 149 |
|
137 | 150 | (testing "printing meta"
|
138 |
| - (are [res expr] (= res (str/rtrim |
| 151 | + (are [res expr] (= res (trim-and-replace-newlines |
139 | 152 | (binding [*print-meta* true]
|
140 | 153 | (with-out-str (pprint/pprint expr)))))
|
141 | 154 | "[]" []
|
|
158 | 171 | (let [long-map (into {} (map #(vector (keyword (python/chr %1)) %2)
|
159 | 172 | (range (python/ord "a") (python/ord "z"))
|
160 | 173 | (range)))]
|
161 |
| - (are [res len expr] (= res (str/rtrim |
| 174 | + (are [res len expr] (= res (trim-and-replace-newlines |
162 | 175 | (binding [pprint/*print-sort-keys* true
|
163 | 176 | *print-length* len]
|
164 | 177 | (with-out-str
|
|
183 | 196 | ...}" 15 long-map)))
|
184 | 197 |
|
185 | 198 | (deftest pprint-base-and-radix-test
|
186 |
| - (are [res base expr] (= res (str/rtrim |
| 199 | + (are [res base expr] (= res (trim-and-replace-newlines |
187 | 200 | (binding [pprint/*print-radix* true
|
188 | 201 | pprint/*print-base* base]
|
189 | 202 | (with-out-str
|
|
208 | 221 | "#18r1" 18 1
|
209 | 222 | "#18r27" 18 43))
|
210 | 223 |
|
211 |
| -(defn ^:private match-ideref |
212 |
| - [v] |
213 |
| - (let [s (with-out-str (pprint/pprint v))] |
214 |
| - (drop 1 (re-matches #"#<(\w+)@0x[0-9a-f]+: ([^>]+)>\r?\n" s)))) |
215 |
| - |
216 | 224 | (deftest pprint-var-test
|
217 |
| - (is (= "#'basilisp.core/map\n" (with-out-str (pprint/pprint #'map))))) |
| 225 | + (is (= "#'basilisp.core/map" |
| 226 | + (str/rtrim |
| 227 | + (with-out-str |
| 228 | + (pprint/pprint #'map)))))) |
218 | 229 |
|
219 | 230 | (defrecord Point [x y z])
|
220 | 231 |
|
221 | 232 | (deftest pprint-record-test
|
222 |
| - (is (= "#Point{:x 1 :y 2 :z 3}\n" |
223 |
| - (with-out-str |
224 |
| - (binding [pprint/*print-sort-keys* true] |
225 |
| - (pprint/pprint (->Point 1 2 3))))))) |
| 233 | + (is (= "#Point{:x 1 :y 2 :z 3}" |
| 234 | + (str/rtrim |
| 235 | + (with-out-str |
| 236 | + (binding [pprint/*print-sort-keys* true] |
| 237 | + (pprint/pprint (->Point 1 2 3)))))))) |
| 238 | + |
| 239 | +(defn ^:private match-ideref |
| 240 | + [v] |
| 241 | + (let [s (trim-and-replace-newlines (with-out-str (pprint/pprint v)))] |
| 242 | + (drop 1 (re-matches #"#<(\w+)@0x[0-9a-f]+: ([^>]+)>" s)))) |
226 | 243 |
|
227 | 244 | (deftest pprint-ideref-test
|
228 | 245 | (testing "delay"
|
|
315 | 332 |
|
316 | 333 | (deftest pprint-miser-test
|
317 | 334 | (binding [pprint/*print-pprint-dispatch* miser-dispatch]
|
318 |
| - (are [res margin] (= res (str/rtrim |
| 335 | + (are [res margin] (= res (trim-and-replace-newlines |
319 | 336 | (binding [pprint/*print-right-margin* margin]
|
320 | 337 | (with-out-str
|
321 | 338 | (pprint/pprint [:abcdefghijklmnop [:abcdefghijklmn :a]])))))
|
|
327 | 344 | "[:abcdefghijklmnop [:abcdefghijklmn :a]]" 50)))
|
328 | 345 |
|
329 | 346 | (deftest pprint-print-level-test
|
330 |
| - (are [res plen expr] (= res (str/rtrim |
| 347 | + (are [res plen expr] (= res (trim-and-replace-newlines |
331 | 348 | (binding [*print-level* plen]
|
332 | 349 | (with-out-str
|
333 | 350 | (pprint/pprint expr)))))
|
|
337 | 354 | "[[:a :b :c] [#]]" 2 [[:a :b :c] [[]]]))
|
338 | 355 |
|
339 | 356 | (deftest pprint-print-length-test
|
340 |
| - (are [res plen expr] (= res (str/rtrim |
| 357 | + (are [res plen expr] (= res (trim-and-replace-newlines |
341 | 358 | (binding [*print-length* plen]
|
342 | 359 | (with-out-str
|
343 | 360 | (pprint/pprint expr)))))
|
|
0 commit comments