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 * Simple common assertion API | 10 * Verify |condition| is truthy and return |condition| if so. |
11 * @param {*} condition The condition to test. Note that this may be used to | 11 * @template {T} |
12 * test whether a value is defined or not, and we don't want to force a | 12 * @param {T} condition A condition to check for truthiness. Note that this |
13 * cast to Boolean. | 13 * may be used to test whether a value is defined or not, and we don't want |
14 * @param {string=} opt_message A message to use in any error. | 14 * to force a cast to Boolean. |
| 15 * @param {string=} opt_message A message to show on failure. |
| 16 * @return {!T} A non-null |condition|. |
15 */ | 17 */ |
16 function assert(condition, opt_message) { | 18 function assert(condition, opt_message) { |
17 'use strict'; | 19 'use strict'; |
18 if (!condition) { | 20 if (!condition) { |
19 var msg = 'Assertion failed'; | 21 var msg = 'Assertion failed'; |
20 if (opt_message) | 22 if (opt_message) |
21 msg = msg + ': ' + opt_message; | 23 msg = msg + ': ' + opt_message; |
22 throw new Error(msg); | 24 throw new Error(msg); |
23 } | 25 } |
| 26 return condition; |
24 } | 27 } |
25 | 28 |
26 /** | 29 /** |
27 * Insert a notReached() in cases where control flow should never hit. | 30 * Insert a notReached() in cases where control flow should never hit. |
28 * @param {string=} opt_message A message to show when this is hit. | 31 * @param {string=} opt_message A message to show when this is hit. |
29 */ | 32 */ |
30 function notReached(opt_message) { | 33 function notReached(opt_message) { |
31 throw new Error(opt_message || "Unreachable code hit"); | 34 throw new Error(opt_message || "Unreachable code hit"); |
32 } | 35 } |
OLD | NEW |