Debian Codegraph @

closure-library/closure/goog/string/stringformat_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.string.formatTest');
16 goog.setTestOnly('goog.string.formatTest');
17
18 goog.require('goog.string.format');
19 goog.require('goog.testing.jsunit');
20
21 // The discussion on naming this functionality is going on.
22 var f = goog.string.format;
23
24 function testImmediateFormatSpecifier() {
25 assertEquals('Empty String', '', f(''));
26 assertEquals('Immediate Value', 'Immediate Value', f('Immediate Value'));
27 }
28
29 function testPercentSign() {
30 assertEquals('%', '%', f('%'));
31 assertEquals('%%', '%', f('%%'));
32 assertEquals('%%%', '%%', f('%%%'));
33 assertEquals('%%%%', '%%', f('%%%%'));
34
35 assertEquals('width of the percent sign ???', '%%', f('%345%%-67.987%'));
36 }
37
38 function testStringConversionSpecifier() {
39 assertEquals('%s', 'abc', f('%s', 'abc'));
40 assertEquals('%2s', 'abc', f('%2s', 'abc'));
41 assertEquals('%6s', ' abc', f('%6s', 'abc'));
42 assertEquals('%-6s', 'abc ', f('%-6s', 'abc'));
43 }
44
45 function testFloatConversionSpecifier() {
46 assertEquals('%f', '123', f('%f', 123));
47 assertEquals('%f', '0.1', f('%f', 0.1));
48 assertEquals('%f', '123.456', f('%f', 123.456));
49
50 // Precisions, paddings and other flags are handled on a flag to flag basis.
51 }
52
53 function testAliasedConversionSpecifiers() {
54 assertEquals('%i vs. %d', f('%i', 123), f('%d', 123));
55 assertEquals('%u vs. %d', f('%u', 123), f('%d', 123));
56 }
57
58 function testIntegerConversion() {
59 assertEquals('%d', '0', f('%d', 0));
60
61 assertEquals('%d', '123', f('%d', 123));
62 assertEquals('%d', '0', f('%d', 0.1));
63 assertEquals('%d', '0', f('%d', 0.9));
64 assertEquals('%d', '123', f('%d', 123.456));
65
66 assertEquals('%d', '-1', f('%d', -1));
67 assertEquals('%d', '0', f('%d', -0.1));
68 assertEquals('%d', '0', f('%d', -0.9));
69 assertEquals('%d', '-123', f('%d', -123.456));
70
71 // Precisions, paddings and other flags are handled on a flag to flag basis.
72 }
73
74 function testSpaceFlag() {
75 assertEquals('zero %+d ', ' 0', f('% d', 0));
76
77 assertEquals('positive % d ', ' 123', f('% d', 123));
78 assertEquals('negative % d ', '-123', f('% d', -123));
79
80 assertEquals('positive % 3d', ' 123', f('% 3d', 123));
81 assertEquals('negative % 3d', '-123', f('% 3d', -123));
82
83 assertEquals('positive % 4d', ' 123', f('% 4d', 123));
84 assertEquals('negative % 4d', '-123', f('% 4d', -123));
85
86 assertEquals('positive % 6d', ' 123', f('% 6d', 123));
87 assertEquals('negative % 6d', '- 123', f('% 6d', -123));
88
89 assertEquals('positive % f ', ' 123.456', f('% f', 123.456));
90 assertEquals('negative % f ', '-123.456', f('% f', -123.456));
91
92 assertEquals('positive % .2f ', ' 123.46', f('% .2f', 123.456));
93 assertEquals('negative % .2f ', '-123.46', f('% .2f', -123.456));
94
95 assertEquals('positive % 6.2f', ' 123.46', f('% 6.2f', 123.456));
96 assertEquals('negative % 6.2f', '-123.46', f('% 6.2f', -123.456));
97
98 assertEquals('positive % 7.2f', ' 123.46', f('% 7.2f', 123.456));
99 assertEquals('negative % 7.2f', '-123.46', f('% 7.2f', -123.456));
100
101 assertEquals('positive % 10.2f', ' 123.46', f('% 10.2f', 123.456));
102 assertEquals('negative % 10.2f', '- 123.46', f('% 10.2f', -123.456));
103
104 assertEquals('string % s ', 'abc', f('% s', 'abc'));
105 assertEquals('string % 3s', 'abc', f('% 3s', 'abc'));
106 assertEquals('string % 4s', ' abc', f('% 4s', 'abc'));
107 assertEquals('string % 6s', ' abc', f('% 6s', 'abc'));
108 }
109
110 function testPlusFlag() {
111 assertEquals('zero %+d ', '+0', f('%+d', 0));
112
113 assertEquals('positive %+d ', '+123', f('%+d', 123));
114 assertEquals('negative %+d ', '-123', f('%+d', -123));
115
116 assertEquals('positive %+3d', '+123', f('%+3d', 123));
117 assertEquals('negative %+3d', '-123', f('%+3d', -123));
118
119 assertEquals('positive %+4d', '+123', f('%+4d', 123));
120 assertEquals('negative %+4d', '-123', f('%+4d', -123));
121
122 assertEquals('positive %+6d', '+ 123', f('%+6d', 123));
123 assertEquals('negative %+6d', '- 123', f('%+6d', -123));
124
125 assertEquals('positive %+f ', '+123.456', f('%+f', 123.456));
126 assertEquals('negative %+f ', '-123.456', f('%+f', -123.456));
127
128 assertEquals('positive %+.2f ', '+123.46', f('%+.2f', 123.456));
129 assertEquals('negative %+.2f ', '-123.46', f('%+.2f', -123.456));
130
131 assertEquals('positive %+6.2f', '+123.46', f('%+6.2f', 123.456));
132 assertEquals('negative %+6.2f', '-123.46', f('%+6.2f', -123.456));
133
134 assertEquals('positive %+7.2f', '+123.46', f('%+7.2f', 123.456));
135 assertEquals('negative %+7.2f', '-123.46', f('%+7.2f', -123.456));
136
137 assertEquals('positive %+10.2f', '+ 123.46', f('%+10.2f', 123.456));
138 assertEquals('negative %+10.2f', '- 123.46', f('%+10.2f', -123.456));
139
140 assertEquals('string %+s ', 'abc', f('%+s', 'abc'));
141 assertEquals('string %+3s', 'abc', f('%+3s', 'abc'));
142 assertEquals('string %+4s', ' abc', f('%+4s', 'abc'));
143 assertEquals('string %+6s', ' abc', f('%+6s', 'abc'));
144 }
145
146 function testPrecision() {
147 assertEquals('%.5d', '0', f('%.5d', 0));
148
149 assertEquals('%d', '123', f('%d', 123.456));
150 assertEquals('%.2d', '123', f('%.2d', 123.456));
151
152 assertEquals('%f', '123.456', f('%f', 123.456));
153 assertEquals('%.2f', '123.46', f('%.2f', 123.456));
154
155 assertEquals('%.3f', '123.456', f('%.3f', 123.456));
156 assertEquals('%.6f', '123.456000', f('%.6f', 123.456));
157 assertEquals('%1.2f', '123.46', f('%1.2f', 123.456));
158 assertEquals('%7.2f', ' 123.46', f('%7.2f', 123.456));
159
160 assertEquals('%5.6f', '123.456000', f('%5.6f', 123.456));
161 assertEquals('%11.6f', ' 123.456000', f('%11.6f', 123.456));
162
163 assertEquals('%07.2f', '0123.46', f('%07.2f', 123.456));
164 assertEquals('%+7.2f', '+123.46', f('%+7.2f', 123.456));
165 }
166
167 function testZeroFlag() {
168 assertEquals('%0s', 'abc', f('%0s', 'abc'));
169 assertEquals('%02s', 'abc', f('%02s', 'abc'));
170 assertEquals('%06s', ' abc', f('%06s', 'abc'));
171
172 assertEquals('%0d', '123', f('%0d', 123));
173 assertEquals('%0d', '-123', f('%0d', -123));
174 assertEquals('%06d', '000123', f('%06d', 123));
175 assertEquals('%06d', '-00123', f('%06d', -123));
176 assertEquals('%010d', '0000000123', f('%010d', 123));
177 assertEquals('%010d', '-000000123', f('%010d', -123));
178 }
179
180 function testFlagMinus() {
181 assertEquals('%-s', 'abc', f('%-s', 'abc'));
182 assertEquals('%-2s', 'abc', f('%-2s', 'abc'));
183 assertEquals('%-6s', 'abc ', f('%-6s', 'abc'));
184
185 assertEquals('%-d', '123', f('%-d', 123));
186 assertEquals('%-d', '-123', f('%-d', -123));
187 assertEquals('%-2d', '123', f('%-2d', 123));
188 assertEquals('%-2d', '-123', f('%-2d', -123));
189 assertEquals('%-4d', '123 ', f('%-4d', 123));
190 assertEquals('%-4d', '-123', f('%-4d', -123));
191
192 assertEquals('%-d', '123', f('%-0d', 123));
193 assertEquals('%-d', '-123', f('%-0d', -123));
194 assertEquals('%-4d', '123 ', f('%-04d', 123));
195 assertEquals('%-4d', '-123', f('%-04d', -123));
196 }
197
198 function testExceptions() {
199 var e = assertThrows(goog.partial(f, '%f%f', 123.456));
200 assertEquals('[goog.string.format] Not enough arguments', e.message);
201
202 e = assertThrows(f);
203 assertEquals('[goog.string.format] Template required', e.message);
204 }
205
206 function testNonParticipatingGroupHandling() {
207 // Firefox supplies empty string instead of undefined for non-participating
208 // capture groups. This can trigger bad behavior if a demuxer only checks
209 // isNaN(val) and not also val == ''. Check for regressions.
210 var format = '%s %d %i %u';
211 var expected = '1 2 3 4';
212 // Good types
213 assertEquals(expected, goog.string.format(format, 1, '2', '3', '4'));
214 // Bad types
215 assertEquals(expected, goog.string.format(format, '1', 2, 3, 4));
216 }
217
218 function testMinusString() {
219 var format = '%0.1f%%';
220 var expected = '-0.7%';
221 assertEquals(expected, goog.string.format(format, '-0.723'));
222 }