OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 * @fileoverview Assertion support. | 6 * @fileoverview Assertion support. |
7 */ | 7 */ |
8 | 8 |
9 /** | 9 /** |
10 * Verify |condition| is truthy and return |condition| if so. | 10 * Verify |condition| is truthy and return |condition| if so. |
11 * @template T | 11 * @template T |
12 * @param {T} condition A condition to check for truthiness. Note that this | 12 * @param {T} condition A condition to check for truthiness. Note that this |
13 * may be used to test whether a value is defined or not, and we don't want | 13 * may be used to test whether a value is defined or not, and we don't want |
14 * to force a cast to Boolean. | 14 * to force a cast to Boolean. |
15 * @param {string=} opt_message A message to show on failure. | 15 * @param {string=} opt_message A message to show on failure. |
16 * @return {T} A non-null |condition|. | 16 * @return {T} A non-null |condition|. |
17 */ | 17 */ |
18 function assert(condition, opt_message) { | 18 function assert(condition, opt_message) { |
19 if (!condition) { | 19 if (!condition) { |
20 var message = 'Assertion failed'; | 20 var message = 'Assertion failed'; |
21 if (opt_message) | 21 if (opt_message) |
22 message = message + ': ' + opt_message; | 22 message = message + ': ' + opt_message; |
23 var error = new Error(message); | 23 var error = new Error(message); |
24 var global = function() { return this; }(); | 24 var global = function() { |
| 25 return this; |
| 26 }(); |
25 if (global.traceAssertionsForTesting) | 27 if (global.traceAssertionsForTesting) |
26 console.warn(error.stack); | 28 console.warn(error.stack); |
27 throw error; | 29 throw error; |
28 } | 30 } |
29 return condition; | 31 return condition; |
30 } | 32 } |
31 | 33 |
32 /** | 34 /** |
33 * Call this from places in the code that should never be reached. | 35 * Call this from places in the code that should never be reached. |
34 * | 36 * |
(...skipping 29 matching lines...) Expand all Loading... |
64 function assertInstanceof(value, type, opt_message) { | 66 function assertInstanceof(value, type, opt_message) { |
65 // We don't use assert immediately here so that we avoid constructing an error | 67 // We don't use assert immediately here so that we avoid constructing an error |
66 // message if we don't have to. | 68 // message if we don't have to. |
67 if (!(value instanceof type)) { | 69 if (!(value instanceof type)) { |
68 assertNotReached( | 70 assertNotReached( |
69 opt_message || | 71 opt_message || |
70 'Value ' + value + ' is not a[n] ' + (type.name || typeof type)); | 72 'Value ' + value + ' is not a[n] ' + (type.name || typeof type)); |
71 } | 73 } |
72 return value; | 74 return value; |
73 } | 75 } |
OLD | NEW |