Sha256: 0f0e160e6c524aae735aff7e156ab7be301a77b549f9d62827822bf9357eb709

Contents?: true

Size: 1.29 KB

Versions: 16

Compression:

Stored size: 1.29 KB

Contents

/*!
 * Should
 * Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
 * MIT Licensed
 */

module.exports = function(should, Assertion) {
  var i = should.format;

  Assertion.add('throw', function(message) {
    var fn = this.obj
      , err = {}
      , errorInfo = ''
      , ok = true;

    try {
      fn();
      ok = false;
    } catch(e) {
      err = e;
    }

    if(ok) {
      if('string' == typeof message) {
        ok = message == err.message;
      } else if(message instanceof RegExp) {
        ok = message.test(err.message);
      } else if('function' == typeof message) {
        ok = err instanceof message;
      }

      if(message && !ok) {
        if('string' == typeof message) {
          errorInfo = " with a message matching '" + message + "', but got '" + err.message + "'";
        } else if(message instanceof RegExp) {
          errorInfo = " with a message matching " + message + ", but got '" + err.message + "'";
        } else if('function' == typeof message) {
          errorInfo = " of type " + message.name + ", but got " + err.constructor.name;
        }
      } else {
        errorInfo = " (got " + i(err) + ")";
      }
    }

    this.params = { operator: 'to throw exception' + errorInfo };

    this.assert(ok);
  });

  Assertion.alias('throw', 'throwError');
};

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
stylus-source-0.42.2 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.42.1 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.42.0 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.41.3 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.41.2 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.41.1 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.41.0 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.40.3 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.40.2 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.40.1 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.40.0 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.39.4 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.39.3 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.39.2 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.39.1 vendor/node_modules/should/lib/ext/error.js
stylus-source-0.39.0 vendor/node_modules/should/lib/ext/error.js