cloudboost-tv
Version:
Database Service that does Storage, Search, Real-time and a whole lot more.
166 lines (146 loc) • 4.64 kB
JavaScript
/*!
* Should
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
var util = require('./util');
/**
* Our function should
*
* @param {*} obj Object to assert
* @returns {should.Assertion} Returns new Assertion for beginning assertion chain
* @example
*
* var should = require('should');
* should('abc').be.a.string;
*/
var should = function should(obj) {
return (new should.Assertion(obj)).proxied();
};
should.AssertionError = require('./assertion-error');
should.Assertion = require('./assertion');
should.format = require('should-format');
should.type = require('should-type');
should.util = util;
/**
* Object with configuration.
* It contains such properties:
* * `checkProtoEql` boolean - Affect if `.eql` will check objects prototypes
* * `useOldDeepEqual` boolean - Use old deepEqual implementation, that was copied from node's assert.deepEqual (will be removed in 5.x)
*
* @type {Object}
* @memberOf should
* @static
* @example
*
* var a = { a: 10 }, b = Object.create(null);
* b.a = 10;
*
* a.should.be.eql(b);
* //not throws
*
* should.config.checkProtoEql = true;
* a.should.be.eql(b);
* //throws AssertionError: expected { a: 10 } to equal { a: 10 } (because A and B have different prototypes)
*/
should.config = require('./config');
//Expose should to external world.
exports = module.exports = should;
/**
* Allow to extend given prototype with should property using given name. This getter will **unwrap** all standard wrappers like `Number`, `Boolean`, `String`.
* Using `should(obj)` is the equivalent of using `obj.should` with known issues (like nulls and method calls etc).
*
* @param {string} [propertyName] Name of property to add. Default is `'should'`.
* @param {Object} [proto] Prototype to extend with. Default is `Object.prototype`.
* @memberOf should
* @returns {{ name: string, descriptor: Object, proto: Object }} Descriptor enough to return all back
* @static
* @example
*
* var prev = should.extend('must', Object.prototype);
*
* 'abc'.must.startWith('a');
*
* var should = should.noConflict(prev);
* should.not.exist(Object.prototype.must);
*/
should.extend = function(propertyName, proto) {
propertyName = propertyName || 'should';
proto = proto || Object.prototype;
var prevDescriptor = Object.getOwnPropertyDescriptor(proto, propertyName);
Object.defineProperty(proto, propertyName, {
set: function() {
},
get: function() {
return should(util.isWrapperType(this) ? this.valueOf() : this);
},
configurable: true
});
return { name: propertyName, descriptor: prevDescriptor, proto: proto };
};
/**
* Delete previous extension. If `desc` missing it will remove default extension.
*
* @param {{ name: string, descriptor: Object, proto: Object }} [desc] Returned from `should.extend` object
* @memberOf should
* @returns {Function} Returns should function
* @static
* @example
*
* var should = require('should').noConflict();
*
* should(Object.prototype).not.have.property('should');
*
* var prev = should.extend('must', Object.prototype);
* 'abc'.must.startWith('a');
* should.noConflict(prev);
*
* should(Object.prototype).not.have.property('must');
*/
should.noConflict = function(desc) {
desc = desc || prevShould;
if(desc) {
delete desc.proto[desc.name];
if(desc.descriptor) {
Object.defineProperty(desc.proto, desc.name, desc.descriptor);
}
}
return should;
};
/**
* Simple utility function for a bit more easier should assertion extension
* @param {Function} f So called plugin function. It should accept 2 arguments: `should` function and `Assertion` constructor
* @memberOf should
* @returns {Function} Returns `should` function
* @static
* @example
*
* should.use(function(should, Assertion) {
* Assertion.add('asset', function() {
* this.params = { operator: 'to be asset' };
*
* this.obj.should.have.property('id').which.is.a.Number;
* this.obj.should.have.property('path');
* })
* })
*/
should.use = function(f) {
f(should, should.Assertion);
return this;
};
should
.use(require('./ext/assert'))
.use(require('./ext/chain'))
.use(require('./ext/bool'))
.use(require('./ext/number'))
.use(require('./ext/eql'))
.use(require('./ext/type'))
.use(require('./ext/string'))
.use(require('./ext/property'))
.use(require('./ext/error'))
.use(require('./ext/match'))
.use(require('./ext/contain'));
var defaultProto = Object.prototype;
var defaultProperty = 'should';
//Expose api via `Object#should`.
var prevShould = should.extend(defaultProperty, defaultProto);