OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Asserts that a given argument's value is undefined. | 6 * Asserts that a given argument's value is undefined. |
7 * @param {object} a The argument to check. | 7 * @param {object} a The argument to check. |
8 */ | 8 */ |
9 function assertUndefined(a) { | 9 function assertUndefined(a) { |
10 if (a !== undefined) { | 10 if (a !== undefined) { |
11 throw new Error('Assertion failed: expected undefined'); | 11 throw new Error('Assertion failed: expected undefined'); |
12 } | 12 } |
13 } | 13 } |
14 | 14 |
15 /** | 15 /** |
16 * Asserts that the argument is neither null nor undefined. | 16 * Asserts that the argument is neither null nor undefined. |
17 * @param {object} obj The argument to check. | 17 * @param {object} obj The argument to check. |
18 * @param {string=} opt_message Error message if the condition is not met. | 18 * @param {string=} opt_message Error message if the condition is not met. |
19 */ | 19 */ |
20 function assertNotNullNorUndefined(obj, opt_message) { | 20 function assertNotNullNorUndefined(obj, opt_message) { |
21 if (obj === undefined || obj === null) { | 21 if (obj === undefined || obj === null) { |
22 throw new Error('Can\'t be null or undefined: ' + (opt_message || '') + | 22 throw new Error( |
23 '\n' + 'Actual: ' + obj); | 23 'Can\'t be null or undefined: ' + (opt_message || '') + '\n' + |
| 24 'Actual: ' + obj); |
24 } | 25 } |
25 } | 26 } |
26 | 27 |
27 /** | 28 /** |
28 * Asserts that a given function call throws an exception. | 29 * Asserts that a given function call throws an exception. |
29 * @param {string} msg Message to print if exception not thrown. | 30 * @param {string} msg Message to print if exception not thrown. |
30 * @param {Function} fn The function to call. | 31 * @param {Function} fn The function to call. |
31 * @param {string} error The name of the exception we expect {@code fn} to | 32 * @param {string} error The name of the exception we expect {@code fn} to |
32 * throw. | 33 * throw. |
33 */ | 34 */ |
34 function assertException(msg, fn, error) { | 35 function assertException(msg, fn, error) { |
35 try { | 36 try { |
36 fn(); | 37 fn(); |
37 } catch (e) { | 38 } catch (e) { |
38 if (error && e.name != error) { | 39 if (error && e.name != error) { |
39 throw new Error('Expected to throw ' + error + ' but threw ' + e.name + | 40 throw new Error( |
40 ' - ' + msg); | 41 'Expected to throw ' + error + ' but threw ' + e.name + ' - ' + msg); |
41 } | 42 } |
42 return; | 43 return; |
43 } | 44 } |
44 | 45 |
45 throw new Error('Expected to throw exception ' + error + ' - ' + msg); | 46 throw new Error('Expected to throw exception ' + error + ' - ' + msg); |
46 } | 47 } |
47 | 48 |
48 /** | 49 /** |
49 * Asserts that two arrays of strings are equal. | 50 * Asserts that two arrays of strings are equal. |
50 * @param {Array<string>} array1 The expected array. | 51 * @param {Array<string>} array1 The expected array. |
51 * @param {Array<string>} array2 The test array. | 52 * @param {Array<string>} array2 The test array. |
52 */ | 53 */ |
53 function assertEqualStringArrays(array1, array2) { | 54 function assertEqualStringArrays(array1, array2) { |
54 var same = true; | 55 var same = true; |
55 if (array1.length != array2.length) { | 56 if (array1.length != array2.length) { |
56 same = false; | 57 same = false; |
57 } | 58 } |
58 for (var i = 0; i < Math.min(array1.length, array2.length); i++) { | 59 for (var i = 0; i < Math.min(array1.length, array2.length); i++) { |
59 if (array1[i].trim() != array2[i].trim()) { | 60 if (array1[i].trim() != array2[i].trim()) { |
60 same = false; | 61 same = false; |
61 } | 62 } |
62 } | 63 } |
63 if (!same) { | 64 if (!same) { |
64 throw new Error('Expected ' + JSON.stringify(array1) + | 65 throw new Error( |
65 ', got ' + JSON.stringify(array2)); | 66 'Expected ' + JSON.stringify(array1) + ', got ' + |
| 67 JSON.stringify(array2)); |
66 } | 68 } |
67 } | 69 } |
68 | 70 |
69 /** | 71 /** |
70 * Asserts that two objects have the same JSON serialization. | 72 * Asserts that two objects have the same JSON serialization. |
71 * @param {Object} expected The expected object. | 73 * @param {Object} expected The expected object. |
72 * @param {Object} actual The actual object. | 74 * @param {Object} actual The actual object. |
73 * @param {string=} opt_message Message used for errors. | 75 * @param {string=} opt_message Message used for errors. |
74 */ | 76 */ |
75 function assertEqualsJSON(expected, actual, opt_message) { | 77 function assertEqualsJSON(expected, actual, opt_message) { |
76 if (JSON.stringify(actual) !== JSON.stringify(expected)) { | 78 if (JSON.stringify(actual) !== JSON.stringify(expected)) { |
77 throw new Error((opt_message ? opt_message + '\n' : '') + | 79 throw new Error( |
78 'Expected ' + JSON.stringify(expected) + '\n' + | 80 (opt_message ? opt_message + '\n' : '') + 'Expected ' + |
| 81 JSON.stringify(expected) + '\n' + |
79 'Got ' + JSON.stringify(actual)); | 82 'Got ' + JSON.stringify(actual)); |
80 } | 83 } |
81 } | 84 } |
82 | 85 |
83 /** | 86 /** |
84 * Asserts that two ArrayBuffers have the same content. | 87 * Asserts that two ArrayBuffers have the same content. |
85 * @param {ArrayBuffer} arrayBufA The expected ArrayBuffer. | 88 * @param {ArrayBuffer} arrayBufA The expected ArrayBuffer. |
86 * @param {ArrayBuffer} arrayBufB The test ArrayBuffer. | 89 * @param {ArrayBuffer} arrayBufB The test ArrayBuffer. |
87 */ | 90 */ |
88 function assertArrayBuffersEquals(arrayBufA, arrayBufB) { | 91 function assertArrayBuffersEquals(arrayBufA, arrayBufB) { |
89 var view1 = new Uint8Array(arrayBufA); | 92 var view1 = new Uint8Array(arrayBufA); |
90 var view2 = new Uint8Array(arrayBufB); | 93 var view2 = new Uint8Array(arrayBufB); |
91 assertEquals(JSON.stringify(view1), JSON.stringify(view2)); | 94 assertEquals(JSON.stringify(view1), JSON.stringify(view2)); |
92 } | 95 } |
93 | 96 |
94 /** | 97 /** |
95 * Asserts that two Arrays have the same content. | 98 * Asserts that two Arrays have the same content. |
96 * @param {ArrayBuffer} arrayA The expected array. | 99 * @param {ArrayBuffer} arrayA The expected array. |
97 * @param {ArrayBuffer} arrayB The test array. | 100 * @param {ArrayBuffer} arrayB The test array. |
98 */ | 101 */ |
99 function assertArraysEquals(arrayA, arrayB) { | 102 function assertArraysEquals(arrayA, arrayB) { |
100 assertEquals(JSON.stringify(arrayA), JSON.stringify(arrayB)); | 103 assertEquals(JSON.stringify(arrayA), JSON.stringify(arrayB)); |
101 } | 104 } |
102 | 105 |
103 assertSame = assertEquals; | 106 assertSame = assertEquals; |
104 assertNotSame = assertNotEquals; | 107 assertNotSame = assertNotEquals; |
OLD | NEW |