String ports use UTF-8; ignore %default-port-encoding.
[bpt/guile.git] / test-suite / tests / print.test
CommitLineData
c5e05a1c 1;;;; -*- coding: utf-8; mode: scheme; -*-
87a6a236 2;;;;
6dce942c 3;;;; Copyright (C) 2010, 2013 Free Software Foundation, Inc.
87a6a236
AW
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.
c5e05a1c 9;;;;
87a6a236
AW
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.
c5e05a1c 14;;;;
87a6a236
AW
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
5bae880e
LC
23(define-syntax prints?
24 ;; #t if EXP prints as RESULT.
25 (syntax-rules ()
26 ((_ exp result)
27 (string=? result
28 (with-output-to-string
29 (lambda ()
30 (pretty-print 'exp)))))))
31
32\f
33(with-test-prefix "pretty-print"
34
35 (pass-if "pair"
36 (prints? (a . b) "(a . b)\n"))
37
38 (pass-if "list"
39 (prints? (a b c) "(a b c)\n"))
40
41 (pass-if "dotted list"
42 (prints? (a b . c) "(a b . c)\n"))
43
44 (pass-if "quote"
45 (prints? 'foo "'foo\n"))
46
47 (pass-if "non-starting quote"
48 (prints? (foo 'bar) "(foo 'bar)\n"))
49
50 (pass-if "nested quote"
51 (prints? (''foo) "(''foo)\n"))
52
53 (pass-if "quasiquote & co."
54 (prints? (define foo `(bar ,(+ 2 3)))
55 "(define foo `(bar ,(+ 2 3)))\n")))
56
57\f
87a6a236
AW
58(with-test-prefix "truncated-print"
59 (define exp '(a b #(c d e) f . g))
c5e05a1c
LC
60
61 (define (tprint x width encoding)
6dce942c
MW
62 (call-with-output-string
63 (lambda (p)
64 (set-port-encoding! p encoding)
65 (truncated-print x p #:width width))))
c5e05a1c 66
6dce942c
MW
67 (pass-if-equal "(a b . #)"
68 (tprint exp 10 "ISO-8859-1"))
c5e05a1c 69
6dce942c
MW
70 (pass-if-equal "(a b # f . g)"
71 (tprint exp 15 "ISO-8859-1"))
c5e05a1c 72
6dce942c
MW
73 (pass-if-equal "(a b #(c ...) . #)"
74 (tprint exp 18 "ISO-8859-1"))
c5e05a1c 75
6dce942c
MW
76 (pass-if-equal "(a b #(c d e) f . g)"
77 (tprint exp 20 "ISO-8859-1"))
c5e05a1c 78
6dce942c
MW
79 (pass-if-equal "\"The quick brown...\""
80 (tprint "The quick brown fox" 20 "ISO-8859-1"))
c5e05a1c 81
6dce942c
MW
82 (pass-if-equal "\"The quick brown f…\""
83 (tprint "The quick brown fox" 20 "UTF-8"))
c5e05a1c 84
6dce942c
MW
85 (pass-if-equal "#<directory (tes...>"
86 (tprint (current-module) 20 "ISO-8859-1"))
c5e05a1c 87
6dce942c
MW
88 (pass-if-equal "#<directory (test-…>"
89 (tprint (current-module) 20 "UTF-8")))