Initial commit
This commit is contained in:
+93
@@ -0,0 +1,93 @@
|
||||
/*!
|
||||
* chai
|
||||
* Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var used = []
|
||||
, exports = module.exports = {};
|
||||
|
||||
/*!
|
||||
* Chai version
|
||||
*/
|
||||
|
||||
exports.version = '3.5.0';
|
||||
|
||||
/*!
|
||||
* Assertion Error
|
||||
*/
|
||||
|
||||
exports.AssertionError = require('assertion-error');
|
||||
|
||||
/*!
|
||||
* Utils for plugins (not exported)
|
||||
*/
|
||||
|
||||
var util = require('./chai/utils');
|
||||
|
||||
/**
|
||||
* # .use(function)
|
||||
*
|
||||
* Provides a way to extend the internals of Chai
|
||||
*
|
||||
* @param {Function}
|
||||
* @returns {this} for chaining
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.use = function (fn) {
|
||||
if (!~used.indexOf(fn)) {
|
||||
fn(this, util);
|
||||
used.push(fn);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Utility Functions
|
||||
*/
|
||||
|
||||
exports.util = util;
|
||||
|
||||
/*!
|
||||
* Configuration
|
||||
*/
|
||||
|
||||
var config = require('./chai/config');
|
||||
exports.config = config;
|
||||
|
||||
/*!
|
||||
* Primary `Assertion` prototype
|
||||
*/
|
||||
|
||||
var assertion = require('./chai/assertion');
|
||||
exports.use(assertion);
|
||||
|
||||
/*!
|
||||
* Core Assertions
|
||||
*/
|
||||
|
||||
var core = require('./chai/core/assertions');
|
||||
exports.use(core);
|
||||
|
||||
/*!
|
||||
* Expect interface
|
||||
*/
|
||||
|
||||
var expect = require('./chai/interface/expect');
|
||||
exports.use(expect);
|
||||
|
||||
/*!
|
||||
* Should interface
|
||||
*/
|
||||
|
||||
var should = require('./chai/interface/should');
|
||||
exports.use(should);
|
||||
|
||||
/*!
|
||||
* Assert interface
|
||||
*/
|
||||
|
||||
var assert = require('./chai/interface/assert');
|
||||
exports.use(assert);
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
/*!
|
||||
* chai
|
||||
* http://chaijs.com
|
||||
* Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var config = require('./config');
|
||||
|
||||
module.exports = function (_chai, util) {
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var AssertionError = _chai.AssertionError
|
||||
, flag = util.flag;
|
||||
|
||||
/*!
|
||||
* Module export.
|
||||
*/
|
||||
|
||||
_chai.Assertion = Assertion;
|
||||
|
||||
/*!
|
||||
* Assertion Constructor
|
||||
*
|
||||
* Creates object for chaining.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function Assertion (obj, msg, stack) {
|
||||
flag(this, 'ssfi', stack || arguments.callee);
|
||||
flag(this, 'object', obj);
|
||||
flag(this, 'message', msg);
|
||||
}
|
||||
|
||||
Object.defineProperty(Assertion, 'includeStack', {
|
||||
get: function() {
|
||||
console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.');
|
||||
return config.includeStack;
|
||||
},
|
||||
set: function(value) {
|
||||
console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.');
|
||||
config.includeStack = value;
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(Assertion, 'showDiff', {
|
||||
get: function() {
|
||||
console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.');
|
||||
return config.showDiff;
|
||||
},
|
||||
set: function(value) {
|
||||
console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.');
|
||||
config.showDiff = value;
|
||||
}
|
||||
});
|
||||
|
||||
Assertion.addProperty = function (name, fn) {
|
||||
util.addProperty(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.addMethod = function (name, fn) {
|
||||
util.addMethod(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.addChainableMethod = function (name, fn, chainingBehavior) {
|
||||
util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
|
||||
};
|
||||
|
||||
Assertion.overwriteProperty = function (name, fn) {
|
||||
util.overwriteProperty(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.overwriteMethod = function (name, fn) {
|
||||
util.overwriteMethod(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.overwriteChainableMethod = function (name, fn, chainingBehavior) {
|
||||
util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior);
|
||||
};
|
||||
|
||||
/**
|
||||
* ### .assert(expression, message, negateMessage, expected, actual, showDiff)
|
||||
*
|
||||
* Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass.
|
||||
*
|
||||
* @name assert
|
||||
* @param {Philosophical} expression to be tested
|
||||
* @param {String|Function} message or function that returns message to display if expression fails
|
||||
* @param {String|Function} negatedMessage or function that returns negatedMessage to display if negated expression fails
|
||||
* @param {Mixed} expected value (remember to check for negation)
|
||||
* @param {Mixed} actual (optional) will default to `this.obj`
|
||||
* @param {Boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) {
|
||||
var ok = util.test(this, arguments);
|
||||
if (true !== showDiff) showDiff = false;
|
||||
if (true !== config.showDiff) showDiff = false;
|
||||
|
||||
if (!ok) {
|
||||
var msg = util.getMessage(this, arguments)
|
||||
, actual = util.getActual(this, arguments);
|
||||
throw new AssertionError(msg, {
|
||||
actual: actual
|
||||
, expected: expected
|
||||
, showDiff: showDiff
|
||||
}, (config.includeStack) ? this.assert : flag(this, 'ssfi'));
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ### ._obj
|
||||
*
|
||||
* Quick reference to stored `actual` value for plugin developers.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Object.defineProperty(Assertion.prototype, '_obj',
|
||||
{ get: function () {
|
||||
return flag(this, 'object');
|
||||
}
|
||||
, set: function (val) {
|
||||
flag(this, 'object', val);
|
||||
}
|
||||
});
|
||||
};
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* ### config.includeStack
|
||||
*
|
||||
* User configurable property, influences whether stack trace
|
||||
* is included in Assertion error message. Default of false
|
||||
* suppresses stack trace in the error message.
|
||||
*
|
||||
* chai.config.includeStack = true; // enable stack on error
|
||||
*
|
||||
* @param {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
includeStack: false,
|
||||
|
||||
/**
|
||||
* ### config.showDiff
|
||||
*
|
||||
* User configurable property, influences whether or not
|
||||
* the `showDiff` flag should be included in the thrown
|
||||
* AssertionErrors. `false` will always be `false`; `true`
|
||||
* will be true when the assertion has requested a diff
|
||||
* be shown.
|
||||
*
|
||||
* @param {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
showDiff: true,
|
||||
|
||||
/**
|
||||
* ### config.truncateThreshold
|
||||
*
|
||||
* User configurable property, sets length threshold for actual and
|
||||
* expected values in assertion errors. If this threshold is exceeded, for
|
||||
* example for large data structures, the value is replaced with something
|
||||
* like `[ Array(3) ]` or `{ Object (prop1, prop2) }`.
|
||||
*
|
||||
* Set it to zero if you want to disable truncating altogether.
|
||||
*
|
||||
* This is especially userful when doing assertions on arrays: having this
|
||||
* set to a reasonable large value makes the failure messages readily
|
||||
* inspectable.
|
||||
*
|
||||
* chai.config.truncateThreshold = 0; // disable truncating
|
||||
*
|
||||
* @param {Number}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
truncateThreshold: 40
|
||||
|
||||
};
|
||||
+1860
File diff suppressed because it is too large
Load Diff
+1645
File diff suppressed because it is too large
Load Diff
+34
@@ -0,0 +1,34 @@
|
||||
/*!
|
||||
* chai
|
||||
* Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
module.exports = function (chai, util) {
|
||||
chai.expect = function (val, message) {
|
||||
return new chai.Assertion(val, message);
|
||||
};
|
||||
|
||||
/**
|
||||
* ### .fail(actual, expected, [message], [operator])
|
||||
*
|
||||
* Throw a failure.
|
||||
*
|
||||
* @name fail
|
||||
* @param {Mixed} actual
|
||||
* @param {Mixed} expected
|
||||
* @param {String} message
|
||||
* @param {String} operator
|
||||
* @namespace Expect
|
||||
* @api public
|
||||
*/
|
||||
|
||||
chai.expect.fail = function (actual, expected, message, operator) {
|
||||
message = message || 'expect.fail()';
|
||||
throw new chai.AssertionError(message, {
|
||||
actual: actual
|
||||
, expected: expected
|
||||
, operator: operator
|
||||
}, chai.expect.fail);
|
||||
};
|
||||
};
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
/*!
|
||||
* chai
|
||||
* Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
module.exports = function (chai, util) {
|
||||
var Assertion = chai.Assertion;
|
||||
|
||||
function loadShould () {
|
||||
// explicitly define this method as function as to have it's name to include as `ssfi`
|
||||
function shouldGetter() {
|
||||
if (this instanceof String || this instanceof Number || this instanceof Boolean ) {
|
||||
return new Assertion(this.valueOf(), null, shouldGetter);
|
||||
}
|
||||
return new Assertion(this, null, shouldGetter);
|
||||
}
|
||||
function shouldSetter(value) {
|
||||
// See https://github.com/chaijs/chai/issues/86: this makes
|
||||
// `whatever.should = someValue` actually set `someValue`, which is
|
||||
// especially useful for `global.should = require('chai').should()`.
|
||||
//
|
||||
// Note that we have to use [[DefineProperty]] instead of [[Put]]
|
||||
// since otherwise we would trigger this very setter!
|
||||
Object.defineProperty(this, 'should', {
|
||||
value: value,
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true
|
||||
});
|
||||
}
|
||||
// modify Object.prototype to have `should`
|
||||
Object.defineProperty(Object.prototype, 'should', {
|
||||
set: shouldSetter
|
||||
, get: shouldGetter
|
||||
, configurable: true
|
||||
});
|
||||
|
||||
var should = {};
|
||||
|
||||
/**
|
||||
* ### .fail(actual, expected, [message], [operator])
|
||||
*
|
||||
* Throw a failure.
|
||||
*
|
||||
* @name fail
|
||||
* @param {Mixed} actual
|
||||
* @param {Mixed} expected
|
||||
* @param {String} message
|
||||
* @param {String} operator
|
||||
* @namespace Should
|
||||
* @api public
|
||||
*/
|
||||
|
||||
should.fail = function (actual, expected, message, operator) {
|
||||
message = message || 'should.fail()';
|
||||
throw new chai.AssertionError(message, {
|
||||
actual: actual
|
||||
, expected: expected
|
||||
, operator: operator
|
||||
}, should.fail);
|
||||
};
|
||||
|
||||
/**
|
||||
* ### .equal(actual, expected, [message])
|
||||
*
|
||||
* Asserts non-strict equality (`==`) of `actual` and `expected`.
|
||||
*
|
||||
* should.equal(3, '3', '== coerces values to strings');
|
||||
*
|
||||
* @name equal
|
||||
* @param {Mixed} actual
|
||||
* @param {Mixed} expected
|
||||
* @param {String} message
|
||||
* @namespace Should
|
||||
* @api public
|
||||
*/
|
||||
|
||||
should.equal = function (val1, val2, msg) {
|
||||
new Assertion(val1, msg).to.equal(val2);
|
||||
};
|
||||
|
||||
/**
|
||||
* ### .throw(function, [constructor/string/regexp], [string/regexp], [message])
|
||||
*
|
||||
* Asserts that `function` will throw an error that is an instance of
|
||||
* `constructor`, or alternately that it will throw an error with message
|
||||
* matching `regexp`.
|
||||
*
|
||||
* should.throw(fn, 'function throws a reference error');
|
||||
* should.throw(fn, /function throws a reference error/);
|
||||
* should.throw(fn, ReferenceError);
|
||||
* should.throw(fn, ReferenceError, 'function throws a reference error');
|
||||
* should.throw(fn, ReferenceError, /function throws a reference error/);
|
||||
*
|
||||
* @name throw
|
||||
* @alias Throw
|
||||
* @param {Function} function
|
||||
* @param {ErrorConstructor} constructor
|
||||
* @param {RegExp} regexp
|
||||
* @param {String} message
|
||||
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
|
||||
* @namespace Should
|
||||
* @api public
|
||||
*/
|
||||
|
||||
should.Throw = function (fn, errt, errs, msg) {
|
||||
new Assertion(fn, msg).to.Throw(errt, errs);
|
||||
};
|
||||
|
||||
/**
|
||||
* ### .exist
|
||||
*
|
||||
* Asserts that the target is neither `null` nor `undefined`.
|
||||
*
|
||||
* var foo = 'hi';
|
||||
*
|
||||
* should.exist(foo, 'foo exists');
|
||||
*
|
||||
* @name exist
|
||||
* @namespace Should
|
||||
* @api public
|
||||
*/
|
||||
|
||||
should.exist = function (val, msg) {
|
||||
new Assertion(val, msg).to.exist;
|
||||
}
|
||||
|
||||
// negation
|
||||
should.not = {}
|
||||
|
||||
/**
|
||||
* ### .not.equal(actual, expected, [message])
|
||||
*
|
||||
* Asserts non-strict inequality (`!=`) of `actual` and `expected`.
|
||||
*
|
||||
* should.not.equal(3, 4, 'these numbers are not equal');
|
||||
*
|
||||
* @name not.equal
|
||||
* @param {Mixed} actual
|
||||
* @param {Mixed} expected
|
||||
* @param {String} message
|
||||
* @namespace Should
|
||||
* @api public
|
||||
*/
|
||||
|
||||
should.not.equal = function (val1, val2, msg) {
|
||||
new Assertion(val1, msg).to.not.equal(val2);
|
||||
};
|
||||
|
||||
/**
|
||||
* ### .throw(function, [constructor/regexp], [message])
|
||||
*
|
||||
* Asserts that `function` will _not_ throw an error that is an instance of
|
||||
* `constructor`, or alternately that it will not throw an error with message
|
||||
* matching `regexp`.
|
||||
*
|
||||
* should.not.throw(fn, Error, 'function does not throw');
|
||||
*
|
||||
* @name not.throw
|
||||
* @alias not.Throw
|
||||
* @param {Function} function
|
||||
* @param {ErrorConstructor} constructor
|
||||
* @param {RegExp} regexp
|
||||
* @param {String} message
|
||||
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
|
||||
* @namespace Should
|
||||
* @api public
|
||||
*/
|
||||
|
||||
should.not.Throw = function (fn, errt, errs, msg) {
|
||||
new Assertion(fn, msg).to.not.Throw(errt, errs);
|
||||
};
|
||||
|
||||
/**
|
||||
* ### .not.exist
|
||||
*
|
||||
* Asserts that the target is neither `null` nor `undefined`.
|
||||
*
|
||||
* var bar = null;
|
||||
*
|
||||
* should.not.exist(bar, 'bar does not exist');
|
||||
*
|
||||
* @name not.exist
|
||||
* @namespace Should
|
||||
* @api public
|
||||
*/
|
||||
|
||||
should.not.exist = function (val, msg) {
|
||||
new Assertion(val, msg).to.not.exist;
|
||||
}
|
||||
|
||||
should['throw'] = should['Throw'];
|
||||
should.not['throw'] = should.not['Throw'];
|
||||
|
||||
return should;
|
||||
};
|
||||
|
||||
chai.should = loadShould;
|
||||
chai.Should = loadShould;
|
||||
};
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
/*!
|
||||
* Chai - addChainingMethod utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
var transferFlags = require('./transferFlags');
|
||||
var flag = require('./flag');
|
||||
var config = require('../config');
|
||||
|
||||
/*!
|
||||
* Module variables
|
||||
*/
|
||||
|
||||
// Check whether `__proto__` is supported
|
||||
var hasProtoSupport = '__proto__' in Object;
|
||||
|
||||
// Without `__proto__` support, this module will need to add properties to a function.
|
||||
// However, some Function.prototype methods cannot be overwritten,
|
||||
// and there seems no easy cross-platform way to detect them (@see chaijs/chai/issues/69).
|
||||
var excludeNames = /^(?:length|name|arguments|caller)$/;
|
||||
|
||||
// Cache `Function` properties
|
||||
var call = Function.prototype.call,
|
||||
apply = Function.prototype.apply;
|
||||
|
||||
/**
|
||||
* ### addChainableMethod (ctx, name, method, chainingBehavior)
|
||||
*
|
||||
* Adds a method to an object, such that the method can also be chained.
|
||||
*
|
||||
* utils.addChainableMethod(chai.Assertion.prototype, 'foo', function (str) {
|
||||
* var obj = utils.flag(this, 'object');
|
||||
* new chai.Assertion(obj).to.be.equal(str);
|
||||
* });
|
||||
*
|
||||
* Can also be accessed directly from `chai.Assertion`.
|
||||
*
|
||||
* chai.Assertion.addChainableMethod('foo', fn, chainingBehavior);
|
||||
*
|
||||
* The result can then be used as both a method assertion, executing both `method` and
|
||||
* `chainingBehavior`, or as a language chain, which only executes `chainingBehavior`.
|
||||
*
|
||||
* expect(fooStr).to.be.foo('bar');
|
||||
* expect(fooStr).to.be.foo.equal('foo');
|
||||
*
|
||||
* @param {Object} ctx object to which the method is added
|
||||
* @param {String} name of method to add
|
||||
* @param {Function} method function to be used for `name`, when called
|
||||
* @param {Function} chainingBehavior function to be called every time the property is accessed
|
||||
* @namespace Utils
|
||||
* @name addChainableMethod
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function (ctx, name, method, chainingBehavior) {
|
||||
if (typeof chainingBehavior !== 'function') {
|
||||
chainingBehavior = function () { };
|
||||
}
|
||||
|
||||
var chainableBehavior = {
|
||||
method: method
|
||||
, chainingBehavior: chainingBehavior
|
||||
};
|
||||
|
||||
// save the methods so we can overwrite them later, if we need to.
|
||||
if (!ctx.__methods) {
|
||||
ctx.__methods = {};
|
||||
}
|
||||
ctx.__methods[name] = chainableBehavior;
|
||||
|
||||
Object.defineProperty(ctx, name,
|
||||
{ get: function () {
|
||||
chainableBehavior.chainingBehavior.call(this);
|
||||
|
||||
var assert = function assert() {
|
||||
var old_ssfi = flag(this, 'ssfi');
|
||||
if (old_ssfi && config.includeStack === false)
|
||||
flag(this, 'ssfi', assert);
|
||||
var result = chainableBehavior.method.apply(this, arguments);
|
||||
return result === undefined ? this : result;
|
||||
};
|
||||
|
||||
// Use `__proto__` if available
|
||||
if (hasProtoSupport) {
|
||||
// Inherit all properties from the object by replacing the `Function` prototype
|
||||
var prototype = assert.__proto__ = Object.create(this);
|
||||
// Restore the `call` and `apply` methods from `Function`
|
||||
prototype.call = call;
|
||||
prototype.apply = apply;
|
||||
}
|
||||
// Otherwise, redefine all properties (slow!)
|
||||
else {
|
||||
var asserterNames = Object.getOwnPropertyNames(ctx);
|
||||
asserterNames.forEach(function (asserterName) {
|
||||
if (!excludeNames.test(asserterName)) {
|
||||
var pd = Object.getOwnPropertyDescriptor(ctx, asserterName);
|
||||
Object.defineProperty(assert, asserterName, pd);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
transferFlags(this, assert);
|
||||
return assert;
|
||||
}
|
||||
, configurable: true
|
||||
});
|
||||
};
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*!
|
||||
* Chai - addMethod utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var config = require('../config');
|
||||
|
||||
/**
|
||||
* ### .addMethod (ctx, name, method)
|
||||
*
|
||||
* Adds a method to the prototype of an object.
|
||||
*
|
||||
* utils.addMethod(chai.Assertion.prototype, 'foo', function (str) {
|
||||
* var obj = utils.flag(this, 'object');
|
||||
* new chai.Assertion(obj).to.be.equal(str);
|
||||
* });
|
||||
*
|
||||
* Can also be accessed directly from `chai.Assertion`.
|
||||
*
|
||||
* chai.Assertion.addMethod('foo', fn);
|
||||
*
|
||||
* Then can be used as any other assertion.
|
||||
*
|
||||
* expect(fooStr).to.be.foo('bar');
|
||||
*
|
||||
* @param {Object} ctx object to which the method is added
|
||||
* @param {String} name of method to add
|
||||
* @param {Function} method function to be used for name
|
||||
* @namespace Utils
|
||||
* @name addMethod
|
||||
* @api public
|
||||
*/
|
||||
var flag = require('./flag');
|
||||
|
||||
module.exports = function (ctx, name, method) {
|
||||
ctx[name] = function () {
|
||||
var old_ssfi = flag(this, 'ssfi');
|
||||
if (old_ssfi && config.includeStack === false)
|
||||
flag(this, 'ssfi', ctx[name]);
|
||||
var result = method.apply(this, arguments);
|
||||
return result === undefined ? this : result;
|
||||
};
|
||||
};
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*!
|
||||
* Chai - addProperty utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var config = require('../config');
|
||||
var flag = require('./flag');
|
||||
|
||||
/**
|
||||
* ### addProperty (ctx, name, getter)
|
||||
*
|
||||
* Adds a property to the prototype of an object.
|
||||
*
|
||||
* utils.addProperty(chai.Assertion.prototype, 'foo', function () {
|
||||
* var obj = utils.flag(this, 'object');
|
||||
* new chai.Assertion(obj).to.be.instanceof(Foo);
|
||||
* });
|
||||
*
|
||||
* Can also be accessed directly from `chai.Assertion`.
|
||||
*
|
||||
* chai.Assertion.addProperty('foo', fn);
|
||||
*
|
||||
* Then can be used as any other assertion.
|
||||
*
|
||||
* expect(myFoo).to.be.foo;
|
||||
*
|
||||
* @param {Object} ctx object to which the property is added
|
||||
* @param {String} name of property to add
|
||||
* @param {Function} getter function to be used for name
|
||||
* @namespace Utils
|
||||
* @name addProperty
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function (ctx, name, getter) {
|
||||
Object.defineProperty(ctx, name,
|
||||
{ get: function addProperty() {
|
||||
var old_ssfi = flag(this, 'ssfi');
|
||||
if (old_ssfi && config.includeStack === false)
|
||||
flag(this, 'ssfi', addProperty);
|
||||
|
||||
var result = getter.call(this);
|
||||
return result === undefined ? this : result;
|
||||
}
|
||||
, configurable: true
|
||||
});
|
||||
};
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/*!
|
||||
* Chai - expectTypes utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### expectTypes(obj, types)
|
||||
*
|
||||
* Ensures that the object being tested against is of a valid type.
|
||||
*
|
||||
* utils.expectTypes(this, ['array', 'object', 'string']);
|
||||
*
|
||||
* @param {Mixed} obj constructed Assertion
|
||||
* @param {Array} type A list of allowed types for this assertion
|
||||
* @namespace Utils
|
||||
* @name expectTypes
|
||||
* @api public
|
||||
*/
|
||||
|
||||
var AssertionError = require('assertion-error');
|
||||
var flag = require('./flag');
|
||||
var type = require('type-detect');
|
||||
|
||||
module.exports = function (obj, types) {
|
||||
var obj = flag(obj, 'object');
|
||||
types = types.map(function (t) { return t.toLowerCase(); });
|
||||
types.sort();
|
||||
|
||||
// Transforms ['lorem', 'ipsum'] into 'a lirum, or an ipsum'
|
||||
var str = types.map(function (t, index) {
|
||||
var art = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(t.charAt(0)) ? 'an' : 'a';
|
||||
var or = types.length > 1 && index === types.length - 1 ? 'or ' : '';
|
||||
return or + art + ' ' + t;
|
||||
}).join(', ');
|
||||
|
||||
if (!types.some(function (expected) { return type(obj) === expected; })) {
|
||||
throw new AssertionError(
|
||||
'object tested must be ' + str + ', but ' + type(obj) + ' given'
|
||||
);
|
||||
}
|
||||
};
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*!
|
||||
* Chai - flag utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### flag(object, key, [value])
|
||||
*
|
||||
* Get or set a flag value on an object. If a
|
||||
* value is provided it will be set, else it will
|
||||
* return the currently set value or `undefined` if
|
||||
* the value is not set.
|
||||
*
|
||||
* utils.flag(this, 'foo', 'bar'); // setter
|
||||
* utils.flag(this, 'foo'); // getter, returns `bar`
|
||||
*
|
||||
* @param {Object} object constructed Assertion
|
||||
* @param {String} key
|
||||
* @param {Mixed} value (optional)
|
||||
* @namespace Utils
|
||||
* @name flag
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function (obj, key, value) {
|
||||
var flags = obj.__flags || (obj.__flags = Object.create(null));
|
||||
if (arguments.length === 3) {
|
||||
flags[key] = value;
|
||||
} else {
|
||||
return flags[key];
|
||||
}
|
||||
};
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/*!
|
||||
* Chai - getActual utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* # getActual(object, [actual])
|
||||
*
|
||||
* Returns the `actual` value for an Assertion
|
||||
*
|
||||
* @param {Object} object (constructed Assertion)
|
||||
* @param {Arguments} chai.Assertion.prototype.assert arguments
|
||||
* @namespace Utils
|
||||
* @name getActual
|
||||
*/
|
||||
|
||||
module.exports = function (obj, args) {
|
||||
return args.length > 4 ? args[4] : obj._obj;
|
||||
};
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/*!
|
||||
* Chai - getEnumerableProperties utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### .getEnumerableProperties(object)
|
||||
*
|
||||
* This allows the retrieval of enumerable property names of an object,
|
||||
* inherited or not.
|
||||
*
|
||||
* @param {Object} object
|
||||
* @returns {Array}
|
||||
* @namespace Utils
|
||||
* @name getEnumerableProperties
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function getEnumerableProperties(object) {
|
||||
var result = [];
|
||||
for (var name in object) {
|
||||
result.push(name);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*!
|
||||
* Chai - message composition utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Module dependancies
|
||||
*/
|
||||
|
||||
var flag = require('./flag')
|
||||
, getActual = require('./getActual')
|
||||
, inspect = require('./inspect')
|
||||
, objDisplay = require('./objDisplay');
|
||||
|
||||
/**
|
||||
* ### .getMessage(object, message, negateMessage)
|
||||
*
|
||||
* Construct the error message based on flags
|
||||
* and template tags. Template tags will return
|
||||
* a stringified inspection of the object referenced.
|
||||
*
|
||||
* Message template tags:
|
||||
* - `#{this}` current asserted object
|
||||
* - `#{act}` actual value
|
||||
* - `#{exp}` expected value
|
||||
*
|
||||
* @param {Object} object (constructed Assertion)
|
||||
* @param {Arguments} chai.Assertion.prototype.assert arguments
|
||||
* @namespace Utils
|
||||
* @name getMessage
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function (obj, args) {
|
||||
var negate = flag(obj, 'negate')
|
||||
, val = flag(obj, 'object')
|
||||
, expected = args[3]
|
||||
, actual = getActual(obj, args)
|
||||
, msg = negate ? args[2] : args[1]
|
||||
, flagMsg = flag(obj, 'message');
|
||||
|
||||
if(typeof msg === "function") msg = msg();
|
||||
msg = msg || '';
|
||||
msg = msg
|
||||
.replace(/#\{this\}/g, function () { return objDisplay(val); })
|
||||
.replace(/#\{act\}/g, function () { return objDisplay(actual); })
|
||||
.replace(/#\{exp\}/g, function () { return objDisplay(expected); });
|
||||
|
||||
return flagMsg ? flagMsg + ': ' + msg : msg;
|
||||
};
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/*!
|
||||
* Chai - getName utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* # getName(func)
|
||||
*
|
||||
* Gets the name of a function, in a cross-browser way.
|
||||
*
|
||||
* @param {Function} a function (usually a constructor)
|
||||
* @namespace Utils
|
||||
* @name getName
|
||||
*/
|
||||
|
||||
module.exports = function (func) {
|
||||
if (func.name) return func.name;
|
||||
|
||||
var match = /^\s?function ([^(]*)\(/.exec(func);
|
||||
return match && match[1] ? match[1] : "";
|
||||
};
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
/*!
|
||||
* Chai - getPathInfo utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var hasProperty = require('./hasProperty');
|
||||
|
||||
/**
|
||||
* ### .getPathInfo(path, object)
|
||||
*
|
||||
* This allows the retrieval of property info in an
|
||||
* object given a string path.
|
||||
*
|
||||
* The path info consists of an object with the
|
||||
* following properties:
|
||||
*
|
||||
* * parent - The parent object of the property referenced by `path`
|
||||
* * name - The name of the final property, a number if it was an array indexer
|
||||
* * value - The value of the property, if it exists, otherwise `undefined`
|
||||
* * exists - Whether the property exists or not
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {Object} object
|
||||
* @returns {Object} info
|
||||
* @namespace Utils
|
||||
* @name getPathInfo
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function getPathInfo(path, obj) {
|
||||
var parsed = parsePath(path),
|
||||
last = parsed[parsed.length - 1];
|
||||
|
||||
var info = {
|
||||
parent: parsed.length > 1 ? _getPathValue(parsed, obj, parsed.length - 1) : obj,
|
||||
name: last.p || last.i,
|
||||
value: _getPathValue(parsed, obj)
|
||||
};
|
||||
info.exists = hasProperty(info.name, info.parent);
|
||||
|
||||
return info;
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
* ## parsePath(path)
|
||||
*
|
||||
* Helper function used to parse string object
|
||||
* paths. Use in conjunction with `_getPathValue`.
|
||||
*
|
||||
* var parsed = parsePath('myobject.property.subprop');
|
||||
*
|
||||
* ### Paths:
|
||||
*
|
||||
* * Can be as near infinitely deep and nested
|
||||
* * Arrays are also valid using the formal `myobject.document[3].property`.
|
||||
* * Literal dots and brackets (not delimiter) must be backslash-escaped.
|
||||
*
|
||||
* @param {String} path
|
||||
* @returns {Object} parsed
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function parsePath (path) {
|
||||
var str = path.replace(/([^\\])\[/g, '$1.[')
|
||||
, parts = str.match(/(\\\.|[^.]+?)+/g);
|
||||
return parts.map(function (value) {
|
||||
var re = /^\[(\d+)\]$/
|
||||
, mArr = re.exec(value);
|
||||
if (mArr) return { i: parseFloat(mArr[1]) };
|
||||
else return { p: value.replace(/\\([.\[\]])/g, '$1') };
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
* ## _getPathValue(parsed, obj)
|
||||
*
|
||||
* Helper companion function for `.parsePath` that returns
|
||||
* the value located at the parsed address.
|
||||
*
|
||||
* var value = getPathValue(parsed, obj);
|
||||
*
|
||||
* @param {Object} parsed definition from `parsePath`.
|
||||
* @param {Object} object to search against
|
||||
* @param {Number} object to search against
|
||||
* @returns {Object|Undefined} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function _getPathValue (parsed, obj, index) {
|
||||
var tmp = obj
|
||||
, res;
|
||||
|
||||
index = (index === undefined ? parsed.length : index);
|
||||
|
||||
for (var i = 0, l = index; i < l; i++) {
|
||||
var part = parsed[i];
|
||||
if (tmp) {
|
||||
if ('undefined' !== typeof part.p)
|
||||
tmp = tmp[part.p];
|
||||
else if ('undefined' !== typeof part.i)
|
||||
tmp = tmp[part.i];
|
||||
if (i == (l - 1)) res = tmp;
|
||||
} else {
|
||||
res = undefined;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*!
|
||||
* Chai - getPathValue utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* @see https://github.com/logicalparadox/filtr
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var getPathInfo = require('./getPathInfo');
|
||||
|
||||
/**
|
||||
* ### .getPathValue(path, object)
|
||||
*
|
||||
* This allows the retrieval of values in an
|
||||
* object given a string path.
|
||||
*
|
||||
* var obj = {
|
||||
* prop1: {
|
||||
* arr: ['a', 'b', 'c']
|
||||
* , str: 'Hello'
|
||||
* }
|
||||
* , prop2: {
|
||||
* arr: [ { nested: 'Universe' } ]
|
||||
* , str: 'Hello again!'
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* The following would be the results.
|
||||
*
|
||||
* getPathValue('prop1.str', obj); // Hello
|
||||
* getPathValue('prop1.att[2]', obj); // b
|
||||
* getPathValue('prop2.arr[0].nested', obj); // Universe
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {Object} object
|
||||
* @returns {Object} value or `undefined`
|
||||
* @namespace Utils
|
||||
* @name getPathValue
|
||||
* @api public
|
||||
*/
|
||||
module.exports = function(path, obj) {
|
||||
var info = getPathInfo(path, obj);
|
||||
return info.value;
|
||||
};
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*!
|
||||
* Chai - getProperties utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### .getProperties(object)
|
||||
*
|
||||
* This allows the retrieval of property names of an object, enumerable or not,
|
||||
* inherited or not.
|
||||
*
|
||||
* @param {Object} object
|
||||
* @returns {Array}
|
||||
* @namespace Utils
|
||||
* @name getProperties
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function getProperties(object) {
|
||||
var result = Object.getOwnPropertyNames(object);
|
||||
|
||||
function addProperty(property) {
|
||||
if (result.indexOf(property) === -1) {
|
||||
result.push(property);
|
||||
}
|
||||
}
|
||||
|
||||
var proto = Object.getPrototypeOf(object);
|
||||
while (proto !== null) {
|
||||
Object.getOwnPropertyNames(proto).forEach(addProperty);
|
||||
proto = Object.getPrototypeOf(proto);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/*!
|
||||
* Chai - hasProperty utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var type = require('type-detect');
|
||||
|
||||
/**
|
||||
* ### .hasProperty(object, name)
|
||||
*
|
||||
* This allows checking whether an object has
|
||||
* named property or numeric array index.
|
||||
*
|
||||
* Basically does the same thing as the `in`
|
||||
* operator but works properly with natives
|
||||
* and null/undefined values.
|
||||
*
|
||||
* var obj = {
|
||||
* arr: ['a', 'b', 'c']
|
||||
* , str: 'Hello'
|
||||
* }
|
||||
*
|
||||
* The following would be the results.
|
||||
*
|
||||
* hasProperty('str', obj); // true
|
||||
* hasProperty('constructor', obj); // true
|
||||
* hasProperty('bar', obj); // false
|
||||
*
|
||||
* hasProperty('length', obj.str); // true
|
||||
* hasProperty(1, obj.str); // true
|
||||
* hasProperty(5, obj.str); // false
|
||||
*
|
||||
* hasProperty('length', obj.arr); // true
|
||||
* hasProperty(2, obj.arr); // true
|
||||
* hasProperty(3, obj.arr); // false
|
||||
*
|
||||
* @param {Objuect} object
|
||||
* @param {String|Number} name
|
||||
* @returns {Boolean} whether it exists
|
||||
* @namespace Utils
|
||||
* @name getPathInfo
|
||||
* @api public
|
||||
*/
|
||||
|
||||
var literals = {
|
||||
'number': Number
|
||||
, 'string': String
|
||||
};
|
||||
|
||||
module.exports = function hasProperty(name, obj) {
|
||||
var ot = type(obj);
|
||||
|
||||
// Bad Object, obviously no props at all
|
||||
if(ot === 'null' || ot === 'undefined')
|
||||
return false;
|
||||
|
||||
// The `in` operator does not work with certain literals
|
||||
// box these before the check
|
||||
if(literals[ot] && typeof obj !== 'object')
|
||||
obj = new literals[ot](obj);
|
||||
|
||||
return name in obj;
|
||||
};
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
/*!
|
||||
* chai
|
||||
* Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Main exports
|
||||
*/
|
||||
|
||||
var exports = module.exports = {};
|
||||
|
||||
/*!
|
||||
* test utility
|
||||
*/
|
||||
|
||||
exports.test = require('./test');
|
||||
|
||||
/*!
|
||||
* type utility
|
||||
*/
|
||||
|
||||
exports.type = require('type-detect');
|
||||
|
||||
/*!
|
||||
* expectTypes utility
|
||||
*/
|
||||
exports.expectTypes = require('./expectTypes');
|
||||
|
||||
/*!
|
||||
* message utility
|
||||
*/
|
||||
|
||||
exports.getMessage = require('./getMessage');
|
||||
|
||||
/*!
|
||||
* actual utility
|
||||
*/
|
||||
|
||||
exports.getActual = require('./getActual');
|
||||
|
||||
/*!
|
||||
* Inspect util
|
||||
*/
|
||||
|
||||
exports.inspect = require('./inspect');
|
||||
|
||||
/*!
|
||||
* Object Display util
|
||||
*/
|
||||
|
||||
exports.objDisplay = require('./objDisplay');
|
||||
|
||||
/*!
|
||||
* Flag utility
|
||||
*/
|
||||
|
||||
exports.flag = require('./flag');
|
||||
|
||||
/*!
|
||||
* Flag transferring utility
|
||||
*/
|
||||
|
||||
exports.transferFlags = require('./transferFlags');
|
||||
|
||||
/*!
|
||||
* Deep equal utility
|
||||
*/
|
||||
|
||||
exports.eql = require('deep-eql');
|
||||
|
||||
/*!
|
||||
* Deep path value
|
||||
*/
|
||||
|
||||
exports.getPathValue = require('./getPathValue');
|
||||
|
||||
/*!
|
||||
* Deep path info
|
||||
*/
|
||||
|
||||
exports.getPathInfo = require('./getPathInfo');
|
||||
|
||||
/*!
|
||||
* Check if a property exists
|
||||
*/
|
||||
|
||||
exports.hasProperty = require('./hasProperty');
|
||||
|
||||
/*!
|
||||
* Function name
|
||||
*/
|
||||
|
||||
exports.getName = require('./getName');
|
||||
|
||||
/*!
|
||||
* add Property
|
||||
*/
|
||||
|
||||
exports.addProperty = require('./addProperty');
|
||||
|
||||
/*!
|
||||
* add Method
|
||||
*/
|
||||
|
||||
exports.addMethod = require('./addMethod');
|
||||
|
||||
/*!
|
||||
* overwrite Property
|
||||
*/
|
||||
|
||||
exports.overwriteProperty = require('./overwriteProperty');
|
||||
|
||||
/*!
|
||||
* overwrite Method
|
||||
*/
|
||||
|
||||
exports.overwriteMethod = require('./overwriteMethod');
|
||||
|
||||
/*!
|
||||
* Add a chainable method
|
||||
*/
|
||||
|
||||
exports.addChainableMethod = require('./addChainableMethod');
|
||||
|
||||
/*!
|
||||
* Overwrite chainable method
|
||||
*/
|
||||
|
||||
exports.overwriteChainableMethod = require('./overwriteChainableMethod');
|
||||
+335
@@ -0,0 +1,335 @@
|
||||
// This is (almost) directly from Node.js utils
|
||||
// https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/util.js
|
||||
|
||||
var getName = require('./getName');
|
||||
var getProperties = require('./getProperties');
|
||||
var getEnumerableProperties = require('./getEnumerableProperties');
|
||||
|
||||
module.exports = inspect;
|
||||
|
||||
/**
|
||||
* Echos the value of a value. Trys to print the value out
|
||||
* in the best way possible given the different types.
|
||||
*
|
||||
* @param {Object} obj The object to print out.
|
||||
* @param {Boolean} showHidden Flag that shows hidden (not enumerable)
|
||||
* properties of objects.
|
||||
* @param {Number} depth Depth in which to descend in object. Default is 2.
|
||||
* @param {Boolean} colors Flag to turn on ANSI escape codes to color the
|
||||
* output. Default is false (no coloring).
|
||||
* @namespace Utils
|
||||
* @name inspect
|
||||
*/
|
||||
function inspect(obj, showHidden, depth, colors) {
|
||||
var ctx = {
|
||||
showHidden: showHidden,
|
||||
seen: [],
|
||||
stylize: function (str) { return str; }
|
||||
};
|
||||
return formatValue(ctx, obj, (typeof depth === 'undefined' ? 2 : depth));
|
||||
}
|
||||
|
||||
// Returns true if object is a DOM element.
|
||||
var isDOMElement = function (object) {
|
||||
if (typeof HTMLElement === 'object') {
|
||||
return object instanceof HTMLElement;
|
||||
} else {
|
||||
return object &&
|
||||
typeof object === 'object' &&
|
||||
object.nodeType === 1 &&
|
||||
typeof object.nodeName === 'string';
|
||||
}
|
||||
};
|
||||
|
||||
function formatValue(ctx, value, recurseTimes) {
|
||||
// Provide a hook for user-specified inspect functions.
|
||||
// Check that value is an object with an inspect function on it
|
||||
if (value && typeof value.inspect === 'function' &&
|
||||
// Filter out the util module, it's inspect function is special
|
||||
value.inspect !== exports.inspect &&
|
||||
// Also filter out any prototype objects using the circular check.
|
||||
!(value.constructor && value.constructor.prototype === value)) {
|
||||
var ret = value.inspect(recurseTimes);
|
||||
if (typeof ret !== 'string') {
|
||||
ret = formatValue(ctx, ret, recurseTimes);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Primitive types cannot have properties
|
||||
var primitive = formatPrimitive(ctx, value);
|
||||
if (primitive) {
|
||||
return primitive;
|
||||
}
|
||||
|
||||
// If this is a DOM element, try to get the outer HTML.
|
||||
if (isDOMElement(value)) {
|
||||
if ('outerHTML' in value) {
|
||||
return value.outerHTML;
|
||||
// This value does not have an outerHTML attribute,
|
||||
// it could still be an XML element
|
||||
} else {
|
||||
// Attempt to serialize it
|
||||
try {
|
||||
if (document.xmlVersion) {
|
||||
var xmlSerializer = new XMLSerializer();
|
||||
return xmlSerializer.serializeToString(value);
|
||||
} else {
|
||||
// Firefox 11- do not support outerHTML
|
||||
// It does, however, support innerHTML
|
||||
// Use the following to render the element
|
||||
var ns = "http://www.w3.org/1999/xhtml";
|
||||
var container = document.createElementNS(ns, '_');
|
||||
|
||||
container.appendChild(value.cloneNode(false));
|
||||
html = container.innerHTML
|
||||
.replace('><', '>' + value.innerHTML + '<');
|
||||
container.innerHTML = '';
|
||||
return html;
|
||||
}
|
||||
} catch (err) {
|
||||
// This could be a non-native DOM implementation,
|
||||
// continue with the normal flow:
|
||||
// printing the element as if it is an object.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Look up the keys of the object.
|
||||
var visibleKeys = getEnumerableProperties(value);
|
||||
var keys = ctx.showHidden ? getProperties(value) : visibleKeys;
|
||||
|
||||
// Some type of object without properties can be shortcutted.
|
||||
// In IE, errors have a single `stack` property, or if they are vanilla `Error`,
|
||||
// a `stack` plus `description` property; ignore those for consistency.
|
||||
if (keys.length === 0 || (isError(value) && (
|
||||
(keys.length === 1 && keys[0] === 'stack') ||
|
||||
(keys.length === 2 && keys[0] === 'description' && keys[1] === 'stack')
|
||||
))) {
|
||||
if (typeof value === 'function') {
|
||||
var name = getName(value);
|
||||
var nameSuffix = name ? ': ' + name : '';
|
||||
return ctx.stylize('[Function' + nameSuffix + ']', 'special');
|
||||
}
|
||||
if (isRegExp(value)) {
|
||||
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
|
||||
}
|
||||
if (isDate(value)) {
|
||||
return ctx.stylize(Date.prototype.toUTCString.call(value), 'date');
|
||||
}
|
||||
if (isError(value)) {
|
||||
return formatError(value);
|
||||
}
|
||||
}
|
||||
|
||||
var base = '', array = false, braces = ['{', '}'];
|
||||
|
||||
// Make Array say that they are Array
|
||||
if (isArray(value)) {
|
||||
array = true;
|
||||
braces = ['[', ']'];
|
||||
}
|
||||
|
||||
// Make functions say that they are functions
|
||||
if (typeof value === 'function') {
|
||||
var name = getName(value);
|
||||
var nameSuffix = name ? ': ' + name : '';
|
||||
base = ' [Function' + nameSuffix + ']';
|
||||
}
|
||||
|
||||
// Make RegExps say that they are RegExps
|
||||
if (isRegExp(value)) {
|
||||
base = ' ' + RegExp.prototype.toString.call(value);
|
||||
}
|
||||
|
||||
// Make dates with properties first say the date
|
||||
if (isDate(value)) {
|
||||
base = ' ' + Date.prototype.toUTCString.call(value);
|
||||
}
|
||||
|
||||
// Make error with message first say the error
|
||||
if (isError(value)) {
|
||||
return formatError(value);
|
||||
}
|
||||
|
||||
if (keys.length === 0 && (!array || value.length == 0)) {
|
||||
return braces[0] + base + braces[1];
|
||||
}
|
||||
|
||||
if (recurseTimes < 0) {
|
||||
if (isRegExp(value)) {
|
||||
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
|
||||
} else {
|
||||
return ctx.stylize('[Object]', 'special');
|
||||
}
|
||||
}
|
||||
|
||||
ctx.seen.push(value);
|
||||
|
||||
var output;
|
||||
if (array) {
|
||||
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
|
||||
} else {
|
||||
output = keys.map(function(key) {
|
||||
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
|
||||
});
|
||||
}
|
||||
|
||||
ctx.seen.pop();
|
||||
|
||||
return reduceToSingleString(output, base, braces);
|
||||
}
|
||||
|
||||
|
||||
function formatPrimitive(ctx, value) {
|
||||
switch (typeof value) {
|
||||
case 'undefined':
|
||||
return ctx.stylize('undefined', 'undefined');
|
||||
|
||||
case 'string':
|
||||
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
|
||||
.replace(/'/g, "\\'")
|
||||
.replace(/\\"/g, '"') + '\'';
|
||||
return ctx.stylize(simple, 'string');
|
||||
|
||||
case 'number':
|
||||
if (value === 0 && (1/value) === -Infinity) {
|
||||
return ctx.stylize('-0', 'number');
|
||||
}
|
||||
return ctx.stylize('' + value, 'number');
|
||||
|
||||
case 'boolean':
|
||||
return ctx.stylize('' + value, 'boolean');
|
||||
}
|
||||
// For some reason typeof null is "object", so special case here.
|
||||
if (value === null) {
|
||||
return ctx.stylize('null', 'null');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function formatError(value) {
|
||||
return '[' + Error.prototype.toString.call(value) + ']';
|
||||
}
|
||||
|
||||
|
||||
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
|
||||
var output = [];
|
||||
for (var i = 0, l = value.length; i < l; ++i) {
|
||||
if (Object.prototype.hasOwnProperty.call(value, String(i))) {
|
||||
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
||||
String(i), true));
|
||||
} else {
|
||||
output.push('');
|
||||
}
|
||||
}
|
||||
keys.forEach(function(key) {
|
||||
if (!key.match(/^\d+$/)) {
|
||||
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
||||
key, true));
|
||||
}
|
||||
});
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
||||
var name, str;
|
||||
if (value.__lookupGetter__) {
|
||||
if (value.__lookupGetter__(key)) {
|
||||
if (value.__lookupSetter__(key)) {
|
||||
str = ctx.stylize('[Getter/Setter]', 'special');
|
||||
} else {
|
||||
str = ctx.stylize('[Getter]', 'special');
|
||||
}
|
||||
} else {
|
||||
if (value.__lookupSetter__(key)) {
|
||||
str = ctx.stylize('[Setter]', 'special');
|
||||
}
|
||||
}
|
||||
}
|
||||
if (visibleKeys.indexOf(key) < 0) {
|
||||
name = '[' + key + ']';
|
||||
}
|
||||
if (!str) {
|
||||
if (ctx.seen.indexOf(value[key]) < 0) {
|
||||
if (recurseTimes === null) {
|
||||
str = formatValue(ctx, value[key], null);
|
||||
} else {
|
||||
str = formatValue(ctx, value[key], recurseTimes - 1);
|
||||
}
|
||||
if (str.indexOf('\n') > -1) {
|
||||
if (array) {
|
||||
str = str.split('\n').map(function(line) {
|
||||
return ' ' + line;
|
||||
}).join('\n').substr(2);
|
||||
} else {
|
||||
str = '\n' + str.split('\n').map(function(line) {
|
||||
return ' ' + line;
|
||||
}).join('\n');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
str = ctx.stylize('[Circular]', 'special');
|
||||
}
|
||||
}
|
||||
if (typeof name === 'undefined') {
|
||||
if (array && key.match(/^\d+$/)) {
|
||||
return str;
|
||||
}
|
||||
name = JSON.stringify('' + key);
|
||||
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
|
||||
name = name.substr(1, name.length - 2);
|
||||
name = ctx.stylize(name, 'name');
|
||||
} else {
|
||||
name = name.replace(/'/g, "\\'")
|
||||
.replace(/\\"/g, '"')
|
||||
.replace(/(^"|"$)/g, "'");
|
||||
name = ctx.stylize(name, 'string');
|
||||
}
|
||||
}
|
||||
|
||||
return name + ': ' + str;
|
||||
}
|
||||
|
||||
|
||||
function reduceToSingleString(output, base, braces) {
|
||||
var numLinesEst = 0;
|
||||
var length = output.reduce(function(prev, cur) {
|
||||
numLinesEst++;
|
||||
if (cur.indexOf('\n') >= 0) numLinesEst++;
|
||||
return prev + cur.length + 1;
|
||||
}, 0);
|
||||
|
||||
if (length > 60) {
|
||||
return braces[0] +
|
||||
(base === '' ? '' : base + '\n ') +
|
||||
' ' +
|
||||
output.join(',\n ') +
|
||||
' ' +
|
||||
braces[1];
|
||||
}
|
||||
|
||||
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
|
||||
}
|
||||
|
||||
function isArray(ar) {
|
||||
return Array.isArray(ar) ||
|
||||
(typeof ar === 'object' && objectToString(ar) === '[object Array]');
|
||||
}
|
||||
|
||||
function isRegExp(re) {
|
||||
return typeof re === 'object' && objectToString(re) === '[object RegExp]';
|
||||
}
|
||||
|
||||
function isDate(d) {
|
||||
return typeof d === 'object' && objectToString(d) === '[object Date]';
|
||||
}
|
||||
|
||||
function isError(e) {
|
||||
return typeof e === 'object' && objectToString(e) === '[object Error]';
|
||||
}
|
||||
|
||||
function objectToString(o) {
|
||||
return Object.prototype.toString.call(o);
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*!
|
||||
* Chai - flag utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Module dependancies
|
||||
*/
|
||||
|
||||
var inspect = require('./inspect');
|
||||
var config = require('../config');
|
||||
|
||||
/**
|
||||
* ### .objDisplay (object)
|
||||
*
|
||||
* Determines if an object or an array matches
|
||||
* criteria to be inspected in-line for error
|
||||
* messages or should be truncated.
|
||||
*
|
||||
* @param {Mixed} javascript object to inspect
|
||||
* @name objDisplay
|
||||
* @namespace Utils
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function (obj) {
|
||||
var str = inspect(obj)
|
||||
, type = Object.prototype.toString.call(obj);
|
||||
|
||||
if (config.truncateThreshold && str.length >= config.truncateThreshold) {
|
||||
if (type === '[object Function]') {
|
||||
return !obj.name || obj.name === ''
|
||||
? '[Function]'
|
||||
: '[Function: ' + obj.name + ']';
|
||||
} else if (type === '[object Array]') {
|
||||
return '[ Array(' + obj.length + ') ]';
|
||||
} else if (type === '[object Object]') {
|
||||
var keys = Object.keys(obj)
|
||||
, kstr = keys.length > 2
|
||||
? keys.splice(0, 2).join(', ') + ', ...'
|
||||
: keys.join(', ');
|
||||
return '{ Object (' + kstr + ') }';
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
};
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*!
|
||||
* Chai - overwriteChainableMethod utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### overwriteChainableMethod (ctx, name, method, chainingBehavior)
|
||||
*
|
||||
* Overwites an already existing chainable method
|
||||
* and provides access to the previous function or
|
||||
* property. Must return functions to be used for
|
||||
* name.
|
||||
*
|
||||
* utils.overwriteChainableMethod(chai.Assertion.prototype, 'length',
|
||||
* function (_super) {
|
||||
* }
|
||||
* , function (_super) {
|
||||
* }
|
||||
* );
|
||||
*
|
||||
* Can also be accessed directly from `chai.Assertion`.
|
||||
*
|
||||
* chai.Assertion.overwriteChainableMethod('foo', fn, fn);
|
||||
*
|
||||
* Then can be used as any other assertion.
|
||||
*
|
||||
* expect(myFoo).to.have.length(3);
|
||||
* expect(myFoo).to.have.length.above(3);
|
||||
*
|
||||
* @param {Object} ctx object whose method / property is to be overwritten
|
||||
* @param {String} name of method / property to overwrite
|
||||
* @param {Function} method function that returns a function to be used for name
|
||||
* @param {Function} chainingBehavior function that returns a function to be used for property
|
||||
* @namespace Utils
|
||||
* @name overwriteChainableMethod
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function (ctx, name, method, chainingBehavior) {
|
||||
var chainableBehavior = ctx.__methods[name];
|
||||
|
||||
var _chainingBehavior = chainableBehavior.chainingBehavior;
|
||||
chainableBehavior.chainingBehavior = function () {
|
||||
var result = chainingBehavior(_chainingBehavior).call(this);
|
||||
return result === undefined ? this : result;
|
||||
};
|
||||
|
||||
var _method = chainableBehavior.method;
|
||||
chainableBehavior.method = function () {
|
||||
var result = method(_method).apply(this, arguments);
|
||||
return result === undefined ? this : result;
|
||||
};
|
||||
};
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*!
|
||||
* Chai - overwriteMethod utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### overwriteMethod (ctx, name, fn)
|
||||
*
|
||||
* Overwites an already existing method and provides
|
||||
* access to previous function. Must return function
|
||||
* to be used for name.
|
||||
*
|
||||
* utils.overwriteMethod(chai.Assertion.prototype, 'equal', function (_super) {
|
||||
* return function (str) {
|
||||
* var obj = utils.flag(this, 'object');
|
||||
* if (obj instanceof Foo) {
|
||||
* new chai.Assertion(obj.value).to.equal(str);
|
||||
* } else {
|
||||
* _super.apply(this, arguments);
|
||||
* }
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* Can also be accessed directly from `chai.Assertion`.
|
||||
*
|
||||
* chai.Assertion.overwriteMethod('foo', fn);
|
||||
*
|
||||
* Then can be used as any other assertion.
|
||||
*
|
||||
* expect(myFoo).to.equal('bar');
|
||||
*
|
||||
* @param {Object} ctx object whose method is to be overwritten
|
||||
* @param {String} name of method to overwrite
|
||||
* @param {Function} method function that returns a function to be used for name
|
||||
* @namespace Utils
|
||||
* @name overwriteMethod
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function (ctx, name, method) {
|
||||
var _method = ctx[name]
|
||||
, _super = function () { return this; };
|
||||
|
||||
if (_method && 'function' === typeof _method)
|
||||
_super = _method;
|
||||
|
||||
ctx[name] = function () {
|
||||
var result = method(_super).apply(this, arguments);
|
||||
return result === undefined ? this : result;
|
||||
}
|
||||
};
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/*!
|
||||
* Chai - overwriteProperty utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### overwriteProperty (ctx, name, fn)
|
||||
*
|
||||
* Overwites an already existing property getter and provides
|
||||
* access to previous value. Must return function to use as getter.
|
||||
*
|
||||
* utils.overwriteProperty(chai.Assertion.prototype, 'ok', function (_super) {
|
||||
* return function () {
|
||||
* var obj = utils.flag(this, 'object');
|
||||
* if (obj instanceof Foo) {
|
||||
* new chai.Assertion(obj.name).to.equal('bar');
|
||||
* } else {
|
||||
* _super.call(this);
|
||||
* }
|
||||
* }
|
||||
* });
|
||||
*
|
||||
*
|
||||
* Can also be accessed directly from `chai.Assertion`.
|
||||
*
|
||||
* chai.Assertion.overwriteProperty('foo', fn);
|
||||
*
|
||||
* Then can be used as any other assertion.
|
||||
*
|
||||
* expect(myFoo).to.be.ok;
|
||||
*
|
||||
* @param {Object} ctx object whose property is to be overwritten
|
||||
* @param {String} name of property to overwrite
|
||||
* @param {Function} getter function that returns a getter function to be used for name
|
||||
* @namespace Utils
|
||||
* @name overwriteProperty
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function (ctx, name, getter) {
|
||||
var _get = Object.getOwnPropertyDescriptor(ctx, name)
|
||||
, _super = function () {};
|
||||
|
||||
if (_get && 'function' === typeof _get.get)
|
||||
_super = _get.get
|
||||
|
||||
Object.defineProperty(ctx, name,
|
||||
{ get: function () {
|
||||
var result = getter(_super).call(this);
|
||||
return result === undefined ? this : result;
|
||||
}
|
||||
, configurable: true
|
||||
});
|
||||
};
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*!
|
||||
* Chai - test utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Module dependancies
|
||||
*/
|
||||
|
||||
var flag = require('./flag');
|
||||
|
||||
/**
|
||||
* # test(object, expression)
|
||||
*
|
||||
* Test and object for expression.
|
||||
*
|
||||
* @param {Object} object (constructed Assertion)
|
||||
* @param {Arguments} chai.Assertion.prototype.assert arguments
|
||||
* @namespace Utils
|
||||
* @name test
|
||||
*/
|
||||
|
||||
module.exports = function (obj, args) {
|
||||
var negate = flag(obj, 'negate')
|
||||
, expr = args[0];
|
||||
return negate ? !expr : expr;
|
||||
};
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
/*!
|
||||
* Chai - transferFlags utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### transferFlags(assertion, object, includeAll = true)
|
||||
*
|
||||
* Transfer all the flags for `assertion` to `object`. If
|
||||
* `includeAll` is set to `false`, then the base Chai
|
||||
* assertion flags (namely `object`, `ssfi`, and `message`)
|
||||
* will not be transferred.
|
||||
*
|
||||
*
|
||||
* var newAssertion = new Assertion();
|
||||
* utils.transferFlags(assertion, newAssertion);
|
||||
*
|
||||
* var anotherAsseriton = new Assertion(myObj);
|
||||
* utils.transferFlags(assertion, anotherAssertion, false);
|
||||
*
|
||||
* @param {Assertion} assertion the assertion to transfer the flags from
|
||||
* @param {Object} object the object to transfer the flags to; usually a new assertion
|
||||
* @param {Boolean} includeAll
|
||||
* @namespace Utils
|
||||
* @name transferFlags
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function (assertion, object, includeAll) {
|
||||
var flags = assertion.__flags || (assertion.__flags = Object.create(null));
|
||||
|
||||
if (!object.__flags) {
|
||||
object.__flags = Object.create(null);
|
||||
}
|
||||
|
||||
includeAll = arguments.length === 3 ? includeAll : true;
|
||||
|
||||
for (var flag in flags) {
|
||||
if (includeAll ||
|
||||
(flag !== 'object' && flag !== 'ssfi' && flag != 'message')) {
|
||||
object.__flags[flag] = flags[flag];
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user