should.js (5667B)
1 /*! 2 * chai 3 * Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com> 4 * MIT Licensed 5 */ 6 7 module.exports = function (chai, util) { 8 var Assertion = chai.Assertion; 9 10 function loadShould () { 11 // explicitly define this method as function as to have it's name to include as `ssfi` 12 function shouldGetter() { 13 if (this instanceof String || this instanceof Number || this instanceof Boolean ) { 14 return new Assertion(this.valueOf(), null, shouldGetter); 15 } 16 return new Assertion(this, null, shouldGetter); 17 } 18 function shouldSetter(value) { 19 // See https://github.com/chaijs/chai/issues/86: this makes 20 // `whatever.should = someValue` actually set `someValue`, which is 21 // especially useful for `global.should = require('chai').should()`. 22 // 23 // Note that we have to use [[DefineProperty]] instead of [[Put]] 24 // since otherwise we would trigger this very setter! 25 Object.defineProperty(this, 'should', { 26 value: value, 27 enumerable: true, 28 configurable: true, 29 writable: true 30 }); 31 } 32 // modify Object.prototype to have `should` 33 Object.defineProperty(Object.prototype, 'should', { 34 set: shouldSetter 35 , get: shouldGetter 36 , configurable: true 37 }); 38 39 var should = {}; 40 41 /** 42 * ### .fail(actual, expected, [message], [operator]) 43 * 44 * Throw a failure. 45 * 46 * @name fail 47 * @param {Mixed} actual 48 * @param {Mixed} expected 49 * @param {String} message 50 * @param {String} operator 51 * @namespace Should 52 * @api public 53 */ 54 55 should.fail = function (actual, expected, message, operator) { 56 message = message || 'should.fail()'; 57 throw new chai.AssertionError(message, { 58 actual: actual 59 , expected: expected 60 , operator: operator 61 }, should.fail); 62 }; 63 64 /** 65 * ### .equal(actual, expected, [message]) 66 * 67 * Asserts non-strict equality (`==`) of `actual` and `expected`. 68 * 69 * should.equal(3, '3', '== coerces values to strings'); 70 * 71 * @name equal 72 * @param {Mixed} actual 73 * @param {Mixed} expected 74 * @param {String} message 75 * @namespace Should 76 * @api public 77 */ 78 79 should.equal = function (val1, val2, msg) { 80 new Assertion(val1, msg).to.equal(val2); 81 }; 82 83 /** 84 * ### .throw(function, [constructor/string/regexp], [string/regexp], [message]) 85 * 86 * Asserts that `function` will throw an error that is an instance of 87 * `constructor`, or alternately that it will throw an error with message 88 * matching `regexp`. 89 * 90 * should.throw(fn, 'function throws a reference error'); 91 * should.throw(fn, /function throws a reference error/); 92 * should.throw(fn, ReferenceError); 93 * should.throw(fn, ReferenceError, 'function throws a reference error'); 94 * should.throw(fn, ReferenceError, /function throws a reference error/); 95 * 96 * @name throw 97 * @alias Throw 98 * @param {Function} function 99 * @param {ErrorConstructor} constructor 100 * @param {RegExp} regexp 101 * @param {String} message 102 * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types 103 * @namespace Should 104 * @api public 105 */ 106 107 should.Throw = function (fn, errt, errs, msg) { 108 new Assertion(fn, msg).to.Throw(errt, errs); 109 }; 110 111 /** 112 * ### .exist 113 * 114 * Asserts that the target is neither `null` nor `undefined`. 115 * 116 * var foo = 'hi'; 117 * 118 * should.exist(foo, 'foo exists'); 119 * 120 * @name exist 121 * @namespace Should 122 * @api public 123 */ 124 125 should.exist = function (val, msg) { 126 new Assertion(val, msg).to.exist; 127 } 128 129 // negation 130 should.not = {} 131 132 /** 133 * ### .not.equal(actual, expected, [message]) 134 * 135 * Asserts non-strict inequality (`!=`) of `actual` and `expected`. 136 * 137 * should.not.equal(3, 4, 'these numbers are not equal'); 138 * 139 * @name not.equal 140 * @param {Mixed} actual 141 * @param {Mixed} expected 142 * @param {String} message 143 * @namespace Should 144 * @api public 145 */ 146 147 should.not.equal = function (val1, val2, msg) { 148 new Assertion(val1, msg).to.not.equal(val2); 149 }; 150 151 /** 152 * ### .throw(function, [constructor/regexp], [message]) 153 * 154 * Asserts that `function` will _not_ throw an error that is an instance of 155 * `constructor`, or alternately that it will not throw an error with message 156 * matching `regexp`. 157 * 158 * should.not.throw(fn, Error, 'function does not throw'); 159 * 160 * @name not.throw 161 * @alias not.Throw 162 * @param {Function} function 163 * @param {ErrorConstructor} constructor 164 * @param {RegExp} regexp 165 * @param {String} message 166 * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types 167 * @namespace Should 168 * @api public 169 */ 170 171 should.not.Throw = function (fn, errt, errs, msg) { 172 new Assertion(fn, msg).to.not.Throw(errt, errs); 173 }; 174 175 /** 176 * ### .not.exist 177 * 178 * Asserts that the target is neither `null` nor `undefined`. 179 * 180 * var bar = null; 181 * 182 * should.not.exist(bar, 'bar does not exist'); 183 * 184 * @name not.exist 185 * @namespace Should 186 * @api public 187 */ 188 189 should.not.exist = function (val, msg) { 190 new Assertion(val, msg).to.not.exist; 191 } 192 193 should['throw'] = should['Throw']; 194 should.not['throw'] = should.not['Throw']; 195 196 return should; 197 }; 198 199 chai.should = loadShould; 200 chai.Should = loadShould; 201 };