7b38398ed0175179cfc6efaf510b489a73bbf591
[platform/framework/web/crosswalk.git] / src / ui / webui / resources / js / assert.js
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.
4
5 /**
6  * @fileoverview Assertion support.
7  */
8
9 /**
10  * Simple common assertion API
11  * @param {*} condition The condition to test.  Note that this may be used to
12  *     test whether a value is defined or not, and we don't want to force a
13  *     cast to Boolean.
14  * @param {string=} opt_message A message to use in any error.
15  */
16 function assert(condition, opt_message) {
17   'use strict';
18   if (!condition) {
19     var msg = 'Assertion failed';
20     if (opt_message)
21       msg = msg + ': ' + opt_message;
22     throw new Error(msg);
23   }
24 }
25
26 /**
27  * Call this from places in the code that should never be reached.
28  *
29  * For example, handling all the values of enum with a switch() like this:
30  *
31  *   function getValueFromEnum(enum) {
32  *     switch (enum) {
33  *       case ENUM_FIRST_OF_TWO:
34  *         return first
35  *       case ENUM_LAST_OF_TWO:
36  *         return last;
37  *     }
38  *     assertNotReached();
39  *     return document;
40  *   }
41  *
42  * This code should only be hit in the case of serious programmer error or
43  * unexpected input.
44  *
45  * @param {string=} opt_message A message to show when this is hit.
46  */
47 function assertNotReached(opt_message) {
48   throw new Error(opt_message || "Unreachable code hit");
49 }