Commit | Line | Data |
---|---|---|
025f75b4 MV |
1 | ;;;; format.test --- test suite for Guile's CL-ish format -*- scheme -*- |
2 | ;;;; Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de> --- June 2001 | |
3 | ;;;; | |
0ce22459 MW |
4 | ;;;; Copyright (C) 2001, 2003, 2004, 2006, 2010, 2011, 2012, |
5 | ;;;; 2014 Free Software Foundation, Inc. | |
1497e87a | 6 | ;;;; |
53befeb7 NJ |
7 | ;;;; This library is free software; you can redistribute it and/or |
8 | ;;;; modify it under the terms of the GNU Lesser General Public | |
9 | ;;;; License as published by the Free Software Foundation; either | |
10 | ;;;; version 3 of the License, or (at your option) any later version. | |
1497e87a | 11 | ;;;; |
53befeb7 | 12 | ;;;; This library is distributed in the hope that it will be useful, |
025f75b4 | 13 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
53befeb7 NJ |
14 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | ;;;; Lesser General Public License for more details. | |
1497e87a | 16 | ;;;; |
53befeb7 NJ |
17 | ;;;; You should have received a copy of the GNU Lesser General Public |
18 | ;;;; License along with this library; if not, write to the Free Software | |
19 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
025f75b4 | 20 | |
8ab3d8a0 KR |
21 | (define-module (test-format) |
22 | #:use-module (test-suite lib) | |
afd08fdf | 23 | #:use-module (ice-9 i18n) |
8ab3d8a0 KR |
24 | #:use-module (ice-9 format)) |
25 | ||
025f75b4 MV |
26 | |
27 | ;;; FORMAT Basic Output | |
28 | ||
29 | (with-test-prefix "format basic output" | |
1497e87a LC |
30 | (pass-if "default to Unicode-capable port" |
31 | ;; `(format #f ...)' should be able to deal with Unicode characters. | |
32 | (with-fluids ((%default-port-encoding "ISO-8859-1")) | |
33 | (let ((alpha (integer->char #x03b1))) ; GREEK SMALL LETTER ALPHA | |
34 | (= 1 (string-length (format #f (string alpha))))))) | |
35 | ||
025f75b4 | 36 | (pass-if "format ~% produces a new line" |
2ce77e6c | 37 | (string=? (format #f "~%") "\n")) |
025f75b4 | 38 | (pass-if "format ~& starts a fresh line" |
2ce77e6c | 39 | (string=? (format #f "~&abc~&~&") "abc\n")) |
025f75b4 MV |
40 | (pass-if "format ~& is stateless but works properly across outputs via port-column" |
41 | (string=? | |
42 | (with-output-to-string | |
43 | (lambda () | |
44 | (display "xyz") | |
45 | (format #t "~&abc") | |
46 | (format #f "~&") ; shall have no effect | |
47 | (format #t "~&~&"))) | |
6c61859f MV |
48 | "xyz\nabc\n")) |
49 | (pass-if "format ~F (format-out-substr) maintains the column correctly" | |
2ce77e6c | 50 | (= (string-length (format #f "~@F~20T" 1)) 20))) |
4fb31801 | 51 | |
d8b189d2 KR |
52 | ;;; |
53 | ;;; misc | |
54 | ;;; | |
55 | ||
56 | (with-test-prefix "format" | |
57 | ||
58 | ;; in guile 1.6.4 and earlier, excess arguments were an error, but this | |
59 | ;; changed to follow the common lisp spec | |
60 | (pass-if "excess arguments ignored A" | |
61 | (string=? (format #f "" 1 2 3 4) "")) | |
62 | (pass-if "excess arguments ignored B" | |
63 | (string=? (format #f "~a ~a" 1 2 3 4) "1 2"))) | |
64 | ||
992b375d KR |
65 | ;;; |
66 | ;;; ~d | |
67 | ;;; | |
68 | ||
69 | (with-test-prefix "~d decimal integer" | |
70 | ||
71 | (with-test-prefix "~@d" | |
72 | ||
73 | (pass-if "-1" | |
74 | (string=? (format #f "~@d" -1) "-1")) | |
d8b189d2 | 75 | |
992b375d KR |
76 | ;; in guile 1.6.4 and earlier, ~@d gave "0" but we think "+0" is what the |
77 | ;; common lisp spec intendes | |
78 | (pass-if "+0" | |
79 | (string=? (format #f "~@d" 0) "+0")) | |
80 | ||
81 | (pass-if "+1" | |
82 | (string=? (format #f "~@d" 1) "+1")))) | |
83 | ||
8ab3d8a0 KR |
84 | ;;; |
85 | ;;; ~f | |
86 | ;;; | |
87 | ||
88 | (with-test-prefix "~f fixed-point" | |
89 | ||
90 | (pass-if "1.5" | |
91 | (string=? "1.5" (format #f "~f" 1.5))) | |
92 | ||
625b43ac AW |
93 | (pass-if "3/2" |
94 | (string=? "1.5" (format #f "~f" 3/2))) | |
95 | ||
8ab3d8a0 KR |
96 | ;; in guile prior to 1.6.9 and 1.8.1, leading zeros were incorrectly |
97 | ;; stripped, moving the decimal point and giving "25.0" here | |
98 | (pass-if "string 02.5" | |
99 | (string=? "2.5" (format #f "~f" "02.5")))) | |
100 | ||
afd08fdf LC |
101 | ;;; |
102 | ;;; ~h | |
103 | ;;; | |
104 | ||
0ce22459 MW |
105 | (when (defined? 'setlocale) |
106 | (setlocale LC_ALL "C")) | |
107 | ||
afd08fdf LC |
108 | (with-test-prefix "~h localized number" |
109 | ||
110 | (pass-if "1234.5" | |
111 | (string=? (format #f "~h" 1234.5) "1234.5")) | |
112 | ||
113 | (pass-if "padding" | |
114 | (string=? (format #f "~6h" 123.2) " 123.2")) | |
115 | ||
116 | (pass-if "padchar" | |
117 | (string=? (format #f "~8,,'*h" 123.2) "***123.2")) | |
118 | ||
119 | (pass-if "decimals" | |
120 | (string=? (format #f "~,2h" 123.4567) | |
121 | "123.45")) | |
122 | ||
123 | (pass-if "locale" | |
124 | (string=? (format #f "~,3:h, ~a" 1234.5678 | |
125 | %global-locale "approximately") | |
126 | "1234.567, approximately"))) | |
127 | ||
4fb31801 KR |
128 | ;;; |
129 | ;;; ~{ | |
130 | ;;; | |
131 | ||
132 | (with-test-prefix "~{ iteration" | |
133 | ||
134 | ;; In Guile 1.6.4 and earlier, the maximum iterations parameter defaulted | |
135 | ;; to 100, but it's now like Common Lisp where the default is no limit | |
136 | (pass-if "no arbitrary iteration limit" | |
2ce77e6c | 137 | (= (string-length (format #f "~{~a~}" (make-list 200 #\b))) 200))) |