Debian Codegraph @

closure-library/closure/goog/format/format_test.js

raw
1 // Copyright 2006 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.formatTest');
16 goog.setTestOnly('goog.formatTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.format');
21 goog.require('goog.string');
22 goog.require('goog.testing.PropertyReplacer');
23 goog.require('goog.testing.jsunit');
24
25 var propertyReplacer = new goog.testing.PropertyReplacer();
26
27 function tearDown() {
28 // set wordBreakHtml back to the original value (some tests edit this member).
29 propertyReplacer.reset();
30 }
31
32 function testFormatFileSize() {
33 var fileSize = goog.format.fileSize;
34
35 assertEquals('45', fileSize(45));
36 assertEquals('45', fileSize(45, 0));
37 assertEquals('45', fileSize(45, 1));
38 assertEquals('45', fileSize(45, 3));
39 assertEquals('454', fileSize(454));
40 assertEquals('600', fileSize(600));
41
42 assertEquals('1K', fileSize(1024));
43 assertEquals('2K', fileSize(2 * 1024));
44 assertEquals('5K', fileSize(5 * 1024));
45 assertEquals('5.123K', fileSize(5.12345 * 1024, 3));
46 assertEquals('5.68K', fileSize(5.678 * 1024, 2));
47
48 assertEquals('1M', fileSize(1024 * 1024));
49 assertEquals('1.5M', fileSize(1.5 * 1024 * 1024));
50 assertEquals('2M', fileSize(1.5 * 1024 * 1024, 0));
51 assertEquals('1.5M', fileSize(1.51 * 1024 * 1024, 1));
52 assertEquals('1.56M', fileSize(1.56 * 1024 * 1024, 2));
53
54 assertEquals('1G', fileSize(1024 * 1024 * 1024));
55 assertEquals('6G', fileSize(6 * 1024 * 1024 * 1024));
56 assertEquals('12.06T', fileSize(12345.6789 * 1024 * 1024 * 1024));
57 }
58
59 function testIsConvertableScaledNumber() {
60 var isConvertableScaledNumber = goog.format.isConvertableScaledNumber;
61
62 assertTrue(isConvertableScaledNumber('0'));
63 assertTrue(isConvertableScaledNumber('45'));
64 assertTrue(isConvertableScaledNumber('45K'));
65 assertTrue(isConvertableScaledNumber('45MB'));
66 assertTrue(isConvertableScaledNumber('45GB'));
67 assertTrue(isConvertableScaledNumber('45T'));
68 assertTrue(isConvertableScaledNumber('2.33P'));
69 assertTrue(isConvertableScaledNumber('3.45E'));
70 assertTrue(isConvertableScaledNumber('5.33Z'));
71 assertTrue(isConvertableScaledNumber('7.22Y'));
72 assertTrue(isConvertableScaledNumber('45m'));
73 assertTrue(isConvertableScaledNumber('45u'));
74 assertTrue(isConvertableScaledNumber('-5.0n'));
75
76 assertFalse(isConvertableScaledNumber('45x'));
77 assertFalse(isConvertableScaledNumber('ux'));
78 assertFalse(isConvertableScaledNumber('K'));
79 }
80
81 function testNumericValueToString() {
82 var numericValueToString = goog.format.numericValueToString;
83
84 assertEquals('0', numericValueToString(0.0));
85 assertEquals('45', numericValueToString(45));
86 assertEquals('454', numericValueToString(454));
87 assertEquals('600', numericValueToString(600));
88
89 assertEquals('1.02K', numericValueToString(1024));
90 assertEquals('2.05K', numericValueToString(2 * 1024));
91 assertEquals('5.12K', numericValueToString(5 * 1024));
92 assertEquals('5.246K', numericValueToString(5.12345 * 1024, 3));
93 assertEquals('5.81K', numericValueToString(5.678 * 1024, 2));
94
95 assertEquals('1.05M', numericValueToString(1024 * 1024));
96 assertEquals('1.57M', numericValueToString(1.5 * 1024 * 1024));
97 assertEquals('2M', numericValueToString(1.5 * 1024 * 1024, 0));
98 assertEquals('1.6M', numericValueToString(1.51 * 1024 * 1024, 1));
99 assertEquals('1.64M', numericValueToString(1.56 * 1024 * 1024, 2));
100
101 assertEquals('1.07G', numericValueToString(1024 * 1024 * 1024));
102 assertEquals('6.44G', numericValueToString(6 * 1024 * 1024 * 1024));
103 assertEquals('13.26T', numericValueToString(12345.6789 * 1024 * 1024 * 1024));
104 assertEquals('50.67P', numericValueToString(45 * Math.pow(1024, 5)));
105 assertEquals('57.65E', numericValueToString(50 * Math.pow(1024, 6)));
106 assertEquals('77.33Z', numericValueToString(65.5 * Math.pow(1024, 7)));
107 assertEquals('97.56Y', numericValueToString(80.7 * Math.pow(1024, 8)));
108
109 assertEquals('23.4m', numericValueToString(0.0234));
110 assertEquals('1.23u', numericValueToString(0.00000123));
111 assertEquals('15.78n', numericValueToString(0.000000015784));
112 assertEquals('0.58u', numericValueToString(0.0000005784));
113 assertEquals('0.5', numericValueToString(0.5));
114
115 assertEquals('-45', numericValueToString(-45.3, 0));
116 assertEquals('-45', numericValueToString(-45.5, 0));
117 assertEquals('-46', numericValueToString(-45.51, 0));
118 }
119
120 function testFormatNumBytes() {
121 var numBytesToString = goog.format.numBytesToString;
122
123 assertEquals('45', numBytesToString(45));
124 assertEquals('454', numBytesToString(454));
125
126 assertEquals('5KB', numBytesToString(5 * 1024));
127 assertEquals('1MB', numBytesToString(1024 * 1024));
128 assertEquals('6GB', numBytesToString(6 * 1024 * 1024 * 1024));
129 assertEquals('12.06TB', numBytesToString(12345.6789 * 1024 * 1024 * 1024));
130 assertEquals('45PB', numBytesToString(45 * Math.pow(1024, 5)));
131 assertEquals('50EB', numBytesToString(50 * Math.pow(1024, 6)));
132 assertEquals('65.5ZB', numBytesToString(65.5 * Math.pow(1024, 7)));
133 assertEquals('80.7YB', numBytesToString(80.7 * Math.pow(1024, 8)));
134
135 assertEquals('454', numBytesToString(454, 2, true, true));
136 assertEquals('5 KB', numBytesToString(5 * 1024, 2, true, true));
137 }
138
139 function testStringToNumeric() {
140 var stringToNumericValue = goog.format.stringToNumericValue;
141 var epsilon = Math.pow(10, -10);
142
143 assertNaN(stringToNumericValue('foo'));
144
145 assertEquals(45, stringToNumericValue('45'));
146 assertEquals(-45, stringToNumericValue('-45'));
147 assertEquals(-45, stringToNumericValue('-45'));
148 assertEquals(454, stringToNumericValue('454'));
149
150 assertEquals(5 * 1024, stringToNumericValue('5KB'));
151 assertEquals(1024 * 1024, stringToNumericValue('1MB'));
152 assertEquals(6 * 1024 * 1024 * 1024, stringToNumericValue('6GB'));
153 assertEquals(13260110230978.56, stringToNumericValue('12.06TB'));
154
155 assertEquals(5010, stringToNumericValue('5.01K'));
156 assertEquals(5100000, stringToNumericValue('5.1M'));
157 assertTrue(Math.abs(0.051 - stringToNumericValue('51.0m')) < epsilon);
158 assertTrue(Math.abs(0.000051 - stringToNumericValue('51.0u')) < epsilon);
159 }
160
161 function testStringToNumBytes() {
162 var stringToNumBytes = goog.format.stringToNumBytes;
163 var epsilon = 0.1;
164
165 assertEquals(45, stringToNumBytes('45'));
166 assertEquals(454, stringToNumBytes('454'));
167
168 assertEquals(5 * 1024, stringToNumBytes('5K'));
169 assertEquals(1024 * 1024, stringToNumBytes('1M'));
170 assertEquals(6 * 1024 * 1024 * 1024, stringToNumBytes('6G'));
171 assertEquals(13260110230978.56, stringToNumBytes('12.06T'));
172 assertTrue(
173 Math.abs(3.191564163782621e+24 - stringToNumBytes('2.64Y')) < epsilon);
174 }
175
176 function testInsertWordBreaks() {
177 // HTML that gets inserted is browser dependent, ensure for the test it is
178 // a constant - browser dependent HTML is for display purposes only.
179 propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');
180
181 var insertWordBreaks = goog.format.insertWordBreaks;
182
183 assertEquals('abcdef', insertWordBreaks('abcdef', 10));
184 assertEquals('ab<wbr>cd<wbr>ef', insertWordBreaks('abcdef', 2));
185 assertEquals(
186 'a<wbr>b<wbr>c<wbr>d<wbr>e<wbr>f', insertWordBreaks('abcdef', 1));
187
188 assertEquals(
189 'a&amp;b=<wbr>=fal<wbr>se', insertWordBreaks('a&amp;b==false', 4));
190 assertEquals(
191 '&lt;&amp;&gt;&raquo;<wbr>&laquo;',
192 insertWordBreaks('&lt;&amp;&gt;&raquo;&laquo;', 4));
193
194 assertEquals('a<wbr>b<wbr>c d<wbr>e<wbr>f', insertWordBreaks('abc def', 1));
195 assertEquals('ab<wbr>c de<wbr>f', insertWordBreaks('abc def', 2));
196 assertEquals('abc def', insertWordBreaks('abc def', 3));
197 assertEquals('abc def', insertWordBreaks('abc def', 4));
198
199 assertEquals('a<b>cd</b>e<wbr>f', insertWordBreaks('a<b>cd</b>ef', 4));
200 assertEquals(
201 'Thi<wbr>s is a <a href="">lin<wbr>k</a>.',
202 insertWordBreaks('This is a <a href="">link</a>.', 3));
203 assertEquals(
204 '<abc a="&amp;&amp;&amp;&amp;&amp;">a<wbr>b',
205 insertWordBreaks('<abc a="&amp;&amp;&amp;&amp;&amp;">ab', 1));
206
207 assertEquals('ab\u0300<wbr>cd', insertWordBreaks('ab\u0300cd', 2));
208 assertEquals('ab\u036F<wbr>cd', insertWordBreaks('ab\u036Fcd', 2));
209 assertEquals('ab<wbr>\u0370c<wbr>d', insertWordBreaks('ab\u0370cd', 2));
210 assertEquals('ab<wbr>\uFE1Fc<wbr>d', insertWordBreaks('ab\uFE1Fcd', 2));
211 assertEquals(
212 'ab\u0300<wbr>c\u0301<wbr>de<wbr>f',
213 insertWordBreaks('ab\u0300c\u0301def', 2));
214 }
215
216 function testInsertWordBreaksWithFormattingCharacters() {
217 // HTML that gets inserted is browser dependent, ensure for the test it is
218 // a constant - browser dependent HTML is for display purposes only.
219 propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');
220 var insertWordBreaks = goog.format.insertWordBreaks;
221
222 // A date in Arabic-Indic digits with Right-to-Left Marks (U+200F).
223 // The date is "11<RLM>/01<RLM>/2012".
224 var textWithRLMs = 'This is a date - ' +
225 '\u0661\u0661\u200f/\u0660\u0661\u200f/\u0662\u0660\u0661\u0662';
226 // A string of 10 Xs with invisible formatting characters in between.
227 // These characters are in the ranges U+200C to U+200F and U+202A to
228 // U+202E, inclusive. See: http://unicode.org/charts/PDF/U2000.pdf
229 var stringWithInvisibleFormatting = 'X\u200cX\u200dX\u200eX\u200fX\u202a' +
230 'X\u202bX\u202cX\u202dX\u202eX';
231 // A string formed by concatenating copies of the previous string alternating
232 // with characters which behave like breaking spaces. Besides the space
233 // character itself, the other characters are in the range U+2000 to U+200B
234 // inclusive, except for the exclusion of U+2007 and inclusion of U+2029.
235 // See: http://unicode.org/charts/PDF/U2000.pdf
236 var stringWithInvisibleFormattingAndSpacelikeCharacters =
237 stringWithInvisibleFormatting + ' ' + stringWithInvisibleFormatting +
238 '\u2000' + stringWithInvisibleFormatting + '\u2001' +
239 stringWithInvisibleFormatting + '\u2002' + stringWithInvisibleFormatting +
240 '\u2003' + stringWithInvisibleFormatting + '\u2005' +
241 stringWithInvisibleFormatting + '\u2006' + stringWithInvisibleFormatting +
242 '\u2008' + stringWithInvisibleFormatting + '\u2009' +
243 stringWithInvisibleFormatting + '\u200A' + stringWithInvisibleFormatting +
244 '\u200B' + stringWithInvisibleFormatting + '\u2029' +
245 stringWithInvisibleFormatting;
246
247 // Test that the word break algorithm does not count RLMs towards word
248 // length, and therefore does not insert word breaks into a typical date
249 // written in Arabic-Indic digits with RTMs (b/5853915).
250 assertEquals(textWithRLMs, insertWordBreaks(textWithRLMs, 10));
251
252 // Test that invisible formatting characters are not counted towards word
253 // length, and that characters which are treated as breaking spaces behave as
254 // breaking spaces.
255 assertEquals(
256 stringWithInvisibleFormattingAndSpacelikeCharacters,
257 insertWordBreaks(
258 stringWithInvisibleFormattingAndSpacelikeCharacters, 10));
259 }
260
261 function testInsertWordBreaksBasic() {
262 // HTML that gets inserted is browser dependent, ensure for the test it is
263 // a constant - browser dependent HTML is for display purposes only.
264 propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');
265 var insertWordBreaksBasic = goog.format.insertWordBreaksBasic;
266
267 assertEquals('abcdef', insertWordBreaksBasic('abcdef', 10));
268 assertEquals('ab<wbr>cd<wbr>ef', insertWordBreaksBasic('abcdef', 2));
269 assertEquals(
270 'a<wbr>b<wbr>c<wbr>d<wbr>e<wbr>f', insertWordBreaksBasic('abcdef', 1));
271 assertEquals(
272 'ab\u0300<wbr>c\u0301<wbr>de<wbr>f',
273 insertWordBreaksBasic('ab\u0300c\u0301def', 2));
274
275 assertEquals(
276 'Inserting word breaks into the word "Russia" should work fine.',
277 '\u0420\u043E<wbr>\u0441\u0441<wbr>\u0438\u044F',
278 insertWordBreaksBasic('\u0420\u043E\u0441\u0441\u0438\u044F', 2));
279
280 // The word 'Internet' in Hindi.
281 var hindiInternet = '\u0907\u0902\u091F\u0930\u0928\u0947\u091F';
282 assertEquals(
283 'The basic algorithm is not good enough to insert word ' +
284 'breaks into Hindi.',
285 hindiInternet, insertWordBreaksBasic(hindiInternet, 2));
286 // The word 'Internet' in Hindi broken into slashes.
287 assertEquals(
288 'Hindi can have word breaks inserted between slashes',
289 hindiInternet + '<wbr>/' + hindiInternet + '<wbr>.' + hindiInternet,
290 insertWordBreaksBasic(
291 hindiInternet + '/' + hindiInternet + '.' + hindiInternet, 2));
292 }
293
294 function testWordBreaksWorking() {
295 var text = goog.string.repeat('test', 20);
296 var textWbr = goog.string.repeat('test' + goog.format.WORD_BREAK_HTML, 20);
297
298 var overflowEl = goog.dom.createDom(
299 goog.dom.TagName.DIV,
300 {'style': 'width: 100px; overflow: hidden; margin 5px'});
301 var wbrEl = goog.dom.createDom(
302 goog.dom.TagName.DIV,
303 {'style': 'width: 100px; overflow: hidden; margin-top: 15px'});
304 goog.dom.appendChild(goog.global.document.body, overflowEl);
305 goog.dom.appendChild(goog.global.document.body, wbrEl);
306
307 overflowEl.innerHTML = text;
308 wbrEl.innerHTML = textWbr;
309
310 assertTrue('Text should overflow', overflowEl.scrollWidth > 100);
311 assertTrue('Text should not overflow', wbrEl.scrollWidth <= 100);
312 }
313
314 function testWordBreaksRemovedFromTextContent() {
315 var expectedText = goog.string.repeat('test', 20);
316 var textWbr = goog.string.repeat('test' + goog.format.WORD_BREAK_HTML, 20);
317
318 var wbrEl = goog.dom.createDom(goog.dom.TagName.DIV, null);
319 wbrEl.innerHTML = textWbr;
320
321 assertEquals(
322 'text content should have wbr character removed', expectedText,
323 goog.dom.getTextContent(wbrEl));
324 }