aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/format/format_test.html
blob: b6f3b850b91780cea7ed886c4c241a415f1c19c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.format</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.format');
  goog.require('goog.string');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.style');
</script>
</head>
<body>
<script>

var propertyReplacer = new goog.testing.PropertyReplacer();

function tearDown() {
  // set wordBreakHtml back to the original value (some tests edit this member).
  propertyReplacer.reset();
}

function testFormatFileSize() {
  var fileSize = goog.format.fileSize;

  assertEquals('45', fileSize(45));
  assertEquals('45', fileSize(45, 0));
  assertEquals('45', fileSize(45, 1));
  assertEquals('45', fileSize(45, 3));
  assertEquals('454', fileSize(454));
  assertEquals('600', fileSize(600));

  assertEquals('1K', fileSize(1024));
  assertEquals('2K', fileSize(2 * 1024));
  assertEquals('5K', fileSize(5 * 1024));
  assertEquals('5.123K', fileSize(5.12345 * 1024, 3));
  assertEquals('5.68K', fileSize(5.678 * 1024, 2));

  assertEquals('1M', fileSize(1024 * 1024));
  assertEquals('1.5M', fileSize(1.5 * 1024 * 1024));
  assertEquals('2M', fileSize(1.5 * 1024 * 1024, 0));
  assertEquals('1.5M', fileSize(1.51 * 1024 * 1024, 1));
  assertEquals('1.56M', fileSize(1.56 * 1024 * 1024, 2));

  assertEquals('1G', fileSize(1024 * 1024 * 1024));
  assertEquals('6G', fileSize(6 * 1024 * 1024 * 1024));
  assertEquals('12.06T', fileSize(12345.6789 * 1024 * 1024 * 1024));
}

function testIsConvertableScaledNumber() {
  var isConvertableScaledNumber = goog.format.isConvertableScaledNumber;

  assertTrue(isConvertableScaledNumber('0'));
  assertTrue(isConvertableScaledNumber('45'));
  assertTrue(isConvertableScaledNumber('45K'));
  assertTrue(isConvertableScaledNumber('45MB'));
  assertTrue(isConvertableScaledNumber('45GB'));
  assertTrue(isConvertableScaledNumber('45T'));
  assertTrue(isConvertableScaledNumber('2.33P'));
  assertTrue(isConvertableScaledNumber('45m'));
  assertTrue(isConvertableScaledNumber('45u'));
  assertTrue(isConvertableScaledNumber('-5.0n'));

  assertFalse(isConvertableScaledNumber('45x'));
  assertFalse(isConvertableScaledNumber('ux'));
  assertFalse(isConvertableScaledNumber('K'));
}

function testNumericValueToString() {
  var numericValueToString = goog.format.numericValueToString;

  assertEquals('0', numericValueToString(0.0));
  assertEquals('45', numericValueToString(45));
  assertEquals('454', numericValueToString(454));
  assertEquals('600', numericValueToString(600));

  assertEquals('1.02K', numericValueToString(1024));
  assertEquals('2.05K', numericValueToString(2 * 1024));
  assertEquals('5.12K', numericValueToString(5 * 1024));
  assertEquals('5.246K', numericValueToString(5.12345 * 1024, 3));
  assertEquals('5.81K', numericValueToString(5.678 * 1024, 2));

  assertEquals('1.05M', numericValueToString(1024 * 1024));
  assertEquals('1.57M', numericValueToString(1.5 * 1024 * 1024));
  assertEquals('2M', numericValueToString(1.5 * 1024 * 1024, 0));
  assertEquals('1.6M', numericValueToString(1.51 * 1024 * 1024, 1));
  assertEquals('1.64M', numericValueToString(1.56 * 1024 * 1024, 2));

  assertEquals('1.07G', numericValueToString(1024 * 1024 * 1024));
  assertEquals('6.44G', numericValueToString(6 * 1024 * 1024 * 1024));
  assertEquals('13.26T', numericValueToString(12345.6789 * 1024 * 1024 * 1024));

  assertEquals('23.4m', numericValueToString(0.0234));
  assertEquals('1.23u', numericValueToString(0.00000123));
  assertEquals('15.78n', numericValueToString(0.000000015784));
  assertEquals('0.58u', numericValueToString(0.0000005784));
  assertEquals('0.5', numericValueToString(0.5));

  assertEquals('-45', numericValueToString(-45.3, 0));
  assertEquals('-45', numericValueToString(-45.5, 0));
  assertEquals('-46', numericValueToString(-45.51, 0));
}

function testFormatNumBytes() {
  var numBytesToString = goog.format.numBytesToString;

  assertEquals('45', numBytesToString(45));
  assertEquals('454', numBytesToString(454));

  assertEquals('5KB', numBytesToString(5 * 1024));
  assertEquals('1MB', numBytesToString(1024 * 1024));
  assertEquals('6GB', numBytesToString(6 * 1024 * 1024 * 1024));
  assertEquals('12.06TB', numBytesToString(12345.6789 * 1024 * 1024 * 1024));
}

function testStringToNumeric() {
  var stringToNumericValue = goog.format.stringToNumericValue;
  var epsilon = Math.pow(10, -10);

  assertNaN(stringToNumericValue('foo'));

  assertEquals(45, stringToNumericValue('45'));
  assertEquals(-45, stringToNumericValue('-45'));
  assertEquals(-45, stringToNumericValue('-45'));
  assertEquals(454, stringToNumericValue('454'));

  assertEquals(5 * 1024, stringToNumericValue('5KB'));
  assertEquals(1024 * 1024, stringToNumericValue('1MB'));
  assertEquals(6 * 1024 * 1024 * 1024, stringToNumericValue('6GB'));
  assertEquals(13260110230978.56, stringToNumericValue('12.06TB'));

  assertEquals(5010, stringToNumericValue('5.01K'));
  assertEquals(5100000, stringToNumericValue('5.1M'));
  assertTrue(Math.abs(0.051 - stringToNumericValue('51.0m')) < epsilon);
  assertTrue(Math.abs(0.000051 - stringToNumericValue('51.0u')) < epsilon);
}

function testStringToNumBytes() {
  var stringToNumBytes = goog.format.stringToNumBytes;

  assertEquals(45, stringToNumBytes('45'));
  assertEquals(454, stringToNumBytes('454'));

  assertEquals(5 * 1024, stringToNumBytes('5K'));
  assertEquals(1024 * 1024, stringToNumBytes('1M'));
  assertEquals(6 * 1024 * 1024 * 1024, stringToNumBytes('6G'));
  assertEquals(13260110230978.56, stringToNumBytes('12.06T'));
}

function testInsertWordBreaks() {
  // HTML that gets inserted is browser dependent, ensure for the test it is
  // a constant - browser dependent HTML is for display purposes only.
  propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');

  var insertWordBreaks = goog.format.insertWordBreaks;

  assertEquals('abcdef', insertWordBreaks('abcdef', 10));
  assertEquals('ab<wbr>cd<wbr>ef', insertWordBreaks('abcdef', 2));
  assertEquals('a<wbr>b<wbr>c<wbr>d<wbr>e<wbr>f', insertWordBreaks('abcdef', 1));

  assertEquals('a&amp;b=<wbr>=fal<wbr>se', insertWordBreaks('a&amp;b==false', 4));
  assertEquals('&lt;&amp;&gt;&raquo;<wbr>&laquo;',
               insertWordBreaks('&lt;&amp;&gt;&raquo;&laquo;', 4));

  assertEquals('a<wbr>b<wbr>c d<wbr>e<wbr>f', insertWordBreaks('abc def', 1));
  assertEquals('ab<wbr>c de<wbr>f', insertWordBreaks('abc def', 2));
  assertEquals('abc def', insertWordBreaks('abc def', 3));
  assertEquals('abc def', insertWordBreaks('abc def', 4));

  assertEquals('a<b>cd</b>e<wbr>f', insertWordBreaks('a<b>cd</b>ef', 4));
  assertEquals('Thi<wbr>s is a <a href="">lin<wbr>k</a>.',
               insertWordBreaks('This is a <a href="">link</a>.', 3));
  assertEquals('<abc a="&amp;&amp;&amp;&amp;&amp;">a<wbr>b',
      insertWordBreaks('<abc a="&amp;&amp;&amp;&amp;&amp;">ab', 1));

  assertEquals('ab\u0300<wbr>cd', insertWordBreaks('ab\u0300cd', 2));
  assertEquals('ab\u036F<wbr>cd', insertWordBreaks('ab\u036Fcd', 2));
  assertEquals('ab<wbr>\u0370c<wbr>d', insertWordBreaks('ab\u0370cd', 2));
  assertEquals('ab<wbr>\uFE1Fc<wbr>d', insertWordBreaks('ab\uFE1Fcd', 2));
  assertEquals('ab\u0300<wbr>c\u0301<wbr>de<wbr>f',
      insertWordBreaks('ab\u0300c\u0301def', 2));
}

function testInsertWordBreaksWithFormattingCharacters() {
  // HTML that gets inserted is browser dependent, ensure for the test it is
  // a constant - browser dependent HTML is for display purposes only.
  propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');
  var insertWordBreaks = goog.format.insertWordBreaks;

  // A date in Arabic-Indic digits with Right-to-Left Marks (U+200F).
  // The date is "11<RLM>/01<RLM>/2012".
  var textWithRLMs = 'This is a date - ' +
      '\u0661\u0661\u200f/\u0660\u0661\u200f/\u0662\u0660\u0661\u0662';
  // A string of 10 Xs with invisible formatting characters in between.
  // These characters are in the ranges U+200C to U+200F and U+202A to
  // U+202E, inclusive. See: http://unicode.org/charts/PDF/U2000.pdf
  var stringWithInvisibleFormatting = 'X\u200cX\u200dX\u200eX\u200fX\u202a' +
      'X\u202bX\u202cX\u202dX\u202eX';
  // A string formed by concatenating copies of the previous string alternating
  // with characters which behave like breaking spaces. Besides the space
  // character itself, the other characters are in the range U+2000 to U+200B
  // inclusive, except for the exclusion of U+2007 and inclusion of U+2029.
  // See: http://unicode.org/charts/PDF/U2000.pdf
  var stringWithInvisibleFormattingAndSpacelikeCharacters =
      stringWithInvisibleFormatting + ' ' +
      stringWithInvisibleFormatting + '\u2000' +
      stringWithInvisibleFormatting + '\u2001' +
      stringWithInvisibleFormatting + '\u2002' +
      stringWithInvisibleFormatting + '\u2003' +
      stringWithInvisibleFormatting + '\u2005' +
      stringWithInvisibleFormatting + '\u2006' +
      stringWithInvisibleFormatting + '\u2008' +
      stringWithInvisibleFormatting + '\u2009' +
      stringWithInvisibleFormatting + '\u200A' +
      stringWithInvisibleFormatting + '\u200B' +
      stringWithInvisibleFormatting + '\u2029' +
      stringWithInvisibleFormatting;

  // Test that the word break algorithm does not count RLMs towards word
  // length, and therefore does not insert word breaks into a typical date
  // written in Arabic-Indic digits with RTMs (b/5853915).
  assertEquals(textWithRLMs, insertWordBreaks(textWithRLMs, 10));

  // Test that invisible formatting characters are not counted towards word
  // length, and that characters which are treated as breaking spaces behave as
  // breaking spaces.
  assertEquals(stringWithInvisibleFormattingAndSpacelikeCharacters,
      insertWordBreaks(stringWithInvisibleFormattingAndSpacelikeCharacters,
      10));
}

function testInsertWordBreaksBasic() {
  // HTML that gets inserted is browser dependent, ensure for the test it is
  // a constant - browser dependent HTML is for display purposes only.
  propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');
  var insertWordBreaksBasic = goog.format.insertWordBreaksBasic;

  assertEquals('abcdef', insertWordBreaksBasic('abcdef', 10));
  assertEquals('ab<wbr>cd<wbr>ef', insertWordBreaksBasic('abcdef', 2));
  assertEquals('a<wbr>b<wbr>c<wbr>d<wbr>e<wbr>f', insertWordBreaksBasic('abcdef', 1));
  assertEquals('ab\u0300<wbr>c\u0301<wbr>de<wbr>f',
      insertWordBreaksBasic('ab\u0300c\u0301def', 2));

  assertEquals(
      'Inserting word breaks into the word "Russia" should work fine.',
      '\u0420\u043E<wbr>\u0441\u0441<wbr>\u0438\u044F',
      insertWordBreaksBasic('\u0420\u043E\u0441\u0441\u0438\u044F', 2));

  // The word 'Internet' in Hindi.
  var hindiInternet = '\u0907\u0902\u091F\u0930\u0928\u0947\u091F';
  assertEquals('The basic algorithm is not good enough to insert word ' +
      'breaks into Hindi.',
      hindiInternet, insertWordBreaksBasic(hindiInternet, 2));
  // The word 'Internet' in Hindi broken into slashes.
  assertEquals('Hindi can have word breaks inserted between slashes',
      hindiInternet + '<wbr>/' + hindiInternet + '<wbr>.' + hindiInternet,
      insertWordBreaksBasic(hindiInternet + '/' + hindiInternet + '.' + hindiInternet, 2));
}

function testWordBreaksWorking() {
  var text = goog.string.repeat('test', 20);
  var textWbr = goog.string.repeat('test' + goog.format.WORD_BREAK_HTML, 20);

  var overflowEl = goog.dom.createDom('div',
      {'style': 'width: 100px; overflow: hidden; margin 5px'});
  var wbrEl = goog.dom.createDom('div',
      {'style': 'width: 100px; overflow: hidden; margin-top: 15px'});
  goog.dom.appendChild(goog.global.document.body, overflowEl);
  goog.dom.appendChild(goog.global.document.body, wbrEl);

  overflowEl.innerHTML = text;
  wbrEl.innerHTML = textWbr;

  assertTrue('Text should overflow', overflowEl.scrollWidth > 100);
  assertTrue('Text should not overflow', wbrEl.scrollWidth <= 100);
}

function testWordBreaksRemovedFromTextContent() {
  var expectedText = goog.string.repeat('test', 20);
  var textWbr = goog.string.repeat('test' + goog.format.WORD_BREAK_HTML, 20);

  var wbrEl = goog.dom.createDom('div', null);
  wbrEl.innerHTML = textWbr;

  assertEquals('text content should have wbr character removed', expectedText,
      goog.dom.getTextContent(wbrEl));

}


</script>
</body>
</html>