123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- /*
- * 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));
- };
- should.AssertionError = require('./assertion-error');
- should.Assertion = require('./assertion');
- should.format = util.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
- * Also it can contain options for should-format.
- *
- * @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 || should._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'));
|