forked from sinonjs/sinon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch-test.js
1321 lines (1003 loc) · 44.1 KB
/
match-test.js
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
"use strict";
var referee = require("@sinonjs/referee");
var assert = referee.assert;
var refute = referee.refute;
var sinonMatch = require("../lib/sinon/match");
function propertyMatcherTests(matcher, additionalTests) {
return function () {
it("returns matcher", function () {
var has = matcher("foo");
assert(sinonMatch.isMatcher(has));
});
it("throws if first argument is not string", function () {
assert.exception(function () {
matcher();
}, {name: "TypeError"});
assert.exception(function () {
matcher(123);
}, {name: "TypeError"});
});
it("returns false if value is undefined or null", function () {
var has = matcher("foo");
assert.isFalse(has.test(undefined));
assert.isFalse(has.test(null));
});
it("returns true if object has property", function () {
var has = matcher("foo");
assert(has.test({ foo: null }));
});
it("returns false if object value is not equal to given value", function () {
var has = matcher("foo", 1);
assert.isFalse(has.test({ foo: null }));
});
it("returns true if object value is equal to given value", function () {
var has = matcher("message", "sinon rocks");
assert(has.test({ message: "sinon rocks" }));
assert(has.test(new Error("sinon rocks")));
});
it("returns true if string property matches", function () {
var has = matcher("length", 5);
assert(has.test("sinon"));
});
it("allows to expect undefined", function () {
var has = matcher("foo", undefined);
assert.isFalse(has.test({ foo: 1 }));
});
it("compares value deeply", function () {
var has = matcher("foo", { bar: "doo", test: 42 });
assert(has.test({ foo: { bar: "doo", test: 42 } }));
});
it("compares with matcher", function () {
var has = matcher("callback", sinonMatch.typeOf("function"));
assert(has.test({ callback: function () {} }));
});
if (typeof additionalTests === "function") {
additionalTests();
}
};
}
describe("sinonMatch", function () {
it("returns matcher", function () {
var match = sinonMatch(function () {});
assert(sinonMatch.isMatcher(match));
});
it("exposes test function", function () {
var test = function () {};
var match = sinonMatch(test);
assert.same(match.test, test);
});
it("returns true if properties are equal", function () {
var match = sinonMatch({ str: "sinon", nr: 1 });
assert(match.test({ str: "sinon", nr: 1, other: "ignored" }));
});
it("returns true if properties are deep equal", function () {
var match = sinonMatch({ deep: { str: "sinon" } });
assert(match.test({ deep: { str: "sinon", ignored: "value" } }));
});
it("returns false if a property is not equal", function () {
var match = sinonMatch({ str: "sinon", nr: 1 });
assert.isFalse(match.test({ str: "sinon", nr: 2 }));
});
it("returns false if a property is missing", function () {
var match = sinonMatch({ str: "sinon", nr: 1 });
assert.isFalse(match.test({ nr: 1 }));
});
it("returns true if array is equal", function () {
var match = sinonMatch({ arr: ["a", "b"] });
assert(match.test({ arr: ["a", "b"] }));
});
it("returns false if array is not equal", function () {
var match = sinonMatch({ arr: ["b", "a"] });
assert.isFalse(match.test({ arr: ["a", "b"] }));
});
it("returns false if array is not equal (even if the contents would match (deep equal))", function () {
var match = sinonMatch([{ str: "sinon" }]);
assert.isFalse(match.test([{ str: "sinon", ignored: "value" }]));
});
it("returns true if number objects are equal", function () {
/*eslint-disable no-new-wrappers*/
var match = sinonMatch({ one: new Number(1) });
assert(match.test({ one: new Number(1) }));
/*eslint-enable no-new-wrappers*/
});
it("returns true if test matches", function () {
var match = sinonMatch({ prop: sinonMatch.typeOf("boolean") });
assert(match.test({ prop: true }));
});
it("returns false if test does not match", function () {
var match = sinonMatch({ prop: sinonMatch.typeOf("boolean") });
assert.isFalse(match.test({ prop: "no" }));
});
it("returns true if deep test matches", function () {
var match = sinonMatch({ deep: { prop: sinonMatch.typeOf("boolean") } });
assert(match.test({ deep: { prop: true } }));
});
it("returns false if deep test does not match", function () {
var match = sinonMatch({ deep: { prop: sinonMatch.typeOf("boolean") } });
assert.isFalse(match.test({ deep: { prop: "no" } }));
});
it("returns false if tested value is null or undefined", function () {
var match = sinonMatch({});
assert.isFalse(match.test(null));
assert.isFalse(match.test(undefined));
});
it("returns true if error message matches", function () {
var match = sinonMatch({ message: "evil error" });
assert(match.test(new Error("evil error")));
});
it("returns true if string property matches", function () {
var match = sinonMatch({ length: 5 });
assert(match.test("sinon"));
});
it("returns true if number property matches", function () {
var match = sinonMatch({ toFixed: sinonMatch.func });
assert(match.test(0));
});
it("returns true for string match", function () {
var match = sinonMatch("sinon");
assert(match.test("sinon"));
});
it("returns true for substring match", function () {
var match = sinonMatch("no");
assert(match.test("sinon"));
});
it("returns false for string mismatch", function () {
var match = sinonMatch("Sinon.JS");
assert.isFalse(match.test(null));
assert.isFalse(match.test({}));
assert.isFalse(match.test("sinon"));
assert.isFalse(match.test("sinon.js"));
});
it("returns true for regexp match", function () {
var match = sinonMatch(/^[sino]+$/);
assert(match.test("sinon"));
});
it("returns false for regexp string mismatch", function () {
var match = sinonMatch(/^[sin]+$/);
assert.isFalse(match.test("sinon"));
});
it("returns false for regexp type mismatch", function () {
var match = sinonMatch(/.*/);
assert.isFalse(match.test());
assert.isFalse(match.test(null));
assert.isFalse(match.test(123));
assert.isFalse(match.test({}));
});
it("returns true for number match", function () {
var match = sinonMatch(1);
assert(match.test(1));
assert(match.test("1"));
assert(match.test(true));
});
it("returns false for number mismatch", function () {
var match = sinonMatch(1);
assert.isFalse(match.test());
assert.isFalse(match.test(null));
assert.isFalse(match.test(2));
assert.isFalse(match.test(false));
assert.isFalse(match.test({}));
});
it("returns true for Symbol match", function () {
if (typeof Symbol === "function") {
var symbol = Symbol();
var match = sinonMatch(symbol);
assert(match.test(symbol));
}
});
it("returns false for Symbol mismatch", function () {
if (typeof Symbol === "function") {
var match = sinonMatch(Symbol());
assert.isFalse(match.test());
assert.isFalse(match.test(Symbol(null)));
assert.isFalse(match.test(Symbol()));
assert.isFalse(match.test(Symbol({})));
}
});
it("returns true for Symbol inside object", function () {
if (typeof Symbol === "function") {
var symbol = Symbol();
var match = sinonMatch({ prop: symbol });
assert(match.test({ prop: symbol }));
}
});
it("returns true if test function in object returns true", function () {
var match = sinonMatch({ test: function () {
return true;
}});
assert(match.test());
});
it("returns false if test function in object returns false", function () {
var match = sinonMatch({ test: function () {
return false;
}});
assert.isFalse(match.test());
});
it("returns false if test function in object returns nothing", function () {
var match = sinonMatch({ test: function () {}});
assert.isFalse(match.test());
});
it("passes actual value to test function in object", function () {
var match = sinonMatch({ test: function (arg) {
return arg;
}});
assert(match.test(true));
});
it("uses matcher", function () {
var match = sinonMatch(sinonMatch("test"));
assert(match.test("testing"));
});
describe(".toString", function () {
it("returns message", function () {
var message = "hello sinonMatch";
var match = sinonMatch(function () {}, message);
assert.same(match.toString(), message);
});
it("defaults to match(functionName)", function () {
var match = sinonMatch(function custom() {});
assert.same(match.toString(), "match(custom)");
});
});
describe(".any", function () {
it("is matcher", function () {
assert(sinonMatch.isMatcher(sinonMatch.any));
});
it("returns true when tested", function () {
assert(sinonMatch.any.test());
});
});
describe(".defined", function () {
it("is matcher", function () {
assert(sinonMatch.isMatcher(sinonMatch.defined));
});
it("returns false if test is called with null", function () {
assert.isFalse(sinonMatch.defined.test(null));
});
it("returns false if test is called with undefined", function () {
assert.isFalse(sinonMatch.defined.test(undefined));
});
it("returns true if test is called with any value", function () {
assert(sinonMatch.defined.test(false));
assert(sinonMatch.defined.test(true));
assert(sinonMatch.defined.test(0));
assert(sinonMatch.defined.test(1));
assert(sinonMatch.defined.test(""));
});
it("returns true if test is called with any object", function () {
assert(sinonMatch.defined.test({}));
assert(sinonMatch.defined.test(function () {}));
});
});
describe(".truthy", function () {
it("is matcher", function () {
assert(sinonMatch.isMatcher(sinonMatch.truthy));
});
it("returns true if test is called with trueish value", function () {
assert(sinonMatch.truthy.test(true));
assert(sinonMatch.truthy.test(1));
assert(sinonMatch.truthy.test("yes"));
});
it("returns false if test is called falsy value", function () {
assert.isFalse(sinonMatch.truthy.test(false));
assert.isFalse(sinonMatch.truthy.test(null));
assert.isFalse(sinonMatch.truthy.test(undefined));
assert.isFalse(sinonMatch.truthy.test(""));
});
});
describe(".falsy", function () {
it("is matcher", function () {
assert(sinonMatch.isMatcher(sinonMatch.falsy));
});
it("returns true if test is called falsy value", function () {
assert(sinonMatch.falsy.test(false));
assert(sinonMatch.falsy.test(null));
assert(sinonMatch.falsy.test(undefined));
assert(sinonMatch.falsy.test(""));
});
it("returns false if test is called with trueish value", function () {
assert.isFalse(sinonMatch.falsy.test(true));
assert.isFalse(sinonMatch.falsy.test(1));
assert.isFalse(sinonMatch.falsy.test("yes"));
});
});
describe(".same", function () {
it("returns matcher", function () {
var same = sinonMatch.same();
assert(sinonMatch.isMatcher(same));
});
it("returns true if test is called with same argument", function () {
var object = {};
var same = sinonMatch.same(object);
assert(same.test(object));
});
it("returns true if test is called with same symbol", function () {
if (typeof Symbol === "function") {
var symbol = Symbol();
var same = sinonMatch.same(symbol);
assert(same.test(symbol));
}
});
it("returns false if test is not called with same argument", function () {
var same = sinonMatch.same({});
assert.isFalse(same.test({}));
});
});
describe(".in", function () {
it("returns matcher", function () {
var inMatcher = sinonMatch.in([]);
assert(sinonMatch.isMatcher(inMatcher));
});
it("throws if given argument is not an array", function () {
var arg = "not-array";
assert.exception(function () {
sinonMatch.in(arg);
}, {name: "TypeError", message: "array expected"});
});
describe("when given argument is an array", function () {
var arrays = [
[1, 2, 3],
["a", "b", "c"],
[{ a: "a" }, { b: "b"}],
[function () {}, function () {}],
[null, undefined]
];
it("returns true if the tested value in the given array", function () {
arrays.forEach(function (array) {
var inMatcher = sinonMatch.in(array);
assert.isTrue(inMatcher.test(array[0]));
});
});
it("returns false if the tested value not in the given array", function () {
arrays.forEach(function (array) {
var inMatcher = sinonMatch.in(array);
assert.isFalse(inMatcher.test("something else"));
});
});
});
});
describe(".typeOf", function () {
it("throws if given argument is not a string", function () {
assert.exception(function () {
sinonMatch.typeOf();
}, {name: "TypeError"});
assert.exception(function () {
sinonMatch.typeOf(123);
}, {name: "TypeError"});
});
it("returns matcher", function () {
var typeOf = sinonMatch.typeOf("string");
assert(sinonMatch.isMatcher(typeOf));
});
it("returns true if test is called with string", function () {
var typeOf = sinonMatch.typeOf("string");
assert(typeOf.test("Sinon.JS"));
});
it("returns false if test is not called with string", function () {
var typeOf = sinonMatch.typeOf("string");
assert.isFalse(typeOf.test(123));
});
it("returns true if test is called with symbol", function () {
if (typeof Symbol === "function") {
var typeOf = sinonMatch.typeOf("symbol");
assert(typeOf.test(Symbol()));
}
});
it("returns true if test is called with regexp", function () {
var typeOf = sinonMatch.typeOf("regexp");
assert(typeOf.test(/.+/));
});
it("returns false if test is not called with regexp", function () {
var typeOf = sinonMatch.typeOf("regexp");
assert.isFalse(typeOf.test(true));
});
});
describe(".instanceOf", function () {
it("throws if given argument is not a function", function () {
assert.exception(function () {
sinonMatch.instanceOf();
}, {name: "TypeError"});
assert.exception(function () {
sinonMatch.instanceOf("foo");
}, {name: "TypeError"});
});
if (typeof Symbol !== "undefined" && typeof Symbol.hasInstance !== "undefined") {
it("does not throw if given argument defines Symbol.hasInstance", function () {
var objectWithCustomTypeChecks = {};
objectWithCustomTypeChecks[Symbol.hasInstance] = function () {};
sinonMatch.instanceOf(objectWithCustomTypeChecks);
});
}
it("returns matcher", function () {
var instanceOf = sinonMatch.instanceOf(function () {});
assert(sinonMatch.isMatcher(instanceOf));
});
it("returns true if test is called with instance of argument", function () {
var instanceOf = sinonMatch.instanceOf(Array);
assert(instanceOf.test([]));
});
it("returns false if test is not called with instance of argument", function () {
var instanceOf = sinonMatch.instanceOf(Array);
assert.isFalse(instanceOf.test({}));
});
});
describe(".has", propertyMatcherTests(sinonMatch.has));
describe(".hasOwn", propertyMatcherTests(sinonMatch.hasOwn));
describe(".hasNested", propertyMatcherTests(sinonMatch.hasNested, function () {
it("compares nested value", function () {
var hasNested = sinonMatch.hasNested("foo.bar", "doo");
assert(hasNested.test({ foo: { bar: "doo" } }));
});
it("compares nested array value", function () {
var hasNested = sinonMatch.hasNested("foo[0].bar", "doo");
assert(hasNested.test({ foo: [{ bar: "doo" }] }));
});
}));
describe(".hasSpecial", function () {
it("returns true if object has inherited property", function () {
var has = sinonMatch.has("toString");
assert(has.test({}));
});
it("only includes property in message", function () {
var has = sinonMatch.has("test");
assert.equals(has.toString(), "has(\"test\")");
});
it("includes property and value in message", function () {
var has = sinonMatch.has("test", undefined);
assert.equals(has.toString(), "has(\"test\", undefined)");
});
it("returns true if string function matches", function () {
var has = sinonMatch.has("toUpperCase", sinonMatch.func);
assert(has.test("sinon"));
});
it("returns true if number function matches", function () {
var has = sinonMatch.has("toFixed", sinonMatch.func);
assert(has.test(0));
});
it("returns true if object has Symbol", function () {
if (typeof Symbol === "function") {
var symbol = Symbol();
var has = sinonMatch.has("prop", symbol);
assert(has.test({ prop: symbol }));
}
});
it("returns true if embedded object has Symbol", function () {
if (typeof Symbol === "function") {
var symbol = Symbol();
var has = sinonMatch.has("prop", sinonMatch.has("embedded", symbol));
assert(has.test({ prop: { embedded: symbol }, ignored: 42 }));
}
});
});
describe(".hasOwnSpecial", function () {
it("returns false if object has inherited property", function () {
var hasOwn = sinonMatch.hasOwn("toString");
assert.isFalse(hasOwn.test({}));
});
it("only includes property in message", function () {
var hasOwn = sinonMatch.hasOwn("test");
assert.equals(hasOwn.toString(), "hasOwn(\"test\")");
});
it("includes property and value in message", function () {
var hasOwn = sinonMatch.hasOwn("test", undefined);
assert.equals(hasOwn.toString(), "hasOwn(\"test\", undefined)");
});
});
describe(".every", function () {
it("throws if given argument is not a matcher", function () {
assert.exception(function () {
sinonMatch.every({});
}, {name: "TypeError"});
assert.exception(function () {
sinonMatch.every(123);
}, {name: "TypeError"});
assert.exception(function () {
sinonMatch.every("123");
}, {name: "TypeError"});
});
it("returns matcher", function () {
var every = sinonMatch.every(sinonMatch.any);
assert(sinonMatch.isMatcher(every));
});
it("wraps the given matcher message with an \"every()\"", function () {
var every = sinonMatch.every(sinonMatch.number);
assert.equals(every.toString(), "every(typeOf(\"number\"))");
});
it("fails to match anything that is not an object or an iterable", function () {
var every = sinonMatch.every(sinonMatch.any);
refute(every.test(1));
refute(every.test("a"));
refute(every.test(null));
refute(every.test(function () {}));
});
it("matches an object if the predicate is true for every property", function () {
var every = sinonMatch.every(sinonMatch.number);
assert(every.test({a: 1, b: 2}));
});
it("fails if the predicate is false for some of the object properties", function () {
var every = sinonMatch.every(sinonMatch.number);
refute(every.test({a: 1, b: "b"}));
});
it("matches an array if the predicate is true for every element", function () {
var every = sinonMatch.every(sinonMatch.number);
assert(every.test([1, 2]));
});
it("fails if the predicate is false for some of the array elements", function () {
var every = sinonMatch.every(sinonMatch.number);
refute(every.test([1, "b"]));
});
if (typeof Set === "function") {
it("matches an iterable if the predicate is true for every element", function () {
var every = sinonMatch.every(sinonMatch.number);
var set = new Set();
set.add(1);
set.add(2);
assert(every.test(set));
});
it("fails if the predicate is false for some of the iterable elements", function () {
var every = sinonMatch.every(sinonMatch.number);
var set = new Set();
set.add(1);
set.add("b");
refute(every.test(set));
});
}
});
describe(".some", function () {
it("throws if given argument is not a matcher", function () {
assert.exception(function () {
sinonMatch.some({});
}, {name: "TypeError"});
assert.exception(function () {
sinonMatch.some(123);
}, {name: "TypeError"});
assert.exception(function () {
sinonMatch.some("123");
}, {name: "TypeError"});
});
it("returns matcher", function () {
var some = sinonMatch.some(sinonMatch.any);
assert(sinonMatch.isMatcher(some));
});
it("wraps the given matcher message with an \"some()\"", function () {
var some = sinonMatch.some(sinonMatch.number);
assert.equals(some.toString(), "some(typeOf(\"number\"))");
});
it("fails to match anything that is not an object or an iterable", function () {
var some = sinonMatch.some(sinonMatch.any);
refute(some.test(1));
refute(some.test("a"));
refute(some.test(null));
refute(some.test(function () {}));
});
it("matches an object if the predicate is true for some of the properties", function () {
var some = sinonMatch.some(sinonMatch.number);
assert(some.test({a: 1, b: "b"}));
});
it("fails if the predicate is false for all of the object properties", function () {
var some = sinonMatch.some(sinonMatch.number);
refute(some.test({a: "a", b: "b"}));
});
it("matches an array if the predicate is true for some element", function () {
var some = sinonMatch.some(sinonMatch.number);
assert(some.test([1, "b"]));
});
it("fails if the predicate is false for all of the array elements", function () {
var some = sinonMatch.some(sinonMatch.number);
refute(some.test(["a", "b"]));
});
if (typeof Set === "function") {
it("matches an iterable if the predicate is true for some element", function () {
var some = sinonMatch.some(sinonMatch.number);
var set = new Set();
set.add(1);
set.add("b");
assert(some.test(set));
});
it("fails if the predicate is false for all of the iterable elements", function () {
var some = sinonMatch.some(sinonMatch.number);
var set = new Set();
set.add("a");
set.add("b");
refute(some.test(set));
});
}
});
describe(".bool", function () {
it("is typeOf boolean matcher", function () {
var bool = sinonMatch.bool;
assert(sinonMatch.isMatcher(bool));
assert.equals(bool.toString(), "typeOf(\"boolean\")");
});
});
describe(".number", function () {
it("is typeOf number matcher", function () {
var number = sinonMatch.number;
assert(sinonMatch.isMatcher(number));
assert.equals(number.toString(), "typeOf(\"number\")");
});
});
describe(".string", function () {
it("is typeOf string matcher", function () {
var string = sinonMatch.string;
assert(sinonMatch.isMatcher(string));
assert.equals(string.toString(), "typeOf(\"string\")");
});
});
describe(".object", function () {
it("is typeOf object matcher", function () {
var object = sinonMatch.object;
assert(sinonMatch.isMatcher(object));
assert.equals(object.toString(), "typeOf(\"object\")");
});
});
describe(".func", function () {
it("is typeOf function matcher", function () {
var func = sinonMatch.func;
assert(sinonMatch.isMatcher(func));
assert.equals(func.toString(), "typeOf(\"function\")");
});
});
describe(".array", function () {
it("is typeOf array matcher", function () {
var array = sinonMatch.array;
assert(sinonMatch.isMatcher(array));
assert.equals(array.toString(), "typeOf(\"array\")");
});
describe("array.deepEquals", function () {
it("has a .deepEquals matcher", function () {
var deepEquals = sinonMatch.array.deepEquals([1, 2, 3]);
assert(sinonMatch.isMatcher(deepEquals));
assert.equals(deepEquals.toString(), "deepEquals([1,2,3])");
});
it("matches arrays with the exact same elements", function () {
var deepEquals = sinonMatch.array.deepEquals([1, 2, 3]);
assert(deepEquals.test([1, 2, 3]));
assert.isFalse(deepEquals.test([1, 2]));
assert.isFalse(deepEquals.test([3]));
});
it("fails when passed a non-array object", function () {
var deepEquals = sinonMatch.array.deepEquals(["one", "two", "three"]);
assert.isFalse(deepEquals.test({0: "one", 1: "two", 2: "three", length: 3}));
});
});
describe("array.startsWith", function () {
it("has a .startsWith matcher", function () {
var startsWith = sinonMatch.array.startsWith([1, 2]);
assert(sinonMatch.isMatcher(startsWith));
assert.equals(startsWith.toString(), "startsWith([1,2])");
});
it("matches arrays starting with the same elements", function () {
assert(sinonMatch.array.startsWith([1]).test([1, 2]));
assert(sinonMatch.array.startsWith([1, 2]).test([1, 2]));
assert.isFalse(sinonMatch.array.startsWith([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.startsWith([2]).test([1, 2]));
});
it("fails when passed a non-array object", function () {
var startsWith = sinonMatch.array.startsWith(["one", "two"]);
assert.isFalse(startsWith.test({0: "one", 1: "two", 2: "three", length: 3}));
});
});
describe("array.endsWith", function () {
it("has an .endsWith matcher", function () {
var endsWith = sinonMatch.array.endsWith([2, 3]);
assert(sinonMatch.isMatcher(endsWith));
assert.equals(endsWith.toString(), "endsWith([2,3])");
});
it("matches arrays ending with the same elements", function () {
assert(sinonMatch.array.endsWith([2]).test([1, 2]));
assert(sinonMatch.array.endsWith([1, 2]).test([1, 2]));
assert.isFalse(sinonMatch.array.endsWith([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.endsWith([3]).test([1, 2]));
});
it("fails when passed a non-array object", function () {
var endsWith = sinonMatch.array.endsWith(["two", "three"]);
assert.isFalse(endsWith.test({0: "one", 1: "two", 2: "three", length: 3}));
});
});
describe("array.contains", function () {
it("has a .contains matcher", function () {
var contains = sinonMatch.array.contains([2, 3]);
assert(sinonMatch.isMatcher(contains));
assert.equals(contains.toString(), "contains([2,3])");
});
it("matches arrays containing all the expected elements", function () {
assert(sinonMatch.array.contains([2]).test([1, 2, 3]));
assert(sinonMatch.array.contains([1, 2]).test([1, 2]));
assert.isFalse(sinonMatch.array.contains([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.contains([3]).test([1, 2]));
});
it("fails when passed a non-array object", function () {
var contains = sinonMatch.array.contains(["one", "three"]);
assert.isFalse(contains.test({0: "one", 1: "two", 2: "three", length: 3}));
});
});
});
describe(".map", function () {
it("is typeOf map matcher", function () {
var map = sinonMatch.map;
assert(sinonMatch.isMatcher(map));
assert.equals(map.toString(), "typeOf(\"map\")");
});
describe("map.deepEquals", function () {
if (typeof Map === "function") {
it("has a .deepEquals matcher", function () {
var mapOne = new Map();
mapOne.set("one", 1);
mapOne.set("two", 2);
mapOne.set("three", 3);
var deepEquals = sinonMatch.map.deepEquals(mapOne);
assert(sinonMatch.isMatcher(deepEquals));
assert.equals(deepEquals.toString(), "deepEquals(Map[['one',1],['two',2],['three',3]])");
});
it("matches maps with the exact same elements", function () {
var mapOne = new Map();
mapOne.set("one", 1);
mapOne.set("two", 2);
mapOne.set("three", 3);
var mapTwo = new Map();
mapTwo.set("one", 1);
mapTwo.set("two", 2);
mapTwo.set("three", 3);
var mapThree = new Map();
mapThree.set("one", 1);
mapThree.set("two", 2);
var deepEquals = sinonMatch.map.deepEquals(mapOne);
assert(deepEquals.test(mapTwo));
assert.isFalse(deepEquals.test(mapThree));
assert.isFalse(deepEquals.test(new Map()));
});