PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Clojure/Clojure.Source/clojure/test_clojure/pprint/test_pretty.clj

http://github.com/richhickey/clojure-clr
Clojure | 317 lines | 250 code | 39 blank | 28 comment | 25 complexity | 01c6f7393ec4a82c1c11c8640a8a9c5e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. ;;; test_pretty.clj -- part of the pretty printer for Clojure
  2. ; Copyright (c) Rich Hickey. All rights reserved.
  3. ; The use and distribution terms for this software are covered by the
  4. ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
  5. ; which can be found in the file epl-v10.html at the root of this distribution.
  6. ; By using this software in any fashion, you are agreeing to be bound by
  7. ; the terms of this license.
  8. ; You must not remove this notice, or any other, from this software.
  9. ;; Author: Tom Faulhaber
  10. ;; April 3, 2009
  11. (in-ns 'clojure.test-clojure.pprint)
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;;;
  14. ;;; Unit tests for the pretty printer
  15. ;;;
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. (simple-tests xp-fill-test
  18. (binding [*print-pprint-dispatch* simple-dispatch
  19. *print-right-margin* 38
  20. *print-miser-width* nil]
  21. (cl-format nil "(let ~:<~@{~:<~w ~_~w~:>~^ ~:_~}~:>~_ ...)~%"
  22. '((x 4) (*print-length* nil) (z 2) (list nil))))
  23. "(let ((x 4) (*print-length* nil)\n (z 2) (list nil))\n ...)\n"
  24. (binding [*print-pprint-dispatch* simple-dispatch
  25. *print-right-margin* 22]
  26. (cl-format nil "(let ~:<~@{~:<~w ~_~w~:>~^ ~:_~}~:>~_ ...)~%"
  27. '((x 4) (*print-length* nil) (z 2) (list nil))))
  28. "(let ((x 4)\n (*print-length*\n nil)\n (z 2)\n (list nil))\n ...)\n")
  29. (simple-tests xp-miser-test
  30. (binding [*print-pprint-dispatch* simple-dispatch
  31. *print-right-margin* 10, *print-miser-width* 9]
  32. (cl-format nil "~:<LIST ~@_~W ~@_~W ~@_~W~:>" '(first second third)))
  33. "(LIST\n first\n second\n third)"
  34. (binding [*print-pprint-dispatch* simple-dispatch
  35. *print-right-margin* 10, *print-miser-width* 8]
  36. (cl-format nil "~:<LIST ~@_~W ~@_~W ~@_~W~:>" '(first second third)))
  37. "(LIST first second third)")
  38. (simple-tests mandatory-fill-test
  39. (cl-format nil
  40. "<pre>~%~<Usage: ~:I~@{*~a*~^~:@_~}~:>~%</pre>~%"
  41. [ "hello" "gooodbye" ])
  42. "<pre>
  43. Usage: *hello*
  44. *gooodbye*
  45. </pre>
  46. ")
  47. (simple-tests prefix-suffix-test
  48. (binding [*print-pprint-dispatch* simple-dispatch
  49. *print-right-margin* 10, *print-miser-width* 10]
  50. (cl-format nil "~<{~;LIST ~@_~W ~@_~W ~@_~W~;}~:>" '(first second third)))
  51. "{LIST\n first\n second\n third}")
  52. (simple-tests pprint-test
  53. (binding [*print-pprint-dispatch* simple-dispatch]
  54. (write '(defn foo [x y]
  55. (let [result (* x y)]
  56. (if (> result 400)
  57. (cl-format true "That number is too big")
  58. (cl-format true "The result of ~d x ~d is ~d" x y result))))
  59. :stream nil))
  60. "(defn
  61. foo
  62. [x y]
  63. (let
  64. [result (* x y)]
  65. (if
  66. (> result 400)
  67. (cl-format true \"That number is too big\")
  68. (cl-format true \"The result of ~d x ~d is ~d\" x y result))))"
  69. (with-pprint-dispatch code-dispatch
  70. (write '(defn foo [x y]
  71. (let [result (* x y)]
  72. (if (> result 400)
  73. (cl-format true "That number is too big")
  74. (cl-format true "The result of ~d x ~d is ~d" x y result))))
  75. :stream nil))
  76. "(defn foo [x y]
  77. (let [result (* x y)]
  78. (if (> result 400)
  79. (cl-format true \"That number is too big\")
  80. (cl-format true \"The result of ~d x ~d is ~d\" x y result))))"
  81. (binding [*print-pprint-dispatch* simple-dispatch
  82. *print-right-margin* 15]
  83. (write '(fn (cons (car x) (cdr y))) :stream nil))
  84. "(fn\n (cons\n (car x)\n (cdr y)))"
  85. (with-pprint-dispatch code-dispatch
  86. (binding [*print-right-margin* 52]
  87. (write
  88. '(add-to-buffer this (make-buffer-blob (str (char c)) nil))
  89. :stream nil)))
  90. "(add-to-buffer\n this\n (make-buffer-blob (str (char c)) nil))"
  91. )
  92. (simple-tests pprint-reader-macro-test
  93. (with-pprint-dispatch code-dispatch
  94. (write (read-string "(map #(first %) [[1 2 3] [4 5 6] [7]])")
  95. :stream nil))
  96. "(map #(first %) [[1 2 3] [4 5 6] [7]])"
  97. (with-pprint-dispatch code-dispatch
  98. (write (read-string "@@(ref (ref 1))")
  99. :stream nil))
  100. "@@(ref (ref 1))"
  101. (with-pprint-dispatch code-dispatch
  102. (write (read-string "'foo")
  103. :stream nil))
  104. "'foo"
  105. )
  106. (simple-tests code-block-tests
  107. (with-out-str
  108. (with-pprint-dispatch code-dispatch
  109. (pprint
  110. '(defn cl-format
  111. "An implementation of a Common Lisp compatible format function"
  112. [stream format-in & args]
  113. (let [compiled-format (if (string? format-in) (compile-format format-in) format-in)
  114. navigator (init-navigator args)]
  115. (execute-format stream compiled-format navigator))))))
  116. "(defn cl-format
  117. \"An implementation of a Common Lisp compatible format function\"
  118. [stream format-in & args]
  119. (let [compiled-format (if (string? format-in)
  120. (compile-format format-in)
  121. format-in)
  122. navigator (init-navigator args)]
  123. (execute-format stream compiled-format navigator)))
  124. "
  125. (with-out-str
  126. (with-pprint-dispatch code-dispatch
  127. (pprint
  128. '(defn pprint-defn [writer alis]
  129. (if (next alis)
  130. (let [[defn-sym defn-name & stuff] alis
  131. [doc-str stuff] (if (string? (first stuff))
  132. [(first stuff) (next stuff)]
  133. [nil stuff])
  134. [attr-map stuff] (if (map? (first stuff))
  135. [(first stuff) (next stuff)]
  136. [nil stuff])]
  137. (pprint-logical-block writer :prefix "(" :suffix ")"
  138. (cl-format true "~w ~1I~@_~w" defn-sym defn-name)
  139. (if doc-str
  140. (cl-format true " ~_~w" doc-str))
  141. (if attr-map
  142. (cl-format true " ~_~w" attr-map))
  143. ;; Note: the multi-defn case will work OK for malformed defns too
  144. (cond
  145. (vector? (first stuff)) (single-defn stuff (or doc-str attr-map))
  146. :else (multi-defn stuff (or doc-str attr-map)))))
  147. (pprint-simple-code-list writer alis))))))
  148. "(defn pprint-defn [writer alis]
  149. (if (next alis)
  150. (let [[defn-sym defn-name & stuff] alis
  151. [doc-str stuff] (if (string? (first stuff))
  152. [(first stuff) (next stuff)]
  153. [nil stuff])
  154. [attr-map stuff] (if (map? (first stuff))
  155. [(first stuff) (next stuff)]
  156. [nil stuff])]
  157. (pprint-logical-block
  158. writer
  159. :prefix
  160. \"(\"
  161. :suffix
  162. \")\"
  163. (cl-format true \"~w ~1I~@_~w\" defn-sym defn-name)
  164. (if doc-str (cl-format true \" ~_~w\" doc-str))
  165. (if attr-map (cl-format true \" ~_~w\" attr-map))
  166. (cond
  167. (vector? (first stuff)) (single-defn
  168. stuff
  169. (or doc-str attr-map))
  170. :else (multi-defn stuff (or doc-str attr-map)))))
  171. (pprint-simple-code-list writer alis)))
  172. ")
  173. (defn tst-pprint
  174. "A helper function to pprint to a string with a restricted right margin"
  175. [right-margin obj]
  176. (binding [*print-right-margin* right-margin
  177. *print-pretty* true]
  178. (write obj :stream nil)))
  179. ;;; A bunch of predefined data to print
  180. (def future-filled (future-call (fn [] 100)))
  181. @future-filled
  182. (def future-unfilled (future-call (fn [] (.WaitOne (System.Threading.Semaphore. 0 3)) ))) ;;; (.acquire (java.util.concurrent.Semaphore. 0))
  183. (def promise-filled (promise))
  184. (deliver promise-filled '(first second third))
  185. (def promise-unfilled (promise))
  186. (def basic-agent (agent '(first second third)))
  187. (def basic-atom (atom '(first second third)))
  188. (def basic-ref (ref '(first second third)))
  189. (def delay-forced (delay '(first second third)))
  190. (force delay-forced)
  191. (def delay-unforced (delay '(first second third)))
  192. (defrecord pprint-test-rec [a b c])
  193. (simple-tests pprint-datastructures-tests
  194. (tst-pprint 20 future-filled) #"#<Future@[0-9a-f]+: \r?\n 100>"
  195. (tst-pprint 20 future-unfilled) #"#<Future@[0-9a-f]+: \r?\n :pending>"
  196. (tst-pprint 20 promise-filled) #"#<Promise@[0-9a-f]+: \r?\n \(first\r?\n second\r?\n third\)>"
  197. ;; This hangs currently, cause we can't figure out whether a promise is filled
  198. ;;(tst-pprint 20 promise-unfilled) #"#<Promise@[0-9a-f]+: \r?\n :pending>"
  199. (tst-pprint 20 basic-agent) #"#<Agent@[0-9a-f]+: \r?\n \(first\r?\n second\r?\n third\)>"
  200. (tst-pprint 20 basic-atom) #"#<Atom@[0-9a-f]+: \r?\n \(first\r?\n second\r?\n third\)>"
  201. (tst-pprint 20 basic-ref) #"#<Ref@[0-9a-f]+: \r?\n \(first\r?\n second\r?\n third\)>"
  202. (tst-pprint 20 delay-forced) #"#<Delay@[0-9a-f]+: \r?\n \(first\r?\n second\r?\n third\)>"
  203. ;; Currently no way not to force the delay
  204. ;;(tst-pprint 20 delay-unforced) #"#<Delay@[0-9a-f]+: \n :pending>"
  205. (tst-pprint 20 (pprint-test-rec. 'first 'second 'third)) "{:a first,\n :b second,\n :c third}"
  206. ;; basic java arrays: fails owing to assembla ticket #346
  207. ;;(tst-pprint 10 (int-array (range 7))) "[0,\n 1,\n 2,\n 3,\n 4,\n 5,\n 6]"
  208. (tst-pprint 15 (reduce conj clojure.lang.PersistentQueue/EMPTY (range 10)))
  209. "<-(0\n 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9)-<"
  210. )
  211. ;;; Some simple tests of dispatch
  212. (defmulti
  213. test-dispatch
  214. "A test dispatch method"
  215. {:added "1.2" :arglists '[[object]]}
  216. #(and (seq %) (not (string? %))))
  217. (defmethod test-dispatch true [avec]
  218. (pprint-logical-block :prefix "[" :suffix "]"
  219. (loop [aseq (seq avec)]
  220. (when aseq
  221. (write-out (first aseq))
  222. (when (next aseq)
  223. (.Write ^System.IO.TextWriter *out* " ") ;;;(.write ^java.io.Writer *out* " ")
  224. (pprint-newline :linear)
  225. (recur (next aseq)))))))
  226. (defmethod test-dispatch false [aval] (pr aval))
  227. (simple-tests dispatch-tests
  228. (with-pprint-dispatch test-dispatch
  229. (with-out-str
  230. (pprint '("hello" "there"))))
  231. "[\"hello\" \"there\"]\n"
  232. )
  233. (simple-tests print-length-tests
  234. (binding [*print-length* 1] (with-out-str (pprint '(a b c d e f))))
  235. "(a ...)\n"
  236. (binding [*print-length* 2] (with-out-str (pprint '(a b c d e f))))
  237. "(a b ...)\n"
  238. (binding [*print-length* 6] (with-out-str (pprint '(a b c d e f))))
  239. "(a b c d e f)\n"
  240. (binding [*print-length* 8] (with-out-str (pprint '(a b c d e f))))
  241. "(a b c d e f)\n"
  242. (binding [*print-length* 1] (with-out-str (pprint [1 2 3 4 5 6])))
  243. "[1 ...]\n"
  244. (binding [*print-length* 2] (with-out-str (pprint [1 2 3 4 5 6])))
  245. "[1 2 ...]\n"
  246. (binding [*print-length* 6] (with-out-str (pprint [1 2 3 4 5 6])))
  247. "[1 2 3 4 5 6]\n"
  248. (binding [*print-length* 8] (with-out-str (pprint [1 2 3 4 5 6])))
  249. "[1 2 3 4 5 6]\n"
  250. ;; This set of tests isn't that great cause it assumes that the set remains
  251. ;; ordered for printing. This is currently (1.3) true, but no future
  252. ;; guarantees
  253. (binding [*print-length* 1] (with-out-str (pprint #{1 2 3 4 5 6})))
  254. "#{1 ...}\n"
  255. (binding [*print-length* 2] (with-out-str (pprint #{1 2 3 4 5 6})))
  256. "#{1 2 ...}\n"
  257. (binding [*print-length* 6] (with-out-str (pprint #{1 2 3 4 5 6})))
  258. "#{1 2 3 4 5 6}\n"
  259. (binding [*print-length* 8] (with-out-str (pprint #{1 2 3 4 5 6})))
  260. "#{1 2 3 4 5 6}\n"
  261. ;; See above comment and apply to this map :)
  262. (binding [*print-length* 1] (with-out-str (pprint {1 2, 3 4, 5 6, 7 8, 9 10, 11 12})))
  263. "{1 2, ...}\n"
  264. (binding [*print-length* 2] (with-out-str (pprint {1 2, 3 4, 5 6, 7 8, 9 10, 11 12})))
  265. "{1 2, 3 4, ...}\n"
  266. (binding [*print-length* 6] (with-out-str (pprint {1 2, 3 4, 5 6, 7 8, 9 10, 11 12})))
  267. "{1 2, 3 4, 5 6, 7 8, 9 10, 11 12}\n"
  268. (binding [*print-length* 8] (with-out-str (pprint {1 2, 3 4, 5 6, 7 8, 9 10, 11 12})))
  269. "{1 2, 3 4, 5 6, 7 8, 9 10, 11 12}\n"
  270. (binding [*print-length* 1] (with-out-str (pprint (int-array [1 2 3 4 5 6]))))
  271. "[1, ...]\n"
  272. (binding [*print-length* 2] (with-out-str (pprint (int-array [1 2 3 4 5 6]))))
  273. "[1, 2, ...]\n"
  274. (binding [*print-length* 6] (with-out-str (pprint (int-array [1 2 3 4 5 6]))))
  275. "[1, 2, 3, 4, 5, 6]\n"
  276. (binding [*print-length* 8] (with-out-str (pprint (int-array [1 2 3 4 5 6]))))
  277. "[1, 2, 3, 4, 5, 6]\n"
  278. )