Chromium Code Reviews| 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. | |
| 17 * @param {object} a The argument to check. | |
|
David Tseng
2014/09/08 21:45:59
arg or obj?
| |
| 18 * @param {string=} opt_message Error message if the condition is not met. | |
| 19 */ | |
| 20 function assertNotNullNorUndefined(a, opt_message) { | |
| 21 if (a === undefined || a === null) { | |
| 22 throw new Error('Can\'t be null or undefined: ' + (opt_message || '') + | |
| 23 '\n' + 'Actual: ' + a); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 /** | |
| 16 * Asserts that a given function call throws an exception. | 28 * Asserts that a given function call throws an exception. |
| 17 * @param {string} msg Message to print if exception not thrown. | 29 * @param {string} msg Message to print if exception not thrown. |
| 18 * @param {Function} fn The function to call. | 30 * @param {Function} fn The function to call. |
| 19 * @param {string} error The name of the exception we expect {@code fn} to | 31 * @param {string} error The name of the exception we expect {@code fn} to |
| 20 * throw. | 32 * throw. |
| 21 */ | 33 */ |
| 22 function assertException(msg, fn, error) { | 34 function assertException(msg, fn, error) { |
| 23 try { | 35 try { |
| 24 fn(); | 36 fn(); |
| 25 } catch (e) { | 37 } catch (e) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 49 } | 61 } |
| 50 } | 62 } |
| 51 if (!same) { | 63 if (!same) { |
| 52 throw new Error('Expected ' + JSON.stringify(array1) + | 64 throw new Error('Expected ' + JSON.stringify(array1) + |
| 53 ', got ' + JSON.stringify(array2)); | 65 ', got ' + JSON.stringify(array2)); |
| 54 } | 66 } |
| 55 } | 67 } |
| 56 | 68 |
| 57 /** | 69 /** |
| 58 * Asserts that two objects have the same JSON serialization. | 70 * Asserts that two objects have the same JSON serialization. |
| 59 * @param {Object} obj1 The expected object. | 71 * @param {Object} expected The expected object. |
| 60 * @param {Object} obj2 The actual object. | 72 * @param {Object} actual The actual object. |
| 73 * @param {string=} opt_message Message used for errors. | |
| 61 */ | 74 */ |
| 62 function assertEqualsJSON(obj1, obj2) { | 75 function assertEqualsJSON(expected, actual, opt_message) { |
| 63 if (!eqJSON(obj1, obj2)) { | 76 if (JSON.stringify(actual) !== JSON.stringify(expected)) { |
| 64 throw new Error('Expected ' + JSON.stringify(obj1) + | 77 throw new Error((opt_message ? opt_message + '\n' : '') + |
| 65 ', got ' + JSON.stringify(obj2)); | 78 'Expected ' + JSON.stringify(expected) + '\n' + |
| 79 'Got ' + JSON.stringify(actual)); | |
| 66 } | 80 } |
| 67 } | 81 } |
| 68 | 82 |
| 69 assertSame = assertEquals; | 83 assertSame = assertEquals; |
| 70 assertNotSame = assertNotEquals; | 84 assertNotSame = assertNotEquals; |
| OLD | NEW |