Coverage

88%
460
409
51

chai.js

94%
19
18
1
LineHitsSource
1/*!
2 * chai
3 * Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
4 * MIT Licensed
5 */
6
71var used = [];
81var exports = module.exports = {};
9
101exports.version = '0.4.2';
11
121exports.Assertion = require('./assertion');
131exports.AssertionError = require('./error');
14
151exports.inspect = require('./utils/inspect');
16
171exports.use = function (fn) {
185 if (!~used.indexOf(fn)) {
194 fn(this);
204 used.push(fn);
21 }
22
235 return this;
24};
25
261exports.fail = function (actual, expected, message, operator, stackStartFunction) {
270 throw new exports.AssertionError({
28 message: message,
29 actual: actual,
30 expected: expected,
31 operator: operator,
32 stackStartFunction: stackStartFunction
33 });
34};
35
361var expect = require('./interface/expect');
371exports.use(expect);
38
391var should = require('./interface/should');
401exports.use(should);
41
421var assert = require('./interface/assert');
431exports.use(assert);

assertion.js

97%
153
149
4
LineHitsSource
1/*!
2 * chai
3 * Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
4 * MIT Licensed
5 *
6 * Primarily a refactor of: should.js
7 * https://github.com/visionmedia/should.js
8 * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
9 * MIT Licensed
10 */
11
12/**
13 * ### BDD Style Introduction
14 *
15 * The BDD style is exposed through `expect` or `should` interfaces. In both
16 * scenarios, you chain together natural language assertions.
17 *
18 * // expect
19 * var expect = require('chai').expect;
20 * expect(foo).to.equal('bar');
21 *
22 * // should
23 * var should = require('chai').should();
24 * foo.should.equal('bar');
25 *
26 * #### Differences
27 *
28 * The `expect` interface provides a function as a starting point for chaining
29 * your language assertions. It works on node.js and in all browsers.
30 *
31 * The `should` interface extends `Object.prototype` to provide a single getter as
32 * the starting point for your language assertions. It works on node.js and in
33 * all browsers except Internet Explorer.
34 *
35 * #### Configuration
36 *
37 * By default, Chai does not show stack traces upon an AssertionError. This can
38 * be changed by modifying the `includeStack` parameter for chai.Assertion. For example:
39 *
40 * var chai = require('chai');
41 * chai.Assertion.includeStack = true; // defaults to false
42 */
43
44/*!
45 * Module dependencies.
46 */
47
481var AssertionError = require('./error')
49 , eql = require('./utils/eql')
50 , toString = Object.prototype.toString
51 , inspect = require('./utils/inspect');
52
53/*!
54 * Module export.
55 */
56
571module.exports = Assertion;
58
59
60/*!
61 * # Assertion Constructor
62 *
63 * Creates object for chaining.
64 *
65 * @api private
66 */
67
681function Assertion (obj, msg, stack) {
69651 this.ssfi = stack || arguments.callee;
70651 this.obj = obj;
71651 this.msg = msg;
72}
73
74/*!
75 * ## Assertion.includeStack
76 * , toString = Object.prototype.toString
77 *
78 * User configurable property, influences whether stack trace
79 * is included in Assertion error message. Default of false
80 * suppresses stack trace in the error message
81 *
82 * Assertion.includeStack = true; // enable stack on error
83 *
84 * @api public
85 */
86
871Assertion.includeStack = false;
88
89/*!
90 * # .assert(expression, message, negateMessage)
91 *
92 * Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass.
93 *
94 * @name assert
95 * @param {Philosophical} expression to be tested
96 * @param {String} message to display if fails
97 * @param {String} negatedMessage to display if negated expression fails
98 * @api private
99 */
100
1011Assertion.prototype.assert = function (expr, msg, negateMsg) {
102645 var msg = (this.negate ? negateMsg : msg)
103 , ok = this.negate ? !expr : expr;
104
105645 if (!ok) {
106143 throw new AssertionError({
107 operator: this.msg,
108 message: msg,
109 stackStartFunction: (Assertion.includeStack) ? this.assert : this.ssfi
110 });
111 }
112};
113
114/*!
115 * # inspect
116 *
117 * Returns the current object stringified.
118 *
119 * @name inspect
120 * @api private
121 */
122
1231Object.defineProperty(Assertion.prototype, 'inspect',
124 { get: function () {
1251292 return inspect(this.obj);
126 },
127 configurable: true
128});
129
130/**
131 * # to
132 *
133 * Language chain.
134 *
135 * @name to
136 * @api public
137 */
138
1391Object.defineProperty(Assertion.prototype, 'to',
140 { get: function () {
141380 return this;
142 },
143 configurable: true
144});
145
146/**
147 * # be
148 *
149 * Language chain.
150 *
151 * @name be
152 * @api public
153 */
154
1551Object.defineProperty(Assertion.prototype, 'be',
156 { get: function () {
157133 return this;
158 },
159 configurable: true
160});
161
162/**
163 * # been
164 *
165 * Language chain. Also tests `tense` to past for addon
166 * modules that use the tense feature.
167 *
168 * @name been
169 * @api public
170 */
171
1721Object.defineProperty(Assertion.prototype, 'been',
173 { get: function () {
1740 this.tense = 'past';
1750 return this;
176 },
177 configurable: true
178});
179
180/**
181 * # an
182 *
183 * Language chain.
184 *
185 * @name an
186 * @api public
187 */
188
1891Object.defineProperty(Assertion.prototype, 'an',
190 { get: function () {
1914 return this;
192 },
193 configurable: true
194});
195/**
196 * # is
197 *
198 * Language chain.
199 *
200 * @name is
201 * @api public
202 */
203
2041Object.defineProperty(Assertion.prototype, 'is',
205 { get: function () {
20680 return this;
207 },
208 configurable: true
209});
210
211/**
212 * # and
213 *
214 * Language chain.
215 *
216 * @name and
217 * @api public
218 */
219
2201Object.defineProperty(Assertion.prototype, 'and',
221 { get: function () {
2221 return this;
223 },
224 configurable: true
225});
226
227/**
228 * # have
229 *
230 * Language chain.
231 *
232 * @name have
233 * @api public
234 */
235
2361Object.defineProperty(Assertion.prototype, 'have',
237 { get: function () {
238105 return this;
239 },
240 configurable: true
241});
242
243/**
244 * # with
245 *
246 * Language chain.
247 *
248 * @name with
249 * @api public
250 */
251
2521Object.defineProperty(Assertion.prototype, 'with',
253 { get: function () {
2542 return this;
255 },
256 configurable: true
257});
258
259/**
260 * # .not
261 *
262 * Negates any of assertions following in the chain.
263 *
264 * @name not
265 * @api public
266 */
267
2681Object.defineProperty(Assertion.prototype, 'not',
269 { get: function () {
270121 this.negate = true;
271121 return this;
272 },
273 configurable: true
274});
275
276/**
277 * # .ok
278 *
279 * Assert object truthiness.
280 *
281 * expect('everthing').to.be.ok;
282 * expect(false).to.not.be.ok;
283 * expect(undefined).to.not.be.ok;
284 * expect(null).to.not.be.ok;
285 *
286 * @name ok
287 * @api public
288 */
289
2901Object.defineProperty(Assertion.prototype, 'ok',
291 { get: function () {
29223 this.assert(
293 this.obj
294 , 'expected ' + this.inspect + ' to be truthy'
295 , 'expected ' + this.inspect + ' to be falsy');
296
29715 return this;
298 },
299 configurable: true
300});
301
302/**
303 * # .true
304 *
305 * Assert object is true
306 *
307 * @name true
308 * @api public
309 */
310
3111Object.defineProperty(Assertion.prototype, 'true',
312 { get: function () {
31312 this.assert(
314 true === this.obj
315 , 'expected ' + this.inspect + ' to be true'
316 , 'expected ' + this.inspect + ' to be false');
317
3187 return this;
319 },
320 configurable: true
321});
322
323/**
324 * # .false
325 *
326 * Assert object is false
327 *
328 * @name false
329 * @api public
330 */
331
3321Object.defineProperty(Assertion.prototype, 'false',
333 { get: function () {
33411 this.assert(
335 false === this.obj
336 , 'expected ' + this.inspect + ' to be false'
337 , 'expected ' + this.inspect + ' to be true');
338
3397 return this;
340 },
341 configurable: true
342});
343
344/**
345 * # .exist
346 *
347 * Assert object exists (null).
348 *
349 * var foo = 'hi'
350 * , bar;
351 * expect(foo).to.exist;
352 * expect(bar).to.not.exist;
353 *
354 * @name exist
355 * @api public
356 */
357
3581Object.defineProperty(Assertion.prototype, 'exist',
359 { get: function () {
3606 this.assert(
361 null != this.obj
362 , 'expected ' + this.inspect + ' to exist'
363 , 'expected ' + this.inspect + ' to not exist');
364
3654 return this;
366 },
367 configurable: true
368});
369
370/**
371 * # .empty
372 *
373 * Assert object's length to be 0.
374 *
375 * expect([]).to.be.empty;
376 *
377 * @name empty
378 * @api public
379 */
380
3811Object.defineProperty(Assertion.prototype, 'empty',
382 { get: function () {
38314 new Assertion(this.obj).to.have.property('length');
384
38512 this.assert(
386 0 === this.obj.length
387 , 'expected ' + this.inspect + ' to be empty'
388 , 'expected ' + this.inspect + ' not to be empty');
389
3906 return this;
391 },
392 configurable: true
393});
394
395/**
396 * # .arguments
397 *
398 * Assert object is an instanceof arguments.
399 *
400 * function test () {
401 * expect(arguments).to.be.arguments;
402 * }
403 *
404 * @name arguments
405 * @api public
406 */
407
4081Object.defineProperty(Assertion.prototype, 'arguments',
409 { get: function () {
4104 this.assert(
411 '[object Arguments]' == Object.prototype.toString.call(this.obj)
412 , 'expected ' + this.inspect + ' to be arguments'
413 , 'expected ' + this.inspect + ' to not be arguments');
414
4154 return this;
416 },
417 configurable: true
418});
419
420/**
421 * # .equal(value)
422 *
423 * Assert strict equality.
424 *
425 * expect('hello').to.equal('hello');
426 *
427 * @name equal
428 * @param {*} value
429 * @api public
430 */
431
4321Assertion.prototype.equal = function (val) {
433142 this.assert(
434 val === this.obj
435 , 'expected ' + this.inspect + ' to equal ' + inspect(val)
436 , 'expected ' + this.inspect + ' to not equal ' + inspect(val));
437
438133 return this;
439};
440
441/**
442 * # .eql(value)
443 *
444 * Assert deep equality.
445 *
446 * expect({ foo: 'bar' }).to.eql({ foo: 'bar' });
447 *
448 * @name eql
449 * @param {*} value
450 * @api public
451 */
452
4531Assertion.prototype.eql = function (obj) {
45414 this.assert(
455 eql(obj, this.obj)
456 , 'expected ' + this.inspect + ' to equal ' + inspect(obj)
457 , 'expected ' + this.inspect + ' to not equal ' + inspect(obj));
45810 return this;
459};
460
461/**
462 * # .above(value)
463 *
464 * Assert greater than `value`.
465 *
466 * expect(10).to.be.above(5);
467 *
468 * @name above
469 * @param {Number} value
470 * @api public
471 */
472
4731Assertion.prototype.above = function (val) {
47412 this.assert(
475 this.obj > val
476 , 'expected ' + this.inspect + ' to be above ' + val
477 , 'expected ' + this.inspect + ' to be below ' + val);
478
4798 return this;
480};
481
482/**
483 * # .below(value)
484 *
485 * Assert less than `value`.
486 *
487 * expect(5).to.be.below(10);
488 *
489 * @name below
490 * @param {Number} value
491 * @api public
492 */
493
4941Assertion.prototype.below = function (val) {
4950 this.assert(
496 this.obj < val
497 , 'expected ' + this.inspect + ' to be below ' + val
498 , 'expected ' + this.inspect + ' to be above ' + val);
499
5000 return this;
501};
502
503/**
504 * # .within(start, finish)
505 *
506 * Assert that a number is within a range.
507 *
508 * expect(7).to.be.within(5,10);
509 *
510 * @name within
511 * @param {Number} start lowerbound inclusive
512 * @param {Number} finish upperbound inclusive
513 * @api public
514 */
515
5161Assertion.prototype.within = function (start, finish) {
51712 var range = start + '..' + finish;
518
51912 this.assert(
520 this.obj >= start && this.obj <= finish
521 , 'expected ' + this.inspect + ' to be within ' + range
522 , 'expected ' + this.inspect + ' to not be within ' + range);
523
5248 return this;
525};
526
527/**
528 * # .a(type)
529 *
530 * Assert typeof.
531 *
532 * expect('test').to.be.a('string');
533 *
534 * @name a
535 * @param {String} type
536 * @api public
537 */
538
5391Assertion.prototype.a = function (type) {
540114 var klass = type.charAt(0).toUpperCase() + type.slice(1);
541
542114 this.assert(
543 '[object ' + klass + ']' === toString.call(this.obj)
544 , 'expected ' + this.inspect + ' to be a ' + type
545 , 'expected ' + this.inspect + ' not to be a ' + type);
546
547100 return this;
548};
549
550/**
551 * # .instanceof(constructor)
552 *
553 * Assert instanceof.
554 *
555 * var Tea = function (name) { this.name = name; }
556 * , Chai = new Tea('chai');
557 *
558 * expect(Chai).to.be.an.instanceOf(Tea);
559 *
560 * @name instanceof
561 * @param {Constructor}
562 * @alias instanceOf
563 * @api public
564 */
565
5661Assertion.prototype.instanceof = function (constructor) {
5679 var name = constructor.name;
5689 this.assert(
569 this.obj instanceof constructor
570 , 'expected ' + this.inspect + ' to be an instance of ' + name
571 , 'expected ' + this.inspect + ' to not be an instance of ' + name);
572
5735 return this;
574};
575
576/**
577 * # .property(name, [value])
578 *
579 * Assert that property of `name` exists, optionally with `value`.
580 *
581 * var obj = { foo: 'bar' }
582 * expect(obj).to.have.property('foo');
583 * expect(obj).to.have.property('foo', 'bar');
584 * expect(obj).to.have.property('foo').to.be.a('string');
585 *
586 * @name property
587 * @param {String} name
588 * @param {*} value (optional)
589 * @returns value of property for chaining
590 * @api public
591 */
592
5931Assertion.prototype.property = function (name, val) {
59451 if (this.negate && undefined !== val) {
5954 if (undefined === this.obj[name]) {
5962 throw new Error(this.inspect + ' has no property ' + inspect(name));
597 }
598 } else {
59947 this.assert(
600 undefined !== this.obj[name]
601 , 'expected ' + this.inspect + ' to have a property ' + inspect(name)
602 , 'expected ' + this.inspect + ' to not have property ' + inspect(name));
603 }
604
60542 if (undefined !== val) {
60611 this.assert(
607 val === this.obj[name]
608 , 'expected ' + this.inspect + ' to have a property ' + inspect(name) + ' of ' +
609 inspect(val) + ', but got ' + inspect(this.obj[name])
610 , 'expected ' + this.inspect + ' to not have a property ' + inspect(name) + ' of ' + inspect(val));
611 }
612
61336 this.obj = this.obj[name];
61436 return this;
615};
616
617/**
618 * # .ownProperty(name)
619 *
620 * Assert that has own property by `name`.
621 *
622 * expect('test').to.have.ownProperty('length');
623 *
624 * @name ownProperty
625 * @alias haveOwnProperty
626 * @param {String} name
627 * @api public
628 */
629
6301Assertion.prototype.ownProperty = function (name) {
6318 this.assert(
632 this.obj.hasOwnProperty(name)
633 , 'expected ' + this.inspect + ' to have own property ' + inspect(name)
634 , 'expected ' + this.inspect + ' to not have own property ' + inspect(name));
6356 return this;
636};
637
638/**
639 * # .length(val)
640 *
641 * Assert that object has expected length.
642 *
643 * expect([1,2,3]).to.have.length(3);
644 * expect('foobar').to.have.length(6);
645 *
646 * @name length
647 * @alias lengthOf
648 * @param {Number} length
649 * @api public
650 */
651
6521Assertion.prototype.length = function (n) {
65316 new Assertion(this.obj).to.have.property('length');
65413 var len = this.obj.length;
655
65613 this.assert(
657 len == n
658 , 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len
659 , 'expected ' + this.inspect + ' to not have a length of ' + len);
660
6619 return this;
662};
663
664/**
665 * # .match(regexp)
666 *
667 * Assert that matches regular expression.
668 *
669 * expect('foobar').to.match(/^foo/);
670 *
671 * @name match
672 * @param {RegExp} RegularExpression
673 * @api public
674 */
675
6761Assertion.prototype.match = function (re) {
67711 this.assert(
678 re.exec(this.obj)
679 , 'expected ' + this.inspect + ' to match ' + re
680 , 'expected ' + this.inspect + ' not to match ' + re);
681
6827 return this;
683};
684
685/**
686 * # .include(obj)
687 *
688 * Assert the inclusion of an object in an Array or substring in string.
689 *
690 * expect([1,2,3]).to.include(2);
691 *
692 * @name include
693 * @param {Object|String|Number} obj
694 * @api public
695 */
696
6971Assertion.prototype.include = function (obj) {
69817 this.assert(
699 ~this.obj.indexOf(obj)
700 , 'expected ' + this.inspect + ' to include ' + inspect(obj)
701 , 'expected ' + this.inspect + ' to not include ' + inspect(obj));
702
70313 return this;
704};
705
706/**
707 * # .string(string)
708 *
709 * Assert inclusion of string in string.
710 *
711 * expect('foobar').to.have.string('bar');
712 *
713 * @name string
714 * @param {String} string
715 * @api public
716 */
717
7181Assertion.prototype.string = function (str) {
71915 new Assertion(this.obj).is.a('string');
720
72113 this.assert(
722 ~this.obj.indexOf(str)
723 , 'expected ' + this.inspect + ' to contain ' + inspect(str)
724 , 'expected ' + this.inspect + ' to not contain ' + inspect(str));
725
7268 return this;
727};
728
729
730
731/**
732 * # contain
733 *
734 * Toggles the `contain` flag for the `keys` assertion.
735 *
736 * @name contain
737 * @api public
738 */
739
7401Object.defineProperty(Assertion.prototype, 'contain',
741 { get: function () {
74237 this.contains = true;
74337 return this;
744 },
745 configurable: true
746});
747
748/**
749 * # .keys(key1, [key2], [...])
750 *
751 * Assert exact keys or the inclusing of keys using the `contain` modifier.
752 *
753 * expect({ foo: 1, bar: 2 }).to.have.keys(['foo', 'bar']);
754 * expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('foo', 'bar');
755 *
756 * @name keys
757 * @alias key
758 * @param {String|Array} Keys
759 * @api public
760 */
761
7621Assertion.prototype.keys = function(keys) {
76356 var str
764 , ok = true;
765
76656 keys = keys instanceof Array
767 ? keys
768 : Array.prototype.slice.call(arguments);
769
77064 if (!keys.length) throw new Error('keys required');
771
77248 var actual = Object.keys(this.obj)
773 , len = keys.length;
774
775 // Inclusion
77648 ok = keys.every(function(key){
77770 return ~actual.indexOf(key);
778 });
779
780 // Strict
78148 if (!this.negate && !this.contains) {
78212 ok = ok && keys.length == actual.length;
783 }
784
785 // Key string
78648 if (len > 1) {
78726 keys = keys.map(function(key){
78854 return inspect(key);
789 });
79026 var last = keys.pop();
79126 str = keys.join(', ') + ', and ' + last;
792 } else {
79322 str = inspect(keys[0]);
794 }
795
796 // Form
79748 str = (len > 1 ? 'keys ' : 'key ') + str;
798
799 // Have / include
80048 str = (this.contains ? 'contain ' : 'have ') + str;
801
802 // Assertion
80348 this.assert(
804 ok
805 , 'expected ' + this.inspect + ' to ' + str
806 , 'expected ' + this.inspect + ' to not ' + str);
807
80832 return this;
809}
810
811/**
812 * # .throw(constructor)
813 *
814 * Assert that a function will throw a specific type of error.
815 *
816 * var fn = function () { throw new ReferenceError(''); }
817 * expect(fn).to.throw(ReferenceError);
818 *
819 * @name throw
820 * @alias throws
821 * @alias Throw
822 * @param {ErrorConstructor} constructor
823 * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
824 * @api public
825 */
826
8271Assertion.prototype.throw = function (constructor) {
82851 new Assertion(this.obj).is.a('function');
829
83051 var thrown = false;
831
83251 try {
83351 this.obj();
834 } catch (err) {
83538 if (constructor && 'function' === typeof constructor && constructor.constructor != RegExp) {
83620 this.assert(
837 err instanceof constructor && err.name == constructor.name
838 , 'expected ' + this.inspect + ' to throw ' + constructor.name + ' but a ' + err.name + ' was thrown'
839 , 'expected ' + this.inspect + ' to not throw ' + constructor.name );
84012 return this;
84118 } else if (constructor && constructor instanceof RegExp) {
8428 this.assert(
843 constructor.exec(err.message)
844 , 'expected ' + this.inspect + ' to throw error matching ' + constructor + ' but got ' + inspect(err.message)
845 , 'expected ' + this.inspect + ' to throw error not matching ' + constructor);
8464 return this;
847 } else {
84810 thrown = true;
849 }
850 }
851
85223 var name = (constructor ? constructor.name : 'an error');
853
85423 this.assert(
855 thrown === true
856 , 'expected ' + this.inspect + ' to throw ' + name
857 , 'expected ' + this.inspect + ' to not throw ' + name);
858
85915 return this;
860};
861
862/**
863 * # .respondTo(method)
864 *
865 * Assert that object/class will respond to a method.
866 *
867 * expect(Klass).to.respondTo('bar');
868 * expect(obj).to.respondTo('bar');
869 *
870 * @name respondTo
871 * @param {String} method
872 * @api public
873 */
874
8751Assertion.prototype.respondTo = function (method) {
87610 var context = ('function' === typeof this.obj)
877 ? this.obj.prototype[method]
878 : this.obj[method];
879
88010 this.assert(
881 'function' === typeof context
882 , 'expected ' + this.inspect + ' to respond to ' + inspect(method)
883 , 'expected ' + this.inspect + ' to not respond to ' + inspect(method));
884
8856 return this;
886};
887
888/**
889 * # .satisfy(method)
890 *
891 * Assert that passes a truth test.
892 *
893 * expect(1).to.satisfy(function(num) { return num > 0; });
894 *
895 * @name satisfy
896 * @param {Function} matcher
897 * @api public
898 */
899
9001Assertion.prototype.satisfy = function (matcher) {
9014 this.assert(
902 matcher(this.obj)
903 , 'expected ' + this.inspect + ' to satisfy ' + inspect(matcher)
904 , 'expected ' + this.inspect + ' to not satisfy' + inspect(matcher));
905
9062 return this;
907};
908
909/**
910 * # .closeTo(expected, delta)
911 *
912 * Assert that actual is equal to +/- delta.
913 *
914 * expect(1.5).to.be.closeTo(1, 0.5);
915 *
916 * @name closeTo
917 * @param {Number} expected
918 * @param {Number} delta
919 * @api public
920 */
921
9221Assertion.prototype.closeTo = function (expected, delta) {
9234 this.assert(
924 (this.obj - delta === expected) || (this.obj + delta === expected)
925 , 'expected ' + this.inspect + ' to be close to ' + expected + ' +/- ' + delta
926 , 'expected ' + this.inspect + ' not to be close to ' + expected + ' +/- ' + delta);
927
9282 return this;
929};
930
931/*!
932 * Aliases.
933 */
934
9351(function alias(name, as){
9368 Assertion.prototype[as] = Assertion.prototype[name];
9378 return alias;
938})
939('length', 'lengthOf')
940('keys', 'key')
941('ownProperty', 'haveOwnProperty')
942('above', 'greaterThan')
943('below', 'lessThan')
944('throw', 'throws')
945('throw', 'Throw') // for troublesome browsers
946('instanceof', 'instanceOf');

error.js

91%
23
21
2
LineHitsSource
1/*!
2 * chai
3 * Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
4 * MIT Licensed
5 */
6
71var fail = require('./chai').fail;
8
91module.exports = AssertionError;
10
11/*!
12 * Inspired by node.js assert module
13 * https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/assert.js
14 */
151function AssertionError (options) {
16143 options = options || {};
17143 this.name = 'AssertionError';
18143 this.message = options.message;
19143 this.actual = options.actual;
20143 this.expected = options.expected;
21143 this.operator = options.operator;
22143 var stackStartFunction = options.stackStartFunction || fail;
23
24143 if (Error.captureStackTrace) {
25143 Error.captureStackTrace(this, stackStartFunction);
26 }
27}
28
291AssertionError.prototype.__proto__ = Error.prototype;
30
311AssertionError.prototype.summary = function() {
322 var str = '';
33
342 if (this.operator) {
350 str += 'In: \'' + this.operator + '\'\n\t';
36 }
37
382 str += '' + this.name + (this.message ? ': ' + this.message : '');
39
402 return str;
41};
42
431AssertionError.prototype.details = function() {
440 return this.summary();
45};
46
471AssertionError.prototype.toString = function() {
482 return this.summary();
49};

utils/eql.js

68%
45
31
14
LineHitsSource
1// This is directly from Node.js assert
2// https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/assert.js
3
4
51module.exports = _deepEqual;
6
7// For browser implementation
81if (!Buffer) {
91 var Buffer = {
10 isBuffer: function () {
1112 return false;
12 }
13 };
14}
15
161function _deepEqual(actual, expected) {
17 // 7.1. All identical values are equivalent, as determined by ===.
1820 if (actual === expected) {
198 return true;
20
2112 } else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
220 if (actual.length != expected.length) return false;
23
240 for (var i = 0; i < actual.length; i++) {
250 if (actual[i] !== expected[i]) return false;
26 }
27
280 return true;
29
30 // 7.2. If the expected value is a Date object, the actual value is
31 // equivalent if it is also a Date object that refers to the same time.
3212 } else if (actual instanceof Date && expected instanceof Date) {
330 return actual.getTime() === expected.getTime();
34
35 // 7.3. Other pairs that do not both pass typeof value == 'object',
36 // equivalence is determined by ==.
3712 } else if (typeof actual != 'object' && typeof expected != 'object') {
386 return actual === expected;
39
40 // 7.4. For all other Object pairs, including Array objects, equivalence is
41 // determined by having the same number of owned properties (as verified
42 // with Object.prototype.hasOwnProperty.call), the same set of keys
43 // (although not necessarily the same order), equivalent values for every
44 // corresponding key, and an identical 'prototype' property. Note: this
45 // accounts for both named and indexed properties on Arrays.
46 } else {
476 return objEquiv(actual, expected);
48 }
49}
50
511function isUndefinedOrNull(value) {
5212 return value === null || value === undefined;
53}
54
551function isArguments(object) {
566 return Object.prototype.toString.call(object) == '[object Arguments]';
57}
58
591function objEquiv(a, b) {
606 if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
610 return false;
62 // an identical 'prototype' property.
636 if (a.prototype !== b.prototype) return false;
64 //~~~I've managed to break Object.keys through screwy arguments passing.
65 // Converting to array solves the problem.
666 if (isArguments(a)) {
670 if (!isArguments(b)) {
680 return false;
69 }
700 a = pSlice.call(a);
710 b = pSlice.call(b);
720 return _deepEqual(a, b);
73 }
746 try {
756 var ka = Object.keys(a),
76 kb = Object.keys(b),
77 key, i;
78 } catch (e) {//happens when one is a string literal and the other isn't
790 return false;
80 }
81 // having the same number of owned properties (keys incorporates
82 // hasOwnProperty)
836 if (ka.length != kb.length)
840 return false;
85 //the same set of keys (although not necessarily the same order),
866 ka.sort();
876 kb.sort();
88 //~~~cheap key test
896 for (i = ka.length - 1; i >= 0; i--) {
906 if (ka[i] != kb[i])
910 return false;
92 }
93 //equivalent values for every corresponding key, and
94 //~~~possibly expensive deep test
956 for (i = ka.length - 1; i >= 0; i--) {
966 key = ka[i];
978 if (!_deepEqual(a[key], b[key])) return false;
98 }
994 return true;
100}

utils/inspect.js

77%
123
95
28
LineHitsSource
1// This is (almost) directly from Node.js utils
2// https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/util.js
3
41module.exports = inspect;
5
6/**
7 * Echos the value of a value. Trys to print the value out
8 * in the best way possible given the different types.
9 *
10 * @param {Object} obj The object to print out.
11 * @param {Boolean} showHidden Flag that shows hidden (not enumerable)
12 * properties of objects.
13 * @param {Number} depth Depth in which to descend in object. Default is 2.
14 * @param {Boolean} colors Flag to turn on ANSI escape codes to color the
15 * output. Default is false (no coloring).
16 */
171function inspect(obj, showHidden, depth, colors) {
182017 var ctx = {
19 showHidden: showHidden,
20 seen: [],
212421 stylize: function (str) { return str; }
22 };
232017 return formatValue(ctx, obj, (typeof depth === 'undefined' ? 2 : depth));
24}
25
261function formatValue(ctx, value, recurseTimes) {
27 // Provide a hook for user-specified inspect functions.
28 // Check that value is an object with an inspect function on it
292447 if (value && typeof value.inspect === 'function' &&
30 // Filter out the util module, it's inspect function is special
31 value.inspect !== exports.inspect &&
32 // Also filter out any prototype objects using the circular check.
33 !(value.constructor && value.constructor.prototype === value)) {
340 return value.inspect(recurseTimes);
35 }
36
37 // Primitive types cannot have properties
382447 var primitive = formatPrimitive(ctx, value);
392447 if (primitive) {
401907 return primitive;
41 }
42
43 // Look up the keys of the object.
44540 var visibleKeys = Object.keys(value);
45540 var keys = ctx.showHidden ? Object.getOwnPropertyNames(value) : visibleKeys;
46
47 // Some type of object without properties can be shortcutted.
48540 if (keys.length === 0) {
49304 if (typeof value === 'function') {
50252 var name = value.name ? ': ' + value.name : '';
51252 return ctx.stylize('[Function' + name + ']', 'special');
52 }
5352 if (isRegExp(value)) {
540 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
55 }
5652 if (isDate(value)) {
570 return ctx.stylize(Date.prototype.toUTCString.call(value), 'date');
58 }
5952 if (isError(value)) {
600 return formatError(value);
61 }
62 }
63
64288 var base = '', array = false, braces = ['{', '}'];
65
66 // Make Array say that they are Array
67288 if (isArray(value)) {
6896 array = true;
6996 braces = ['[', ']'];
70 }
71
72 // Make functions say that they are functions
73288 if (typeof value === 'function') {
740 var n = value.name ? ': ' + value.name : '';
750 base = ' [Function' + n + ']';
76 }
77
78 // Make RegExps say that they are RegExps
79288 if (isRegExp(value)) {
800 base = ' ' + RegExp.prototype.toString.call(value);
81 }
82
83 // Make dates with properties first say the date
84288 if (isDate(value)) {
850 base = ' ' + Date.prototype.toUTCString.call(value);
86 }
87
88 // Make error with message first say the error
89288 if (isError(value)) {
900 base = ' ' + formatError(value);
91 }
92
93288 if (keys.length === 0 && (!array || value.length == 0)) {
9452 return braces[0] + base + braces[1];
95 }
96
97236 if (recurseTimes < 0) {
980 if (isRegExp(value)) {
990 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
100 } else {
1010 return ctx.stylize('[Object]', 'special');
102 }
103 }
104
105236 ctx.seen.push(value);
106
107236 var output;
108236 if (array) {
10972 output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
110 } else {
111164 output = keys.map(function(key) {
112262 return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
113 });
114 }
115
116236 ctx.seen.pop();
117
118236 return reduceToSingleString(output, base, braces);
119}
120
121
1221function formatPrimitive(ctx, value) {
1232447 switch (typeof value) {
124 case 'undefined':
12530 return ctx.stylize('undefined', 'undefined');
126
127 case 'string':
1281341 var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
129 .replace(/'/g, "\\'")
130 .replace(/\\"/g, '"') + '\'';
1311341 return ctx.stylize(simple, 'string');
132
133 case 'number':
134468 return ctx.stylize('' + value, 'number');
135
136 case 'boolean':
13752 return ctx.stylize('' + value, 'boolean');
138 }
139 // For some reason typeof null is "object", so special case here.
140556 if (value === null) {
14116 return ctx.stylize('null', 'null');
142 }
143}
144
145
1461function formatError(value) {
1470 return '[' + Error.prototype.toString.call(value) + ']';
148}
149
150
1511function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
15272 var output = [];
15372 for (var i = 0, l = value.length; i < l; ++i) {
154168 if (Object.prototype.hasOwnProperty.call(value, String(i))) {
155168 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
156 String(i), true));
157 } else {
1580 output.push('');
159 }
160 }
16172 keys.forEach(function(key) {
162168 if (!key.match(/^\d+$/)) {
1630 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
164 key, true));
165 }
166 });
16772 return output;
168}
169
170
1711function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
172430 var name, str;
173430 if (value.__lookupGetter__) {
174430 if (value.__lookupGetter__(key)) {
1750 if (value.__lookupSetter__(key)) {
1760 str = ctx.stylize('[Getter/Setter]', 'special');
177 } else {
1780 str = ctx.stylize('[Getter]', 'special');
179 }
180 } else {
181430 if (value.__lookupSetter__(key)) {
1820 str = ctx.stylize('[Setter]', 'special');
183 }
184 }
185 }
186430 if (visibleKeys.indexOf(key) < 0) {
1870 name = '[' + key + ']';
188 }
189430 if (!str) {
190430 if (ctx.seen.indexOf(value[key]) < 0) {
191430 if (recurseTimes === null) {
1920 str = formatValue(ctx, value[key], null);
193 } else {
194430 str = formatValue(ctx, value[key], recurseTimes - 1);
195 }
196430 if (str.indexOf('\n') > -1) {
1970 if (array) {
1980 str = str.split('\n').map(function(line) {
1990 return ' ' + line;
200 }).join('\n').substr(2);
201 } else {
2020 str = '\n' + str.split('\n').map(function(line) {
2030 return ' ' + line;
204 }).join('\n');
205 }
206 }
207 } else {
2080 str = ctx.stylize('[Circular]', 'special');
209 }
210 }
211430 if (typeof name === 'undefined') {
212430 if (array && key.match(/^\d+$/)) {
213168 return str;
214 }
215262 name = JSON.stringify('' + key);
216262 if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
217244 name = name.substr(1, name.length - 2);
218244 name = ctx.stylize(name, 'name');
219 } else {
22018 name = name.replace(/'/g, "\\'")
221 .replace(/\\"/g, '"')
222 .replace(/(^"|"$)/g, "'");
22318 name = ctx.stylize(name, 'string');
224 }
225 }
226
227262 return name + ': ' + str;
228}
229
230
2311function reduceToSingleString(output, base, braces) {
232236 var numLinesEst = 0;
233236 var length = output.reduce(function(prev, cur) {
234430 numLinesEst++;
235430 if (cur.indexOf('\n') >= 0) numLinesEst++;
236430 return prev + cur.length + 1;
237 }, 0);
238
239236 if (length > 60) {
2400 return braces[0] +
241 (base === '' ? '' : base + '\n ') +
242 ' ' +
243 output.join(',\n ') +
244 ' ' +
245 braces[1];
246 }
247
248236 return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
249}
250
2511function isArray(ar) {
252288 return Array.isArray(ar) ||
253 (typeof ar === 'object' && objectToString(ar) === '[object Array]');
254}
255
2561function isRegExp(re) {
257340 return typeof re === 'object' && objectToString(re) === '[object RegExp]';
258}
259
2601function isDate(d) {
261340 return typeof d === 'object' && objectToString(d) === '[object Date]';
262}
263
2641function isError(e) {
265340 return typeof e === 'object' && objectToString(e) === '[object Error]';
266}
267
2681function objectToString(o) {
2691212 return Object.prototype.toString.call(o);
270}

interface/expect.js

100%
3
3
0
LineHitsSource
1/*!
2 * chai
3 * Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
4 * MIT Licensed
5 */
6
71module.exports = function (chai) {
81 chai.expect = function (val, message) {
9224 return new chai.Assertion(val, message);
10 };
11};
12

interface/should.js

91%
24
22
2
LineHitsSource
1/*!
2 * chai
3 * Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
4 * MIT Licensed
5 */
6
71module.exports = function (chai) {
81 var Assertion = chai.Assertion;
9
101 chai.should = function () {
11 // modify Object.prototype to have `should`
121 Object.defineProperty(Object.prototype, 'should', {
13 set: function(){},
14 get: function(){
15153 if (this instanceof String || this instanceof Number) {
161 return new Assertion(this.constructor(this));
17152 } else if (this instanceof Boolean) {
180 return new Assertion(this == true);
19 }
20152 return new Assertion(this);
21 },
22 configurable: true
23 });
24
251 var should = {};
26
271 should.equal = function (val1, val2) {
2861 new Assertion(val1).to.equal(val2);
29 };
30
311 should.throw = function (fn, err) {
322 new Assertion(fn).to.throw(err);
33 };
34
351 should.exist = function (val) {
362 new Assertion(val).to.exist;
37 }
38
39 // negation
401 should.not = {}
41
421 should.not.equal = function (val1, val2) {
430 new Assertion(val1).to.not.equal(val2);
44 };
45
461 should.not.throw = function (fn, err) {
472 new Assertion(fn).to.not.throw(err);
48 };
49
501 should.not.exist = function (val) {
512 new Assertion(val).to.not.exist;
52 }
53
541 return should;
55 };
56};

interface/assert.js

100%
70
70
0
LineHitsSource
1/*!
2 * chai
3 * Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
4 * MIT Licensed
5 */
6
7/**
8 * ### TDD Style Introduction
9 *
10 * The TDD style is exposed through `assert` interfaces. This provides
11 * the classic assert.`test` notation, similiar to that packaged with
12 * node.js. This assert module, however, provides several additional
13 * tests and is browser compatible.
14 *
15 * // assert
16 * var assert = require('chai').assert;
17 * , foo = 'bar';
18 *
19 * assert.typeOf(foo, 'string');
20 * assert.equal(foo, 'bar');
21 *
22 * #### Configuration
23 *
24 * By default, Chai does not show stack traces upon an AssertionError. This can
25 * be changed by modifying the `includeStack` parameter for chai.Assertion. For example:
26 *
27 * var chai = require('chai');
28 * chai.Assertion.includeStack = true; // defaults to false
29 */
30
311module.exports = function (chai) {
32 /*!
33 * Chai dependencies.
34 */
351 var Assertion = chai.Assertion
36 , inspect = chai.inspect;
37
38 /*!
39 * Module export.
40 */
41
421 var assert = chai.assert = {};
43
44 /**
45 * # .ok(object, [message])
46 *
47 * Assert object is truthy.
48 *
49 * assert.ok('everthing', 'everything is ok');
50 * assert.ok(false, 'this will fail');
51 *
52 * @name ok
53 * @param {*} object to test
54 * @param {String} message
55 * @api public
56 */
57
581 assert.ok = function (val, msg) {
597 new Assertion(val, msg).is.ok;
60 };
61
62 /**
63 * # .equal(actual, expected, [message])
64 *
65 * Assert strict equality.
66 *
67 * assert.equal(3, 3, 'these numbers are equal');
68 *
69 * @name equal
70 * @param {*} actual
71 * @param {*} expected
72 * @param {String} message
73 * @api public
74 */
75
761 assert.equal = function (act, exp, msg) {
7735 var test = new Assertion(act, msg);
78
7935 test.assert(
80 exp == test.obj
81 , 'expected ' + test.inspect + ' to equal ' + inspect(exp)
82 , 'expected ' + test.inspect + ' to not equal ' + inspect(exp));
83 };
84
85 /**
86 * # .notEqual(actual, expected, [message])
87 *
88 * Assert not equal.
89 *
90 * assert.notEqual(3, 4, 'these numbers are not equal');
91 *
92 * @name notEqual
93 * @param {*} actual
94 * @param {*} expected
95 * @param {String} message
96 * @api public
97 */
98
991 assert.notEqual = function (act, exp, msg) {
1002 var test = new Assertion(act, msg);
101
1022 test.assert(
103 exp != test.obj
104 , 'expected ' + test.inspect + ' to equal ' + inspect(exp)
105 , 'expected ' + test.inspect + ' to not equal ' + inspect(exp));
106 };
107
108 /**
109 * # .strictEqual(actual, expected, [message])
110 *
111 * Assert strict equality.
112 *
113 * assert.strictEqual(true, true, 'these booleans are strictly equal');
114 *
115 * @name strictEqual
116 * @param {*} actual
117 * @param {*} expected
118 * @param {String} message
119 * @api public
120 */
121
1221 assert.strictEqual = function (act, exp, msg) {
1232 new Assertion(act, msg).to.equal(exp);
124 };
125
126 /**
127 * # .notStrictEqual(actual, expected, [message])
128 *
129 * Assert strict equality.
130 *
131 * assert.notStrictEqual(1, true, 'these booleans are not strictly equal');
132 *
133 * @name notStrictEqual
134 * @param {*} actual
135 * @param {*} expected
136 * @param {String} message
137 * @api public
138 */
139
1401 assert.notStrictEqual = function (act, exp, msg) {
1412 new Assertion(act, msg).to.not.equal(exp);
142 };
143
144 /**
145 * # .deepEqual(actual, expected, [message])
146 *
147 * Assert not deep equality.
148 *
149 * assert.deepEqual({ tea: 'green' }, { tea: 'green' });
150 *
151 * @name deepEqual
152 * @param {*} actual
153 * @param {*} expected
154 * @param {String} message
155 * @api public
156 */
157
1581 assert.deepEqual = function (act, exp, msg) {
1592 new Assertion(act, msg).to.eql(exp);
160 };
161
162 /**
163 * # .notDeepEqual(actual, expected, [message])
164 *
165 * Assert not deep equality.
166 *
167 * assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' });
168 *
169 * @name notDeepEqual
170 * @param {*} actual
171 * @param {*} expected
172 * @param {String} message
173 * @api public
174 */
175
1761 assert.notDeepEqual = function (act, exp, msg) {
1772 new Assertion(act, msg).to.not.eql(exp);
178 };
179
180 /**
181 * # .isTrue(value, [message])
182 *
183 * Assert `value` is true.
184 *
185 * var tea_served = true;
186 * assert.isTrue(tea_served, 'the tea has been served');
187 *
188 * @name isTrue
189 * @param {Boolean} value
190 * @param {String} message
191 * @api public
192 */
193
1941 assert.isTrue = function (val, msg) {
1954 new Assertion(val, msg).is.true;
196 };
197
198 /**
199 * # .isFalse(value, [message])
200 *
201 * Assert `value` is false.
202 *
203 * var tea_served = false;
204 * assert.isFalse(tea_served, 'no tea yet? hmm...');
205 *
206 * @name isFalse
207 * @param {Boolean} value
208 * @param {String} message
209 * @api public
210 */
211
2121 assert.isFalse = function (val, msg) {
2133 new Assertion(val, msg).is.false;
214 };
215
216 /**
217 * # .isNull(value, [message])
218 *
219 * Assert `value` is null.
220 *
221 * assert.isNull(err, 'no errors');
222 *
223 * @name isNull
224 * @param {*} value
225 * @param {String} message
226 * @api public
227 */
228
2291 assert.isNull = function (val, msg) {
2302 new Assertion(val, msg).to.equal(null);
231 };
232
233 /**
234 * # .isNotNull(value, [message])
235 *
236 * Assert `value` is not null.
237 *
238 * var tea = 'tasty chai';
239 * assert.isNotNull(tea, 'great, time for tea!');
240 *
241 * @name isNotNull
242 * @param {*} value
243 * @param {String} message
244 * @api public
245 */
246
2471 assert.isNotNull = function (val, msg) {
2482 new Assertion(val, msg).to.not.equal(null);
249 };
250
251 /**
252 * # .isUndefined(value, [message])
253 *
254 * Assert `value` is undefined.
255 *
256 * assert.isUndefined(tea, 'no tea defined');
257 *
258 * @name isUndefined
259 * @param {*} value
260 * @param {String} message
261 * @api public
262 */
263
2641 assert.isUndefined = function (val, msg) {
2652 new Assertion(val, msg).to.equal(undefined);
266 };
267
268 /**
269 * # .isFunction(value, [message])
270 *
271 * Assert `value` is a function.
272 *
273 * var serve_tea = function () { return 'cup of tea'; };
274 * assert.isFunction(serve_tea, 'great, we can have tea now');
275 *
276 * @name isFunction
277 * @param {Function} value
278 * @param {String} message
279 * @api public
280 */
281
2821 assert.isFunction = function (val, msg) {
2832 new Assertion(val, msg).to.be.a('function');
284 };
285
286 /**
287 * # .isObject(value, [message])
288 *
289 * Assert `value` is an object.
290 *
291 * var selection = { name: 'Chai', serve: 'with spices' };
292 * assert.isObject(selection, 'tea selection is an object');
293 *
294 * @name isObject
295 * @param {Object} value
296 * @param {String} message
297 * @api public
298 */
299
3001 assert.isObject = function (val, msg) {
3015 new Assertion(val, msg).to.be.a('object');
302 };
303
304 /**
305 * # .isArray(value, [message])
306 *
307 * Assert `value` is an instance of Array.
308 *
309 * var menu = [ 'green', 'chai', 'oolong' ];
310 * assert.isArray(menu, 'what kind of tea do we want?');
311 *
312 * @name isArray
313 * @param {*} value
314 * @param {String} message
315 * @api public
316 */
317
3181 assert.isArray = function (val, msg) {
3193 new Assertion(val, msg).to.be.instanceof(Array);
320 };
321
322 /**
323 * # .isString(value, [message])
324 *
325 * Assert `value` is a string.
326 *
327 * var teaorder = 'chai';
328 * assert.isString(tea_order, 'order placed');
329 *
330 * @name isString
331 * @param {String} value
332 * @param {String} message
333 * @api public
334 */
335
3361 assert.isString = function (val, msg) {
3373 new Assertion(val, msg).to.be.a('string');
338 };
339
340 /**
341 * # .isNumber(value, [message])
342 *
343 * Assert `value` is a number
344 *
345 * var cups = 2;
346 * assert.isNumber(cups, 'how many cups');
347 *
348 * @name isNumber
349 * @param {Number} value
350 * @param {String} message
351 * @api public
352 */
353
3541 assert.isNumber = function (val, msg) {
3553 new Assertion(val, msg).to.be.a('number');
356 };
357
358 /**
359 * # .isBoolean(value, [message])
360 *
361 * Assert `value` is a boolean
362 *
363 * var teaready = true
364 * , teaserved = false;
365 *
366 * assert.isBoolean(tea_ready, 'is the tea ready');
367 * assert.isBoolean(tea_served, 'has tea been served');
368 *
369 * @name isBoolean
370 * @param {*} value
371 * @param {String} message
372 * @api public
373 */
374
3751 assert.isBoolean = function (val, msg) {
3763 new Assertion(val, msg).to.be.a('boolean');
377 };
378
379 /**
380 * # .typeOf(value, name, [message])
381 *
382 * Assert typeof `value` is `name`.
383 *
384 * assert.typeOf('tea', 'string', 'we have a string');
385 *
386 * @name typeOf
387 * @param {*} value
388 * @param {String} typeof name
389 * @param {String} message
390 * @api public
391 */
392
3931 assert.typeOf = function (val, type, msg) {
3944 new Assertion(val, msg).to.be.a(type);
395 };
396
397 /**
398 * # .instanceOf(object, constructor, [message])
399 *
400 * Assert `value` is instanceof `constructor`.
401 *
402 * var Tea = function (name) { this.name = name; }
403 * , Chai = new Tea('chai');
404 *
405 * assert.instanceOf(Chai, Tea, 'chai is an instance of tea');
406 *
407 * @name instanceOf
408 * @param {Object} object
409 * @param {Constructor} constructor
410 * @param {String} message
411 * @api public
412 */
413
4141 assert.instanceOf = function (val, type, msg) {
4152 new Assertion(val, msg).to.be.instanceof(type);
416 };
417
418 /**
419 * # .include(value, includes, [message])
420 *
421 * Assert the inclusion of an object in another. Works
422 * for strings and arrays.
423 *
424 * assert.include('foobar', 'bar', 'foobar contains string `var`);
425 * assert.include([ 1, 2, 3], 3, 'array contains value);
426 *
427 * @name include
428 * @param {Array|String} value
429 * @param {*} includes
430 * @param {String} message
431 * @api public
432 */
433
4341 assert.include = function (exp, inc, msg) {
4354 var obj = new Assertion(exp, msg);
436
4374 if (Array.isArray(exp)) {
4381 obj.to.include(inc);
4393 } else if ('string' === typeof exp) {
4403 obj.to.contain.string(inc);
441 }
442 };
443
444 /**
445 * # .match(value, regex, [message])
446 *
447 * Assert that `value` matches regular expression.
448 *
449 * assert.match('foobar', /^foo/, 'Regexp matches');
450 *
451 * @name match
452 * @param {*} value
453 * @param {RegExp} RegularExpression
454 * @param {String} message
455 * @api public
456 */
457
4581 assert.match = function (exp, re, msg) {
4591 new Assertion(exp, msg).to.match(re);
460 };
461
462 /**
463 * # .length(value, constructor, [message])
464 *
465 * Assert that object has expected length.
466 *
467 * assert.length([1,2,3], 3, 'Array has length of 3');
468 * assert.length('foobar', 5, 'String has length of 6');
469 *
470 * @name length
471 * @param {*} value
472 * @param {Number} length
473 * @param {String} message
474 * @api public
475 */
476
4771 assert.length = function (exp, len, msg) {
4784 new Assertion(exp, msg).to.have.length(len);
479 };
480
481 /**
482 * # .throws(function, [constructor/regexp], [message])
483 *
484 * Assert that a function will throw a specific
485 * type of error.
486 *
487 * assert.throw(fn, ReferenceError, 'function throw reference error');
488 *
489 * @name throws
490 * @alias throw
491 * @param {Function} function to test
492 * @param {ErrorConstructor} constructor
493 * @param {String} message
494 * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
495 * @api public
496 */
497
4981 assert.throws = function (fn, type, msg) {
4993 if ('string' === typeof type) {
5001 msg = type;
5011 type = null;
502 }
503
5043 new Assertion(fn, msg).to.throw(type);
505 };
506
507 /**
508 * # .doesNotThrow(function, [constructor/regexp], [message])
509 *
510 * Assert that a function will throw a specific
511 * type of error.
512 *
513 * var fn = function (err) { if (err) throw Error(err) };
514 * assert.doesNotThrow(fn, Error, 'function throw reference error');
515 *
516 * @name doesNotThrow
517 * @param {Function} function to test
518 * @param {ErrorConstructor} constructor
519 * @param {String} message
520 * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
521 * @api public
522 */
523
5241 assert.doesNotThrow = function (fn, type, msg) {
5253 if ('string' === typeof type) {
5261 msg = type;
5271 type = null;
528 }
529
5303 new Assertion(fn, msg).to.not.throw(type);
531 };
532
533 /*!
534 * Undocumented / untested
535 */
536
5371 assert.ifError = function (val, msg) {
5384 new Assertion(val, msg).to.not.be.ok;
539 };
540
541 /*!
542 * Aliases.
543 */
544
5451 (function alias(name, as){
5462 assert[as] = assert[name];
5472 return alias;
548 })
549 ('length', 'lengthOf')
550 ('throws', 'throw');
551};