pastebin

Paste Search Dynamic
Recent pastes
/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.  * Verify |condition| is truthy and return |condition| if so.
  11.  * @template T
  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|.
  17.  * @closurePrimitive {asserts.truthy}
  18.  */
  19. /* #export */ function assert(condition, opt_message) {
  20.   if (!condition) {
  21.     let message = 'Assertion failed';
  22.     if (opt_message) {
  23.       message = message + ': ' + opt_message;
  24.     }
  25.     const error = new Error(message);
  26.     const global = function() {
  27.       const thisOrSelf = this || self;
  28.       /** @type {boolean} */
  29.       thisOrSelf.traceAssertionsForTesting;
  30.       return thisOrSelf;
  31.     }();
  32.     if (global.traceAssertionsForTesting) {
  33.       console.warn(error.stack);
  34.     }
  35.     throw error;
  36.   }
  37.   return condition;
  38. }
  39.  
  40. /**
  41.  * Call this from places in the code that should never be reached.
  42.  *
  43.  * For example, handling all the values of enum with a switch() like this:
  44.  *
  45.  *   function getValueFromEnum(enum) {
  46.  *     switch (enum) {
  47.  *       case ENUM_FIRST_OF_TWO:
  48.  *         return first
  49.  *       case ENUM_LAST_OF_TWO:
  50.  *         return last;
  51.  *     }
  52.  *     assertNotReached();
  53.  *     return document;
  54.  *   }
  55.  *
  56.  * This code should only be hit in the case of serious programmer error or
  57.  * unexpected input.
  58.  *
  59.  * @param {string=} opt_message A message to show when this is hit.
  60.  * @closurePrimitive {asserts.fail}
  61.  */
  62. /* #export */ function assertNotReached(opt_message) {
  63.   assert(false, opt_message || 'Unreachable code hit');
  64. }
  65.  
  66. /**
  67.  * @param {*} value The value to check.
  68.  * @param {function(new: T, ...)} type A user-defined constructor.
  69.  * @param {string=} opt_message A message to show when this is hit.
  70.  * @return {T}
  71.  * @template T
  72.  */
  73. /* #export */ function assertInstanceof(value, type, opt_message) {
  74.   // We don't use assert immediately here so that we avoid constructing an error
  75.   // message if we don't have to.
  76.   if (!(value instanceof type)) {
  77.     assertNotReached(
  78.         opt_message ||
  79.         'Value ' + value + ' is not a[n] ' + (type.name || typeof type));
  80.   }
  81.   return value;
  82. }
Parsed in 0.007 seconds