[484] | 1 | /*! |
---|
| 2 | * Should |
---|
| 3 | * Copyright(c) 2010-2012 TJ Holowaychuk <tj@vision-media.ca> |
---|
| 4 | * MIT Licensed |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * Module dependencies. |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | var util = require('./util') |
---|
| 12 | , assert = require('assert') |
---|
| 13 | , AssertionError = assert.AssertionError |
---|
| 14 | , statusCodes = require('./http').STATUS_CODES |
---|
| 15 | , eql = require('./eql') |
---|
| 16 | , inspect = require('util').inspect; |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * Our function should |
---|
| 20 | * @param obj |
---|
| 21 | * @returns {Assertion} |
---|
| 22 | */ |
---|
| 23 | var should = function(obj) { |
---|
| 24 | return new Assertion(util.isWrapperType(obj) ? obj.valueOf(): obj); |
---|
| 25 | }; |
---|
| 26 | |
---|
[516] | 27 | should.inspect = inspect; |
---|
[484] | 28 | |
---|
[516] | 29 | function i(value) { |
---|
| 30 | if(util.isDate(value) && typeof value.inspect !== 'function') return value.toISOString(); //show millis in dates |
---|
| 31 | return should.inspect(value); |
---|
| 32 | } |
---|
| 33 | |
---|
[484] | 34 | /** |
---|
| 35 | * Expose assert to should |
---|
| 36 | * |
---|
| 37 | * This allows you to do things like below |
---|
| 38 | * without require()ing the assert module. |
---|
| 39 | * |
---|
| 40 | * should.equal(foo.bar, undefined); |
---|
| 41 | * |
---|
| 42 | */ |
---|
| 43 | util.merge(should, assert); |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Assert _obj_ exists, with optional message. |
---|
| 48 | * |
---|
| 49 | * @param {*} obj |
---|
| 50 | * @param {String} [msg] |
---|
| 51 | * @api public |
---|
| 52 | */ |
---|
| 53 | should.exist = should.exists = function(obj, msg) { |
---|
| 54 | if(null == obj) { |
---|
| 55 | throw new AssertionError({ |
---|
[516] | 56 | message: msg || ('expected ' + i(obj) + ' to exist') |
---|
[484] | 57 | , stackStartFunction: should.exist |
---|
| 58 | }); |
---|
| 59 | } |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Asserts _obj_ does not exist, with optional message. |
---|
| 64 | * |
---|
| 65 | * @param {*} obj |
---|
| 66 | * @param {String} [msg] |
---|
| 67 | * @api public |
---|
| 68 | */ |
---|
| 69 | |
---|
| 70 | should.not = {}; |
---|
| 71 | should.not.exist = should.not.exists = function(obj, msg){ |
---|
| 72 | if (null != obj) { |
---|
| 73 | throw new AssertionError({ |
---|
[516] | 74 | message: msg || ('expected ' + i(obj) + ' to not exist') |
---|
[484] | 75 | , stackStartFunction: should.not.exist |
---|
| 76 | }); |
---|
| 77 | } |
---|
| 78 | }; |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Expose should to external world. |
---|
| 82 | */ |
---|
| 83 | exports = module.exports = should; |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * Expose api via `Object#should`. |
---|
| 88 | * |
---|
| 89 | * @api public |
---|
| 90 | */ |
---|
| 91 | |
---|
| 92 | Object.defineProperty(Object.prototype, 'should', { |
---|
| 93 | set: function(){}, |
---|
| 94 | get: function(){ |
---|
| 95 | return should(this); |
---|
| 96 | }, |
---|
| 97 | configurable: true |
---|
| 98 | }); |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Initialize a new `Assertion` with the given _obj_. |
---|
| 102 | * |
---|
| 103 | * @param {*} obj |
---|
| 104 | * @api private |
---|
| 105 | */ |
---|
| 106 | |
---|
| 107 | var Assertion = should.Assertion = function Assertion(obj) { |
---|
| 108 | this.obj = obj; |
---|
| 109 | }; |
---|
| 110 | |
---|
| 111 | var hasOwnProperty = Object.prototype.hasOwnProperty; |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Prototype. |
---|
| 115 | */ |
---|
| 116 | |
---|
| 117 | Assertion.prototype = { |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * Assert _expr_ with the given _msg_ and _negatedMsg_. |
---|
| 121 | * |
---|
| 122 | * @param {Boolean} expr |
---|
| 123 | * @param {function} msg |
---|
| 124 | * @param {function} negatedMsg |
---|
| 125 | * @param {Object} [expected] |
---|
| 126 | * @param {Boolean} [showDiff] |
---|
| 127 | * @param {String} [description] |
---|
| 128 | * @api private |
---|
| 129 | */ |
---|
| 130 | |
---|
| 131 | assert: function(expr, msg, negatedMsg, expected, showDiff, description){ |
---|
| 132 | msg = this.negate ? negatedMsg : msg |
---|
| 133 | |
---|
| 134 | var ok = this.negate ? !expr : expr |
---|
| 135 | , obj = this.obj; |
---|
| 136 | |
---|
| 137 | if (ok) return; |
---|
| 138 | |
---|
| 139 | var err = new AssertionError({ |
---|
| 140 | message: msg.call(this) |
---|
| 141 | , actual: obj |
---|
| 142 | , expected: expected |
---|
| 143 | , stackStartFunction: this.assert |
---|
| 144 | , negated: this.negate |
---|
| 145 | }); |
---|
| 146 | |
---|
| 147 | err.showDiff = showDiff; |
---|
| 148 | err.description = description |
---|
| 149 | |
---|
| 150 | throw err; |
---|
| 151 | }, |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * Dummy getter. |
---|
| 155 | * |
---|
| 156 | * @api public |
---|
| 157 | */ |
---|
| 158 | |
---|
| 159 | get an() { |
---|
| 160 | return this; |
---|
| 161 | }, |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * Dummy getter. |
---|
| 165 | * |
---|
| 166 | * @api public |
---|
| 167 | */ |
---|
| 168 | |
---|
| 169 | get of() { |
---|
| 170 | return this; |
---|
| 171 | }, |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Dummy getter. |
---|
| 175 | * |
---|
| 176 | * @api public |
---|
| 177 | */ |
---|
| 178 | |
---|
| 179 | get a() { |
---|
| 180 | return this; |
---|
| 181 | }, |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * Dummy getter. |
---|
| 185 | * |
---|
| 186 | * @api public |
---|
| 187 | */ |
---|
| 188 | |
---|
| 189 | get and() { |
---|
| 190 | return this; |
---|
| 191 | }, |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * Dummy getter. |
---|
| 195 | * |
---|
| 196 | * @api public |
---|
| 197 | */ |
---|
| 198 | |
---|
| 199 | get be() { |
---|
| 200 | return this; |
---|
| 201 | }, |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Dummy getter. |
---|
| 205 | * |
---|
| 206 | * @api public |
---|
| 207 | */ |
---|
| 208 | |
---|
| 209 | get have() { |
---|
| 210 | return this; |
---|
| 211 | }, |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * Dummy getter. |
---|
| 215 | * |
---|
| 216 | * @api public |
---|
| 217 | */ |
---|
| 218 | |
---|
| 219 | get with() { |
---|
| 220 | return this; |
---|
| 221 | }, |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | * Negation modifier. |
---|
| 225 | * |
---|
| 226 | * @api public |
---|
| 227 | */ |
---|
| 228 | |
---|
| 229 | get not() { |
---|
| 230 | this.negate = true; |
---|
| 231 | return this; |
---|
| 232 | }, |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | * Get object inspection string. |
---|
| 236 | * |
---|
| 237 | * @return {String} |
---|
| 238 | * @api private |
---|
| 239 | */ |
---|
| 240 | |
---|
| 241 | get inspect() { |
---|
[516] | 242 | return i(this.obj); |
---|
[484] | 243 | }, |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * Assert instanceof `Arguments`. |
---|
| 247 | * |
---|
| 248 | * @api public |
---|
| 249 | */ |
---|
| 250 | |
---|
| 251 | get arguments() { |
---|
| 252 | this.assert( |
---|
| 253 | util.isArguments(this.obj) |
---|
| 254 | , function(){ return 'expected ' + this.inspect + ' to be arguments' } |
---|
| 255 | , function(){ return 'expected ' + this.inspect + ' to not be arguments' }); |
---|
| 256 | return this; |
---|
| 257 | }, |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | * Assert that object is empty. |
---|
| 261 | * |
---|
| 262 | * @api public |
---|
| 263 | */ |
---|
| 264 | |
---|
| 265 | get empty() { |
---|
| 266 | var length = this.obj.length; |
---|
| 267 | |
---|
| 268 | if(util.isString(this.obj) || Array.isArray(this.obj) || util.isArguments(this.obj)) { |
---|
| 269 | this.assert( |
---|
| 270 | 0 === length |
---|
| 271 | , function(){ return 'expected ' + this.inspect + ' to be empty' } |
---|
| 272 | , function(){ return 'expected ' + this.inspect + ' not to be empty' }); |
---|
| 273 | } else { |
---|
| 274 | var ok = true; |
---|
| 275 | for (var prop in this.obj) { |
---|
| 276 | if(hasOwnProperty.call(this.obj, prop)) { |
---|
| 277 | ok = false; |
---|
| 278 | break; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | this.assert( |
---|
| 283 | ok |
---|
| 284 | , function(){ return 'expected ' + this.inspect + ' to be empty' } |
---|
| 285 | , function(){ return 'expected ' + this.inspect + ' not to be empty' }); |
---|
| 286 | |
---|
| 287 | } |
---|
| 288 | return this; |
---|
| 289 | }, |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | * Assert ok. |
---|
| 293 | * |
---|
| 294 | * @api public |
---|
| 295 | */ |
---|
| 296 | |
---|
| 297 | get ok() { |
---|
| 298 | this.assert( |
---|
| 299 | this.obj |
---|
| 300 | , function(){ return 'expected ' + this.inspect + ' to be truthy' } |
---|
| 301 | , function(){ return 'expected ' + this.inspect + ' to be falsey' }); |
---|
| 302 | return this; |
---|
| 303 | }, |
---|
| 304 | |
---|
| 305 | /** |
---|
| 306 | * Assert true. |
---|
| 307 | * |
---|
| 308 | * @api public |
---|
| 309 | */ |
---|
| 310 | |
---|
| 311 | get true() { |
---|
| 312 | this.assert( |
---|
| 313 | true === this.obj |
---|
| 314 | , function(){ return 'expected ' + this.inspect + ' to be true' } |
---|
| 315 | , function(){ return 'expected ' + this.inspect + ' not to be true' }); |
---|
| 316 | return this; |
---|
| 317 | }, |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | * Assert false. |
---|
| 321 | * |
---|
| 322 | * @api public |
---|
| 323 | */ |
---|
| 324 | |
---|
| 325 | get false() { |
---|
| 326 | this.assert( |
---|
| 327 | false === this.obj |
---|
| 328 | , function(){ return 'expected ' + this.inspect + ' to be false' } |
---|
| 329 | , function(){ return 'expected ' + this.inspect + ' not to be false' }); |
---|
| 330 | return this; |
---|
| 331 | }, |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | * Assert NaN. |
---|
| 335 | * |
---|
| 336 | * @api public |
---|
| 337 | */ |
---|
| 338 | |
---|
| 339 | get NaN() { |
---|
| 340 | this.assert( |
---|
| 341 | util.isNumber(this.obj) && isNaN(this.obj) |
---|
| 342 | , function(){ return 'expected ' + this.inspect + ' to be NaN' } |
---|
| 343 | , function(){ return 'expected ' + this.inspect + ' not to be NaN' }); |
---|
| 344 | return this; |
---|
| 345 | }, |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | * Assert Infinity. |
---|
| 349 | * |
---|
| 350 | * @api public |
---|
| 351 | */ |
---|
| 352 | |
---|
| 353 | get Infinity() { |
---|
| 354 | this.assert( |
---|
| 355 | util.isNumber(this.obj) && !isNaN(this.obj) && !isFinite(this.obj) |
---|
| 356 | , function(){ return 'expected ' + this.inspect + ' to be Infinity' } |
---|
| 357 | , function(){ return 'expected ' + this.inspect + ' not to be Infinity' }); |
---|
| 358 | return this; |
---|
| 359 | }, |
---|
| 360 | |
---|
| 361 | /** |
---|
| 362 | * Assert equal. |
---|
| 363 | * |
---|
| 364 | * @param {*} val |
---|
| 365 | * @param {String} description |
---|
| 366 | * @api public |
---|
| 367 | */ |
---|
| 368 | |
---|
| 369 | eql: function(val, description){ |
---|
| 370 | this.assert( |
---|
| 371 | eql(val, this.obj) |
---|
[516] | 372 | , function(){ return 'expected ' + this.inspect + ' to equal ' + i(val) + (description ? " | " + description : "") } |
---|
| 373 | , function(){ return 'expected ' + this.inspect + ' to not equal ' + i(val) + (description ? " | " + description : "") } |
---|
[484] | 374 | , val |
---|
| 375 | , true |
---|
| 376 | , description); |
---|
| 377 | return this; |
---|
| 378 | }, |
---|
| 379 | |
---|
| 380 | /** |
---|
| 381 | * Assert strict equal. |
---|
| 382 | * |
---|
| 383 | * @param {*} val |
---|
| 384 | * @param {String} description |
---|
| 385 | * @api public |
---|
| 386 | */ |
---|
| 387 | |
---|
| 388 | equal: function(val, description){ |
---|
| 389 | this.assert( |
---|
| 390 | val === this.obj |
---|
[516] | 391 | , function(){ return 'expected ' + this.inspect + ' to equal ' + i(val) + (description ? " | " + description : "") } |
---|
| 392 | , function(){ return 'expected ' + this.inspect + ' to not equal ' + i(val) + (description ? " | " + description : "") } |
---|
[484] | 393 | , val |
---|
| 394 | , void 0 |
---|
| 395 | , description); |
---|
| 396 | return this; |
---|
| 397 | }, |
---|
| 398 | |
---|
| 399 | /** |
---|
| 400 | * Assert within start to finish (inclusive). |
---|
| 401 | * |
---|
| 402 | * @param {Number} start |
---|
| 403 | * @param {Number} finish |
---|
| 404 | * @param {String} description |
---|
| 405 | * @api public |
---|
| 406 | */ |
---|
| 407 | |
---|
| 408 | within: function(start, finish, description){ |
---|
| 409 | var range = start + '..' + finish; |
---|
| 410 | this.assert( |
---|
| 411 | this.obj >= start && this.obj <= finish |
---|
| 412 | , function(){ return 'expected ' + this.inspect + ' to be within ' + range + (description ? " | " + description : "") } |
---|
| 413 | , function(){ return 'expected ' + this.inspect + ' to not be within ' + range + (description ? " | " + description : "") } |
---|
| 414 | , void 0 |
---|
| 415 | , void 0 |
---|
| 416 | , description); |
---|
| 417 | return this; |
---|
| 418 | }, |
---|
| 419 | |
---|
| 420 | /** |
---|
| 421 | * Assert within value +- delta (inclusive). |
---|
| 422 | * |
---|
| 423 | * @param {Number} value |
---|
| 424 | * @param {Number} delta |
---|
| 425 | * @param {String} description |
---|
| 426 | * @api public |
---|
| 427 | */ |
---|
| 428 | |
---|
| 429 | approximately: function(value, delta, description) { |
---|
| 430 | this.assert( |
---|
| 431 | Math.abs(this.obj - value) <= delta |
---|
| 432 | , function(){ return 'expected ' + this.inspect + ' to be approximately ' + value + " +- " + delta + (description ? " | " + description : "") } |
---|
| 433 | , function(){ return 'expected ' + this.inspect + ' to not be approximately ' + value + " +- " + delta + (description ? " | " + description : "") } |
---|
| 434 | , void 0 |
---|
| 435 | , void 0 |
---|
| 436 | , description); |
---|
| 437 | return this; |
---|
| 438 | }, |
---|
| 439 | |
---|
| 440 | /** |
---|
| 441 | * Assert typeof. |
---|
| 442 | * |
---|
| 443 | * @param {*} type |
---|
| 444 | * @param {String} description |
---|
| 445 | * @api public |
---|
| 446 | */ |
---|
| 447 | type: function(type, description){ |
---|
| 448 | this.assert( |
---|
| 449 | type == typeof this.obj |
---|
| 450 | , function(){ return 'expected ' + this.inspect + ' to have type ' + type + (description ? " | " + description : "") } |
---|
| 451 | , function(){ return 'expected ' + this.inspect + ' not to have type ' + type + (description ? " | " + description : "") } |
---|
| 452 | , void 0 |
---|
| 453 | , void 0 |
---|
| 454 | , description); |
---|
| 455 | return this; |
---|
| 456 | }, |
---|
| 457 | |
---|
| 458 | /** |
---|
| 459 | * Assert instanceof. |
---|
| 460 | * |
---|
| 461 | * @param {Function} constructor |
---|
| 462 | * @param {String} description |
---|
| 463 | * @api public |
---|
| 464 | */ |
---|
| 465 | |
---|
| 466 | instanceof: function(constructor, description){ |
---|
| 467 | var name = constructor.name; |
---|
| 468 | this.assert( |
---|
| 469 | this.obj instanceof constructor |
---|
| 470 | , function(){ return 'expected ' + this.inspect + ' to be an instance of ' + name + (description ? " | " + description : "") } |
---|
| 471 | , function(){ return 'expected ' + this.inspect + ' not to be an instance of ' + name + (description ? " | " + description : "") } |
---|
| 472 | , void 0 |
---|
| 473 | , void 0 |
---|
| 474 | , description); |
---|
| 475 | return this; |
---|
| 476 | }, |
---|
| 477 | |
---|
| 478 | /** |
---|
| 479 | * Assert if given object is a function. |
---|
| 480 | */ |
---|
| 481 | get Function(){ |
---|
| 482 | this.assert( |
---|
| 483 | util.isFunction(this.obj) |
---|
| 484 | , function(){ return 'expected ' + this.inspect + ' to be a function' } |
---|
| 485 | , function(){ return 'expected ' + this.inspect + ' not to be a function' }); |
---|
| 486 | return this; |
---|
| 487 | }, |
---|
| 488 | |
---|
| 489 | /** |
---|
| 490 | * Assert given object is an object. |
---|
| 491 | */ |
---|
| 492 | get Object(){ |
---|
| 493 | this.assert( |
---|
| 494 | util.isObject(this.obj) && !Array.isArray(this.obj) |
---|
| 495 | , function(){ return 'expected ' + this.inspect + ' to be an object' } |
---|
| 496 | , function(){ return 'expected ' + this.inspect + ' not to be an object' }); |
---|
| 497 | return this; |
---|
| 498 | }, |
---|
| 499 | |
---|
| 500 | /** |
---|
| 501 | * Assert given object is a string |
---|
| 502 | */ |
---|
| 503 | get String(){ |
---|
| 504 | this.assert( |
---|
| 505 | util.isString(this.obj) |
---|
| 506 | , function(){ return 'expected ' + this.inspect + ' to be a string' } |
---|
| 507 | , function(){ return 'expected ' + this.inspect + ' not to be a string' }); |
---|
| 508 | return this; |
---|
| 509 | }, |
---|
| 510 | |
---|
| 511 | /** |
---|
| 512 | * Assert given object is an array |
---|
| 513 | */ |
---|
| 514 | get Array(){ |
---|
| 515 | this.assert( |
---|
| 516 | Array.isArray(this.obj) |
---|
| 517 | , function(){ return 'expected ' + this.inspect + ' to be an array' } |
---|
| 518 | , function(){ return 'expected ' + this.inspect + ' not to be an array' }); |
---|
| 519 | return this; |
---|
| 520 | }, |
---|
| 521 | |
---|
| 522 | /** |
---|
| 523 | * Assert given object is a number. NaN and Infinity are not numbers. |
---|
| 524 | */ |
---|
| 525 | get Number(){ |
---|
| 526 | this.assert( |
---|
| 527 | util.isNumber(this.obj) && isFinite(this.obj) && !isNaN(this.obj) |
---|
| 528 | , function(){ return 'expected ' + this.inspect + ' to be a number' } |
---|
| 529 | , function(){ return 'expected ' + this.inspect + ' not to be a number' }); |
---|
| 530 | return this; |
---|
| 531 | }, |
---|
| 532 | |
---|
| 533 | /** |
---|
| 534 | * Assert given object is a boolean |
---|
| 535 | */ |
---|
| 536 | get Boolean(){ |
---|
| 537 | this.assert( |
---|
| 538 | util.isBoolean(this.obj) |
---|
| 539 | , function(){ return 'expected ' + this.inspect + ' to be a boolean' } |
---|
| 540 | , function(){ return 'expected ' + this.inspect + ' not to be a boolean' }); |
---|
| 541 | return this; |
---|
| 542 | }, |
---|
| 543 | |
---|
| 544 | /** |
---|
| 545 | * Assert given object is an error |
---|
| 546 | */ |
---|
| 547 | get Error() { |
---|
| 548 | this.assert( |
---|
| 549 | util.isError(this.obj) |
---|
| 550 | , function(){ return 'expected ' + this.inspect + ' to be an error' } |
---|
| 551 | , function(){ return 'expected ' + this.inspect + ' not to be an error' }); |
---|
| 552 | return this; |
---|
| 553 | }, |
---|
| 554 | /** |
---|
| 555 | * Assert numeric value above _n_. |
---|
| 556 | * |
---|
| 557 | * @param {Number} n |
---|
| 558 | * @param {String} description |
---|
| 559 | * @api public |
---|
| 560 | */ |
---|
| 561 | |
---|
| 562 | above: function(n, description){ |
---|
| 563 | this.assert( |
---|
| 564 | this.obj > n |
---|
| 565 | , function(){ return 'expected ' + this.inspect + ' to be above ' + n + (description ? " | " + description : "") } |
---|
| 566 | , function(){ return 'expected ' + this.inspect + ' to be below ' + n + (description ? " | " + description : "") } |
---|
| 567 | , void 0 |
---|
| 568 | , void 0 |
---|
| 569 | , description); |
---|
| 570 | return this; |
---|
| 571 | }, |
---|
| 572 | |
---|
| 573 | /** |
---|
| 574 | * Assert numeric value below _n_. |
---|
| 575 | * |
---|
| 576 | * @param {Number} n |
---|
| 577 | * @param {String} description |
---|
| 578 | * @api public |
---|
| 579 | */ |
---|
| 580 | |
---|
| 581 | below: function(n, description){ |
---|
| 582 | this.assert( |
---|
| 583 | this.obj < n |
---|
| 584 | , function(){ return 'expected ' + this.inspect + ' to be below ' + n + (description ? " | " + description : "") } |
---|
| 585 | , function(){ return 'expected ' + this.inspect + ' to be above ' + n + (description ? " | " + description : "") } |
---|
| 586 | , void 0 |
---|
| 587 | , void 0 |
---|
| 588 | , description); |
---|
| 589 | return this; |
---|
| 590 | }, |
---|
| 591 | |
---|
| 592 | /** |
---|
| 593 | * Assert string value matches _regexp_. |
---|
| 594 | * |
---|
| 595 | * @param {RegExp} regexp |
---|
| 596 | * @param {String} description |
---|
| 597 | * @api public |
---|
| 598 | */ |
---|
| 599 | |
---|
| 600 | match: function(regexp, description){ |
---|
| 601 | this.assert( |
---|
| 602 | regexp.exec(this.obj) |
---|
| 603 | , function(){ return 'expected ' + this.inspect + ' to match ' + regexp + (description ? " | " + description : "") } |
---|
| 604 | , function(){ return 'expected ' + this.inspect + ' not to match ' + regexp + (description ? " | " + description : "") } |
---|
| 605 | , void 0 |
---|
| 606 | , void 0 |
---|
| 607 | , description); |
---|
| 608 | return this; |
---|
| 609 | }, |
---|
| 610 | |
---|
| 611 | /** |
---|
| 612 | * Assert property "length" exists and has value of _n_. |
---|
| 613 | * |
---|
| 614 | * @param {Number} n |
---|
| 615 | * @param {String} description |
---|
| 616 | * @api public |
---|
| 617 | */ |
---|
| 618 | |
---|
| 619 | length: function(n, description){ |
---|
| 620 | this.obj.should.have.property('length'); |
---|
| 621 | var len = this.obj.length; |
---|
| 622 | this.assert( |
---|
| 623 | n == len |
---|
| 624 | , function(){ return 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len + (description ? " | " + description : "") } |
---|
| 625 | , function(){ return 'expected ' + this.inspect + ' to not have a length of ' + len + (description ? " | " + description : "") } |
---|
| 626 | , void 0 |
---|
| 627 | , void 0 |
---|
| 628 | , description); |
---|
| 629 | return this; |
---|
| 630 | }, |
---|
| 631 | |
---|
| 632 | /** |
---|
| 633 | * Assert property _name_ exists, with optional _val_. |
---|
| 634 | * |
---|
| 635 | * @param {String} name |
---|
| 636 | * @param {*} [val] |
---|
| 637 | * @param {String} description |
---|
| 638 | * @api public |
---|
| 639 | */ |
---|
| 640 | |
---|
| 641 | property: function(name, val, description){ |
---|
| 642 | if (this.negate && undefined !== val) { |
---|
| 643 | if (undefined === this.obj[name]) { |
---|
[516] | 644 | throw new Error(this.inspect + ' has no property ' + i(name) + (description ? " | " + description : "")); |
---|
[484] | 645 | } |
---|
| 646 | } else { |
---|
| 647 | this.assert( |
---|
| 648 | undefined !== this.obj[name] |
---|
[516] | 649 | , function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) + (description ? " | " + description : "") } |
---|
| 650 | , function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + (description ? " | " + description : "") } |
---|
[484] | 651 | , void 0 |
---|
| 652 | , void 0 |
---|
| 653 | , description); |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | if (undefined !== val) { |
---|
| 657 | this.assert( |
---|
| 658 | val === this.obj[name] |
---|
[516] | 659 | , function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) |
---|
| 660 | + ' of ' + i(val) + ', but got ' + i(this.obj[name]) + (description ? " | " + description : "") } |
---|
| 661 | , function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + ' of ' + i(val) + (description ? " | " + description : "") } |
---|
[484] | 662 | , void 0 |
---|
| 663 | , void 0 |
---|
| 664 | , description); |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | this.obj = this.obj[name]; |
---|
| 668 | return this; |
---|
| 669 | }, |
---|
| 670 | /** |
---|
| 671 | * Asset have given properties |
---|
| 672 | * @param {Array|String ...} names |
---|
| 673 | * @api public |
---|
| 674 | */ |
---|
| 675 | properties: function(names) { |
---|
| 676 | var str |
---|
| 677 | , ok = true; |
---|
| 678 | |
---|
| 679 | names = names instanceof Array |
---|
| 680 | ? names |
---|
| 681 | : Array.prototype.slice.call(arguments); |
---|
| 682 | |
---|
| 683 | var len = names.length; |
---|
| 684 | |
---|
| 685 | if (!len) throw new Error('names required'); |
---|
| 686 | |
---|
| 687 | // make sure they're all present |
---|
| 688 | ok = names.every(function(name){ |
---|
| 689 | return this.obj[name] !== undefined; |
---|
| 690 | }, this); |
---|
| 691 | |
---|
| 692 | // key string |
---|
| 693 | if (len > 1) { |
---|
| 694 | names = names.map(function(name){ |
---|
[516] | 695 | return i(name); |
---|
[484] | 696 | }); |
---|
| 697 | var last = names.pop(); |
---|
| 698 | str = names.join(', ') + ', and ' + last; |
---|
| 699 | } else { |
---|
[516] | 700 | str = i(names[0]); |
---|
[484] | 701 | } |
---|
| 702 | |
---|
| 703 | // message |
---|
| 704 | str = 'have ' + (len > 1 ? 'properties ' : 'a property ') + str; |
---|
| 705 | |
---|
| 706 | this.assert( |
---|
| 707 | ok |
---|
| 708 | , function(){ return 'expected ' + this.inspect + ' to ' + str } |
---|
| 709 | , function(){ return 'expected ' + this.inspect + ' to not ' + str }); |
---|
| 710 | |
---|
| 711 | return this; |
---|
| 712 | }, |
---|
| 713 | |
---|
| 714 | /** |
---|
| 715 | * Assert own property _name_ exists. |
---|
| 716 | * |
---|
| 717 | * @param {String} name |
---|
| 718 | * @param {String} description |
---|
| 719 | * @api public |
---|
| 720 | */ |
---|
| 721 | |
---|
| 722 | ownProperty: function(name, description){ |
---|
| 723 | this.assert( |
---|
| 724 | hasOwnProperty.call(this.obj, name) |
---|
[516] | 725 | , function(){ return 'expected ' + this.inspect + ' to have own property ' + i(name) + (description ? " | " + description : "") } |
---|
| 726 | , function(){ return 'expected ' + this.inspect + ' to not have own property ' + i(name) + (description ? " | " + description : "") } |
---|
[484] | 727 | , void 0 |
---|
| 728 | , void 0 |
---|
| 729 | , description); |
---|
| 730 | this.obj = this.obj[name]; |
---|
| 731 | return this; |
---|
| 732 | }, |
---|
| 733 | |
---|
| 734 | /** |
---|
| 735 | * Assert that string starts with `str`. |
---|
| 736 | * @param {String} str |
---|
| 737 | * @param {String} description |
---|
| 738 | * @api public |
---|
| 739 | */ |
---|
| 740 | |
---|
| 741 | startWith: function(str, description) { |
---|
| 742 | this.assert(0 === this.obj.indexOf(str) |
---|
[516] | 743 | , function() { return 'expected ' + this.inspect + ' to start with ' + i(str) + (description ? " | " + description : "") } |
---|
| 744 | , function() { return 'expected ' + this.inspect + ' to not start with ' + i(str) + (description ? " | " + description : "") } |
---|
[484] | 745 | , void 0 |
---|
| 746 | , void 0 |
---|
| 747 | , description); |
---|
| 748 | return this; |
---|
| 749 | }, |
---|
| 750 | |
---|
| 751 | /** |
---|
| 752 | * Assert that string ends with `str`. |
---|
| 753 | * @param {String} str |
---|
| 754 | * @param {String} description |
---|
| 755 | * @api public |
---|
| 756 | */ |
---|
| 757 | |
---|
| 758 | endWith: function(str, description) { |
---|
| 759 | this.assert(-1 !== this.obj.indexOf(str, this.obj.length - str.length) |
---|
[516] | 760 | , function() { return 'expected ' + this.inspect + ' to end with ' + i(str) + (description ? " | " + description : "") } |
---|
| 761 | , function() { return 'expected ' + this.inspect + ' to not end with ' + i(str) + (description ? " | " + description : "") } |
---|
[484] | 762 | , void 0 |
---|
| 763 | , void 0 |
---|
| 764 | , description); |
---|
| 765 | return this; |
---|
| 766 | }, |
---|
| 767 | |
---|
| 768 | /** |
---|
| 769 | * Assert that `obj` is present via `.indexOf()` or that `obj` contains some sub-object. |
---|
| 770 | * |
---|
| 771 | * @param {*} obj |
---|
| 772 | * @param {String} description |
---|
| 773 | * @api public |
---|
| 774 | */ |
---|
| 775 | |
---|
| 776 | include: function(obj, description){ |
---|
| 777 | if (!Array.isArray(this.obj) && !util.isString(this.obj)){ |
---|
| 778 | var cmp = {}; |
---|
| 779 | for (var key in obj) cmp[key] = this.obj[key]; |
---|
| 780 | this.assert( |
---|
| 781 | eql(cmp, obj) |
---|
[516] | 782 | , function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (description ? " | " + description : "") } |
---|
| 783 | , function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (description ? " | " + description : "") } |
---|
[484] | 784 | , void 0 |
---|
| 785 | , void 0 |
---|
| 786 | , description); |
---|
| 787 | } else { |
---|
| 788 | this.assert( |
---|
| 789 | ~this.obj.indexOf(obj) |
---|
[516] | 790 | , function(){ return 'expected ' + this.inspect + ' to include ' + i(obj) + (description ? " | " + description : "") } |
---|
| 791 | , function(){ return 'expected ' + this.inspect + ' to not include ' + i(obj) + (description ? " | " + description : "") } |
---|
[484] | 792 | , void 0 |
---|
| 793 | , void 0 |
---|
| 794 | , description); |
---|
| 795 | } |
---|
| 796 | return this; |
---|
| 797 | }, |
---|
| 798 | |
---|
| 799 | /** |
---|
| 800 | * Assert that an object equal to `obj` is present. |
---|
| 801 | * |
---|
| 802 | * @param {Array} obj |
---|
| 803 | * @param {String} description |
---|
| 804 | * @api public |
---|
| 805 | */ |
---|
| 806 | |
---|
| 807 | includeEql: function(obj, description){ |
---|
| 808 | this.assert( |
---|
| 809 | this.obj.some(function(item) { return eql(obj, item); }) |
---|
[516] | 810 | , function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (description ? " | " + description : "") } |
---|
| 811 | , function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (description ? " | " + description : "") } |
---|
[484] | 812 | , void 0 |
---|
| 813 | , void 0 |
---|
| 814 | , description); |
---|
| 815 | return this; |
---|
| 816 | }, |
---|
| 817 | |
---|
| 818 | /** |
---|
| 819 | * Assert exact keys or inclusion of keys by using |
---|
| 820 | * the `.include` modifier. |
---|
| 821 | * |
---|
| 822 | * @param {Array|String ...} keys |
---|
| 823 | * @api public |
---|
| 824 | */ |
---|
| 825 | |
---|
| 826 | keys: function(keys){ |
---|
| 827 | var str |
---|
| 828 | , ok = true; |
---|
| 829 | |
---|
| 830 | keys = keys instanceof Array |
---|
| 831 | ? keys |
---|
| 832 | : Array.prototype.slice.call(arguments); |
---|
| 833 | |
---|
| 834 | if (!keys.length) throw new Error('keys required'); |
---|
| 835 | |
---|
| 836 | var actual = Object.keys(this.obj) |
---|
| 837 | , len = keys.length; |
---|
| 838 | |
---|
| 839 | // make sure they're all present |
---|
| 840 | ok = keys.every(function(key){ |
---|
| 841 | return ~actual.indexOf(key); |
---|
| 842 | }); |
---|
| 843 | |
---|
| 844 | // matching length |
---|
| 845 | ok = ok && keys.length == actual.length; |
---|
| 846 | |
---|
| 847 | // key string |
---|
| 848 | if (len > 1) { |
---|
| 849 | keys = keys.map(function(key){ |
---|
[516] | 850 | return i(key); |
---|
[484] | 851 | }); |
---|
| 852 | var last = keys.pop(); |
---|
| 853 | str = keys.join(', ') + ', and ' + last; |
---|
| 854 | } else { |
---|
[516] | 855 | str = i(keys[0]); |
---|
[484] | 856 | } |
---|
| 857 | |
---|
| 858 | // message |
---|
| 859 | str = 'have ' + (len > 1 ? 'keys ' : 'key ') + str; |
---|
| 860 | |
---|
| 861 | this.assert( |
---|
| 862 | ok |
---|
| 863 | , function(){ return 'expected ' + this.inspect + ' to ' + str } |
---|
| 864 | , function(){ return 'expected ' + this.inspect + ' to not ' + str }); |
---|
| 865 | |
---|
| 866 | return this; |
---|
| 867 | }, |
---|
| 868 | |
---|
| 869 | /** |
---|
| 870 | * Assert that header `field` has the given `val`. |
---|
| 871 | * |
---|
| 872 | * @param {String} field |
---|
| 873 | * @param {String} val |
---|
| 874 | * @return {Assertion} for chaining |
---|
| 875 | * @api public |
---|
| 876 | */ |
---|
| 877 | |
---|
| 878 | header: function(field, val){ |
---|
| 879 | this.obj.should |
---|
| 880 | .have.property('headers').and |
---|
| 881 | .have.property(field.toLowerCase(), val); |
---|
| 882 | return this; |
---|
| 883 | }, |
---|
| 884 | |
---|
| 885 | /** |
---|
| 886 | * Assert `.statusCode` of `code`. |
---|
| 887 | * |
---|
| 888 | * @param {Number} code |
---|
| 889 | * @return {Assertion} for chaining |
---|
| 890 | * @api public |
---|
| 891 | */ |
---|
| 892 | |
---|
| 893 | status: function(code){ |
---|
| 894 | this.obj.should.have.property('statusCode'); |
---|
| 895 | var status = this.obj.statusCode; |
---|
| 896 | |
---|
| 897 | this.assert( |
---|
| 898 | code == status |
---|
[516] | 899 | , function(){ return 'expected response code of ' + code + ' ' + i(statusCodes[code]) |
---|
| 900 | + ', but got ' + status + ' ' + i(statusCodes[status]) } |
---|
| 901 | , function(){ return 'expected to not respond with ' + code + ' ' + i(statusCodes[code]) }); |
---|
[484] | 902 | |
---|
| 903 | return this; |
---|
| 904 | }, |
---|
| 905 | |
---|
| 906 | /** |
---|
| 907 | * Assert that this response has content-type: application/json. |
---|
| 908 | * |
---|
| 909 | * @return {Assertion} for chaining |
---|
| 910 | * @api public |
---|
| 911 | */ |
---|
| 912 | |
---|
| 913 | get json() { |
---|
| 914 | this.obj.should.have.property('headers'); |
---|
| 915 | this.obj.headers.should.have.property('content-type'); |
---|
| 916 | this.obj.headers['content-type'].should.include('application/json'); |
---|
| 917 | return this; |
---|
| 918 | }, |
---|
| 919 | |
---|
| 920 | /** |
---|
| 921 | * Assert that this response has content-type: text/html. |
---|
| 922 | * |
---|
| 923 | * @return {Assertion} for chaining |
---|
| 924 | * @api public |
---|
| 925 | */ |
---|
| 926 | |
---|
| 927 | get html() { |
---|
| 928 | this.obj.should.have.property('headers'); |
---|
| 929 | this.obj.headers.should.have.property('content-type'); |
---|
| 930 | this.obj.headers['content-type'].should.include('text/html'); |
---|
| 931 | return this; |
---|
| 932 | }, |
---|
| 933 | |
---|
| 934 | /** |
---|
| 935 | * Assert that this function will or will not |
---|
| 936 | * throw an exception. |
---|
| 937 | * |
---|
| 938 | * @return {Assertion} for chaining |
---|
| 939 | * @api public |
---|
| 940 | */ |
---|
| 941 | |
---|
| 942 | throw: function(message){ |
---|
| 943 | var fn = this.obj |
---|
| 944 | , err = {} |
---|
| 945 | , errorInfo = '' |
---|
| 946 | , ok = true; |
---|
| 947 | |
---|
| 948 | try { |
---|
| 949 | fn(); |
---|
| 950 | ok = false; |
---|
| 951 | } catch (e) { |
---|
| 952 | err = e; |
---|
| 953 | } |
---|
| 954 | |
---|
| 955 | if (ok) { |
---|
| 956 | if ('string' == typeof message) { |
---|
| 957 | ok = message == err.message; |
---|
| 958 | } else if (message instanceof RegExp) { |
---|
| 959 | ok = message.test(err.message); |
---|
| 960 | } else if ('function' == typeof message) { |
---|
| 961 | ok = err instanceof message; |
---|
| 962 | } |
---|
| 963 | |
---|
| 964 | if (message && !ok) { |
---|
| 965 | if ('string' == typeof message) { |
---|
| 966 | errorInfo = " with a message matching '" + message + "', but got '" + err.message + "'"; |
---|
| 967 | } else if (message instanceof RegExp) { |
---|
| 968 | errorInfo = " with a message matching " + message + ", but got '" + err.message + "'"; |
---|
| 969 | } else if ('function' == typeof message) { |
---|
| 970 | errorInfo = " of type " + message.name + ", but got " + err.constructor.name; |
---|
| 971 | } |
---|
| 972 | } |
---|
| 973 | } |
---|
| 974 | |
---|
| 975 | this.assert( |
---|
| 976 | ok |
---|
| 977 | , function(){ return 'expected an exception to be thrown' + errorInfo } |
---|
| 978 | , function(){ return 'expected no exception to be thrown, got "' + err.message + '"' }); |
---|
| 979 | |
---|
| 980 | return this; |
---|
| 981 | } |
---|
| 982 | }; |
---|
| 983 | |
---|
| 984 | /** |
---|
| 985 | * Aliases. |
---|
| 986 | */ |
---|
| 987 | |
---|
| 988 | (function alias(name, as){ |
---|
| 989 | Assertion.prototype[as] = Assertion.prototype[name]; |
---|
| 990 | return alias; |
---|
| 991 | }) |
---|
| 992 | ('instanceof', 'instanceOf') |
---|
| 993 | ('throw', 'throwError') |
---|
| 994 | ('length', 'lengthOf') |
---|
| 995 | ('keys', 'key') |
---|
| 996 | ('ownProperty', 'haveOwnProperty') |
---|
| 997 | ('above', 'greaterThan') |
---|
| 998 | ('below', 'lessThan') |
---|
| 999 | ('include', 'contain') |
---|
| 1000 | ('equal', 'exactly'); |
---|
| 1001 | |
---|