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 * @param {T} condition A condition to check for truthiness. Note that this |
12 * test whether a value is defined or not, and we don't want to force a | 12 * may be used to test whether a value is defined or not, and we don't want |
13 * cast to Boolean. | 13 * to force a cast to Boolean. |
14 * @param {string=} opt_message A message to use in any error. | 14 * @param {string=} opt_message A message to show on failure. |
15 * @return {!T} A non-null |condition|. | |
16 * @template T | |
Dan Beam
2014/08/06 16:06:59
this didn't work :(
Vitaly Pavlenko
2014/08/07 18:18:13
Reverted.
| |
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 * Call this from places in the code that should never be reached. | 30 * Call this from places in the code that should never be reached. |
28 * | 31 * |
29 * For example, handling all the values of enum with a switch() like this: | 32 * For example, handling all the values of enum with a switch() like this: |
30 * | 33 * |
31 * function getValueFromEnum(enum) { | 34 * function getValueFromEnum(enum) { |
32 * switch (enum) { | 35 * switch (enum) { |
33 * case ENUM_FIRST_OF_TWO: | 36 * case ENUM_FIRST_OF_TWO: |
34 * return first | 37 * return first |
35 * case ENUM_LAST_OF_TWO: | 38 * case ENUM_LAST_OF_TWO: |
36 * return last; | 39 * return last; |
37 * } | 40 * } |
38 * assertNotReached(); | 41 * assertNotReached(); |
39 * return document; | 42 * return document; |
40 * } | 43 * } |
41 * | 44 * |
42 * Without serious programmer error or unexpected input, assertNotReached() shou ld | 45 * Without serious programmer error or unexpected input, assertNotReached() shou ld |
43 * never be hit. | 46 * never be hit. |
44 * | 47 * |
45 * @param {string=} opt_message A message to show when this is hit. | 48 * @param {string=} opt_message A message to show when this is hit. |
46 */ | 49 */ |
47 function assertNotReached(opt_message) { | 50 function assertNotReached(opt_message) { |
48 throw new Error(opt_message || "Unreachable code hit"); | 51 throw new Error(opt_message || "Unreachable code hit"); |
49 } | 52 } |
OLD | NEW |