1 ;;;; -*- coding: utf-8; mode: scheme; -*-
3 ;;;; Copyright (C) 2010, 2013, 2014 Free Software Foundation, Inc.
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.
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.
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
19 (define-module (test-suite test-print)
20 #:use-module (ice-9 pretty-print)
21 #:use-module (test-suite lib))
23 (define-syntax prints?
24 ;; #t if EXP prints as RESULT.
28 (with-output-to-string
30 (pretty-print 'exp)))))))
32 (define (with-print-options opts thunk)
33 (let ((saved-options (print-options)))
39 (print-options saved-options)))))
41 (define-syntax-rule (write-with-options opts x)
42 (with-print-options opts (lambda ()
43 (with-output-to-string
48 (with-test-prefix "write"
50 (with-test-prefix "r7rs-symbols"
52 (pass-if-equal "basic"
54 (write-with-options '(r7rs-symbols)
55 (string->symbol "foo bar")))
57 (pass-if-equal "escapes"
58 "|bar \\| backslash \\x5c; alarm \\a backspace \\b tab \\t newline \\n cr \\r null \\x0; del \\x7f;|"
63 "bar | backslash \\ alarm \a backspace \b tab \t newline \n cr \r null \0 del "
66 (pass-if-equal "brackets"
68 (write-with-options '(r7rs-symbols)
69 (string->symbol "()[]{}")))
71 (pass-if-equal "starts with bar"
73 (write-with-options '(r7rs-symbols)
74 (string->symbol "|foo")))
76 (pass-if-equal "ends with bar"
78 (write-with-options '(r7rs-symbols)
79 (string->symbol "foo|")))
81 (pass-if-equal "starts with backslash"
83 (write-with-options '(r7rs-symbols)
84 (string->symbol "\\foo")))
86 (pass-if-equal "ends with backslash"
88 (write-with-options '(r7rs-symbols)
89 (string->symbol "foo\\")))))
92 (with-test-prefix "pretty-print"
95 (prints? (a . b) "(a . b)\n"))
98 (prints? (a b c) "(a b c)\n"))
100 (pass-if "dotted list"
101 (prints? (a b . c) "(a b . c)\n"))
104 (prints? 'foo "'foo\n"))
106 (pass-if "non-starting quote"
107 (prints? (foo 'bar) "(foo 'bar)\n"))
109 (pass-if "nested quote"
110 (prints? (''foo) "(''foo)\n"))
112 (pass-if "quasiquote & co."
113 (prints? (define foo `(bar ,(+ 2 3)))
114 "(define foo `(bar ,(+ 2 3)))\n")))
117 (with-test-prefix "truncated-print"
118 (define exp '(a b #(c d e) f . g))
120 (define (tprint x width encoding)
121 (call-with-output-string
123 (set-port-encoding! p encoding)
124 (truncated-print x p #:width width))))
126 (pass-if-equal "(a b . #)"
127 (tprint exp 10 "ISO-8859-1"))
129 (pass-if-equal "(a b # f . g)"
130 (tprint exp 15 "ISO-8859-1"))
132 (pass-if-equal "(a b #(c ...) . #)"
133 (tprint exp 18 "ISO-8859-1"))
135 (pass-if-equal "(a b #(c d e) f . g)"
136 (tprint exp 20 "ISO-8859-1"))
138 (pass-if-equal "\"The quick brown...\""
139 (tprint "The quick brown fox" 20 "ISO-8859-1"))
141 (pass-if-equal "\"The quick brown f…\""
142 (tprint "The quick brown fox" 20 "UTF-8"))
144 (pass-if-equal "#<directory (tes...>"
145 (tprint (current-module) 20 "ISO-8859-1"))
147 (pass-if-equal "#<directory (test-…>"
148 (tprint (current-module) 20 "UTF-8")))