Debian Codegraph @

test-suite/tests/print.test

raw
1 ;;;; -*- coding: utf-8; mode: scheme; -*-
2 ;;;;
3 ;;;; Copyright (C) 2010, 2013, 2014, 2023 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (test-suite test-print)
20 #:use-module (ice-9 pretty-print)
21 #:use-module (test-suite lib))
22
23 (define-syntax prints?
24 ;; #t if EXP prints as RESULT.
25 (syntax-rules ()
26 ((_ exp result arg ...)
27 (string=? result
28 (with-output-to-string
29 (lambda ()
30 (pretty-print 'exp arg ...)))))))
31
32 (define (with-print-options opts thunk)
33 (let ((saved-options (print-options)))
34 (dynamic-wind
35 (lambda ()
36 (print-options opts))
37 thunk
38 (lambda ()
39 (print-options saved-options)))))
40
41 (define-syntax-rule (write-with-options opts x)
42 (with-print-options opts (lambda ()
43 (with-output-to-string
44 (lambda ()
45 (write x))))))
46
47
48 (with-test-prefix "write"
49
50 (with-test-prefix "r7rs-symbols"
51
52 (pass-if-equal "basic"
53 "|foo bar|"
54 (write-with-options '(r7rs-symbols)
55 (string->symbol "foo bar")))
56
57 (pass-if-equal "escapes"
58 "|bar \\| backslash \\x5c; alarm \\a backspace \\b tab \\t newline \\n cr \\r null \\x0; del \\x7f;|"
59 (write-with-options
60 '(r7rs-symbols)
61 (string->symbol
62 (string-append
63 "bar | backslash \\ alarm \a backspace \b tab \t newline \n cr \r null \0 del "
64 (string #\del)))))
65
66 (pass-if-equal "brackets"
67 "|()[]{}|"
68 (write-with-options '(r7rs-symbols)
69 (string->symbol "()[]{}")))
70
71 (pass-if-equal "starts with bar"
72 "|\\|foo|"
73 (write-with-options '(r7rs-symbols)
74 (string->symbol "|foo")))
75
76 (pass-if-equal "ends with bar"
77 "|foo\\||"
78 (write-with-options '(r7rs-symbols)
79 (string->symbol "foo|")))
80
81 (pass-if-equal "starts with backslash"
82 "|\\x5c;foo|"
83 (write-with-options '(r7rs-symbols)
84 (string->symbol "\\foo")))
85
86 (pass-if-equal "ends with backslash"
87 "|foo\\x5c;|"
88 (write-with-options '(r7rs-symbols)
89 (string->symbol "foo\\")))))
90
91
92 (with-test-prefix "pretty-print"
93
94 (pass-if "pair"
95 (prints? (a . b) "(a . b)\n"))
96
97 (pass-if "list"
98 (prints? (a b c) "(a b c)\n"))
99
100 (pass-if "dotted list"
101 (prints? (a b . c) "(a b . c)\n"))
102
103 (pass-if "quote"
104 (prints? 'foo "'foo\n"))
105
106 (pass-if "non-starting quote"
107 (prints? (foo 'bar) "(foo 'bar)\n"))
108
109 (pass-if "nested quote"
110 (prints? (''foo) "(''foo)\n"))
111
112 (pass-if "quasiquote & co."
113 (prints? (define foo `(bar ,(+ 2 3)))
114 "(define foo `(bar ,(+ 2 3)))\n"))
115
116 (pass-if "indent"
117 (prints? (9 (8 (7 (6 (5 (4 (3 (2 (1 (0 0))))))))))
118 (string-append
119 "(9\n"
120 " (8\n"
121 " (7\n"
122 " (6\n"
123 " (5\n"
124 " (4\n"
125 " (3\n"
126 " (2\n"
127 " (1\n"
128 " (0\n"
129 " 0))))))))))\n")
130 #:width 10))
131
132 (pass-if "prefix"
133 (prints? (9 (8 (7 (6 (5 (4 (3 (2 (1 (0 0))))))))))
134 (string-append
135 "> (9\n"
136 "> (8\n"
137 "> (7\n"
138 "> (6\n"
139 "> (5\n"
140 "> (4\n"
141 "> (3\n"
142 "> (2\n"
143 "> (1\n"
144 "> (0\n"
145 "> 0))))))))))\n")
146 #:width 10
147 #:per-line-prefix "> ")))
148
149
150 (with-test-prefix "truncated-print"
151 (define exp '(a b #(c d e) f . g))
152
153 (define (tprint x width encoding)
154 (call-with-output-string
155 (lambda (p)
156 (set-port-encoding! p encoding)
157 (truncated-print x p #:width width))))
158
159 (pass-if-equal "(a b . #)"
160 (tprint exp 10 "ISO-8859-1"))
161
162 (pass-if-equal "(a b # f . g)"
163 (tprint exp 15 "ISO-8859-1"))
164
165 (pass-if-equal "(a b #(c ...) . #)"
166 (tprint exp 18 "ISO-8859-1"))
167
168 (pass-if-equal "(a b #(c d e) f . g)"
169 (tprint exp 20 "ISO-8859-1"))
170
171 (pass-if-equal "\"The quick brown...\""
172 (tprint "The quick brown fox" 20 "ISO-8859-1"))
173
174 (pass-if-equal "\"The quick brown f…\""
175 (tprint "The quick brown fox" 20 "UTF-8"))
176
177 (pass-if-equal "#<directory (tes...>"
178 (tprint (current-module) 20 "ISO-8859-1"))
179
180 (pass-if-equal "#<directory (test-…>"
181 (tprint (current-module) 20 "UTF-8"))
182
183 ;; bitvectors
184
185 (let ((testv (bitvector #t #f #f #t #t #f #t #t)))
186 (pass-if-equal "#*10011011"
187 (tprint testv 11 "UTF-8"))
188
189 (pass-if-equal "#*10011011"
190 (tprint testv 11 "ISO-8859-1"))
191
192 (pass-if-equal "#*10011…"
193 (tprint testv 8 "UTF-8"))
194
195 (pass-if-equal "#*100..."
196 (tprint testv 8 "ISO-8859-1"))
197
198 (pass-if-equal "#*10…"
199 (tprint testv 5 "UTF-8"))
200
201 (pass-if-equal "#*..."
202 (tprint testv 5 "ISO-8859-1"))
203
204 (pass-if-equal "#*1…"
205 (tprint testv 4 "UTF-8"))
206
207 (pass-if-equal "#"
208 (tprint testv 4 "ISO-8859-1")))
209
210 ;; rank 0 arrays
211
212 (pass-if-equal "#0(#)"
213 (tprint (make-typed-array #t 9.0) 6 "UTF-8"))
214
215 (pass-if-equal "#0(9.0)"
216 (tprint (make-typed-array #t 9.0) 7 "UTF-8"))
217
218 (pass-if-equal "#0f64(#)"
219 (tprint (make-typed-array 'f64 9.0) 8 "UTF-8"))
220
221 (pass-if-equal "#0f64(9.0)"
222 (tprint (make-typed-array 'f64 9.0) 10 "UTF-8"))
223
224 (pass-if-equal "#"
225 (tprint (make-typed-array 's32 0 20 20) 7 "UTF-8"))
226
227 ;; higher dimensional arrays
228
229 (let ((testa (make-typed-array 's32 0 20 20)))
230 (pass-if-equal "#2s32(…)"
231 (tprint testa 8 "UTF-8"))
232
233 (pass-if-equal "#2s32(# …)"
234 (tprint testa 10 "UTF-8"))
235
236 (pass-if-equal "#2s32((…) …)"
237 (tprint testa 12 "UTF-8"))
238
239 (pass-if-equal "#2s32((0 …) …)"
240 (tprint testa 14 "UTF-8")))
241
242 ;; check that bounds are printed correctly
243
244 (pass-if-equal "#2@-1@0((foo foo foo foo …) …)"
245 (tprint (make-array 'foo '(-1 3) 5) 30 "UTF-8"))
246
247 (pass-if-equal "#3@-1:5@0:0@0:5(() () () # #)"
248 (tprint (make-array 'foo '(-1 3) 0 5) 30 "UTF-8"))
249
250 ;; nested objects including arrays
251
252 (pass-if-equal "#2((#(9 9) #(9 9)) (#(9 9) #(9 9)))"
253 (tprint (make-typed-array #t (make-typed-array #t 9 2) 2 2) 40 "UTF-8"))
254
255 (pass-if-equal "#(#2((9 9) (9 9)) #2((9 9) (9 9)))"
256 (tprint (make-vector 2 (make-typed-array #t 9 2 2)) 40 "UTF-8"))
257
258 (pass-if-equal "(#2((9 9) (9 9)) #2((9 9) (9 9)))"
259 (tprint (make-list 2 (make-typed-array #t 9 2 2)) 40 "UTF-8"))
260
261 (pass-if-equal "(#0(9) #0(9))"
262 (tprint (make-list 2 (make-typed-array #t 9)) 20 "UTF-8"))
263
264 (pass-if-equal "(#0(9) #)"
265 (tprint (make-list 2 (make-typed-array #t 9)) 10 "UTF-8")))