Debian Codegraph @

closure-library/closure/goog/testing/asserts_test.js

raw
1 // Copyright 2007 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.testing.assertsTest');
16 goog.setTestOnly('goog.testing.assertsTest');
17
18 goog.require('goog.array');
19 goog.require('goog.dom');
20 goog.require('goog.iter.Iterator');
21 goog.require('goog.iter.StopIteration');
22 goog.require('goog.labs.userAgent.browser');
23 goog.require('goog.string');
24 goog.require('goog.structs.Map');
25 goog.require('goog.structs.Set');
26 goog.require('goog.testing.TestCase');
27 goog.require('goog.testing.asserts');
28 goog.require('goog.testing.jsunit');
29 goog.require('goog.userAgent');
30 goog.require('goog.userAgent.product');
31
32 function setUp() {
33 // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
34 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
35 }
36
37 function testAssertTrue() {
38 assertTrue(true);
39 assertTrue('Good assertion', true);
40 assertThrowsJsUnitException(function() {
41 assertTrue(false);
42 }, 'Call to assertTrue(boolean) with false');
43 assertThrowsJsUnitException(function() {
44 assertTrue('Should be true', false);
45 }, 'Should be true\nCall to assertTrue(boolean) with false');
46 assertThrowsJsUnitException(function() {
47 assertTrue(null);
48 }, 'Bad argument to assertTrue(boolean)');
49 assertThrowsJsUnitException(function() {
50 assertTrue(undefined);
51 }, 'Bad argument to assertTrue(boolean)');
52 }
53
54 function testAssertFalse() {
55 assertFalse(false);
56 assertFalse('Good assertion', false);
57 assertThrowsJsUnitException(function() {
58 assertFalse(true);
59 }, 'Call to assertFalse(boolean) with true');
60 assertThrowsJsUnitException(function() {
61 assertFalse('Should be false', true);
62 }, 'Should be false\nCall to assertFalse(boolean) with true');
63 assertThrowsJsUnitException(function() {
64 assertFalse(null);
65 }, 'Bad argument to assertFalse(boolean)');
66 assertThrowsJsUnitException(function() {
67 assertFalse(undefined);
68 }, 'Bad argument to assertFalse(boolean)');
69 }
70
71 function testAssertEqualsWithString() {
72 assertEquals('a', 'a');
73 assertEquals('Good assertion', 'a', 'a');
74 assertThrowsJsUnitException(function() {
75 assertEquals('a', 'b');
76 }, 'Expected <a> (String) but was <b> (String)');
77 assertThrowsJsUnitException(function() {
78 assertEquals('Bad assertion', 'a', 'b');
79 }, 'Bad assertion\nExpected <a> (String) but was <b> (String)');
80 }
81
82 function testAssertEqualsWithInteger() {
83 assertEquals(1, 1);
84 assertEquals('Good assertion', 1, 1);
85 assertThrowsJsUnitException(function() {
86 assertEquals(1, 2);
87 }, 'Expected <1> (Number) but was <2> (Number)');
88 assertThrowsJsUnitException(function() {
89 assertEquals('Bad assertion', 1, 2);
90 }, 'Bad assertion\nExpected <1> (Number) but was <2> (Number)');
91 }
92
93 function testAssertNotEquals() {
94 assertNotEquals('a', 'b');
95 assertNotEquals('a', 'a', 'b');
96 assertThrowsJsUnitException(function() {
97 assertNotEquals('a', 'a');
98 }, 'Expected not to be <a> (String)');
99 assertThrowsJsUnitException(function() {
100 assertNotEquals('a', 'a', 'a');
101 }, 'a\nExpected not to be <a> (String)');
102 }
103
104 function testAssertNull() {
105 assertNull(null);
106 assertNull('Good assertion', null);
107 assertThrowsJsUnitException(function() {
108 assertNull(true);
109 }, 'Expected <null> but was <true> (Boolean)');
110 assertThrowsJsUnitException(function() {
111 assertNull('Should be null', false);
112 }, 'Should be null\nExpected <null> but was <false> (Boolean)');
113 assertThrowsJsUnitException(function() {
114 assertNull(undefined);
115 }, 'Expected <null> but was <undefined>');
116 assertThrowsJsUnitException(function() {
117 assertNull(1);
118 }, 'Expected <null> but was <1> (Number)');
119 }
120
121 function testAssertNotNull() {
122 assertNotNull(true);
123 assertNotNull('Good assertion', true);
124 assertNotNull(false);
125 assertNotNull(undefined);
126 assertNotNull(1);
127 assertNotNull('a');
128 assertThrowsJsUnitException(function() {
129 assertNotNull(null);
130 }, 'Expected not to be <null>');
131 assertThrowsJsUnitException(function() {
132 assertNotNull('Should not be null', null);
133 }, 'Should not be null\nExpected not to be <null>');
134 }
135
136 function testAssertUndefined() {
137 assertUndefined(undefined);
138 assertUndefined('Good assertion', undefined);
139 assertThrowsJsUnitException(function() {
140 assertUndefined(true);
141 }, 'Expected <undefined> but was <true> (Boolean)');
142 assertThrowsJsUnitException(function() {
143 assertUndefined('Should be undefined', false);
144 }, 'Should be undefined\nExpected <undefined> but was <false> (Boolean)');
145 assertThrowsJsUnitException(function() {
146 assertUndefined(null);
147 }, 'Expected <undefined> but was <null>');
148 assertThrowsJsUnitException(function() {
149 assertUndefined(1);
150 }, 'Expected <undefined> but was <1> (Number)');
151 }
152
153 function testAssertNotUndefined() {
154 assertNotUndefined(true);
155 assertNotUndefined('Good assertion', true);
156 assertNotUndefined(false);
157 assertNotUndefined(null);
158 assertNotUndefined(1);
159 assertNotUndefined('a');
160 assertThrowsJsUnitException(function() {
161 assertNotUndefined(undefined);
162 }, 'Expected not to be <undefined>');
163 assertThrowsJsUnitException(function() {
164 assertNotUndefined('Should not be undefined', undefined);
165 }, 'Should not be undefined\nExpected not to be <undefined>');
166 }
167
168 function testAssertNotNullNorUndefined() {
169 assertNotNullNorUndefined(true);
170 assertNotNullNorUndefined('Good assertion', true);
171 assertNotNullNorUndefined(false);
172 assertNotNullNorUndefined(1);
173 assertNotNullNorUndefined(0);
174 assertNotNullNorUndefined('a');
175 assertThrowsJsUnitException(function() {
176 assertNotNullNorUndefined(undefined);
177 }, 'Expected not to be <undefined>');
178 assertThrowsJsUnitException(function() {
179 assertNotNullNorUndefined('Should not be undefined', undefined);
180 }, 'Should not be undefined\nExpected not to be <undefined>');
181 assertThrowsJsUnitException(function() {
182 assertNotNullNorUndefined(null);
183 }, 'Expected not to be <null>');
184 assertThrowsJsUnitException(function() {
185 assertNotNullNorUndefined('Should not be null', null);
186 }, 'Should not be null\nExpected not to be <null>');
187 }
188
189 function testAssertNonEmptyString() {
190 assertNonEmptyString('hello');
191 assertNonEmptyString('Good assertion', 'hello');
192 assertNonEmptyString('true');
193 assertNonEmptyString('false');
194 assertNonEmptyString('1');
195 assertNonEmptyString('null');
196 assertNonEmptyString('undefined');
197 assertNonEmptyString('\n');
198 assertNonEmptyString(' ');
199 assertThrowsJsUnitException(function() {
200 assertNonEmptyString('');
201 }, 'Expected non-empty string but was <> (String)');
202 assertThrowsJsUnitException(
203 function() { assertNonEmptyString('Should be non-empty string', ''); },
204 'Should be non-empty string\n' +
205 'Expected non-empty string but was <> (String)');
206 assertThrowsJsUnitException(function() {
207 assertNonEmptyString(true);
208 }, 'Expected non-empty string but was <true> (Boolean)');
209 assertThrowsJsUnitException(function() {
210 assertNonEmptyString(false);
211 }, 'Expected non-empty string but was <false> (Boolean)');
212 assertThrowsJsUnitException(function() {
213 assertNonEmptyString(1);
214 }, 'Expected non-empty string but was <1> (Number)');
215 assertThrowsJsUnitException(function() {
216 assertNonEmptyString(null);
217 }, 'Expected non-empty string but was <null>');
218 assertThrowsJsUnitException(function() {
219 assertNonEmptyString(undefined);
220 }, 'Expected non-empty string but was <undefined>');
221 assertThrowsJsUnitException(function() {
222 assertNonEmptyString(['hello']);
223 }, 'Expected non-empty string but was <hello> (Array)');
224 // Different browsers return different values/types in the failure message
225 // so don't bother checking if the message is exactly as expected.
226 assertThrowsJsUnitException(function() {
227 assertNonEmptyString(goog.dom.createTextNode('hello'));
228 });
229 }
230
231 function testAssertNaN() {
232 assertNaN(NaN);
233 assertNaN('Good assertion', NaN);
234 assertThrowsJsUnitException(function() { assertNaN(1); }, 'Expected NaN');
235 assertThrowsJsUnitException(function() {
236 assertNaN('Should be NaN', 1);
237 }, 'Should be NaN\nExpected NaN');
238 assertThrowsJsUnitException(function() { assertNaN(true); }, 'Expected NaN');
239 assertThrowsJsUnitException(function() { assertNaN(false); }, 'Expected NaN');
240 assertThrowsJsUnitException(function() { assertNaN(null); }, 'Expected NaN');
241 assertThrowsJsUnitException(function() { assertNaN(''); }, 'Expected NaN');
242
243 // TODO(user): These assertions fail. We should decide on the
244 // semantics of assertNaN
245 // assertThrowsJsUnitException(function() { assertNaN(undefined); },
246 // 'Expected NaN');
247 // assertThrowsJsUnitException(function() { assertNaN('a'); },
248 // 'Expected NaN');
249 }
250
251 function testAssertNotNaN() {
252 assertNotNaN(1);
253 assertNotNaN('Good assertion', 1);
254 assertNotNaN(true);
255 assertNotNaN(false);
256 assertNotNaN('');
257 assertNotNaN(null);
258
259 // TODO(user): These assertions fail. We should decide on the
260 // semantics of assertNotNaN
261 // assertNotNaN(undefined);
262 // assertNotNaN('a');
263
264 assertThrowsJsUnitException(function() {
265 assertNotNaN(Number.NaN);
266 }, 'Expected not NaN');
267 assertThrowsJsUnitException(function() {
268 assertNotNaN('Should not be NaN', Number.NaN);
269 }, 'Should not be NaN\nExpected not NaN');
270 }
271
272 function testAssertObjectEquals() {
273 var obj1 = [{'a': 'hello', 'b': 'world'}];
274 var obj2 = [{'a': 'hello', 'c': 'dear', 'b': 'world'}];
275
276 // Check with obj1 and obj2 as first and second arguments respectively.
277 assertThrows('Objects should not be equal', function() {
278 assertObjectEquals(obj1, obj2);
279 });
280
281 // Check with obj1 and obj2 as second and first arguments respectively.
282 assertThrows('Objects should not be equal', function() {
283 assertObjectEquals(obj2, obj1);
284 });
285
286 // Test if equal objects are considered equal.
287 var obj3 = [{'b': 'world', 'a': 'hello'}];
288 assertObjectEquals(obj1, obj3);
289 assertObjectEquals(obj3, obj1);
290
291 // Test with a case where one of the members has an undefined value.
292 var obj4 = [{'a': 'hello', 'b': undefined}];
293 var obj5 = [{'a': 'hello'}];
294
295 // Check with obj4 and obj5 as first and second arguments respectively.
296 assertThrows('Objects should not be equal', function() {
297 assertObjectEquals(obj4, obj5);
298 });
299
300 // Check with obj5 and obj4 as first and second arguments respectively.
301 assertThrows('Objects should not be equal', function() {
302 assertObjectEquals(obj5, obj4);
303 });
304 }
305
306 function testAssertObjectNotEquals() {
307 var obj1 = [{'a': 'hello', 'b': 'world'}];
308 var obj2 = [{'a': 'hello', 'c': 'dear', 'b': 'world'}];
309
310 // Check with obj1 and obj2 as first and second arguments respectively.
311 assertObjectNotEquals(obj1, obj2);
312
313 // Check with obj1 and obj2 as second and first arguments respectively.
314 assertObjectNotEquals(obj2, obj1);
315
316 // Test if equal objects are considered equal.
317 var obj3 = [{'b': 'world', 'a': 'hello'}];
318 var error = assertThrows('Objects should be equal', function() {
319 assertObjectNotEquals(obj1, obj3);
320 });
321 assertContains('Objects should not be equal', error.message);
322 error = assertThrows('Objects should be equal', function() {
323 assertObjectNotEquals(obj3, obj1);
324 });
325 assertContains('Objects should not be equal', error.message);
326
327 // Test with a case where one of the members has an undefined value.
328 var obj4 = [{'a': 'hello', 'b': undefined}];
329 var obj5 = [{'a': 'hello'}];
330
331 // Check with obj4 and obj5 as first and second arguments respectively.
332 assertObjectNotEquals(obj4, obj5);
333
334 // Check with obj5 and obj4 as first and second arguments respectively.
335 assertObjectNotEquals(obj5, obj4);
336
337 assertObjectNotEquals(new Map([['a', '1']]), new Map([['b', '1']]));
338 assertObjectNotEquals(new Set(['a', 'b']), new Set(['a']));
339 }
340
341 function testAssertObjectEquals2() {
342 // NOTE: (0 in [undefined]) is true on FF but false on IE.
343 // (0 in {'0': undefined}) is true on both.
344 // grrr.
345 assertObjectEquals('arrays should be equal', [undefined], [undefined]);
346 assertThrows('arrays should not be equal', function() {
347 assertObjectEquals([undefined, undefined], [undefined]);
348 });
349 assertThrows('arrays should not be equal', function() {
350 assertObjectEquals([undefined], [undefined, undefined]);
351 });
352 }
353
354 function testAssertObjectEquals3() {
355 // Check that objects that contain identical Map objects compare
356 // as equals. We can't do a negative test because on browsers that
357 // implement __iterator__ we can't check the values of the iterated
358 // properties.
359 var obj1 = [
360 {'a': 'hi', 'b': new goog.structs.Map('hola', 'amigo', 'como', 'estas?')},
361 14, 'yes', true
362 ];
363 var obj2 = [
364 {'a': 'hi', 'b': new goog.structs.Map('hola', 'amigo', 'como', 'estas?')},
365 14, 'yes', true
366 ];
367 assertObjectEquals('Objects should be equal', obj1, obj2);
368
369 var obj3 = {'a': [1, 2]};
370 var obj4 = {'a': [1, 2, 3]};
371 assertThrows('inner arrays should not be equal', function() {
372 assertObjectEquals(obj3, obj4);
373 });
374 assertThrows('inner arrays should not be equal', function() {
375 assertObjectEquals(obj4, obj3);
376 });
377 }
378
379 function testAssertObjectEqualsSet() {
380 // verify that Sets compare equal, when run in an environment that
381 // supports iterators
382 var set1 = new goog.structs.Set();
383 var set2 = new goog.structs.Set();
384
385 set1.add('a');
386 set1.add('b');
387 set1.add(13);
388
389 set2.add('a');
390 set2.add('b');
391 set2.add(13);
392
393 assertObjectEquals('sets should be equal', set1, set2);
394
395 set2.add('hey');
396 assertThrows('sets should not be equal', function() {
397 assertObjectEquals(set1, set2);
398 });
399 }
400
401 function testAssertObjectEqualsIterNoEquals() {
402 // an object with an iterator but no equals() and no map_ cannot
403 // be compared
404 function Thing() { this.what = []; }
405 Thing.prototype.add = function(n, v) { this.what.push(n + '@' + v); };
406 Thing.prototype.get = function(n) {
407 var m = new RegExp('^' + n + '@(.*)$', '');
408 for (var i = 0; i < this.what.length; ++i) {
409 var match = this.what[i].match(m);
410 if (match) {
411 return match[1];
412 }
413 }
414 return null;
415 };
416 Thing.prototype.__iterator__ = function() {
417 var iter = new goog.iter.Iterator;
418 iter.index = 0;
419 iter.thing = this;
420 iter.next = function() {
421 if (this.index < this.thing.what.length) {
422 return this.thing.what[this.index++].split('@')[0];
423 } else {
424 throw goog.iter.StopIteration;
425 }
426 };
427 return iter;
428 };
429
430 var thing1 = new Thing();
431 thing1.name = 'thing1';
432 var thing2 = new Thing();
433 thing2.name = 'thing2';
434 thing1.add('red', 'fish');
435 thing1.add('blue', 'fish');
436
437 thing2.add('red', 'fish');
438 thing2.add('blue', 'fish');
439
440 assertThrows('things should not be equal', function() {
441 assertObjectEquals(thing1, thing2);
442 });
443 }
444
445 function testAssertObjectEqualsWithDates() {
446 var date = new Date(2010, 0, 1);
447 var dateWithMilliseconds = new Date(2010, 0, 1, 0, 0, 0, 1);
448 assertObjectEquals(new Date(2010, 0, 1), date);
449 assertThrows(goog.partial(assertObjectEquals, date, dateWithMilliseconds));
450 }
451
452 function testAssertObjectEqualsSparseArrays() {
453 var arr1 = [, 2, , 4];
454 var arr2 = [1, 2, 3, 4, 5];
455
456 assertThrows('Sparse arrays should not be equal', function() {
457 assertObjectEquals(arr1, arr2);
458 });
459
460 assertThrows('Sparse arrays should not be equal', function() {
461 assertObjectEquals(arr2, arr1);
462 });
463 }
464
465 function testAssertObjectEqualsSparseArrays2() {
466 // On IE6-8, the expression "1 in [4,undefined]" evaluates to false,
467 // but true on other browsers. FML. This test verifies a regression
468 // where IE reported that arr4 was not equal to arr1 or arr2.
469 var arr1 = [1, , 3];
470 var arr2 = [1, undefined, 3];
471 var arr3 = goog.array.clone(arr1);
472 var arr4 = [];
473 arr4.push(1, undefined, 3);
474
475 // Assert that all those arrays are equivalent pairwise.
476 var arrays = [arr1, arr2, arr3, arr4];
477 for (var i = 0; i < arrays.length; i++) {
478 for (var j = 0; j < arrays.length; j++) {
479 assertArrayEquals(arrays[i], arrays[j]);
480 }
481 }
482 }
483
484 function testAssertObjectEqualsArraysWithExtraProps() {
485 var arr1 = [1];
486 var arr2 = [1];
487 arr2.foo = 3;
488
489 assertThrows('Aarrays should not be equal', function() {
490 assertObjectEquals(arr1, arr2);
491 });
492
493 assertThrows('Arrays should not be equal', function() {
494 assertObjectEquals(arr2, arr1);
495 });
496 }
497
498 function testAssertSameElementsOnArray() {
499 assertSameElements([1, 2], [2, 1]);
500 assertSameElements('Good assertion', [1, 2], [2, 1]);
501 assertSameElements('Good assertion with duplicates', [1, 1, 2], [2, 1, 1]);
502 assertThrowsJsUnitException(function() {
503 assertSameElements([1, 2], [1]);
504 }, 'Expected 2 elements: [1,2], got 1 elements: [1]');
505 assertThrowsJsUnitException(function() {
506 assertSameElements('Should match', [1, 2], [1]);
507 }, 'Should match\nExpected 2 elements: [1,2], got 1 elements: [1]');
508 assertThrowsJsUnitException(function() {
509 assertSameElements([1, 2], [1, 3]);
510 }, 'Expected [1,2], got [1,3]');
511 assertThrowsJsUnitException(function() {
512 assertSameElements('Should match', [1, 2], [1, 3]);
513 }, 'Should match\nExpected [1,2], got [1,3]');
514 assertThrowsJsUnitException(function() {
515 assertSameElements([1, 1, 2], [2, 2, 1]);
516 }, 'Expected [1,1,2], got [2,2,1]');
517 }
518
519 function testAssertSameElementsOnArrayLike() {
520 assertSameElements({0: 0, 1: 1, length: 2}, {length: 2, 1: 1, 0: 0});
521 assertThrowsJsUnitException(function() {
522 assertSameElements({0: 0, 1: 1, length: 2}, {0: 0, length: 1});
523 }, 'Expected 2 elements: [0,1], got 1 elements: [0]');
524 }
525
526 function testAssertSameElementsWithBadArguments() {
527 assertThrowsJsUnitException(
528 function() { assertSameElements([], new goog.structs.Set()); },
529 'Bad arguments to assertSameElements(opt_message, expected: ' +
530 'ArrayLike, actual: ArrayLike)\n' +
531 'Call to assertTrue(boolean) with false');
532 }
533
534 var implicitlyTrue = [true, 1, -1, ' ', 'string', Infinity, new Object()];
535
536 var implicitlyFalse = [false, 0, '', null, undefined, NaN];
537
538 function testAssertEvaluatesToTrue() {
539 assertEvaluatesToTrue(true);
540 assertEvaluatesToTrue('', true);
541 assertEvaluatesToTrue('Good assertion', true);
542 assertThrowsJsUnitException(function() {
543 assertEvaluatesToTrue(false);
544 }, 'Expected to evaluate to true');
545 assertThrowsJsUnitException(function() {
546 assertEvaluatesToTrue('Should be true', false);
547 }, 'Should be true\nExpected to evaluate to true');
548 for (var i = 0; i < implicitlyTrue.length; i++) {
549 assertEvaluatesToTrue(
550 String('Test ' + implicitlyTrue[i] + ' [' + i + ']'),
551 implicitlyTrue[i]);
552 }
553 for (var i = 0; i < implicitlyFalse.length; i++) {
554 assertThrowsJsUnitException(function() {
555 assertEvaluatesToTrue(implicitlyFalse[i]);
556 }, 'Expected to evaluate to true');
557 }
558 }
559
560 function testAssertEvaluatesToFalse() {
561 assertEvaluatesToFalse(false);
562 assertEvaluatesToFalse('Good assertion', false);
563 assertThrowsJsUnitException(function() {
564 assertEvaluatesToFalse(true);
565 }, 'Expected to evaluate to false');
566 assertThrowsJsUnitException(function() {
567 assertEvaluatesToFalse('Should be false', true);
568 }, 'Should be false\nExpected to evaluate to false');
569 for (var i = 0; i < implicitlyFalse.length; i++) {
570 assertEvaluatesToFalse(
571 String('Test ' + implicitlyFalse[i] + ' [' + i + ']'),
572 implicitlyFalse[i]);
573 }
574 for (var i = 0; i < implicitlyTrue.length; i++) {
575 assertThrowsJsUnitException(function() {
576 assertEvaluatesToFalse(implicitlyTrue[i]);
577 }, 'Expected to evaluate to false');
578 }
579 }
580
581 function testAssertHTMLEquals() {
582 // TODO
583 }
584
585 function testAssertHashEquals() {
586 assertHashEquals({a: 1, b: 2}, {b: 2, a: 1});
587 assertHashEquals('Good assertion', {a: 1, b: 2}, {b: 2, a: 1});
588 assertHashEquals({a: undefined}, {a: undefined});
589 // Missing key.
590 assertThrowsJsUnitException(function() {
591 assertHashEquals({a: 1, b: 2}, {a: 1});
592 }, 'Expected hash had key b that was not found');
593 assertThrowsJsUnitException(function() {
594 assertHashEquals('Should match', {a: 1, b: 2}, {a: 1});
595 }, 'Should match\nExpected hash had key b that was not found');
596 assertThrowsJsUnitException(function() {
597 assertHashEquals({a: undefined}, {});
598 }, 'Expected hash had key a that was not found');
599 // Not equal key.
600 assertThrowsJsUnitException(function() {
601 assertHashEquals({a: 1}, {a: 5});
602 }, 'Value for key a mismatch - expected = 1, actual = 5');
603 assertThrowsJsUnitException(function() {
604 assertHashEquals('Should match', {a: 1}, {a: 5});
605 }, 'Should match\nValue for key a mismatch - expected = 1, actual = 5');
606 assertThrowsJsUnitException(function() {
607 assertHashEquals({a: undefined}, {a: 1})
608 }, 'Value for key a mismatch - expected = undefined, actual = 1');
609 // Extra key.
610 assertThrowsJsUnitException(function() {
611 assertHashEquals({a: 1}, {a: 1, b: 1});
612 }, 'Actual hash had key b that was not expected');
613 assertThrowsJsUnitException(function() {
614 assertHashEquals('Should match', {a: 1}, {a: 1, b: 1});
615 }, 'Should match\nActual hash had key b that was not expected');
616 }
617
618 function testAssertRoughlyEquals() {
619 assertRoughlyEquals(1, 1, 0);
620 assertRoughlyEquals('Good assertion', 1, 1, 0);
621 assertRoughlyEquals(1, 1.1, 0.11);
622 assertRoughlyEquals(1.1, 1, 0.11);
623 assertThrowsJsUnitException(function() {
624 assertRoughlyEquals(1, 1.1, 0.05);
625 }, 'Expected 1, but got 1.1 which was more than 0.05 away');
626 assertThrowsJsUnitException(function() {
627 assertRoughlyEquals('Close enough', 1, 1.1, 0.05);
628 }, 'Close enough\nExpected 1, but got 1.1 which was more than 0.05 away');
629 }
630
631 function testAssertContains() {
632 var a = [1, 2, 3];
633 assertContains(1, [1, 2, 3]);
634 assertContains('Should contain', 1, [1, 2, 3]);
635 assertThrowsJsUnitException(function() {
636 assertContains(4, [1, 2, 3]);
637 }, 'Expected \'1,2,3\' to contain \'4\'');
638 assertThrowsJsUnitException(function() {
639 assertContains('Should contain', 4, [1, 2, 3]);
640 }, 'Should contain\nExpected \'1,2,3\' to contain \'4\'');
641 // assertContains uses ===.
642 var o = new Object();
643 assertContains(o, [o, 2, 3]);
644 assertThrowsJsUnitException(function() {
645 assertContains(o, [1, 2, 3]);
646 }, 'Expected \'1,2,3\' to contain \'[object Object]\'');
647 }
648
649 function testAssertNotContains() {
650 var a = [1, 2, 3];
651 assertNotContains(4, [1, 2, 3]);
652 assertNotContains('Should not contain', 4, [1, 2, 3]);
653 assertThrowsJsUnitException(function() {
654 assertNotContains(1, [1, 2, 3]);
655 }, 'Expected \'1,2,3\' not to contain \'1\'');
656 assertThrowsJsUnitException(function() {
657 assertNotContains('Should not contain', 1, [1, 2, 3]);
658 }, "Should not contain\nExpected '1,2,3' not to contain '1'");
659 // assertNotContains uses ===.
660 var o = new Object();
661 assertNotContains({}, [o, 2, 3]);
662 assertThrowsJsUnitException(function() {
663 assertNotContains(o, [o, 2, 3]);
664 }, 'Expected \'[object Object],2,3\' not to contain \'[object Object]\'');
665 }
666
667 function testAssertRegExp() {
668 var a = 'I like turtles';
669 assertRegExp(/turtles$/, a);
670 assertRegExp('turtles$', a);
671 assertRegExp('Expected subject to be about turtles', /turtles$/, a);
672 assertRegExp('Expected subject to be about turtles', 'turtles$', a);
673
674 var b = 'Hello';
675 assertThrowsJsUnitException(function() {
676 assertRegExp(/turtles$/, b);
677 }, 'Expected \'Hello\' to match RegExp /turtles$/');
678 assertThrowsJsUnitException(function() {
679 assertRegExp('turtles$', b);
680 }, 'Expected \'Hello\' to match RegExp /turtles$/');
681 }
682
683 function testAssertThrows() {
684 var failed = false;
685 try {
686 assertThrows('assertThrows should not pass with null param', null);
687 failed = true;
688 } catch (e) {
689 }
690 assertFalse('Fail should not get set to true', failed);
691
692 try {
693 assertThrows(
694 'assertThrows should not pass with undefined param', undefined);
695 failed = true;
696 } catch (e) {
697 }
698 assertFalse('Fail should not get set to true', failed);
699
700 try {
701 assertThrows('assertThrows should not pass with number param', 1);
702 failed = true;
703 } catch (e) {
704 }
705 assertFalse('Fail should not get set to true', failed);
706
707 try {
708 assertThrows('assertThrows should not pass with string param', 'string');
709 failed = true;
710 } catch (e) {
711 }
712 assertFalse('Fail should not get set to true', failed);
713
714 try {
715 assertThrows('assertThrows should not pass with object param', {});
716 failed = true;
717 } catch (e) {
718 }
719 assertFalse('Fail should not get set to true', failed);
720
721 try {
722 var error = assertThrows(
723 'valid function throws Error', function() { throw new Error('test'); });
724 } catch (e) {
725 fail('assertThrows incorrectly doesn\'t detect a thrown exception');
726 }
727 assertEquals('error message', 'test', error.message);
728
729 try {
730 var stringError = assertThrows(
731 'valid function throws string error',
732 function() { throw 'string error test'; });
733 } catch (e) {
734 fail('assertThrows doesn\'t detect a thrown string exception');
735 }
736 assertEquals('string error', 'string error test', stringError);
737 }
738
739 function testAssertThrowsThrowsIfJsUnitException() {
740 // TODO(b/25875505): We currently need this true for this part of the test.
741 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = true;
742
743 // Asserts that assertThrows will throw a JsUnitException if the method
744 // passed to assertThrows throws a JsUnitException of its own. assertThrows
745 // should not be used for catching JsUnitExceptions with
746 // "failOnUnreportedAsserts" enabled.
747 var e = assertThrowsJsUnitException(function() {
748 assertThrows(function() {
749 // We need to invalidate this exception so it's not flagged as a
750 // legitimate failure by the test framework. The only way to get at the
751 // exception thrown by assertTrue is to catch it so we can invalidate it.
752 // We then need to rethrow it so the surrounding assertThrows behaves as
753 // expected.
754 try {
755 assertTrue(false);
756 } catch (ex) {
757 goog.testing.TestCase.getActiveTestCase().invalidateAssertionException(
758 ex);
759 throw ex;
760 }
761 });
762 });
763 assertContains(
764 'Function passed to assertThrows caught a JsUnitException', e.message);
765
766 // TODO(b/25875505): Set back to false so the rest of the tests pass.
767 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
768 }
769
770 function testAssertThrowsJsUnitException() {
771 var error = assertThrowsJsUnitException(function() { assertTrue(false); });
772 assertEquals('Call to assertTrue(boolean) with false', error.message);
773
774 error = assertThrowsJsUnitException(function() {
775 assertThrowsJsUnitException(function() { throw new Error('fail'); });
776 });
777 assertEquals('Call to fail()\nExpected a JsUnitException', error.message);
778
779 error = assertThrowsJsUnitException(function() {
780 assertThrowsJsUnitException(goog.nullFunction);
781 });
782 assertEquals('Expected a failure', error.message);
783 }
784
785 function testAssertNotThrows() {
786 if (goog.userAgent.product.SAFARI) {
787 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
788 // suite running in a continuous build. Will investigate later.
789 return;
790 }
791
792 var failed = false;
793 try {
794 assertNotThrows('assertNotThrows should not pass with null param', null);
795 failed = true;
796 } catch (e) {
797 }
798 assertFalse('Fail should not get set to true', failed);
799
800 try {
801 assertNotThrows(
802 'assertNotThrows should not pass with undefined param', undefined);
803 failed = true;
804 } catch (e) {
805 }
806 assertFalse('Fail should not get set to true', failed);
807
808 try {
809 assertNotThrows('assertNotThrows should not pass with number param', 1);
810 failed = true;
811 } catch (e) {
812 }
813 assertFalse('Fail should not get set to true', failed);
814
815 try {
816 assertNotThrows(
817 'assertNotThrows should not pass with string param', 'string');
818 failed = true;
819 } catch (e) {
820 }
821 assertFalse('Fail should not get set to true', failed);
822
823
824 try {
825 assertNotThrows('assertNotThrows should not pass with object param', {});
826 failed = true;
827 } catch (e) {
828 }
829 assertFalse('Fail should not get set to true', failed);
830
831
832 var result;
833 try {
834 result =
835 assertNotThrows('valid function', function() { return 'some value'; });
836 } catch (e) {
837 // Shouldn't be here: throw exception.
838 fail('assertNotThrows returned failure on a valid function');
839 }
840 assertEquals(
841 'assertNotThrows should return the result of the function.', 'some value',
842 result);
843
844 var errorDescription = 'a test error exception';
845 try {
846 assertNotThrows('non valid error throwing function', function() {
847 throw new Error(errorDescription);
848 });
849 failed = true;
850 } catch (e) {
851 // Some browsers don't have a stack trace so expect to at least have the
852 // error description. For Gecko and IE7 not even that is included.
853 if (!goog.userAgent.GECKO &&
854 (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('8'))) {
855 assertContains(errorDescription, e.message);
856 }
857 }
858 assertFalse('assertNotThrows did not fail on a thrown exception', failed);
859 }
860
861 function testAssertArrayEquals() {
862 var a1 = [0, 1, 2];
863 var a2 = [0, 1, 2];
864 assertArrayEquals('Arrays should be equal', a1, a2);
865
866 assertThrows('Should have thrown because args are not arrays', function() {
867 assertArrayEquals(true, true);
868 });
869
870 a1 = [0, undefined, 2];
871 a2 = [0, , 2];
872 // The following test fails unexpectedly. The bug is tracked at
873 // http://code.google.com/p/closure-library/issues/detail?id=419
874 // assertThrows(
875 // 'assertArrayEquals distinguishes undefined items from sparse arrays',
876 // function() {
877 // assertArrayEquals(a1, a2);
878 // });
879
880 // For the record. This behavior will probably change in the future.
881 assertArrayEquals(
882 'Bug: sparse arrays and undefined items are not distinguished',
883 [0, undefined, 2], [0, , 2]);
884
885 assertThrows('The array elements should be compared with ===', function() {
886 assertArrayEquals([0], ['0']);
887 });
888
889 assertThrows('Arrays with different length should be different', function() {
890 assertArrayEquals([0, undefined], [0]);
891 });
892
893 a1 = [0];
894 a2 = [0];
895 a2[-1] = -1;
896 assertArrayEquals('Negative indexes are ignored', a1, a2);
897
898 a1 = [0];
899 a2 = [0];
900 a2['extra'] = 1;
901 assertArrayEquals(
902 'Extra properties are ignored. Use assertObjectEquals to compare them.',
903 a1, a2);
904
905 assertArrayEquals(
906 'An example where assertObjectEquals would fail in IE.', ['x'],
907 'x'.match(/x/g));
908 }
909
910 function testAssertObjectsEqualsDifferentArrays() {
911 assertThrows('Should have thrown because args are different', function() {
912 var a1 = ['className1'];
913 var a2 = ['className2'];
914 assertObjectEquals(a1, a2);
915 });
916 }
917
918 function testAssertObjectsEqualsNegativeArrayIndexes() {
919 var a1 = [0];
920 var a2 = [0];
921 a2[-1] = -1;
922 // The following test fails unexpectedly. The bug is tracked at
923 // http://code.google.com/p/closure-library/issues/detail?id=418
924 // assertThrows('assertObjectEquals compares negative indexes', function() {
925 // assertObjectEquals(a1, a2);
926 // });
927 }
928
929 function testAssertObjectsEqualsDifferentTypeSameToString() {
930 assertThrows('Should have thrown because args are different', function() {
931 var a1 = 'className1';
932 var a2 = ['className1'];
933 assertObjectEquals(a1, a2);
934 });
935
936 assertThrows('Should have thrown because args are different', function() {
937 var a1 = ['className1'];
938 var a2 = {'0': 'className1'};
939 assertObjectEquals(a1, a2);
940 });
941
942 assertThrows('Should have thrown because args are different', function() {
943 var a1 = ['className1'];
944 var a2 = [['className1']];
945 assertObjectEquals(a1, a2);
946 });
947 }
948
949 function testAssertObjectsRoughlyEquals() {
950 assertObjectRoughlyEquals({'a': 1}, {'a': 1.2}, 0.3);
951 assertThrowsJsUnitException(
952 function() { assertObjectRoughlyEquals({'a': 1}, {'a': 1.2}, 0.1); },
953 'Expected <[object Object]> (Object) but was <[object Object]> ' +
954 '(Object)\n a: Expected <1> (Number) but was <1.2> (Number) which ' +
955 'was more than 0.1 away');
956 }
957
958 function testAssertObjectRoughlyEqualsWithStrings() {
959 // Check that objects with string properties are compared properly.
960 var obj1 = {'description': [{'colName': 'x1'}]};
961 var obj2 = {'description': [{'colName': 'x2'}]};
962 assertThrowsJsUnitException(
963 function() {
964 assertObjectRoughlyEquals(obj1, obj2, 0.00001);
965 },
966 'Expected <[object Object]> (Object)' +
967 ' but was <[object Object]> (Object)' +
968 '\n description[0].colName: Expected String "x1" but got "x2"');
969 }
970
971 function testFindDifferences_equal() {
972 assertNull(goog.testing.asserts.findDifferences(true, true));
973 assertNull(goog.testing.asserts.findDifferences(null, null));
974 assertNull(goog.testing.asserts.findDifferences(undefined, undefined));
975 assertNull(goog.testing.asserts.findDifferences(1, 1));
976 assertNull(goog.testing.asserts.findDifferences([1, 'a'], [1, 'a']));
977 assertNull(
978 goog.testing.asserts.findDifferences([[1, 2], [3, 4]], [[1, 2], [3, 4]]));
979 assertNull(
980 goog.testing.asserts.findDifferences([{a: 1, b: 2}], [{b: 2, a: 1}]));
981 assertNull(goog.testing.asserts.findDifferences(null, null));
982 assertNull(goog.testing.asserts.findDifferences(undefined, undefined));
983 assertNull(goog.testing.asserts.findDifferences(
984 new Map([['a', 1], ['b', 2]]), new Map([['b', 2], ['a', 1]])));
985 assertNull(goog.testing.asserts.findDifferences(
986 new Set(['a', 'b']), new Set(['b', 'a'])));
987 }
988
989 function testFindDifferences_unequal() {
990 assertNotNull(goog.testing.asserts.findDifferences(true, false));
991 assertNotNull(
992 goog.testing.asserts.findDifferences([{a: 1, b: 2}], [{a: 2, b: 1}]));
993 assertNotNull(
994 goog.testing.asserts.findDifferences([{a: 1}], [{a: 1, b: [2]}]));
995 assertNotNull(
996 goog.testing.asserts.findDifferences([{a: 1, b: [2]}], [{a: 1}]));
997
998 assertNotNull(
999 'Second map is missing key "a"; first map is missing key "b"',
1000 goog.testing.asserts.findDifferences(
1001 new Map([['a', 1]]), new Map([['b', 2]])));
1002 assertNotNull(
1003 'Value for key "a" differs by value',
1004 goog.testing.asserts.findDifferences(
1005 new Map([['a', '1']]), new Map([['a', '2']])));
1006 assertNotNull(
1007 'Value for key "a" differs by type',
1008 goog.testing.asserts.findDifferences(
1009 new Map([['a', '1']]), new Map([['a', 1]])));
1010
1011 assertNotNull(
1012 'Second set is missing key "a"',
1013 goog.testing.asserts.findDifferences(
1014 new Set(['a', 'b']), new Set(['b'])));
1015 assertNotNull(
1016 'First set is missing key "b"',
1017 goog.testing.asserts.findDifferences(
1018 new Set(['a']), new Set(['a', 'b'])));
1019 assertNotNull(
1020 'Values have different types"',
1021 goog.testing.asserts.findDifferences(new Set(['1']), new Set([1])));
1022 }
1023
1024 function testFindDifferences_objectsAndNull() {
1025 assertNotNull(goog.testing.asserts.findDifferences({a: 1}, null));
1026 assertNotNull(goog.testing.asserts.findDifferences(null, {a: 1}));
1027 assertNotNull(goog.testing.asserts.findDifferences(null, []));
1028 assertNotNull(goog.testing.asserts.findDifferences([], null));
1029 assertNotNull(goog.testing.asserts.findDifferences([], undefined));
1030 }
1031
1032 function testFindDifferences_basicCycle() {
1033 var a = {};
1034 var b = {};
1035 a.self = a;
1036 b.self = b;
1037 assertNull(goog.testing.asserts.findDifferences(a, b));
1038
1039 a.unique = 1;
1040 assertNotNull(goog.testing.asserts.findDifferences(a, b));
1041 }
1042
1043 function testFindDifferences_crossedCycle() {
1044 var a = {};
1045 var b = {};
1046 a.self = b;
1047 b.self = a;
1048 assertNull(goog.testing.asserts.findDifferences(a, b));
1049
1050 a.unique = 1;
1051 assertNotNull(goog.testing.asserts.findDifferences(a, b));
1052 }
1053
1054 function testFindDifferences_asymmetricCycle() {
1055 var a = {};
1056 var b = {};
1057 var c = {};
1058 var d = {};
1059 var e = {};
1060 a.self = b;
1061 b.self = a;
1062 c.self = d;
1063 d.self = e;
1064 e.self = c;
1065 assertNotNull(goog.testing.asserts.findDifferences(a, c));
1066 }
1067
1068 function testFindDifferences_basicCycleArray() {
1069 var a = [];
1070 var b = [];
1071 a[0] = a;
1072 b[0] = b;
1073 assertNull(goog.testing.asserts.findDifferences(a, b));
1074
1075 a[1] = 1;
1076 assertNotNull(goog.testing.asserts.findDifferences(a, b));
1077 }
1078
1079 function testFindDifferences_crossedCycleArray() {
1080 var a = [];
1081 var b = [];
1082 a[0] = b;
1083 b[0] = a;
1084 assertNull(goog.testing.asserts.findDifferences(a, b));
1085
1086 a[1] = 1;
1087 assertNotNull(goog.testing.asserts.findDifferences(a, b));
1088 }
1089
1090 function testFindDifferences_asymmetricCycleArray() {
1091 var a = [];
1092 var b = [];
1093 var c = [];
1094 var d = [];
1095 var e = [];
1096 a[0] = b;
1097 b[0] = a;
1098 c[0] = d;
1099 d[0] = e;
1100 e[0] = c;
1101 assertNotNull(goog.testing.asserts.findDifferences(a, c));
1102 }
1103
1104 function testFindDifferences_multiCycles() {
1105 var a = {};
1106 a.cycle1 = a;
1107 a.test = {cycle2: a};
1108
1109 var b = {};
1110 b.cycle1 = b;
1111 b.test = {cycle2: b};
1112 assertNull(goog.testing.asserts.findDifferences(a, b));
1113 }
1114
1115 function testFindDifferences_binaryTree() {
1116 function createBinTree(depth, root) {
1117 if (depth == 0) {
1118 return {root: root};
1119 } else {
1120 var node = {};
1121 node.left = createBinTree(depth - 1, root || node);
1122 node.right = createBinTree(depth - 1, root || node);
1123 return node;
1124 }
1125 }
1126
1127 // TODO(gboyer,user): This test does not terminate with the current
1128 // algorithm. Can be enabled when (if) the algorithm is improved.
1129 // assertNull(goog.testing.asserts.findDifferences(
1130 // createBinTree(5, null), createBinTree(5, null)));
1131 assertNotNull(
1132 goog.testing.asserts.findDifferences(
1133 createBinTree(4, null), createBinTree(5, null)));
1134 }
1135
1136 function testStringForWindowIE() {
1137 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('8')) {
1138 // NOTE(user): This test sees of we are being affected by a JScript bug
1139 // in try/finally handling. This bug only affects the lowest try/finally
1140 // block in the stack. Calling this function via VBScript allows
1141 // us to run the test synchronously in an empty JS stack.
1142 window.execScript('stringForWindowIEHelper()', 'vbscript');
1143 assertEquals('<[object]> (Object)', window.stringForWindowIEResult);
1144 }
1145 }
1146
1147 function testStringSamePrefix() {
1148 assertThrowsJsUnitException(
1149 function() { assertEquals('abcdefghi', 'abcdefghx'); },
1150 'Expected <abcdefghi> (String) but was <abcdefghx> (String)\n' +
1151 'Difference was at position 8. Expected [...ghi] vs. actual [...ghx]');
1152 }
1153
1154 function testStringSameSuffix() {
1155 assertThrowsJsUnitException(
1156 function() { assertEquals('xbcdefghi', 'abcdefghi'); },
1157 'Expected <xbcdefghi> (String) but was <abcdefghi> (String)\n' +
1158 'Difference was at position 0. Expected [xbc...] vs. actual [abc...]');
1159 }
1160
1161 function testStringLongComparedValues() {
1162 assertThrowsJsUnitException(
1163 function() {
1164 assertEquals(
1165 'abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz',
1166 'abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz');
1167 },
1168 'Expected\n' +
1169 '<abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz> (String)\n' +
1170 'but was\n' +
1171 '<abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz> (String)\n' +
1172 'Difference was at position 40. Expected [...kkklmnopqrstuvwxyz] vs. actual [...kklmnopqrstuvwxyz]');
1173 }
1174
1175 function testStringLongDiff() {
1176 assertThrowsJsUnitException(
1177 function() {
1178 assertEquals(
1179 'abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz',
1180 'abc...xyz');
1181 },
1182 'Expected\n' +
1183 '<abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz> (String)\n' +
1184 'but was\n' +
1185 '<abc...xyz> (String)\n' +
1186 'Difference was at position 3. Expected\n' +
1187 '[...bcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxy...]\n' +
1188 'vs. actual\n' +
1189 '[...bc...xy...]');
1190 }
1191
1192 function testStringDissimilarShort() {
1193 assertThrowsJsUnitException(function() {
1194 assertEquals('x', 'y');
1195 }, 'Expected <x> (String) but was <y> (String)');
1196 }
1197
1198 function testStringDissimilarLong() {
1199 assertThrowsJsUnitException(function() {
1200 assertEquals('xxxxxxxxxx', 'yyyyyyyyyy');
1201 }, 'Expected <xxxxxxxxxx> (String) but was <yyyyyyyyyy> (String)');
1202 }
1203
1204 function testAssertElementsEquals() {
1205 assertElementsEquals([1, 2], [1, 2]);
1206 assertElementsEquals([1, 2], {0: 1, 1: 2, length: 2});
1207 assertElementsEquals('Good assertion', [1, 2], [1, 2]);
1208 assertThrowsJsUnitException(
1209 function() {
1210 assertElementsEquals('Message', [1, 2], [1]);
1211 },
1212 'length mismatch: Message\n' +
1213 'Expected <2> (Number) but was <1> (Number)');
1214 }
1215
1216 function testStackTrace() {
1217 try {
1218 assertTrue(false);
1219 } catch (e) {
1220 if (Error.captureStackTrace) {
1221 assertNotUndefined(e.stack);
1222 }
1223 if (e.stack) {
1224 var stack = e.stack;
1225 var stackTraceContainsTestName =
1226 goog.string.contains(stack, 'testStackTrace');
1227 if (!stackTraceContainsTestName &&
1228 goog.labs.userAgent.browser.isChrome()) {
1229 // Occasionally Chrome does not give us a full stack trace, making for
1230 // a flaky test. If we don't have the full stack trace, at least
1231 // check that we got the error string.
1232 // Filed a bug on Chromium here:
1233 // https://code.google.com/p/chromium/issues/detail?id=403029
1234 var expected = 'Call to assertTrue(boolean) with false';
1235 assertContains(expected, stack);
1236 return;
1237 }
1238
1239 assertTrue(
1240 'Expected the stack trace to contain string "testStackTrace"',
1241 stackTraceContainsTestName);
1242 }
1243 }
1244 }
1245
1246 function stringForWindowIEHelper() {
1247 window.stringForWindowIEResult = _displayStringForValue(window);
1248 }
1249
1250 function testDisplayStringForValue() {
1251 assertEquals('<hello> (String)', _displayStringForValue('hello'));
1252 assertEquals('<1> (Number)', _displayStringForValue(1));
1253 assertEquals('<null>', _displayStringForValue(null));
1254 assertEquals('<undefined>', _displayStringForValue(undefined));
1255 assertEquals(
1256 '<hello,,,,1> (Array)',
1257 _displayStringForValue(['hello', /* array hole */, undefined, null, 1]));
1258 }
1259
1260 function testDisplayStringForValue_exception() {
1261 assertEquals(
1262 '<toString failed: foo message> (Object)', _displayStringForValue({
1263 toString: function() {
1264 throw new Error('foo message');
1265 }
1266 }));
1267 }
1268
1269 function testDisplayStringForValue_cycle() {
1270 var cycle = ['cycle'];
1271 cycle.push(cycle);
1272 assertTrue(
1273 'Computing string should terminate and result in a reasonable length',
1274 _displayStringForValue(cycle).length < 1000);
1275 }