pprint-indent.lsp 11.1 KB
Newer Older
1 2 3 4 5 6 7 8
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Jul  3 08:50:40 2004
;;;; Contains: Tests of PPRINT-INDENT

(in-package :cl-test)

(deftest pprint-indent.1
9
  (with-standard-io-syntax
10 11
   (let ((*print-pretty* nil))
     (with-open-stream (*standard-output* (make-string-output-stream))
sds's avatar
sds committed
12
                       (pprint-indent :block 0))))
13 14 15
  nil)

(deftest pprint-indent.2
16
  (with-standard-io-syntax
17 18
   (let ((*print-pretty* nil))
     (with-open-stream (*standard-output* (make-broadcast-stream))
sds's avatar
sds committed
19
                       (pprint-indent :current 0))))
20 21 22
  nil)

(deftest pprint-indent.3
23
  (with-standard-io-syntax
24 25
   (let ((*print-pretty* nil))
     (with-open-stream (s (make-string-output-stream))
sds's avatar
sds committed
26
                       (pprint-indent :current 10 s))))
27 28 29
  nil)

(deftest pprint-indent.4
30
  (with-standard-io-syntax
31 32
   (let ((*print-pretty* nil))
     (with-open-stream (s (make-string-output-stream))
sds's avatar
sds committed
33
                       (pprint-indent :block 1/2 s))))
34 35 36
  nil)

(deftest pprint-indent.5
37
  (with-standard-io-syntax
38 39
   (let ((*print-pretty* nil))
     (with-open-stream (s (make-string-output-stream))
sds's avatar
sds committed
40
                       (pprint-indent :block 0.1 s))))
41 42
  nil)

43
(deftest pprint-indent.6
44
  (with-standard-io-syntax
45 46
   (let ((*print-pretty* nil))
     (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
47 48 49 50 51 52 53
           unless
           (equal
            (multiple-value-list
             (with-open-stream (s (make-string-output-stream))
                               (pprint-indent :block x s)))
            '(nil))
           collect x)))
54 55
  nil)

56
(deftest pprint-indent.7
57
  (with-standard-io-syntax
58 59
   (let ((*print-pretty* nil))
     (with-open-stream (*standard-output* (make-broadcast-stream))
sds's avatar
sds committed
60
                       (pprint-indent :current 0 nil))))
61
  nil)
62

63
(deftest pprint-indent.8
64
  (with-standard-io-syntax
65 66 67 68 69 70
   (let ((*print-pretty* nil))
     (with-open-stream
      (os (make-string-output-stream))
      (with-open-stream
       (is (make-string-input-stream ""))
       (with-open-stream (*terminal-io* (make-two-way-stream is os))
sds's avatar
sds committed
71
                         (pprint-indent :current 0 t))))))
72
  nil)
73

74
;;; Now test with pprint-logical-block
75

76 77
;;; :current

78 79 80
(deftest pprint-indent.9
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
81 82 83
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
84 85 86
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
87 88 89 90 91
        (os '(|M| |M|))
        (write '|M| :stream os)
        (pprint-indent :current 3 os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)))))
92 93 94 95 96 97
  "M
    M")

(deftest pprint-indent.10
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
98 99 100
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
101 102 103
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
104 105 106 107 108
        (os '(|M| |M|) :prefix "(" :suffix ")")
        (write '|M| :stream os)
        (pprint-indent :current 1 os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)))))
109 110 111
  "(M
   M)")

112 113 114
(deftest pprint-indent.11
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
115 116 117
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
118 119 120
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
121 122 123 124 125
        (os '(|M| |M|) :prefix "(" :suffix ")")
        (write '|M| :stream os)
        (pprint-indent :current -1 os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)))))
126 127 128 129 130 131
  "(M
 M)")

(deftest pprint-indent.12
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
132 133 134
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
135 136 137
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
138 139 140 141 142
        (os '(|M| |M|) :prefix "(" :suffix ")")
        (write '|M| :stream os)
        (pprint-indent :current -2.0 os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)))))
143 144 145 146 147 148 149 150
  "(M
M)")

;;; :block

(deftest pprint-indent.13
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
151 152 153
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
154 155 156
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
157 158 159 160 161
        (os '(|M| |M|))
        (write '|MMM| :stream os)
        (pprint-indent :block 0 os)
        (pprint-newline :mandatory os)
        (write '|MMMMM| :stream os)))))
162 163 164 165 166 167
  "MMM
MMMMM")

(deftest pprint-indent.13a
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
168 169 170
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
171 172 173
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
174 175 176 177 178
        (os '(|M| |M|) :prefix "(" :suffix ")")
        (write '|MMM| :stream os)
        (pprint-indent :block 0 os)
        (pprint-newline :mandatory os)
        (write '|MMMMM| :stream os)))))
179 180 181 182 183 184
  "(MMM
 MMMMM)")

(deftest pprint-indent.14
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
185 186 187
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
188 189 190
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
191 192 193 194 195
        (os '(|M| |M|))
        (write '|MMM| :stream os)
        (pprint-indent :block 1 os)
        (pprint-newline :mandatory os)
        (write '|MMMMM| :stream os)))))
196 197 198 199 200 201
  "MMM
 MMMMM")

(deftest pprint-indent.15
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
202 203 204
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
205 206 207
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
208 209 210 211 212
        (os '(|M| |M|))
        (write '|MMM| :stream os)
        (pprint-indent :block -1 os)
        (pprint-newline :mandatory os)
        (write '|MMMMM| :stream os)))))
213 214 215 216 217
  "MMM
MMMMM")

(deftest pprint-indent.16
  (loop for n in '(3.0s0 3.0f0 3.0d0 3.0l0)
sds's avatar
sds committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        unless (string=
                (with-standard-io-syntax
                 (let ((*print-pretty* t)
                       (*print-readably* nil)
                       (*print-right-margin* 100)
                       (*print-escape* nil))
                   (with-output-to-string
                     (os)
                     (pprint-logical-block
                      (os '(|M| |M|))
                      (write '|MMM| :stream os)
                      (pprint-indent :block n os)
                      (pprint-newline :mandatory os)
                      (write '|MMMMM| :stream os)))))
                "MMM
233
   MMMMM")
sds's avatar
sds committed
234
        collect n)
235 236 237 238 239 240 241
  nil)

;;; *print-pretty* must be true for pprint-indent to have an effect

(deftest pprint-indent.17
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
242 243 244
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
245 246 247
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
248 249 250 251 252
        (os '(|M| |M|))
        (write '|M| :stream os)
        (let ((*print-pretty* nil)) (pprint-indent :current 3 os))
        (pprint-newline :mandatory os)
        (write '|M| :stream os)))))
253 254 255 256 257 258
  "M
M")

(deftest pprint-indent.18
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
259 260 261
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
262 263 264
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
265 266 267 268 269
        (os '(|M| |M|))
        (write '|M| :stream os)
        (let ((*print-pretty* nil)) (pprint-indent :block 3 os))
        (pprint-newline :mandatory os)
        (write '|M| :stream os)))))
270 271 272 273
  "M
M")

;;; indentation interaction with :per-line-prefix
274

275 276 277
(deftest pprint-indent.19
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
278 279 280
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
281 282 283
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
284 285 286 287 288 289 290
        (os '(|M| |M| |M|) :per-line-prefix ">>>>")
        (write '|M| :stream os)
        (pprint-indent :block 2 os)
        (write #\Space :stream os)
        (write '|M| :stream os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)))))
291 292 293 294 295 296
  ">>>>M M
>>>>  M")

(deftest pprint-indent.20
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
297 298 299
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
300 301 302
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
303 304 305 306 307
        (os '(|M| |M|) :per-line-prefix ">>>>")
        (write '|M| :stream os)
        (pprint-indent :block -1 os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)))))
308 309 310 311 312 313
  ">>>>M
>>>>M")

(deftest pprint-indent.21
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
314 315 316
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
317 318 319
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
320 321 322 323 324 325 326 327 328 329 330 331 332
        (os '(|M| |M| |M| |M|) :per-line-prefix ">>>>")
        (write '|M| :stream os)
        (pprint-indent :block 3 os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)
        (pprint-indent :current -2 os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)
        (pprint-indent :current -5 os)
        (pprint-newline :mandatory os)
        (write '|M| :stream os)
        ))))

333 334 335 336 337 338 339 340 341 342
  ">>>>M
>>>>   M
>>>>  M
>>>>M")

;;; In miser mode, indentation is ignored

(deftest pprint-indent.22
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
343 344 345 346
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-miser-width* 200)
         (*print-escape* nil))
347 348 349
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
350 351 352 353 354 355 356 357
        (os '(1 2 3) :prefix "(" :suffix ")")
        (write 1 :stream os)
        (pprint-indent :current 1 os)
        (pprint-newline :mandatory os)
        (write 2 :stream os)
        (pprint-indent :block 3 os)
        (pprint-newline :mandatory os)
        (write 3 :stream os)))))
358 359 360 361 362 363 364 365 366
  "(1
 2
 3)")

;;; TERPRI or printing newline characters does not invoke indentation

(deftest pprint-indent.23
  (with-standard-io-syntax
   (let ((*print-pretty* t)
sds's avatar
sds committed
367 368 369
         (*print-readably* nil)
         (*print-right-margin* 100)
         (*print-escape* nil))
370 371 372
     (with-output-to-string
       (os)
       (pprint-logical-block
sds's avatar
sds committed
373 374 375 376 377 378 379 380 381
        (os '(1 2 3 4))
        (pprint-indent :block 2 os)
        (write 1 :stream os)
        (terpri os)
        (write 2 :stream os)
        (write #\Newline :stream os)
        (write 3 :stream os)
        (pprint-newline :mandatory os)
        (write 4 :stream os)))))
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  "1
2
3
  4")

;;; Error cases

(deftest pprint-indent.error.1
  (signals-error (pprint-indent) program-error)
  t)

(deftest pprint-indent.error.2
  (signals-error (pprint-indent :current) program-error)
  t)

(deftest pprint-indent.error.3
  (signals-error (pprint-indent :block 0 *standard-output* nil) program-error)
  t)

(deftest pprint-indent.error.4
  (loop for x in *mini-universe*
sds's avatar
sds committed
403 404 405
        when (and (not (member x '(:block :current)))
                  (not (eval `(signals-error (pprint-indent ',x 0) error))))
        collect x)
406
  nil)
407 408 409

(deftest pprint-indent.error.4-unsafe
  (loop for x in *mini-universe*
sds's avatar
sds committed
410 411 412 413
        when (and (not (member x '(:block :current)))
                  (not (eval `(signals-error (locally (declare (optimize (safety 0))) (pprint-indent ',x 0))
                                             error))))
        collect x)
414 415
  nil)