forked from shouldjs/should.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshould.js
More file actions
3532 lines (3021 loc) · 99.4 KB
/
should.js
File metadata and controls
3532 lines (3021 loc) · 99.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* should - test framework agnostic BDD-style assertions
* @version v7.1.1
* @author TJ Holowaychuk <tj@vision-media.ca> and contributors
* @link https://github.com/shouldjs/should.js
* @license MIT
*/
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Should = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var should = require('./lib/should');
var defaultProto = Object.prototype;
var defaultProperty = 'should';
//Expose api via `Object#should`.
try {
var prevShould = should.extend(defaultProperty, defaultProto);
should._prevShould = prevShould;
} catch(e) {
//ignore errors
}
module.exports = should;
},{"./lib/should":17}],2:[function(require,module,exports){
var util = require('./util');
/**
* should AssertionError
* @param {Object} options
* @constructor
* @memberOf should
* @static
*/
var AssertionError = function AssertionError(options) {
util.merge(this, options);
if(!options.message) {
Object.defineProperty(this, 'message', {
get: function() {
if(!this._message) {
this._message = this.generateMessage();
this.generatedMessage = true;
}
return this._message;
},
configurable: true,
enumerable: false
}
);
}
if(Error.captureStackTrace) {
Error.captureStackTrace(this, this.stackStartFunction);
} else {
// non v8 browsers so we can have a stacktrace
var err = new Error();
if(err.stack) {
var out = err.stack;
if(this.stackStartFunction) {
// try to strip useless frames
var fn_name = util.functionName(this.stackStartFunction);
var idx = out.indexOf('\n' + fn_name);
if(idx >= 0) {
// once we have located the function frame
// we need to strip out everything before it (and its line)
var next_line = out.indexOf('\n', idx + 1);
out = out.substring(next_line + 1);
}
}
this.stack = out;
}
}
};
var indent = ' ';
function prependIndent(line) {
return indent + line;
}
function indentLines(text) {
return text.split('\n').map(prependIndent).join('\n');
}
// assert.AssertionError instanceof Error
AssertionError.prototype = Object.create(Error.prototype, {
name: {
value: 'AssertionError'
},
generateMessage: {
value: function() {
if(!this.operator && this.previous) {
return this.previous.message;
}
var actual = util.format(this.actual);
var expected = 'expected' in this ? ' ' + util.format(this.expected) : '';
var details = 'details' in this && this.details ? ' (' + this.details + ')' : '';
var previous = this.previous ? '\n' + indentLines(this.previous.message) : '';
return 'expected ' + actual + (this.negate ? ' not ' : ' ') + this.operator + expected + details + previous;
}
}
});
module.exports = AssertionError;
},{"./util":18}],3:[function(require,module,exports){
var AssertionError = require('./assertion-error');
var util = require('./util');
/**
* should Assertion
* @param {*} obj Given object for assertion
* @constructor
* @memberOf should
* @static
*/
function Assertion(obj) {
this.obj = obj;
this.anyOne = false;
this.negate = false;
this.params = {actual: obj};
}
/**
* Way to extend Assertion function. It uses some logic
* to define only positive assertions and itself rule with negative assertion.
*
* All actions happen in subcontext and this method take care about negation.
* Potentially we can add some more modifiers that does not depends from state of assertion.
* @memberOf Assertion
* @category assertion
* @static
* @param {String} name Name of assertion. It will be used for defining method or getter on Assertion.prototype
* @param {Function} func Function that will be called on executing assertion
* @example
*
* 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');
* });
*/
Assertion.add = function(name, func) {
var prop = {enumerable: true, configurable: true};
prop.value = function() {
var context = new Assertion(this.obj, this, name);
context.anyOne = this.anyOne;
try {
func.apply(context, arguments);
} catch(e) {
//check for fail
if(e instanceof AssertionError) {
//negative fail
if(this.negate) {
this.obj = context.obj;
this.negate = false;
return this;
}
if(context !== e.assertion) {
context.params.previous = e;
}
//positive fail
context.negate = false;
context.fail();
}
// throw if it is another exception
throw e;
}
//negative pass
if(this.negate) {
context.negate = true;//because .fail will set negate
context.params.details = 'false negative fail';
context.fail();
}
//positive pass
if(!this.params.operator) this.params = context.params;//shortcut
this.obj = context.obj;
this.negate = false;
return this;
};
Object.defineProperty(Assertion.prototype, name, prop);
};
Assertion.addChain = function(name, onCall) {
onCall = onCall || function() {
};
Object.defineProperty(Assertion.prototype, name, {
get: function() {
onCall();
return this;
},
enumerable: true
});
};
/**
* Create alias for some `Assertion` property
*
* @memberOf Assertion
* @category assertion
* @static
* @param {String} from Name of to map
* @param {String} to Name of alias
* @example
*
* Assertion.alias('true', 'True');
*/
Assertion.alias = function(from, to) {
var desc = Object.getOwnPropertyDescriptor(Assertion.prototype, from);
if(!desc) throw new Error('Alias ' + from + ' -> ' + to + ' could not be created as ' + from + ' not defined');
Object.defineProperty(Assertion.prototype, to, desc);
};
Assertion.prototype = {
constructor: Assertion,
/**
* Base method for assertions. Before calling this method need to fill Assertion#params object. This method usually called from other assertion methods.
* `Assertion#params` can contain such properties:
* * `operator` - required string containing description of this assertion
* * `obj` - optional replacement for this.obj, it usefull if you prepare more clear object then given
* * `message` - if this property filled with string any others will be ignored and this one used as assertion message
* * `expected` - any object used when you need to assert relation between given object and expected. Like given == expected (== is a relation)
* * `details` - additional string with details to generated message
*
* @memberOf Assertion
* @category assertion
* @param {*} expr Any expression that will be used as a condition for asserting.
* @example
*
* var a = new should.Assertion(42);
*
* a.params = {
* operator: 'to be magic number',
* }
*
* a.assert(false);
* //throws AssertionError: expected 42 to be magic number
*/
assert: function(expr) {
if(expr) return this;
var params = this.params;
if('obj' in params && !('actual' in params)) {
params.actual = params.obj;
} else if(!('obj' in params) && !('actual' in params)) {
params.actual = this.obj;
}
params.stackStartFunction = params.stackStartFunction || this.assert;
params.negate = this.negate;
params.assertion = this;
throw new AssertionError(params);
},
/**
* Shortcut for `Assertion#assert(false)`.
*
* @memberOf Assertion
* @category assertion
* @example
*
* var a = new should.Assertion(42);
*
* a.params = {
* operator: 'to be magic number',
* }
*
* a.fail();
* //throws AssertionError: expected 42 to be magic number
*/
fail: function() {
return this.assert(false);
},
/**
* Negation modifier. Current assertion chain become negated. Each call invert negation on current assertion.
*
* @memberOf Assertion
* @category assertion
*/
get not() {
this.negate = !this.negate;
return this;
},
/**
* Any modifier - it affect on execution of sequenced assertion to do not `check all`, but `check any of`.
*
* @memberOf Assertion
* @category assertion
*/
get any() {
this.anyOne = true;
return this;
}
};
module.exports = Assertion;
},{"./assertion-error":2,"./util":18}],4:[function(require,module,exports){
var Formatter = require('should-format').Formatter;
var config = {
checkProtoEql: false,
getFormatter: function(opts) {
return new Formatter(opts || config);
}
};
module.exports = config;
},{"should-format":21}],5:[function(require,module,exports){
// implement assert interface using already written peaces of should.js
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
//
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
//
// Originally from narwhal.js (http://narwhaljs.org)
// Copyright (c) 2009 Thomas Robinson <280north.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// when used in node, this will actually load the util module we depend on
// versus loading the builtin util module as happens otherwise
// this is a bug in node module loading as far as I am concerned
var Assertion = require('./../assertion');
var _deepEqual = require('should-equal');
var pSlice = Array.prototype.slice;
// 1. The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
// assert module must conform to the following interface.
var assert = module.exports = ok;
// 3. All of the following functions must throw an AssertionError
// when a corresponding condition is not met, with a message that
// may be undefined if not provided. All assertion methods provide
// both the actual and expected values to the assertion error for
// display purposes.
/**
* Node.js standard [`assert.fail`](http://nodejs.org/api/assert.html#assert_assert_fail_actual_expected_message_operator).
* @static
* @memberOf should
* @category assertion assert
* @param {*} actual Actual object
* @param {*} expected Expected object
* @param {string} message Message for assertion
* @param {string} operator Operator text
*/
function fail(actual, expected, message, operator, stackStartFunction) {
var a = new Assertion(actual);
a.params = {
operator: operator,
expected: expected,
message: message,
stackStartFunction: stackStartFunction || fail
};
a.fail();
}
// EXTENSION! allows for well behaved errors defined elsewhere.
assert.fail = fail;
// 4. Pure assertion tests whether a value is truthy, as determined
// by !!guard.
// assert.ok(guard, message_opt);
// This statement is equivalent to assert.equal(true, !!guard,
// message_opt);. To test strictly for the value true, use
// assert.strictEqual(true, guard, message_opt);.
/**
* Node.js standard [`assert.ok`](http://nodejs.org/api/assert.html#assert_assert_value_message_assert_ok_value_message).
* @static
* @memberOf should
* @category assertion assert
* @param {*} value
* @param {string} [message]
*/
function ok(value, message) {
if(!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;
// 5. The equality assertion tests shallow, coercive equality with
// ==.
// assert.equal(actual, expected, message_opt);
/**
* Node.js standard [`assert.equal`](http://nodejs.org/api/assert.html#assert_assert_equal_actual_expected_message).
* @static
* @memberOf should
* @category assertion assert
* @param {*} actual
* @param {*} expected
* @param {string} [message]
*/
assert.equal = function equal(actual, expected, message) {
if(actual != expected) fail(actual, expected, message, '==', assert.equal);
};
// 6. The non-equality assertion tests for whether two objects are not equal
// with != assert.notEqual(actual, expected, message_opt);
/**
* Node.js standard [`assert.notEqual`](http://nodejs.org/api/assert.html#assert_assert_notequal_actual_expected_message).
* @static
* @memberOf should
* @category assertion assert
* @param {*} actual
* @param {*} expected
* @param {string} [message]
*/
assert.notEqual = function notEqual(actual, expected, message) {
if(actual == expected) {
fail(actual, expected, message, '!=', assert.notEqual);
}
};
// 7. The equivalence assertion tests a deep equality relation.
// assert.deepEqual(actual, expected, message_opt);
/**
* Node.js standard [`assert.deepEqual`](http://nodejs.org/api/assert.html#assert_assert_deepequal_actual_expected_message).
* @static
* @memberOf should
* @category assertion assert
* @param {*} actual
* @param {*} expected
* @param {string} [message]
*/
assert.deepEqual = function deepEqual(actual, expected, message) {
if(!_deepEqual(actual, expected).result) {
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
}
};
// 8. The non-equivalence assertion tests for any deep inequality.
// assert.notDeepEqual(actual, expected, message_opt);
/**
* Node.js standard [`assert.notDeepEqual`](http://nodejs.org/api/assert.html#assert_assert_notdeepequal_actual_expected_message).
* @static
* @memberOf should
* @category assertion assert
* @param {*} actual
* @param {*} expected
* @param {string} [message]
*/
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if(_deepEqual(actual, expected).result) {
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
}
};
// 9. The strict equality assertion tests strict equality, as determined by ===.
// assert.strictEqual(actual, expected, message_opt);
/**
* Node.js standard [`assert.strictEqual`](http://nodejs.org/api/assert.html#assert_assert_strictequal_actual_expected_message).
* @static
* @memberOf should
* @category assertion assert
* @param {*} actual
* @param {*} expected
* @param {string} [message]
*/
assert.strictEqual = function strictEqual(actual, expected, message) {
if(actual !== expected) {
fail(actual, expected, message, '===', assert.strictEqual);
}
};
// 10. The strict non-equality assertion tests for strict inequality, as
// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
/**
* Node.js standard [`assert.notStrictEqual`](http://nodejs.org/api/assert.html#assert_assert_notstrictequal_actual_expected_message).
* @static
* @memberOf should
* @category assertion assert
* @param {*} actual
* @param {*} expected
* @param {string} [message]
*/
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if(actual === expected) {
fail(actual, expected, message, '!==', assert.notStrictEqual);
}
};
function expectedException(actual, expected) {
if(!actual || !expected) {
return false;
}
if(Object.prototype.toString.call(expected) == '[object RegExp]') {
return expected.test(actual);
} else if(actual instanceof expected) {
return true;
} else if(expected.call({}, actual) === true) {
return true;
}
return false;
}
function _throws(shouldThrow, block, expected, message) {
var actual;
if(typeof expected == 'string') {
message = expected;
expected = null;
}
try {
block();
} catch(e) {
actual = e;
}
message = (expected && expected.name ? ' (' + expected.name + ')' : '.') +
(message ? ' ' + message : '.');
if(shouldThrow && !actual) {
fail(actual, expected, 'Missing expected exception' + message);
}
if(!shouldThrow && expectedException(actual, expected)) {
fail(actual, expected, 'Got unwanted exception' + message);
}
if((shouldThrow && actual && expected && !expectedException(actual, expected)) || (!shouldThrow && actual)) {
throw actual;
}
}
// 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt);
/**
* Node.js standard [`assert.throws`](http://nodejs.org/api/assert.html#assert_assert_throws_block_error_message).
* @static
* @memberOf should
* @category assertion assert
* @param {Function} block
* @param {Function} [error]
* @param {String} [message]
*/
assert.throws = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [true].concat(pSlice.call(arguments)));
};
// EXTENSION! This is annoying to write outside this module.
/**
* Node.js standard [`assert.doesNotThrow`](http://nodejs.org/api/assert.html#assert_assert_doesnotthrow_block_message).
* @static
* @memberOf should
* @category assertion assert
* @param {Function} block
* @param {String} [message]
*/
assert.doesNotThrow = function(block, /*optional*/message) {
_throws.apply(this, [false].concat(pSlice.call(arguments)));
};
/**
* Node.js standard [`assert.ifError`](http://nodejs.org/api/assert.html#assert_assert_iferror_value).
* @static
* @memberOf should
* @category assertion assert
* @param {Error} err
*/
assert.ifError = function(err) {
if(err) {
throw err;
}
};
},{"./../assertion":3,"should-equal":20}],6:[function(require,module,exports){
/*
* Should
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
var util = require('../util')
, assert = require('./_assert')
, AssertionError = require('../assertion-error');
module.exports = function(should) {
var i = should.format;
/*
* Expose assert to should
*
* This allows you to do things like below
* without require()ing the assert module.
*
* should.equal(foo.bar, undefined);
*
*/
util.merge(should, assert);
/**
* Assert _obj_ exists, with optional message.
*
* @static
* @memberOf should
* @category assertion assert
* @alias should.exists
* @param {*} obj
* @param {String} [msg]
* @example
*
* should.exist(1);
* should.exist(new Date());
*/
should.exist = should.exists = function(obj, msg) {
if(null == obj) {
throw new AssertionError({
message: msg || ('expected ' + i(obj) + ' to exist'), stackStartFunction: should.exist
});
}
};
should.not = {};
/**
* Asserts _obj_ does not exist, with optional message.
*
* @name not.exist
* @static
* @memberOf should
* @category assertion assert
* @alias should.not.exists
* @param {*} obj
* @param {String} [msg]
* @example
*
* should.not.exist(null);
* should.not.exist(void 0);
*/
should.not.exist = should.not.exists = function(obj, msg) {
if(null != obj) {
throw new AssertionError({
message: msg || ('expected ' + i(obj) + ' to not exist'), stackStartFunction: should.not.exist
});
}
};
};
},{"../assertion-error":2,"../util":18,"./_assert":5}],7:[function(require,module,exports){
/*
* Should
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
module.exports = function(should, Assertion) {
/**
* Assert given object is exactly `true`.
*
* @name true
* @memberOf Assertion
* @category assertion bool
* @alias Assertion#True
* @example
*
* (true).should.be.true();
* false.should.not.be.true();
*
* ({ a: 10}).should.not.be.true();
*/
Assertion.add('true', function() {
this.is.exactly(true);
});
Assertion.alias('true', 'True');
/**
* Assert given object is exactly `false`.
*
* @name false
* @memberOf Assertion
* @category assertion bool
* @alias Assertion#False
* @example
*
* (true).should.not.be.false();
* false.should.be.false();
*/
Assertion.add('false', function() {
this.is.exactly(false);
});
Assertion.alias('false', 'False');
/**
* Assert given object is thuthy according javascript type conversions.
*
* @name ok
* @memberOf Assertion
* @category assertion bool
* @example
*
* (true).should.be.ok();
* ''.should.not.be.ok();
* should(null).not.be.ok();
* should(void 0).not.be.ok();
*
* (10).should.be.ok();
* (0).should.not.be.ok();
*/
Assertion.add('ok', function() {
this.params = { operator: 'to be truthy' };
this.assert(this.obj);
});
};
},{}],8:[function(require,module,exports){
module.exports = function(should, Assertion) {
/**
* Simple chaining. It actually do nothing.
*
* @memberOf Assertion
* @name be
* @property {should.Assertion} be
* @alias Assertion#an
* @alias Assertion#of
* @alias Assertion#a
* @alias Assertion#and
* @alias Assertion#have
* @alias Assertion#has
* @alias Assertion#with
* @alias Assertion#is
* @alias Assertion#which
* @alias Assertion#the
* @alias Assertion#it
* @category assertion chaining
*/
['an', 'of', 'a', 'and', 'be', 'has', 'have', 'with', 'is', 'which', 'the', 'it'].forEach(function(name) {
Assertion.addChain(name);
});
};
},{}],9:[function(require,module,exports){
/*
* Should
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
var util = require('../util');
var eql = require('should-equal');
module.exports = function(should, Assertion) {
var i = should.format;
/**
* Assert that given object contain something that equal to `other`. It uses `should-equal` for equality checks.
* If given object is array it search that one of elements was equal to `other`.
* If given object is string it checks if `other` is a substring - expected that `other` is a string.
* If given object is Object it checks that `other` is a subobject - expected that `other` is a object.
*
* @name containEql
* @memberOf Assertion
* @category assertion contain
* @param {*} other Nested object
* @example
*
* [1, 2, 3].should.containEql(1);
* [{ a: 1 }, 'a', 10].should.containEql({ a: 1 });
*
* 'abc'.should.containEql('b');
* 'ab1c'.should.containEql(1);
*
* ({ a: 10, c: { d: 10 }}).should.containEql({ a: 10 });
* ({ a: 10, c: { d: 10 }}).should.containEql({ c: { d: 10 }});
* ({ a: 10, c: { d: 10 }}).should.containEql({ b: 10 });
* // throws AssertionError: expected { a: 10, c: { d: 10 } } to contain { b: 10 }
* // expected { a: 10, c: { d: 10 } } to have property b
*/
Assertion.add('containEql', function(other) {
this.params = {operator: 'to contain ' + i(other)};
this.is.not.null().and.not.undefined();
var obj = this.obj;
if(typeof obj == 'string') {
this.assert(obj.indexOf(String(other)) >= 0);
} else if(util.isIndexable(obj)) {
this.assert(util.some(obj, function(v) {
return eql(v, other).result;
}));
} else {
this.have.properties(other);
}
});
/**
* Assert that given object is contain equally structured object on the same depth level.
* If given object is an array and `other` is an array it checks that the eql elements is going in the same sequence in given array (recursive)
* If given object is an object it checks that the same keys contain deep equal values (recursive)
* On other cases it try to check with `.eql`
*
* @name containDeepOrdered
* @memberOf Assertion
* @category assertion contain
* @param {*} other Nested object
* @example
*
* [ 1, 2, 3].should.containDeepOrdered([1, 2]);
* [ 1, 2, [ 1, 2, 3 ]].should.containDeepOrdered([ 1, [ 2, 3 ]]);
*
* ({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({a: 10});
* ({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({b: {c: 10}});
* ({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({b: {d: [1, 3]}});
*/
Assertion.add('containDeepOrdered', function(other) {
this.params = {operator: 'to contain ' + i(other)};
var obj = this.obj;
if(typeof obj == 'string') {// expect other to be string
this.is.equal(String(other));
} else if(util.isIndexable(obj) && util.isIndexable(other)) {
for(var objIdx = 0, otherIdx = 0, objLength = util.length(obj), otherLength = util.length(other); objIdx < objLength && otherIdx < otherLength; objIdx++) {
try {
should(obj[objIdx]).containDeepOrdered(other[otherIdx]);
otherIdx++;
} catch(e) {
if(e instanceof should.AssertionError) {
continue;
}
throw e;
}
}
this.assert(otherIdx === otherLength);
} else if(obj != null && other != null && typeof obj == 'object' && typeof other == 'object') {// object contains object case
util.forEach(other, function(value, key) {
should(obj[key]).containDeepOrdered(value);
});
// if both objects is empty means we finish traversing - and we need to compare for hidden values
if(util.isEmptyObject(other)) {
this.eql(other);
}
} else {
this.eql(other);
}
});
/**
* The same like `Assertion#containDeepOrdered` but all checks on arrays without order.
*
* @name containDeep
* @memberOf Assertion
* @category assertion contain
* @param {*} other Nested object
* @example
*
* [ 1, 2, 3].should.containDeep([2, 1]);
* [ 1, 2, [ 1, 2, 3 ]].should.containDeep([ 1, [ 3, 1 ]]);
*/
Assertion.add('containDeep', function(other) {
this.params = {operator: 'to contain ' + i(other)};
var obj = this.obj;
if(typeof obj == 'string') {// expect other to be string
this.is.equal(String(other));
} else if(util.isIndexable(obj) && util.isIndexable(other)) {
var usedKeys = {};
util.forEach(other, function(otherItem) {
this.assert(util.some(obj, function(item, index) {
if(index in usedKeys) return false;
try {
should(item).containDeep(otherItem);
usedKeys[index] = true;
return true;
} catch(e) {
if(e instanceof should.AssertionError) {
return false;
}
throw e;
}
}));
}, this);
} else if(obj != null && other != null && typeof obj == 'object' && typeof other == 'object') {// object contains object case
util.forEach(other, function(value, key) {
should(obj[key]).containDeep(value);
});
// if both objects is empty means we finish traversing - and we need to compare for hidden values
if(util.isEmptyObject(other)) {
this.eql(other);
}
} else {
this.eql(other);
}
});
};
},{"../util":18,"should-equal":20}],10:[function(require,module,exports){
/*
* Should
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
var eql = require('should-equal');
var type = require('should-type');
var util = require('../util');
function formatEqlResult(r, a, b) {
return ((r.path.length > 0 ? 'at ' + r.path.map(util.formatProp).join(' -> ') : '') +
(r.a === a ? '' : ', A has ' + util.format(r.a)) +
(r.b === b ? '' : ' and B has ' + util.format(r.b)) +
(r.showReason ? ' because ' + r.reason : '')).trim();
}
module.exports = function(should, Assertion) {
/**
* Deep object equality comparison. For full spec see [`should-equal tests`](https://github.com/shouldjs/equal/blob/master/test.js).
*
* @name eql
* @memberOf Assertion
* @category assertion equality
* @alias Assertion#deepEqual
* @param {*} val Expected value
* @param {string} [description] Optional message
* @example
*
* (10).should.be.eql(10);
* ('10').should.not.be.eql(10);
* (-0).should.not.be.eql(+0);
*
* NaN.should.be.eql(NaN);
*
* ({ a: 10}).should.be.eql({ a: 10 });
* [ 'a' ].should.not.be.eql({ '0': 'a' });
*/
Assertion.add('eql', function(val, description) {
this.params = {operator: 'to equal', expected: val, message: description};
var result = eql(this.obj, val, should.config);
this.params.details = result.result ? '' : formatEqlResult(result, this.obj, val);
this.params.showDiff = eql(type(this.obj), type(val)).result;
this.assert(result.result);
});
/**
* Exact comparison using ===.
*
* @name equal
* @memberOf Assertion
* @category assertion equality
* @alias Assertion#exactly
* @param {*} val Expected value
* @param {string} [description] Optional message
* @example