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
3 // found in the LICENSE file.
6 * @fileoverview Assertion support.
10 * Verify |condition| is truthy and return |condition| if so.
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
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|.
18 function assert(condition
, opt_message
) {
21 var msg
= 'Assertion failed';
23 msg
= msg
+ ': ' + opt_message
;
30 * Call this from places in the code that should never be reached.
32 * For example, handling all the values of enum with a switch() like this:
34 * function getValueFromEnum(enum) {
36 * case ENUM_FIRST_OF_TWO:
38 * case ENUM_LAST_OF_TWO:
45 * This code should only be hit in the case of serious programmer error or
48 * @param {string=} opt_message A message to show when this is hit.
50 function assertNotReached(opt_message
) {
51 throw new Error(opt_message
|| 'Unreachable code hit');
55 * @param {*} value The value to check.
56 * @param {function(new: T, ...)} type A user-defined constructor.
57 * @param {string=} opt_message A message to show when this is hit.
61 function assertInstanceof(value
, type
, opt_message
) {
62 if (!(value
instanceof type
)) {
63 throw new Error(opt_message
||
64 value
+ ' is not a[n] ' + (type
.name
|| typeof type
));