konacha-chai-matchers 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitmodules +33 -27
- data/lib/konacha-chai-matchers/version.rb +1 -1
- data/tasks/update.rb +39 -12
- data/vendor/assets/javascripts/chai-fuzzy.js +90 -0
- data/vendor/assets/javascripts/chai-things.js +143 -0
- data/vendor/assets/javascripts/sinon.js +1 -3429
- metadata +11 -3
@@ -1,38 +1,3 @@
|
|
1
|
-
/**
|
2
|
-
* Sinon.JS 1.5.2, 2013/01/14
|
3
|
-
*
|
4
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
5
|
-
* @author Contributors: https://github.com/cjohansen/Sinon.JS/blob/master/AUTHORS
|
6
|
-
*
|
7
|
-
* (The BSD License)
|
8
|
-
*
|
9
|
-
* Copyright (c) 2010-2012, Christian Johansen, christian@cjohansen.no
|
10
|
-
* All rights reserved.
|
11
|
-
*
|
12
|
-
* Redistribution and use in source and binary forms, with or without modification,
|
13
|
-
* are permitted provided that the following conditions are met:
|
14
|
-
*
|
15
|
-
* * Redistributions of source code must retain the above copyright notice,
|
16
|
-
* this list of conditions and the following disclaimer.
|
17
|
-
* * Redistributions in binary form must reproduce the above copyright notice,
|
18
|
-
* this list of conditions and the following disclaimer in the documentation
|
19
|
-
* and/or other materials provided with the distribution.
|
20
|
-
* * Neither the name of Christian Johansen nor the names of his contributors
|
21
|
-
* may be used to endorse or promote products derived from this software
|
22
|
-
* without specific prior written permission.
|
23
|
-
*
|
24
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
25
|
-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
26
|
-
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
27
|
-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
28
|
-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
29
|
-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
30
|
-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
31
|
-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
32
|
-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
33
|
-
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
-
*/
|
35
|
-
|
36
1
|
/*jslint eqeqeq: false, onevar: false, forin: true, nomen: false, regexp: false, plusplus: false*/
|
37
2
|
/*global module, require, __dirname, document*/
|
38
3
|
/**
|
@@ -43,6 +8,7 @@
|
|
43
8
|
*
|
44
9
|
* Copyright (c) 2010-2011 Christian Johansen
|
45
10
|
*/
|
11
|
+
"use strict";
|
46
12
|
|
47
13
|
var sinon = (function (buster) {
|
48
14
|
var div = typeof document != "undefined" && document.createElement("div");
|
@@ -375,3397 +341,3 @@ var sinon = (function (buster) {
|
|
375
341
|
|
376
342
|
return sinon;
|
377
343
|
}(typeof buster == "object" && buster));
|
378
|
-
|
379
|
-
/* @depend ../sinon.js */
|
380
|
-
/*jslint eqeqeq: false, onevar: false, plusplus: false*/
|
381
|
-
/*global module, require, sinon*/
|
382
|
-
/**
|
383
|
-
* Match functions
|
384
|
-
*
|
385
|
-
* @author Maximilian Antoni (mail@maxantoni.de)
|
386
|
-
* @license BSD
|
387
|
-
*
|
388
|
-
* Copyright (c) 2012 Maximilian Antoni
|
389
|
-
*/
|
390
|
-
|
391
|
-
(function (sinon) {
|
392
|
-
var commonJSModule = typeof module == "object" && typeof require == "function";
|
393
|
-
|
394
|
-
if (!sinon && commonJSModule) {
|
395
|
-
sinon = require("../sinon");
|
396
|
-
}
|
397
|
-
|
398
|
-
if (!sinon) {
|
399
|
-
return;
|
400
|
-
}
|
401
|
-
|
402
|
-
function assertType(value, type, name) {
|
403
|
-
var actual = sinon.typeOf(value);
|
404
|
-
if (actual !== type) {
|
405
|
-
throw new TypeError("Expected type of " + name + " to be " +
|
406
|
-
type + ", but was " + actual);
|
407
|
-
}
|
408
|
-
}
|
409
|
-
|
410
|
-
var matcher = {
|
411
|
-
toString: function () {
|
412
|
-
return this.message;
|
413
|
-
}
|
414
|
-
};
|
415
|
-
|
416
|
-
function isMatcher(object) {
|
417
|
-
return matcher.isPrototypeOf(object);
|
418
|
-
}
|
419
|
-
|
420
|
-
function matchObject(expectation, actual) {
|
421
|
-
if (actual === null || actual === undefined) {
|
422
|
-
return false;
|
423
|
-
}
|
424
|
-
for (var key in expectation) {
|
425
|
-
if (expectation.hasOwnProperty(key)) {
|
426
|
-
var exp = expectation[key];
|
427
|
-
var act = actual[key];
|
428
|
-
if (match.isMatcher(exp)) {
|
429
|
-
if (!exp.test(act)) {
|
430
|
-
return false;
|
431
|
-
}
|
432
|
-
} else if (sinon.typeOf(exp) === "object") {
|
433
|
-
if (!matchObject(exp, act)) {
|
434
|
-
return false;
|
435
|
-
}
|
436
|
-
} else if (!sinon.deepEqual(exp, act)) {
|
437
|
-
return false;
|
438
|
-
}
|
439
|
-
}
|
440
|
-
}
|
441
|
-
return true;
|
442
|
-
}
|
443
|
-
|
444
|
-
matcher.or = function (m2) {
|
445
|
-
if (!isMatcher(m2)) {
|
446
|
-
throw new TypeError("Matcher expected");
|
447
|
-
}
|
448
|
-
var m1 = this;
|
449
|
-
var or = sinon.create(matcher);
|
450
|
-
or.test = function (actual) {
|
451
|
-
return m1.test(actual) || m2.test(actual);
|
452
|
-
};
|
453
|
-
or.message = m1.message + ".or(" + m2.message + ")";
|
454
|
-
return or;
|
455
|
-
};
|
456
|
-
|
457
|
-
matcher.and = function (m2) {
|
458
|
-
if (!isMatcher(m2)) {
|
459
|
-
throw new TypeError("Matcher expected");
|
460
|
-
}
|
461
|
-
var m1 = this;
|
462
|
-
var and = sinon.create(matcher);
|
463
|
-
and.test = function (actual) {
|
464
|
-
return m1.test(actual) && m2.test(actual);
|
465
|
-
};
|
466
|
-
and.message = m1.message + ".and(" + m2.message + ")";
|
467
|
-
return and;
|
468
|
-
};
|
469
|
-
|
470
|
-
var match = function (expectation, message) {
|
471
|
-
var m = sinon.create(matcher);
|
472
|
-
var type = sinon.typeOf(expectation);
|
473
|
-
switch (type) {
|
474
|
-
case "object":
|
475
|
-
if (typeof expectation.test === "function") {
|
476
|
-
m.test = function (actual) {
|
477
|
-
return expectation.test(actual) === true;
|
478
|
-
};
|
479
|
-
m.message = "match(" + sinon.functionName(expectation.test) + ")";
|
480
|
-
return m;
|
481
|
-
}
|
482
|
-
var str = [];
|
483
|
-
for (var key in expectation) {
|
484
|
-
if (expectation.hasOwnProperty(key)) {
|
485
|
-
str.push(key + ": " + expectation[key]);
|
486
|
-
}
|
487
|
-
}
|
488
|
-
m.test = function (actual) {
|
489
|
-
return matchObject(expectation, actual);
|
490
|
-
};
|
491
|
-
m.message = "match(" + str.join(", ") + ")";
|
492
|
-
break;
|
493
|
-
case "number":
|
494
|
-
m.test = function (actual) {
|
495
|
-
return expectation == actual;
|
496
|
-
};
|
497
|
-
break;
|
498
|
-
case "string":
|
499
|
-
m.test = function (actual) {
|
500
|
-
if (typeof actual !== "string") {
|
501
|
-
return false;
|
502
|
-
}
|
503
|
-
return actual.indexOf(expectation) !== -1;
|
504
|
-
};
|
505
|
-
m.message = "match(\"" + expectation + "\")";
|
506
|
-
break;
|
507
|
-
case "regexp":
|
508
|
-
m.test = function (actual) {
|
509
|
-
if (typeof actual !== "string") {
|
510
|
-
return false;
|
511
|
-
}
|
512
|
-
return expectation.test(actual);
|
513
|
-
};
|
514
|
-
break;
|
515
|
-
case "function":
|
516
|
-
m.test = expectation;
|
517
|
-
if (message) {
|
518
|
-
m.message = message;
|
519
|
-
} else {
|
520
|
-
m.message = "match(" + sinon.functionName(expectation) + ")";
|
521
|
-
}
|
522
|
-
break;
|
523
|
-
default:
|
524
|
-
m.test = function (actual) {
|
525
|
-
return sinon.deepEqual(expectation, actual);
|
526
|
-
};
|
527
|
-
}
|
528
|
-
if (!m.message) {
|
529
|
-
m.message = "match(" + expectation + ")";
|
530
|
-
}
|
531
|
-
return m;
|
532
|
-
};
|
533
|
-
|
534
|
-
match.isMatcher = isMatcher;
|
535
|
-
|
536
|
-
match.any = match(function () {
|
537
|
-
return true;
|
538
|
-
}, "any");
|
539
|
-
|
540
|
-
match.defined = match(function (actual) {
|
541
|
-
return actual !== null && actual !== undefined;
|
542
|
-
}, "defined");
|
543
|
-
|
544
|
-
match.truthy = match(function (actual) {
|
545
|
-
return !!actual;
|
546
|
-
}, "truthy");
|
547
|
-
|
548
|
-
match.falsy = match(function (actual) {
|
549
|
-
return !actual;
|
550
|
-
}, "falsy");
|
551
|
-
|
552
|
-
match.same = function (expectation) {
|
553
|
-
return match(function (actual) {
|
554
|
-
return expectation === actual;
|
555
|
-
}, "same(" + expectation + ")");
|
556
|
-
};
|
557
|
-
|
558
|
-
match.typeOf = function (type) {
|
559
|
-
assertType(type, "string", "type");
|
560
|
-
return match(function (actual) {
|
561
|
-
return sinon.typeOf(actual) === type;
|
562
|
-
}, "typeOf(\"" + type + "\")");
|
563
|
-
};
|
564
|
-
|
565
|
-
match.instanceOf = function (type) {
|
566
|
-
assertType(type, "function", "type");
|
567
|
-
return match(function (actual) {
|
568
|
-
return actual instanceof type;
|
569
|
-
}, "instanceOf(" + sinon.functionName(type) + ")");
|
570
|
-
};
|
571
|
-
|
572
|
-
function createPropertyMatcher(propertyTest, messagePrefix) {
|
573
|
-
return function (property, value) {
|
574
|
-
assertType(property, "string", "property");
|
575
|
-
var onlyProperty = arguments.length === 1;
|
576
|
-
var message = messagePrefix + "(\"" + property + "\"";
|
577
|
-
if (!onlyProperty) {
|
578
|
-
message += ", " + value;
|
579
|
-
}
|
580
|
-
message += ")";
|
581
|
-
return match(function (actual) {
|
582
|
-
if (actual === undefined || actual === null ||
|
583
|
-
!propertyTest(actual, property)) {
|
584
|
-
return false;
|
585
|
-
}
|
586
|
-
return onlyProperty || sinon.deepEqual(value, actual[property]);
|
587
|
-
}, message);
|
588
|
-
};
|
589
|
-
}
|
590
|
-
|
591
|
-
match.has = createPropertyMatcher(function (actual, property) {
|
592
|
-
if (typeof actual === "object") {
|
593
|
-
return property in actual;
|
594
|
-
}
|
595
|
-
return actual[property] !== undefined;
|
596
|
-
}, "has");
|
597
|
-
|
598
|
-
match.hasOwn = createPropertyMatcher(function (actual, property) {
|
599
|
-
return actual.hasOwnProperty(property);
|
600
|
-
}, "hasOwn");
|
601
|
-
|
602
|
-
match.bool = match.typeOf("boolean");
|
603
|
-
match.number = match.typeOf("number");
|
604
|
-
match.string = match.typeOf("string");
|
605
|
-
match.object = match.typeOf("object");
|
606
|
-
match.func = match.typeOf("function");
|
607
|
-
match.array = match.typeOf("array");
|
608
|
-
match.regexp = match.typeOf("regexp");
|
609
|
-
match.date = match.typeOf("date");
|
610
|
-
|
611
|
-
if (commonJSModule) {
|
612
|
-
module.exports = match;
|
613
|
-
} else {
|
614
|
-
sinon.match = match;
|
615
|
-
}
|
616
|
-
}(typeof sinon == "object" && sinon || null));
|
617
|
-
|
618
|
-
/**
|
619
|
-
* @depend ../sinon.js
|
620
|
-
* @depend match.js
|
621
|
-
*/
|
622
|
-
/*jslint eqeqeq: false, onevar: false, plusplus: false*/
|
623
|
-
/*global module, require, sinon*/
|
624
|
-
/**
|
625
|
-
* Spy functions
|
626
|
-
*
|
627
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
628
|
-
* @license BSD
|
629
|
-
*
|
630
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
631
|
-
*/
|
632
|
-
|
633
|
-
(function (sinon) {
|
634
|
-
var commonJSModule = typeof module == "object" && typeof require == "function";
|
635
|
-
var spyCall;
|
636
|
-
var callId = 0;
|
637
|
-
var push = [].push;
|
638
|
-
var slice = Array.prototype.slice;
|
639
|
-
|
640
|
-
if (!sinon && commonJSModule) {
|
641
|
-
sinon = require("../sinon");
|
642
|
-
}
|
643
|
-
|
644
|
-
if (!sinon) {
|
645
|
-
return;
|
646
|
-
}
|
647
|
-
|
648
|
-
function spy(object, property) {
|
649
|
-
if (!property && typeof object == "function") {
|
650
|
-
return spy.create(object);
|
651
|
-
}
|
652
|
-
|
653
|
-
if (!object && !property) {
|
654
|
-
return spy.create(function () {});
|
655
|
-
}
|
656
|
-
|
657
|
-
var method = object[property];
|
658
|
-
return sinon.wrapMethod(object, property, spy.create(method));
|
659
|
-
}
|
660
|
-
|
661
|
-
sinon.extend(spy, (function () {
|
662
|
-
|
663
|
-
function delegateToCalls(api, method, matchAny, actual, notCalled) {
|
664
|
-
api[method] = function () {
|
665
|
-
if (!this.called) {
|
666
|
-
if (notCalled) {
|
667
|
-
return notCalled.apply(this, arguments);
|
668
|
-
}
|
669
|
-
return false;
|
670
|
-
}
|
671
|
-
|
672
|
-
var currentCall;
|
673
|
-
var matches = 0;
|
674
|
-
|
675
|
-
for (var i = 0, l = this.callCount; i < l; i += 1) {
|
676
|
-
currentCall = this.getCall(i);
|
677
|
-
|
678
|
-
if (currentCall[actual || method].apply(currentCall, arguments)) {
|
679
|
-
matches += 1;
|
680
|
-
|
681
|
-
if (matchAny) {
|
682
|
-
return true;
|
683
|
-
}
|
684
|
-
}
|
685
|
-
}
|
686
|
-
|
687
|
-
return matches === this.callCount;
|
688
|
-
};
|
689
|
-
}
|
690
|
-
|
691
|
-
function matchingFake(fakes, args, strict) {
|
692
|
-
if (!fakes) {
|
693
|
-
return;
|
694
|
-
}
|
695
|
-
|
696
|
-
var alen = args.length;
|
697
|
-
|
698
|
-
for (var i = 0, l = fakes.length; i < l; i++) {
|
699
|
-
if (fakes[i].matches(args, strict)) {
|
700
|
-
return fakes[i];
|
701
|
-
}
|
702
|
-
}
|
703
|
-
}
|
704
|
-
|
705
|
-
function incrementCallCount() {
|
706
|
-
this.called = true;
|
707
|
-
this.callCount += 1;
|
708
|
-
this.notCalled = false;
|
709
|
-
this.calledOnce = this.callCount == 1;
|
710
|
-
this.calledTwice = this.callCount == 2;
|
711
|
-
this.calledThrice = this.callCount == 3;
|
712
|
-
}
|
713
|
-
|
714
|
-
function createCallProperties() {
|
715
|
-
this.firstCall = this.getCall(0);
|
716
|
-
this.secondCall = this.getCall(1);
|
717
|
-
this.thirdCall = this.getCall(2);
|
718
|
-
this.lastCall = this.getCall(this.callCount - 1);
|
719
|
-
}
|
720
|
-
|
721
|
-
var vars = "a,b,c,d,e,f,g,h,i,j,k,l";
|
722
|
-
function createProxy(func) {
|
723
|
-
// Retain the function length:
|
724
|
-
var p;
|
725
|
-
if (func.length) {
|
726
|
-
eval("p = (function proxy(" + vars.substring(0, func.length * 2 - 1) +
|
727
|
-
") { return p.invoke(func, this, slice.call(arguments)); });");
|
728
|
-
}
|
729
|
-
else {
|
730
|
-
p = function proxy() {
|
731
|
-
return p.invoke(func, this, slice.call(arguments));
|
732
|
-
};
|
733
|
-
}
|
734
|
-
return p;
|
735
|
-
}
|
736
|
-
|
737
|
-
var uuid = 0;
|
738
|
-
|
739
|
-
// Public API
|
740
|
-
var spyApi = {
|
741
|
-
reset: function () {
|
742
|
-
this.called = false;
|
743
|
-
this.notCalled = true;
|
744
|
-
this.calledOnce = false;
|
745
|
-
this.calledTwice = false;
|
746
|
-
this.calledThrice = false;
|
747
|
-
this.callCount = 0;
|
748
|
-
this.firstCall = null;
|
749
|
-
this.secondCall = null;
|
750
|
-
this.thirdCall = null;
|
751
|
-
this.lastCall = null;
|
752
|
-
this.args = [];
|
753
|
-
this.returnValues = [];
|
754
|
-
this.thisValues = [];
|
755
|
-
this.exceptions = [];
|
756
|
-
this.callIds = [];
|
757
|
-
if (this.fakes) {
|
758
|
-
for (var i = 0; i < this.fakes.length; i++) {
|
759
|
-
this.fakes[i].reset();
|
760
|
-
}
|
761
|
-
}
|
762
|
-
},
|
763
|
-
|
764
|
-
create: function create(func) {
|
765
|
-
var name;
|
766
|
-
|
767
|
-
if (typeof func != "function") {
|
768
|
-
func = function () {};
|
769
|
-
} else {
|
770
|
-
name = sinon.functionName(func);
|
771
|
-
}
|
772
|
-
|
773
|
-
var proxy = createProxy(func);
|
774
|
-
|
775
|
-
sinon.extend(proxy, spy);
|
776
|
-
delete proxy.create;
|
777
|
-
sinon.extend(proxy, func);
|
778
|
-
|
779
|
-
proxy.reset();
|
780
|
-
proxy.prototype = func.prototype;
|
781
|
-
proxy.displayName = name || "spy";
|
782
|
-
proxy.toString = sinon.functionToString;
|
783
|
-
proxy._create = sinon.spy.create;
|
784
|
-
proxy.id = "spy#" + uuid++;
|
785
|
-
|
786
|
-
return proxy;
|
787
|
-
},
|
788
|
-
|
789
|
-
invoke: function invoke(func, thisValue, args) {
|
790
|
-
var matching = matchingFake(this.fakes, args);
|
791
|
-
var exception, returnValue;
|
792
|
-
|
793
|
-
incrementCallCount.call(this);
|
794
|
-
push.call(this.thisValues, thisValue);
|
795
|
-
push.call(this.args, args);
|
796
|
-
push.call(this.callIds, callId++);
|
797
|
-
|
798
|
-
try {
|
799
|
-
if (matching) {
|
800
|
-
returnValue = matching.invoke(func, thisValue, args);
|
801
|
-
} else {
|
802
|
-
returnValue = (this.func || func).apply(thisValue, args);
|
803
|
-
}
|
804
|
-
} catch (e) {
|
805
|
-
push.call(this.returnValues, undefined);
|
806
|
-
exception = e;
|
807
|
-
throw e;
|
808
|
-
} finally {
|
809
|
-
push.call(this.exceptions, exception);
|
810
|
-
}
|
811
|
-
|
812
|
-
push.call(this.returnValues, returnValue);
|
813
|
-
|
814
|
-
createCallProperties.call(this);
|
815
|
-
|
816
|
-
return returnValue;
|
817
|
-
},
|
818
|
-
|
819
|
-
getCall: function getCall(i) {
|
820
|
-
if (i < 0 || i >= this.callCount) {
|
821
|
-
return null;
|
822
|
-
}
|
823
|
-
|
824
|
-
return spyCall.create(this, this.thisValues[i], this.args[i],
|
825
|
-
this.returnValues[i], this.exceptions[i],
|
826
|
-
this.callIds[i]);
|
827
|
-
},
|
828
|
-
|
829
|
-
calledBefore: function calledBefore(spyFn) {
|
830
|
-
if (!this.called) {
|
831
|
-
return false;
|
832
|
-
}
|
833
|
-
|
834
|
-
if (!spyFn.called) {
|
835
|
-
return true;
|
836
|
-
}
|
837
|
-
|
838
|
-
return this.callIds[0] < spyFn.callIds[spyFn.callIds.length - 1];
|
839
|
-
},
|
840
|
-
|
841
|
-
calledAfter: function calledAfter(spyFn) {
|
842
|
-
if (!this.called || !spyFn.called) {
|
843
|
-
return false;
|
844
|
-
}
|
845
|
-
|
846
|
-
return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1];
|
847
|
-
},
|
848
|
-
|
849
|
-
withArgs: function () {
|
850
|
-
var args = slice.call(arguments);
|
851
|
-
|
852
|
-
if (this.fakes) {
|
853
|
-
var match = matchingFake(this.fakes, args, true);
|
854
|
-
|
855
|
-
if (match) {
|
856
|
-
return match;
|
857
|
-
}
|
858
|
-
} else {
|
859
|
-
this.fakes = [];
|
860
|
-
}
|
861
|
-
|
862
|
-
var original = this;
|
863
|
-
var fake = this._create();
|
864
|
-
fake.matchingAguments = args;
|
865
|
-
push.call(this.fakes, fake);
|
866
|
-
|
867
|
-
fake.withArgs = function () {
|
868
|
-
return original.withArgs.apply(original, arguments);
|
869
|
-
};
|
870
|
-
|
871
|
-
for (var i = 0; i < this.args.length; i++) {
|
872
|
-
if (fake.matches(this.args[i])) {
|
873
|
-
incrementCallCount.call(fake);
|
874
|
-
push.call(fake.thisValues, this.thisValues[i]);
|
875
|
-
push.call(fake.args, this.args[i]);
|
876
|
-
push.call(fake.returnValues, this.returnValues[i]);
|
877
|
-
push.call(fake.exceptions, this.exceptions[i]);
|
878
|
-
push.call(fake.callIds, this.callIds[i]);
|
879
|
-
}
|
880
|
-
}
|
881
|
-
createCallProperties.call(fake);
|
882
|
-
|
883
|
-
return fake;
|
884
|
-
},
|
885
|
-
|
886
|
-
matches: function (args, strict) {
|
887
|
-
var margs = this.matchingAguments;
|
888
|
-
|
889
|
-
if (margs.length <= args.length &&
|
890
|
-
sinon.deepEqual(margs, args.slice(0, margs.length))) {
|
891
|
-
return !strict || margs.length == args.length;
|
892
|
-
}
|
893
|
-
},
|
894
|
-
|
895
|
-
printf: function (format) {
|
896
|
-
var spy = this;
|
897
|
-
var args = slice.call(arguments, 1);
|
898
|
-
var formatter;
|
899
|
-
|
900
|
-
return (format || "").replace(/%(.)/g, function (match, specifyer) {
|
901
|
-
formatter = spyApi.formatters[specifyer];
|
902
|
-
|
903
|
-
if (typeof formatter == "function") {
|
904
|
-
return formatter.call(null, spy, args);
|
905
|
-
} else if (!isNaN(parseInt(specifyer), 10)) {
|
906
|
-
return sinon.format(args[specifyer - 1]);
|
907
|
-
}
|
908
|
-
|
909
|
-
return "%" + specifyer;
|
910
|
-
});
|
911
|
-
}
|
912
|
-
};
|
913
|
-
|
914
|
-
delegateToCalls(spyApi, "calledOn", true);
|
915
|
-
delegateToCalls(spyApi, "alwaysCalledOn", false, "calledOn");
|
916
|
-
delegateToCalls(spyApi, "calledWith", true);
|
917
|
-
delegateToCalls(spyApi, "calledWithMatch", true);
|
918
|
-
delegateToCalls(spyApi, "alwaysCalledWith", false, "calledWith");
|
919
|
-
delegateToCalls(spyApi, "alwaysCalledWithMatch", false, "calledWithMatch");
|
920
|
-
delegateToCalls(spyApi, "calledWithExactly", true);
|
921
|
-
delegateToCalls(spyApi, "alwaysCalledWithExactly", false, "calledWithExactly");
|
922
|
-
delegateToCalls(spyApi, "neverCalledWith", false, "notCalledWith",
|
923
|
-
function () { return true; });
|
924
|
-
delegateToCalls(spyApi, "neverCalledWithMatch", false, "notCalledWithMatch",
|
925
|
-
function () { return true; });
|
926
|
-
delegateToCalls(spyApi, "threw", true);
|
927
|
-
delegateToCalls(spyApi, "alwaysThrew", false, "threw");
|
928
|
-
delegateToCalls(spyApi, "returned", true);
|
929
|
-
delegateToCalls(spyApi, "alwaysReturned", false, "returned");
|
930
|
-
delegateToCalls(spyApi, "calledWithNew", true);
|
931
|
-
delegateToCalls(spyApi, "alwaysCalledWithNew", false, "calledWithNew");
|
932
|
-
delegateToCalls(spyApi, "callArg", false, "callArgWith", function () {
|
933
|
-
throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
|
934
|
-
});
|
935
|
-
spyApi.callArgWith = spyApi.callArg;
|
936
|
-
delegateToCalls(spyApi, "yield", false, "yield", function () {
|
937
|
-
throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
|
938
|
-
});
|
939
|
-
// "invokeCallback" is an alias for "yield" since "yield" is invalid in strict mode.
|
940
|
-
spyApi.invokeCallback = spyApi.yield;
|
941
|
-
delegateToCalls(spyApi, "yieldTo", false, "yieldTo", function (property) {
|
942
|
-
throw new Error(this.toString() + " cannot yield to '" + property +
|
943
|
-
"' since it was not yet invoked.");
|
944
|
-
});
|
945
|
-
|
946
|
-
spyApi.formatters = {
|
947
|
-
"c": function (spy) {
|
948
|
-
return sinon.timesInWords(spy.callCount);
|
949
|
-
},
|
950
|
-
|
951
|
-
"n": function (spy) {
|
952
|
-
return spy.toString();
|
953
|
-
},
|
954
|
-
|
955
|
-
"C": function (spy) {
|
956
|
-
var calls = [];
|
957
|
-
|
958
|
-
for (var i = 0, l = spy.callCount; i < l; ++i) {
|
959
|
-
var stringifiedCall = " " + spy.getCall(i).toString();
|
960
|
-
if (/\n/.test(calls[i - 1])) {
|
961
|
-
stringifiedCall = "\n" + stringifiedCall;
|
962
|
-
}
|
963
|
-
push.call(calls, stringifiedCall);
|
964
|
-
}
|
965
|
-
|
966
|
-
return calls.length > 0 ? "\n" + calls.join("\n") : "";
|
967
|
-
},
|
968
|
-
|
969
|
-
"t": function (spy) {
|
970
|
-
var objects = [];
|
971
|
-
|
972
|
-
for (var i = 0, l = spy.callCount; i < l; ++i) {
|
973
|
-
push.call(objects, sinon.format(spy.thisValues[i]));
|
974
|
-
}
|
975
|
-
|
976
|
-
return objects.join(", ");
|
977
|
-
},
|
978
|
-
|
979
|
-
"*": function (spy, args) {
|
980
|
-
var formatted = [];
|
981
|
-
|
982
|
-
for (var i = 0, l = args.length; i < l; ++i) {
|
983
|
-
push.call(formatted, sinon.format(args[i]));
|
984
|
-
}
|
985
|
-
|
986
|
-
return formatted.join(", ");
|
987
|
-
}
|
988
|
-
};
|
989
|
-
|
990
|
-
return spyApi;
|
991
|
-
}()));
|
992
|
-
|
993
|
-
spyCall = (function () {
|
994
|
-
|
995
|
-
function throwYieldError(proxy, text, args) {
|
996
|
-
var msg = sinon.functionName(proxy) + text;
|
997
|
-
if (args.length) {
|
998
|
-
msg += " Received [" + slice.call(args).join(", ") + "]";
|
999
|
-
}
|
1000
|
-
throw new Error(msg);
|
1001
|
-
}
|
1002
|
-
|
1003
|
-
var callApi = {
|
1004
|
-
create: function create(spy, thisValue, args, returnValue, exception, id) {
|
1005
|
-
var proxyCall = sinon.create(spyCall);
|
1006
|
-
delete proxyCall.create;
|
1007
|
-
proxyCall.proxy = spy;
|
1008
|
-
proxyCall.thisValue = thisValue;
|
1009
|
-
proxyCall.args = args;
|
1010
|
-
proxyCall.returnValue = returnValue;
|
1011
|
-
proxyCall.exception = exception;
|
1012
|
-
proxyCall.callId = typeof id == "number" && id || callId++;
|
1013
|
-
|
1014
|
-
return proxyCall;
|
1015
|
-
},
|
1016
|
-
|
1017
|
-
calledOn: function calledOn(thisValue) {
|
1018
|
-
if (sinon.match && sinon.match.isMatcher(thisValue)) {
|
1019
|
-
return thisValue.test(this.thisValue);
|
1020
|
-
}
|
1021
|
-
return this.thisValue === thisValue;
|
1022
|
-
},
|
1023
|
-
|
1024
|
-
calledWith: function calledWith() {
|
1025
|
-
for (var i = 0, l = arguments.length; i < l; i += 1) {
|
1026
|
-
if (!sinon.deepEqual(arguments[i], this.args[i])) {
|
1027
|
-
return false;
|
1028
|
-
}
|
1029
|
-
}
|
1030
|
-
|
1031
|
-
return true;
|
1032
|
-
},
|
1033
|
-
|
1034
|
-
calledWithMatch: function calledWithMatch() {
|
1035
|
-
for (var i = 0, l = arguments.length; i < l; i += 1) {
|
1036
|
-
var actual = this.args[i];
|
1037
|
-
var expectation = arguments[i];
|
1038
|
-
if (!sinon.match || !sinon.match(expectation).test(actual)) {
|
1039
|
-
return false;
|
1040
|
-
}
|
1041
|
-
}
|
1042
|
-
return true;
|
1043
|
-
},
|
1044
|
-
|
1045
|
-
calledWithExactly: function calledWithExactly() {
|
1046
|
-
return arguments.length == this.args.length &&
|
1047
|
-
this.calledWith.apply(this, arguments);
|
1048
|
-
},
|
1049
|
-
|
1050
|
-
notCalledWith: function notCalledWith() {
|
1051
|
-
return !this.calledWith.apply(this, arguments);
|
1052
|
-
},
|
1053
|
-
|
1054
|
-
notCalledWithMatch: function notCalledWithMatch() {
|
1055
|
-
return !this.calledWithMatch.apply(this, arguments);
|
1056
|
-
},
|
1057
|
-
|
1058
|
-
returned: function returned(value) {
|
1059
|
-
return sinon.deepEqual(value, this.returnValue);
|
1060
|
-
},
|
1061
|
-
|
1062
|
-
threw: function threw(error) {
|
1063
|
-
if (typeof error == "undefined" || !this.exception) {
|
1064
|
-
return !!this.exception;
|
1065
|
-
}
|
1066
|
-
|
1067
|
-
if (typeof error == "string") {
|
1068
|
-
return this.exception.name == error;
|
1069
|
-
}
|
1070
|
-
|
1071
|
-
return this.exception === error;
|
1072
|
-
},
|
1073
|
-
|
1074
|
-
calledWithNew: function calledWithNew(thisValue) {
|
1075
|
-
return this.thisValue instanceof this.proxy;
|
1076
|
-
},
|
1077
|
-
|
1078
|
-
calledBefore: function (other) {
|
1079
|
-
return this.callId < other.callId;
|
1080
|
-
},
|
1081
|
-
|
1082
|
-
calledAfter: function (other) {
|
1083
|
-
return this.callId > other.callId;
|
1084
|
-
},
|
1085
|
-
|
1086
|
-
callArg: function (pos) {
|
1087
|
-
this.args[pos]();
|
1088
|
-
},
|
1089
|
-
|
1090
|
-
callArgWith: function (pos) {
|
1091
|
-
var args = slice.call(arguments, 1);
|
1092
|
-
this.args[pos].apply(null, args);
|
1093
|
-
},
|
1094
|
-
|
1095
|
-
"yield": function () {
|
1096
|
-
var args = this.args;
|
1097
|
-
for (var i = 0, l = args.length; i < l; ++i) {
|
1098
|
-
if (typeof args[i] === "function") {
|
1099
|
-
args[i].apply(null, slice.call(arguments));
|
1100
|
-
return;
|
1101
|
-
}
|
1102
|
-
}
|
1103
|
-
throwYieldError(this.proxy, " cannot yield since no callback was passed.", args);
|
1104
|
-
},
|
1105
|
-
|
1106
|
-
yieldTo: function (prop) {
|
1107
|
-
var args = this.args;
|
1108
|
-
for (var i = 0, l = args.length; i < l; ++i) {
|
1109
|
-
if (args[i] && typeof args[i][prop] === "function") {
|
1110
|
-
args[i][prop].apply(null, slice.call(arguments, 1));
|
1111
|
-
return;
|
1112
|
-
}
|
1113
|
-
}
|
1114
|
-
throwYieldError(this.proxy, " cannot yield to '" + prop +
|
1115
|
-
"' since no callback was passed.", args);
|
1116
|
-
},
|
1117
|
-
|
1118
|
-
toString: function () {
|
1119
|
-
var callStr = this.proxy.toString() + "(";
|
1120
|
-
var args = [];
|
1121
|
-
|
1122
|
-
for (var i = 0, l = this.args.length; i < l; ++i) {
|
1123
|
-
push.call(args, sinon.format(this.args[i]));
|
1124
|
-
}
|
1125
|
-
|
1126
|
-
callStr = callStr + args.join(", ") + ")";
|
1127
|
-
|
1128
|
-
if (typeof this.returnValue != "undefined") {
|
1129
|
-
callStr += " => " + sinon.format(this.returnValue);
|
1130
|
-
}
|
1131
|
-
|
1132
|
-
if (this.exception) {
|
1133
|
-
callStr += " !" + this.exception.name;
|
1134
|
-
|
1135
|
-
if (this.exception.message) {
|
1136
|
-
callStr += "(" + this.exception.message + ")";
|
1137
|
-
}
|
1138
|
-
}
|
1139
|
-
|
1140
|
-
return callStr;
|
1141
|
-
}
|
1142
|
-
};
|
1143
|
-
callApi.invokeCallback = callApi.yield;
|
1144
|
-
return callApi;
|
1145
|
-
}());
|
1146
|
-
|
1147
|
-
spy.spyCall = spyCall;
|
1148
|
-
|
1149
|
-
// This steps outside the module sandbox and will be removed
|
1150
|
-
sinon.spyCall = spyCall;
|
1151
|
-
|
1152
|
-
if (commonJSModule) {
|
1153
|
-
module.exports = spy;
|
1154
|
-
} else {
|
1155
|
-
sinon.spy = spy;
|
1156
|
-
}
|
1157
|
-
}(typeof sinon == "object" && sinon || null));
|
1158
|
-
|
1159
|
-
/**
|
1160
|
-
* @depend ../sinon.js
|
1161
|
-
* @depend spy.js
|
1162
|
-
*/
|
1163
|
-
/*jslint eqeqeq: false, onevar: false*/
|
1164
|
-
/*global module, require, sinon*/
|
1165
|
-
/**
|
1166
|
-
* Stub functions
|
1167
|
-
*
|
1168
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
1169
|
-
* @license BSD
|
1170
|
-
*
|
1171
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
1172
|
-
*/
|
1173
|
-
|
1174
|
-
(function (sinon) {
|
1175
|
-
var commonJSModule = typeof module == "object" && typeof require == "function";
|
1176
|
-
|
1177
|
-
if (!sinon && commonJSModule) {
|
1178
|
-
sinon = require("../sinon");
|
1179
|
-
}
|
1180
|
-
|
1181
|
-
if (!sinon) {
|
1182
|
-
return;
|
1183
|
-
}
|
1184
|
-
|
1185
|
-
function stub(object, property, func) {
|
1186
|
-
if (!!func && typeof func != "function") {
|
1187
|
-
throw new TypeError("Custom stub should be function");
|
1188
|
-
}
|
1189
|
-
|
1190
|
-
var wrapper;
|
1191
|
-
|
1192
|
-
if (func) {
|
1193
|
-
wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func;
|
1194
|
-
} else {
|
1195
|
-
wrapper = stub.create();
|
1196
|
-
}
|
1197
|
-
|
1198
|
-
if (!object && !property) {
|
1199
|
-
return sinon.stub.create();
|
1200
|
-
}
|
1201
|
-
|
1202
|
-
if (!property && !!object && typeof object == "object") {
|
1203
|
-
for (var prop in object) {
|
1204
|
-
if (typeof object[prop] === "function") {
|
1205
|
-
stub(object, prop);
|
1206
|
-
}
|
1207
|
-
}
|
1208
|
-
|
1209
|
-
return object;
|
1210
|
-
}
|
1211
|
-
|
1212
|
-
return sinon.wrapMethod(object, property, wrapper);
|
1213
|
-
}
|
1214
|
-
|
1215
|
-
function getChangingValue(stub, property) {
|
1216
|
-
var index = stub.callCount - 1;
|
1217
|
-
var prop = index in stub[property] ? stub[property][index] : stub[property + "Last"];
|
1218
|
-
stub[property + "Last"] = prop;
|
1219
|
-
|
1220
|
-
return prop;
|
1221
|
-
}
|
1222
|
-
|
1223
|
-
function getCallback(stub, args) {
|
1224
|
-
var callArgAt = getChangingValue(stub, "callArgAts");
|
1225
|
-
|
1226
|
-
if (callArgAt < 0) {
|
1227
|
-
var callArgProp = getChangingValue(stub, "callArgProps");
|
1228
|
-
|
1229
|
-
for (var i = 0, l = args.length; i < l; ++i) {
|
1230
|
-
if (!callArgProp && typeof args[i] == "function") {
|
1231
|
-
return args[i];
|
1232
|
-
}
|
1233
|
-
|
1234
|
-
if (callArgProp && args[i] &&
|
1235
|
-
typeof args[i][callArgProp] == "function") {
|
1236
|
-
return args[i][callArgProp];
|
1237
|
-
}
|
1238
|
-
}
|
1239
|
-
|
1240
|
-
return null;
|
1241
|
-
}
|
1242
|
-
|
1243
|
-
return args[callArgAt];
|
1244
|
-
}
|
1245
|
-
|
1246
|
-
var join = Array.prototype.join;
|
1247
|
-
|
1248
|
-
function getCallbackError(stub, func, args) {
|
1249
|
-
if (stub.callArgAtsLast < 0) {
|
1250
|
-
var msg;
|
1251
|
-
|
1252
|
-
if (stub.callArgPropsLast) {
|
1253
|
-
msg = sinon.functionName(stub) +
|
1254
|
-
" expected to yield to '" + stub.callArgPropsLast +
|
1255
|
-
"', but no object with such a property was passed."
|
1256
|
-
} else {
|
1257
|
-
msg = sinon.functionName(stub) +
|
1258
|
-
" expected to yield, but no callback was passed."
|
1259
|
-
}
|
1260
|
-
|
1261
|
-
if (args.length > 0) {
|
1262
|
-
msg += " Received [" + join.call(args, ", ") + "]";
|
1263
|
-
}
|
1264
|
-
|
1265
|
-
return msg;
|
1266
|
-
}
|
1267
|
-
|
1268
|
-
return "argument at index " + stub.callArgAtsLast + " is not a function: " + func;
|
1269
|
-
}
|
1270
|
-
|
1271
|
-
var nextTick = (function () {
|
1272
|
-
if (typeof process === "object" && typeof process.nextTick === "function") {
|
1273
|
-
return process.nextTick;
|
1274
|
-
} else if (typeof msSetImmediate === "function") {
|
1275
|
-
return msSetImmediate.bind(window);
|
1276
|
-
} else if (typeof setImmediate === "function") {
|
1277
|
-
return setImmediate;
|
1278
|
-
} else {
|
1279
|
-
return function (callback) {
|
1280
|
-
setTimeout(callback, 0);
|
1281
|
-
};
|
1282
|
-
}
|
1283
|
-
})();
|
1284
|
-
|
1285
|
-
function callCallback(stub, args) {
|
1286
|
-
if (stub.callArgAts.length > 0) {
|
1287
|
-
var func = getCallback(stub, args);
|
1288
|
-
|
1289
|
-
if (typeof func != "function") {
|
1290
|
-
throw new TypeError(getCallbackError(stub, func, args));
|
1291
|
-
}
|
1292
|
-
|
1293
|
-
var index = stub.callCount - 1;
|
1294
|
-
|
1295
|
-
var callbackArguments = getChangingValue(stub, "callbackArguments");
|
1296
|
-
var callbackContext = getChangingValue(stub, "callbackContexts");
|
1297
|
-
|
1298
|
-
if (stub.callbackAsync) {
|
1299
|
-
nextTick(function() {
|
1300
|
-
func.apply(callbackContext, callbackArguments);
|
1301
|
-
});
|
1302
|
-
} else {
|
1303
|
-
func.apply(callbackContext, callbackArguments);
|
1304
|
-
}
|
1305
|
-
}
|
1306
|
-
}
|
1307
|
-
|
1308
|
-
var uuid = 0;
|
1309
|
-
|
1310
|
-
sinon.extend(stub, (function () {
|
1311
|
-
var slice = Array.prototype.slice, proto;
|
1312
|
-
|
1313
|
-
function throwsException(error, message) {
|
1314
|
-
if (typeof error == "string") {
|
1315
|
-
this.exception = new Error(message || "");
|
1316
|
-
this.exception.name = error;
|
1317
|
-
} else if (!error) {
|
1318
|
-
this.exception = new Error("Error");
|
1319
|
-
} else {
|
1320
|
-
this.exception = error;
|
1321
|
-
}
|
1322
|
-
|
1323
|
-
return this;
|
1324
|
-
}
|
1325
|
-
|
1326
|
-
proto = {
|
1327
|
-
create: function create() {
|
1328
|
-
var functionStub = function () {
|
1329
|
-
|
1330
|
-
callCallback(functionStub, arguments);
|
1331
|
-
|
1332
|
-
if (functionStub.exception) {
|
1333
|
-
throw functionStub.exception;
|
1334
|
-
} else if (typeof functionStub.returnArgAt == 'number') {
|
1335
|
-
return arguments[functionStub.returnArgAt];
|
1336
|
-
} else if (functionStub.returnThis) {
|
1337
|
-
return this;
|
1338
|
-
}
|
1339
|
-
return functionStub.returnValue;
|
1340
|
-
};
|
1341
|
-
|
1342
|
-
functionStub.id = "stub#" + uuid++;
|
1343
|
-
var orig = functionStub;
|
1344
|
-
functionStub = sinon.spy.create(functionStub);
|
1345
|
-
functionStub.func = orig;
|
1346
|
-
|
1347
|
-
functionStub.callArgAts = [];
|
1348
|
-
functionStub.callbackArguments = [];
|
1349
|
-
functionStub.callbackContexts = [];
|
1350
|
-
functionStub.callArgProps = [];
|
1351
|
-
|
1352
|
-
sinon.extend(functionStub, stub);
|
1353
|
-
functionStub._create = sinon.stub.create;
|
1354
|
-
functionStub.displayName = "stub";
|
1355
|
-
functionStub.toString = sinon.functionToString;
|
1356
|
-
|
1357
|
-
return functionStub;
|
1358
|
-
},
|
1359
|
-
|
1360
|
-
resetBehavior: function () {
|
1361
|
-
var i;
|
1362
|
-
|
1363
|
-
this.callArgAts = [];
|
1364
|
-
this.callbackArguments = [];
|
1365
|
-
this.callbackContexts = [];
|
1366
|
-
this.callArgProps = [];
|
1367
|
-
|
1368
|
-
delete this.returnValue;
|
1369
|
-
delete this.returnArgAt;
|
1370
|
-
this.returnThis = false;
|
1371
|
-
|
1372
|
-
if (this.fakes) {
|
1373
|
-
for (i = 0; i < this.fakes.length; i++) {
|
1374
|
-
this.fakes[i].resetBehavior();
|
1375
|
-
}
|
1376
|
-
}
|
1377
|
-
},
|
1378
|
-
|
1379
|
-
returns: function returns(value) {
|
1380
|
-
this.returnValue = value;
|
1381
|
-
|
1382
|
-
return this;
|
1383
|
-
},
|
1384
|
-
|
1385
|
-
returnsArg: function returnsArg(pos) {
|
1386
|
-
if (typeof pos != "number") {
|
1387
|
-
throw new TypeError("argument index is not number");
|
1388
|
-
}
|
1389
|
-
|
1390
|
-
this.returnArgAt = pos;
|
1391
|
-
|
1392
|
-
return this;
|
1393
|
-
},
|
1394
|
-
|
1395
|
-
returnsThis: function returnsThis() {
|
1396
|
-
this.returnThis = true;
|
1397
|
-
|
1398
|
-
return this;
|
1399
|
-
},
|
1400
|
-
|
1401
|
-
"throws": throwsException,
|
1402
|
-
throwsException: throwsException,
|
1403
|
-
|
1404
|
-
callsArg: function callsArg(pos) {
|
1405
|
-
if (typeof pos != "number") {
|
1406
|
-
throw new TypeError("argument index is not number");
|
1407
|
-
}
|
1408
|
-
|
1409
|
-
this.callArgAts.push(pos);
|
1410
|
-
this.callbackArguments.push([]);
|
1411
|
-
this.callbackContexts.push(undefined);
|
1412
|
-
this.callArgProps.push(undefined);
|
1413
|
-
|
1414
|
-
return this;
|
1415
|
-
},
|
1416
|
-
|
1417
|
-
callsArgOn: function callsArgOn(pos, context) {
|
1418
|
-
if (typeof pos != "number") {
|
1419
|
-
throw new TypeError("argument index is not number");
|
1420
|
-
}
|
1421
|
-
if (typeof context != "object") {
|
1422
|
-
throw new TypeError("argument context is not an object");
|
1423
|
-
}
|
1424
|
-
|
1425
|
-
this.callArgAts.push(pos);
|
1426
|
-
this.callbackArguments.push([]);
|
1427
|
-
this.callbackContexts.push(context);
|
1428
|
-
this.callArgProps.push(undefined);
|
1429
|
-
|
1430
|
-
return this;
|
1431
|
-
},
|
1432
|
-
|
1433
|
-
callsArgWith: function callsArgWith(pos) {
|
1434
|
-
if (typeof pos != "number") {
|
1435
|
-
throw new TypeError("argument index is not number");
|
1436
|
-
}
|
1437
|
-
|
1438
|
-
this.callArgAts.push(pos);
|
1439
|
-
this.callbackArguments.push(slice.call(arguments, 1));
|
1440
|
-
this.callbackContexts.push(undefined);
|
1441
|
-
this.callArgProps.push(undefined);
|
1442
|
-
|
1443
|
-
return this;
|
1444
|
-
},
|
1445
|
-
|
1446
|
-
callsArgOnWith: function callsArgWith(pos, context) {
|
1447
|
-
if (typeof pos != "number") {
|
1448
|
-
throw new TypeError("argument index is not number");
|
1449
|
-
}
|
1450
|
-
if (typeof context != "object") {
|
1451
|
-
throw new TypeError("argument context is not an object");
|
1452
|
-
}
|
1453
|
-
|
1454
|
-
this.callArgAts.push(pos);
|
1455
|
-
this.callbackArguments.push(slice.call(arguments, 2));
|
1456
|
-
this.callbackContexts.push(context);
|
1457
|
-
this.callArgProps.push(undefined);
|
1458
|
-
|
1459
|
-
return this;
|
1460
|
-
},
|
1461
|
-
|
1462
|
-
yields: function () {
|
1463
|
-
this.callArgAts.push(-1);
|
1464
|
-
this.callbackArguments.push(slice.call(arguments, 0));
|
1465
|
-
this.callbackContexts.push(undefined);
|
1466
|
-
this.callArgProps.push(undefined);
|
1467
|
-
|
1468
|
-
return this;
|
1469
|
-
},
|
1470
|
-
|
1471
|
-
yieldsOn: function (context) {
|
1472
|
-
if (typeof context != "object") {
|
1473
|
-
throw new TypeError("argument context is not an object");
|
1474
|
-
}
|
1475
|
-
|
1476
|
-
this.callArgAts.push(-1);
|
1477
|
-
this.callbackArguments.push(slice.call(arguments, 1));
|
1478
|
-
this.callbackContexts.push(context);
|
1479
|
-
this.callArgProps.push(undefined);
|
1480
|
-
|
1481
|
-
return this;
|
1482
|
-
},
|
1483
|
-
|
1484
|
-
yieldsTo: function (prop) {
|
1485
|
-
this.callArgAts.push(-1);
|
1486
|
-
this.callbackArguments.push(slice.call(arguments, 1));
|
1487
|
-
this.callbackContexts.push(undefined);
|
1488
|
-
this.callArgProps.push(prop);
|
1489
|
-
|
1490
|
-
return this;
|
1491
|
-
},
|
1492
|
-
|
1493
|
-
yieldsToOn: function (prop, context) {
|
1494
|
-
if (typeof context != "object") {
|
1495
|
-
throw new TypeError("argument context is not an object");
|
1496
|
-
}
|
1497
|
-
|
1498
|
-
this.callArgAts.push(-1);
|
1499
|
-
this.callbackArguments.push(slice.call(arguments, 2));
|
1500
|
-
this.callbackContexts.push(context);
|
1501
|
-
this.callArgProps.push(prop);
|
1502
|
-
|
1503
|
-
return this;
|
1504
|
-
}
|
1505
|
-
};
|
1506
|
-
|
1507
|
-
// create asynchronous versions of callsArg* and yields* methods
|
1508
|
-
for (var method in proto) {
|
1509
|
-
// need to avoid creating anotherasync versions of the newly added async methods
|
1510
|
-
if (proto.hasOwnProperty(method) &&
|
1511
|
-
method.match(/^(callsArg|yields|thenYields$)/) &&
|
1512
|
-
!method.match(/Async/)) {
|
1513
|
-
proto[method + 'Async'] = (function (syncFnName) {
|
1514
|
-
return function () {
|
1515
|
-
this.callbackAsync = true;
|
1516
|
-
return this[syncFnName].apply(this, arguments);
|
1517
|
-
};
|
1518
|
-
})(method);
|
1519
|
-
}
|
1520
|
-
}
|
1521
|
-
|
1522
|
-
return proto;
|
1523
|
-
|
1524
|
-
}()));
|
1525
|
-
|
1526
|
-
if (commonJSModule) {
|
1527
|
-
module.exports = stub;
|
1528
|
-
} else {
|
1529
|
-
sinon.stub = stub;
|
1530
|
-
}
|
1531
|
-
}(typeof sinon == "object" && sinon || null));
|
1532
|
-
|
1533
|
-
/**
|
1534
|
-
* @depend ../sinon.js
|
1535
|
-
* @depend stub.js
|
1536
|
-
*/
|
1537
|
-
/*jslint eqeqeq: false, onevar: false, nomen: false*/
|
1538
|
-
/*global module, require, sinon*/
|
1539
|
-
/**
|
1540
|
-
* Mock functions.
|
1541
|
-
*
|
1542
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
1543
|
-
* @license BSD
|
1544
|
-
*
|
1545
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
1546
|
-
*/
|
1547
|
-
|
1548
|
-
(function (sinon) {
|
1549
|
-
var commonJSModule = typeof module == "object" && typeof require == "function";
|
1550
|
-
var push = [].push;
|
1551
|
-
|
1552
|
-
if (!sinon && commonJSModule) {
|
1553
|
-
sinon = require("../sinon");
|
1554
|
-
}
|
1555
|
-
|
1556
|
-
if (!sinon) {
|
1557
|
-
return;
|
1558
|
-
}
|
1559
|
-
|
1560
|
-
function mock(object) {
|
1561
|
-
if (!object) {
|
1562
|
-
return sinon.expectation.create("Anonymous mock");
|
1563
|
-
}
|
1564
|
-
|
1565
|
-
return mock.create(object);
|
1566
|
-
}
|
1567
|
-
|
1568
|
-
sinon.mock = mock;
|
1569
|
-
|
1570
|
-
sinon.extend(mock, (function () {
|
1571
|
-
function each(collection, callback) {
|
1572
|
-
if (!collection) {
|
1573
|
-
return;
|
1574
|
-
}
|
1575
|
-
|
1576
|
-
for (var i = 0, l = collection.length; i < l; i += 1) {
|
1577
|
-
callback(collection[i]);
|
1578
|
-
}
|
1579
|
-
}
|
1580
|
-
|
1581
|
-
return {
|
1582
|
-
create: function create(object) {
|
1583
|
-
if (!object) {
|
1584
|
-
throw new TypeError("object is null");
|
1585
|
-
}
|
1586
|
-
|
1587
|
-
var mockObject = sinon.extend({}, mock);
|
1588
|
-
mockObject.object = object;
|
1589
|
-
delete mockObject.create;
|
1590
|
-
|
1591
|
-
return mockObject;
|
1592
|
-
},
|
1593
|
-
|
1594
|
-
expects: function expects(method) {
|
1595
|
-
if (!method) {
|
1596
|
-
throw new TypeError("method is falsy");
|
1597
|
-
}
|
1598
|
-
|
1599
|
-
if (!this.expectations) {
|
1600
|
-
this.expectations = {};
|
1601
|
-
this.proxies = [];
|
1602
|
-
}
|
1603
|
-
|
1604
|
-
if (!this.expectations[method]) {
|
1605
|
-
this.expectations[method] = [];
|
1606
|
-
var mockObject = this;
|
1607
|
-
|
1608
|
-
sinon.wrapMethod(this.object, method, function () {
|
1609
|
-
return mockObject.invokeMethod(method, this, arguments);
|
1610
|
-
});
|
1611
|
-
|
1612
|
-
push.call(this.proxies, method);
|
1613
|
-
}
|
1614
|
-
|
1615
|
-
var expectation = sinon.expectation.create(method);
|
1616
|
-
push.call(this.expectations[method], expectation);
|
1617
|
-
|
1618
|
-
return expectation;
|
1619
|
-
},
|
1620
|
-
|
1621
|
-
restore: function restore() {
|
1622
|
-
var object = this.object;
|
1623
|
-
|
1624
|
-
each(this.proxies, function (proxy) {
|
1625
|
-
if (typeof object[proxy].restore == "function") {
|
1626
|
-
object[proxy].restore();
|
1627
|
-
}
|
1628
|
-
});
|
1629
|
-
},
|
1630
|
-
|
1631
|
-
verify: function verify() {
|
1632
|
-
var expectations = this.expectations || {};
|
1633
|
-
var messages = [], met = [];
|
1634
|
-
|
1635
|
-
each(this.proxies, function (proxy) {
|
1636
|
-
each(expectations[proxy], function (expectation) {
|
1637
|
-
if (!expectation.met()) {
|
1638
|
-
push.call(messages, expectation.toString());
|
1639
|
-
} else {
|
1640
|
-
push.call(met, expectation.toString());
|
1641
|
-
}
|
1642
|
-
});
|
1643
|
-
});
|
1644
|
-
|
1645
|
-
this.restore();
|
1646
|
-
|
1647
|
-
if (messages.length > 0) {
|
1648
|
-
sinon.expectation.fail(messages.concat(met).join("\n"));
|
1649
|
-
} else {
|
1650
|
-
sinon.expectation.pass(messages.concat(met).join("\n"));
|
1651
|
-
}
|
1652
|
-
|
1653
|
-
return true;
|
1654
|
-
},
|
1655
|
-
|
1656
|
-
invokeMethod: function invokeMethod(method, thisValue, args) {
|
1657
|
-
var expectations = this.expectations && this.expectations[method];
|
1658
|
-
var length = expectations && expectations.length || 0, i;
|
1659
|
-
|
1660
|
-
for (i = 0; i < length; i += 1) {
|
1661
|
-
if (!expectations[i].met() &&
|
1662
|
-
expectations[i].allowsCall(thisValue, args)) {
|
1663
|
-
return expectations[i].apply(thisValue, args);
|
1664
|
-
}
|
1665
|
-
}
|
1666
|
-
|
1667
|
-
var messages = [], available, exhausted = 0;
|
1668
|
-
|
1669
|
-
for (i = 0; i < length; i += 1) {
|
1670
|
-
if (expectations[i].allowsCall(thisValue, args)) {
|
1671
|
-
available = available || expectations[i];
|
1672
|
-
} else {
|
1673
|
-
exhausted += 1;
|
1674
|
-
}
|
1675
|
-
push.call(messages, " " + expectations[i].toString());
|
1676
|
-
}
|
1677
|
-
|
1678
|
-
if (exhausted === 0) {
|
1679
|
-
return available.apply(thisValue, args);
|
1680
|
-
}
|
1681
|
-
|
1682
|
-
messages.unshift("Unexpected call: " + sinon.spyCall.toString.call({
|
1683
|
-
proxy: method,
|
1684
|
-
args: args
|
1685
|
-
}));
|
1686
|
-
|
1687
|
-
sinon.expectation.fail(messages.join("\n"));
|
1688
|
-
}
|
1689
|
-
};
|
1690
|
-
}()));
|
1691
|
-
|
1692
|
-
var times = sinon.timesInWords;
|
1693
|
-
|
1694
|
-
sinon.expectation = (function () {
|
1695
|
-
var slice = Array.prototype.slice;
|
1696
|
-
var _invoke = sinon.spy.invoke;
|
1697
|
-
|
1698
|
-
function callCountInWords(callCount) {
|
1699
|
-
if (callCount == 0) {
|
1700
|
-
return "never called";
|
1701
|
-
} else {
|
1702
|
-
return "called " + times(callCount);
|
1703
|
-
}
|
1704
|
-
}
|
1705
|
-
|
1706
|
-
function expectedCallCountInWords(expectation) {
|
1707
|
-
var min = expectation.minCalls;
|
1708
|
-
var max = expectation.maxCalls;
|
1709
|
-
|
1710
|
-
if (typeof min == "number" && typeof max == "number") {
|
1711
|
-
var str = times(min);
|
1712
|
-
|
1713
|
-
if (min != max) {
|
1714
|
-
str = "at least " + str + " and at most " + times(max);
|
1715
|
-
}
|
1716
|
-
|
1717
|
-
return str;
|
1718
|
-
}
|
1719
|
-
|
1720
|
-
if (typeof min == "number") {
|
1721
|
-
return "at least " + times(min);
|
1722
|
-
}
|
1723
|
-
|
1724
|
-
return "at most " + times(max);
|
1725
|
-
}
|
1726
|
-
|
1727
|
-
function receivedMinCalls(expectation) {
|
1728
|
-
var hasMinLimit = typeof expectation.minCalls == "number";
|
1729
|
-
return !hasMinLimit || expectation.callCount >= expectation.minCalls;
|
1730
|
-
}
|
1731
|
-
|
1732
|
-
function receivedMaxCalls(expectation) {
|
1733
|
-
if (typeof expectation.maxCalls != "number") {
|
1734
|
-
return false;
|
1735
|
-
}
|
1736
|
-
|
1737
|
-
return expectation.callCount == expectation.maxCalls;
|
1738
|
-
}
|
1739
|
-
|
1740
|
-
return {
|
1741
|
-
minCalls: 1,
|
1742
|
-
maxCalls: 1,
|
1743
|
-
|
1744
|
-
create: function create(methodName) {
|
1745
|
-
var expectation = sinon.extend(sinon.stub.create(), sinon.expectation);
|
1746
|
-
delete expectation.create;
|
1747
|
-
expectation.method = methodName;
|
1748
|
-
|
1749
|
-
return expectation;
|
1750
|
-
},
|
1751
|
-
|
1752
|
-
invoke: function invoke(func, thisValue, args) {
|
1753
|
-
this.verifyCallAllowed(thisValue, args);
|
1754
|
-
|
1755
|
-
return _invoke.apply(this, arguments);
|
1756
|
-
},
|
1757
|
-
|
1758
|
-
atLeast: function atLeast(num) {
|
1759
|
-
if (typeof num != "number") {
|
1760
|
-
throw new TypeError("'" + num + "' is not number");
|
1761
|
-
}
|
1762
|
-
|
1763
|
-
if (!this.limitsSet) {
|
1764
|
-
this.maxCalls = null;
|
1765
|
-
this.limitsSet = true;
|
1766
|
-
}
|
1767
|
-
|
1768
|
-
this.minCalls = num;
|
1769
|
-
|
1770
|
-
return this;
|
1771
|
-
},
|
1772
|
-
|
1773
|
-
atMost: function atMost(num) {
|
1774
|
-
if (typeof num != "number") {
|
1775
|
-
throw new TypeError("'" + num + "' is not number");
|
1776
|
-
}
|
1777
|
-
|
1778
|
-
if (!this.limitsSet) {
|
1779
|
-
this.minCalls = null;
|
1780
|
-
this.limitsSet = true;
|
1781
|
-
}
|
1782
|
-
|
1783
|
-
this.maxCalls = num;
|
1784
|
-
|
1785
|
-
return this;
|
1786
|
-
},
|
1787
|
-
|
1788
|
-
never: function never() {
|
1789
|
-
return this.exactly(0);
|
1790
|
-
},
|
1791
|
-
|
1792
|
-
once: function once() {
|
1793
|
-
return this.exactly(1);
|
1794
|
-
},
|
1795
|
-
|
1796
|
-
twice: function twice() {
|
1797
|
-
return this.exactly(2);
|
1798
|
-
},
|
1799
|
-
|
1800
|
-
thrice: function thrice() {
|
1801
|
-
return this.exactly(3);
|
1802
|
-
},
|
1803
|
-
|
1804
|
-
exactly: function exactly(num) {
|
1805
|
-
if (typeof num != "number") {
|
1806
|
-
throw new TypeError("'" + num + "' is not a number");
|
1807
|
-
}
|
1808
|
-
|
1809
|
-
this.atLeast(num);
|
1810
|
-
return this.atMost(num);
|
1811
|
-
},
|
1812
|
-
|
1813
|
-
met: function met() {
|
1814
|
-
return !this.failed && receivedMinCalls(this);
|
1815
|
-
},
|
1816
|
-
|
1817
|
-
verifyCallAllowed: function verifyCallAllowed(thisValue, args) {
|
1818
|
-
if (receivedMaxCalls(this)) {
|
1819
|
-
this.failed = true;
|
1820
|
-
sinon.expectation.fail(this.method + " already called " + times(this.maxCalls));
|
1821
|
-
}
|
1822
|
-
|
1823
|
-
if ("expectedThis" in this && this.expectedThis !== thisValue) {
|
1824
|
-
sinon.expectation.fail(this.method + " called with " + thisValue + " as thisValue, expected " +
|
1825
|
-
this.expectedThis);
|
1826
|
-
}
|
1827
|
-
|
1828
|
-
if (!("expectedArguments" in this)) {
|
1829
|
-
return;
|
1830
|
-
}
|
1831
|
-
|
1832
|
-
if (!args) {
|
1833
|
-
sinon.expectation.fail(this.method + " received no arguments, expected " +
|
1834
|
-
sinon.format(this.expectedArguments));
|
1835
|
-
}
|
1836
|
-
|
1837
|
-
if (args.length < this.expectedArguments.length) {
|
1838
|
-
sinon.expectation.fail(this.method + " received too few arguments (" + sinon.format(args) +
|
1839
|
-
"), expected " + sinon.format(this.expectedArguments));
|
1840
|
-
}
|
1841
|
-
|
1842
|
-
if (this.expectsExactArgCount &&
|
1843
|
-
args.length != this.expectedArguments.length) {
|
1844
|
-
sinon.expectation.fail(this.method + " received too many arguments (" + sinon.format(args) +
|
1845
|
-
"), expected " + sinon.format(this.expectedArguments));
|
1846
|
-
}
|
1847
|
-
|
1848
|
-
for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
|
1849
|
-
if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
|
1850
|
-
sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) +
|
1851
|
-
", expected " + sinon.format(this.expectedArguments));
|
1852
|
-
}
|
1853
|
-
}
|
1854
|
-
},
|
1855
|
-
|
1856
|
-
allowsCall: function allowsCall(thisValue, args) {
|
1857
|
-
if (this.met() && receivedMaxCalls(this)) {
|
1858
|
-
return false;
|
1859
|
-
}
|
1860
|
-
|
1861
|
-
if ("expectedThis" in this && this.expectedThis !== thisValue) {
|
1862
|
-
return false;
|
1863
|
-
}
|
1864
|
-
|
1865
|
-
if (!("expectedArguments" in this)) {
|
1866
|
-
return true;
|
1867
|
-
}
|
1868
|
-
|
1869
|
-
args = args || [];
|
1870
|
-
|
1871
|
-
if (args.length < this.expectedArguments.length) {
|
1872
|
-
return false;
|
1873
|
-
}
|
1874
|
-
|
1875
|
-
if (this.expectsExactArgCount &&
|
1876
|
-
args.length != this.expectedArguments.length) {
|
1877
|
-
return false;
|
1878
|
-
}
|
1879
|
-
|
1880
|
-
for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
|
1881
|
-
if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
|
1882
|
-
return false;
|
1883
|
-
}
|
1884
|
-
}
|
1885
|
-
|
1886
|
-
return true;
|
1887
|
-
},
|
1888
|
-
|
1889
|
-
withArgs: function withArgs() {
|
1890
|
-
this.expectedArguments = slice.call(arguments);
|
1891
|
-
return this;
|
1892
|
-
},
|
1893
|
-
|
1894
|
-
withExactArgs: function withExactArgs() {
|
1895
|
-
this.withArgs.apply(this, arguments);
|
1896
|
-
this.expectsExactArgCount = true;
|
1897
|
-
return this;
|
1898
|
-
},
|
1899
|
-
|
1900
|
-
on: function on(thisValue) {
|
1901
|
-
this.expectedThis = thisValue;
|
1902
|
-
return this;
|
1903
|
-
},
|
1904
|
-
|
1905
|
-
toString: function () {
|
1906
|
-
var args = (this.expectedArguments || []).slice();
|
1907
|
-
|
1908
|
-
if (!this.expectsExactArgCount) {
|
1909
|
-
push.call(args, "[...]");
|
1910
|
-
}
|
1911
|
-
|
1912
|
-
var callStr = sinon.spyCall.toString.call({
|
1913
|
-
proxy: this.method, args: args
|
1914
|
-
});
|
1915
|
-
|
1916
|
-
var message = callStr.replace(", [...", "[, ...") + " " +
|
1917
|
-
expectedCallCountInWords(this);
|
1918
|
-
|
1919
|
-
if (this.met()) {
|
1920
|
-
return "Expectation met: " + message;
|
1921
|
-
}
|
1922
|
-
|
1923
|
-
return "Expected " + message + " (" +
|
1924
|
-
callCountInWords(this.callCount) + ")";
|
1925
|
-
},
|
1926
|
-
|
1927
|
-
verify: function verify() {
|
1928
|
-
if (!this.met()) {
|
1929
|
-
sinon.expectation.fail(this.toString());
|
1930
|
-
} else {
|
1931
|
-
sinon.expectation.pass(this.toString());
|
1932
|
-
}
|
1933
|
-
|
1934
|
-
return true;
|
1935
|
-
},
|
1936
|
-
|
1937
|
-
pass: function(message) {
|
1938
|
-
sinon.assert.pass(message);
|
1939
|
-
},
|
1940
|
-
fail: function (message) {
|
1941
|
-
var exception = new Error(message);
|
1942
|
-
exception.name = "ExpectationError";
|
1943
|
-
|
1944
|
-
throw exception;
|
1945
|
-
}
|
1946
|
-
};
|
1947
|
-
}());
|
1948
|
-
|
1949
|
-
if (commonJSModule) {
|
1950
|
-
module.exports = mock;
|
1951
|
-
} else {
|
1952
|
-
sinon.mock = mock;
|
1953
|
-
}
|
1954
|
-
}(typeof sinon == "object" && sinon || null));
|
1955
|
-
|
1956
|
-
/**
|
1957
|
-
* @depend ../sinon.js
|
1958
|
-
* @depend stub.js
|
1959
|
-
* @depend mock.js
|
1960
|
-
*/
|
1961
|
-
/*jslint eqeqeq: false, onevar: false, forin: true*/
|
1962
|
-
/*global module, require, sinon*/
|
1963
|
-
/**
|
1964
|
-
* Collections of stubs, spies and mocks.
|
1965
|
-
*
|
1966
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
1967
|
-
* @license BSD
|
1968
|
-
*
|
1969
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
1970
|
-
*/
|
1971
|
-
|
1972
|
-
(function (sinon) {
|
1973
|
-
var commonJSModule = typeof module == "object" && typeof require == "function";
|
1974
|
-
var push = [].push;
|
1975
|
-
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
1976
|
-
|
1977
|
-
if (!sinon && commonJSModule) {
|
1978
|
-
sinon = require("../sinon");
|
1979
|
-
}
|
1980
|
-
|
1981
|
-
if (!sinon) {
|
1982
|
-
return;
|
1983
|
-
}
|
1984
|
-
|
1985
|
-
function getFakes(fakeCollection) {
|
1986
|
-
if (!fakeCollection.fakes) {
|
1987
|
-
fakeCollection.fakes = [];
|
1988
|
-
}
|
1989
|
-
|
1990
|
-
return fakeCollection.fakes;
|
1991
|
-
}
|
1992
|
-
|
1993
|
-
function each(fakeCollection, method) {
|
1994
|
-
var fakes = getFakes(fakeCollection);
|
1995
|
-
|
1996
|
-
for (var i = 0, l = fakes.length; i < l; i += 1) {
|
1997
|
-
if (typeof fakes[i][method] == "function") {
|
1998
|
-
fakes[i][method]();
|
1999
|
-
}
|
2000
|
-
}
|
2001
|
-
}
|
2002
|
-
|
2003
|
-
function compact(fakeCollection) {
|
2004
|
-
var fakes = getFakes(fakeCollection);
|
2005
|
-
var i = 0;
|
2006
|
-
while (i < fakes.length) {
|
2007
|
-
fakes.splice(i, 1);
|
2008
|
-
}
|
2009
|
-
}
|
2010
|
-
|
2011
|
-
var collection = {
|
2012
|
-
verify: function resolve() {
|
2013
|
-
each(this, "verify");
|
2014
|
-
},
|
2015
|
-
|
2016
|
-
restore: function restore() {
|
2017
|
-
each(this, "restore");
|
2018
|
-
compact(this);
|
2019
|
-
},
|
2020
|
-
|
2021
|
-
verifyAndRestore: function verifyAndRestore() {
|
2022
|
-
var exception;
|
2023
|
-
|
2024
|
-
try {
|
2025
|
-
this.verify();
|
2026
|
-
} catch (e) {
|
2027
|
-
exception = e;
|
2028
|
-
}
|
2029
|
-
|
2030
|
-
this.restore();
|
2031
|
-
|
2032
|
-
if (exception) {
|
2033
|
-
throw exception;
|
2034
|
-
}
|
2035
|
-
},
|
2036
|
-
|
2037
|
-
add: function add(fake) {
|
2038
|
-
push.call(getFakes(this), fake);
|
2039
|
-
return fake;
|
2040
|
-
},
|
2041
|
-
|
2042
|
-
spy: function spy() {
|
2043
|
-
return this.add(sinon.spy.apply(sinon, arguments));
|
2044
|
-
},
|
2045
|
-
|
2046
|
-
stub: function stub(object, property, value) {
|
2047
|
-
if (property) {
|
2048
|
-
var original = object[property];
|
2049
|
-
|
2050
|
-
if (typeof original != "function") {
|
2051
|
-
if (!hasOwnProperty.call(object, property)) {
|
2052
|
-
throw new TypeError("Cannot stub non-existent own property " + property);
|
2053
|
-
}
|
2054
|
-
|
2055
|
-
object[property] = value;
|
2056
|
-
|
2057
|
-
return this.add({
|
2058
|
-
restore: function () {
|
2059
|
-
object[property] = original;
|
2060
|
-
}
|
2061
|
-
});
|
2062
|
-
}
|
2063
|
-
}
|
2064
|
-
if (!property && !!object && typeof object == "object") {
|
2065
|
-
var stubbedObj = sinon.stub.apply(sinon, arguments);
|
2066
|
-
|
2067
|
-
for (var prop in stubbedObj) {
|
2068
|
-
if (typeof stubbedObj[prop] === "function") {
|
2069
|
-
this.add(stubbedObj[prop]);
|
2070
|
-
}
|
2071
|
-
}
|
2072
|
-
|
2073
|
-
return stubbedObj;
|
2074
|
-
}
|
2075
|
-
|
2076
|
-
return this.add(sinon.stub.apply(sinon, arguments));
|
2077
|
-
},
|
2078
|
-
|
2079
|
-
mock: function mock() {
|
2080
|
-
return this.add(sinon.mock.apply(sinon, arguments));
|
2081
|
-
},
|
2082
|
-
|
2083
|
-
inject: function inject(obj) {
|
2084
|
-
var col = this;
|
2085
|
-
|
2086
|
-
obj.spy = function () {
|
2087
|
-
return col.spy.apply(col, arguments);
|
2088
|
-
};
|
2089
|
-
|
2090
|
-
obj.stub = function () {
|
2091
|
-
return col.stub.apply(col, arguments);
|
2092
|
-
};
|
2093
|
-
|
2094
|
-
obj.mock = function () {
|
2095
|
-
return col.mock.apply(col, arguments);
|
2096
|
-
};
|
2097
|
-
|
2098
|
-
return obj;
|
2099
|
-
}
|
2100
|
-
};
|
2101
|
-
|
2102
|
-
if (commonJSModule) {
|
2103
|
-
module.exports = collection;
|
2104
|
-
} else {
|
2105
|
-
sinon.collection = collection;
|
2106
|
-
}
|
2107
|
-
}(typeof sinon == "object" && sinon || null));
|
2108
|
-
|
2109
|
-
/*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/
|
2110
|
-
/*global module, require, window*/
|
2111
|
-
/**
|
2112
|
-
* Fake timer API
|
2113
|
-
* setTimeout
|
2114
|
-
* setInterval
|
2115
|
-
* clearTimeout
|
2116
|
-
* clearInterval
|
2117
|
-
* tick
|
2118
|
-
* reset
|
2119
|
-
* Date
|
2120
|
-
*
|
2121
|
-
* Inspired by jsUnitMockTimeOut from JsUnit
|
2122
|
-
*
|
2123
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
2124
|
-
* @license BSD
|
2125
|
-
*
|
2126
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
2127
|
-
*/
|
2128
|
-
|
2129
|
-
if (typeof sinon == "undefined") {
|
2130
|
-
var sinon = {};
|
2131
|
-
}
|
2132
|
-
|
2133
|
-
(function (global) {
|
2134
|
-
var id = 1;
|
2135
|
-
|
2136
|
-
function addTimer(args, recurring) {
|
2137
|
-
if (args.length === 0) {
|
2138
|
-
throw new Error("Function requires at least 1 parameter");
|
2139
|
-
}
|
2140
|
-
|
2141
|
-
var toId = id++;
|
2142
|
-
var delay = args[1] || 0;
|
2143
|
-
|
2144
|
-
if (!this.timeouts) {
|
2145
|
-
this.timeouts = {};
|
2146
|
-
}
|
2147
|
-
|
2148
|
-
this.timeouts[toId] = {
|
2149
|
-
id: toId,
|
2150
|
-
func: args[0],
|
2151
|
-
callAt: this.now + delay,
|
2152
|
-
invokeArgs: Array.prototype.slice.call(args, 2)
|
2153
|
-
};
|
2154
|
-
|
2155
|
-
if (recurring === true) {
|
2156
|
-
this.timeouts[toId].interval = delay;
|
2157
|
-
}
|
2158
|
-
|
2159
|
-
return toId;
|
2160
|
-
}
|
2161
|
-
|
2162
|
-
function parseTime(str) {
|
2163
|
-
if (!str) {
|
2164
|
-
return 0;
|
2165
|
-
}
|
2166
|
-
|
2167
|
-
var strings = str.split(":");
|
2168
|
-
var l = strings.length, i = l;
|
2169
|
-
var ms = 0, parsed;
|
2170
|
-
|
2171
|
-
if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
|
2172
|
-
throw new Error("tick only understands numbers and 'h:m:s'");
|
2173
|
-
}
|
2174
|
-
|
2175
|
-
while (i--) {
|
2176
|
-
parsed = parseInt(strings[i], 10);
|
2177
|
-
|
2178
|
-
if (parsed >= 60) {
|
2179
|
-
throw new Error("Invalid time " + str);
|
2180
|
-
}
|
2181
|
-
|
2182
|
-
ms += parsed * Math.pow(60, (l - i - 1));
|
2183
|
-
}
|
2184
|
-
|
2185
|
-
return ms * 1000;
|
2186
|
-
}
|
2187
|
-
|
2188
|
-
function createObject(object) {
|
2189
|
-
var newObject;
|
2190
|
-
|
2191
|
-
if (Object.create) {
|
2192
|
-
newObject = Object.create(object);
|
2193
|
-
} else {
|
2194
|
-
var F = function () {};
|
2195
|
-
F.prototype = object;
|
2196
|
-
newObject = new F();
|
2197
|
-
}
|
2198
|
-
|
2199
|
-
newObject.Date.clock = newObject;
|
2200
|
-
return newObject;
|
2201
|
-
}
|
2202
|
-
|
2203
|
-
sinon.clock = {
|
2204
|
-
now: 0,
|
2205
|
-
|
2206
|
-
create: function create(now) {
|
2207
|
-
var clock = createObject(this);
|
2208
|
-
|
2209
|
-
if (typeof now == "number") {
|
2210
|
-
clock.now = now;
|
2211
|
-
}
|
2212
|
-
|
2213
|
-
if (!!now && typeof now == "object") {
|
2214
|
-
throw new TypeError("now should be milliseconds since UNIX epoch");
|
2215
|
-
}
|
2216
|
-
|
2217
|
-
return clock;
|
2218
|
-
},
|
2219
|
-
|
2220
|
-
setTimeout: function setTimeout(callback, timeout) {
|
2221
|
-
return addTimer.call(this, arguments, false);
|
2222
|
-
},
|
2223
|
-
|
2224
|
-
clearTimeout: function clearTimeout(timerId) {
|
2225
|
-
if (!this.timeouts) {
|
2226
|
-
this.timeouts = [];
|
2227
|
-
}
|
2228
|
-
|
2229
|
-
if (timerId in this.timeouts) {
|
2230
|
-
delete this.timeouts[timerId];
|
2231
|
-
}
|
2232
|
-
},
|
2233
|
-
|
2234
|
-
setInterval: function setInterval(callback, timeout) {
|
2235
|
-
return addTimer.call(this, arguments, true);
|
2236
|
-
},
|
2237
|
-
|
2238
|
-
clearInterval: function clearInterval(timerId) {
|
2239
|
-
this.clearTimeout(timerId);
|
2240
|
-
},
|
2241
|
-
|
2242
|
-
tick: function tick(ms) {
|
2243
|
-
ms = typeof ms == "number" ? ms : parseTime(ms);
|
2244
|
-
var tickFrom = this.now, tickTo = this.now + ms, previous = this.now;
|
2245
|
-
var timer = this.firstTimerInRange(tickFrom, tickTo);
|
2246
|
-
|
2247
|
-
var firstException;
|
2248
|
-
while (timer && tickFrom <= tickTo) {
|
2249
|
-
if (this.timeouts[timer.id]) {
|
2250
|
-
tickFrom = this.now = timer.callAt;
|
2251
|
-
try {
|
2252
|
-
this.callTimer(timer);
|
2253
|
-
} catch (e) {
|
2254
|
-
firstException = firstException || e;
|
2255
|
-
}
|
2256
|
-
}
|
2257
|
-
|
2258
|
-
timer = this.firstTimerInRange(previous, tickTo);
|
2259
|
-
previous = tickFrom;
|
2260
|
-
}
|
2261
|
-
|
2262
|
-
this.now = tickTo;
|
2263
|
-
|
2264
|
-
if (firstException) {
|
2265
|
-
throw firstException;
|
2266
|
-
}
|
2267
|
-
},
|
2268
|
-
|
2269
|
-
firstTimerInRange: function (from, to) {
|
2270
|
-
var timer, smallest, originalTimer;
|
2271
|
-
|
2272
|
-
for (var id in this.timeouts) {
|
2273
|
-
if (this.timeouts.hasOwnProperty(id)) {
|
2274
|
-
if (this.timeouts[id].callAt < from || this.timeouts[id].callAt > to) {
|
2275
|
-
continue;
|
2276
|
-
}
|
2277
|
-
|
2278
|
-
if (!smallest || this.timeouts[id].callAt < smallest) {
|
2279
|
-
originalTimer = this.timeouts[id];
|
2280
|
-
smallest = this.timeouts[id].callAt;
|
2281
|
-
|
2282
|
-
timer = {
|
2283
|
-
func: this.timeouts[id].func,
|
2284
|
-
callAt: this.timeouts[id].callAt,
|
2285
|
-
interval: this.timeouts[id].interval,
|
2286
|
-
id: this.timeouts[id].id,
|
2287
|
-
invokeArgs: this.timeouts[id].invokeArgs
|
2288
|
-
};
|
2289
|
-
}
|
2290
|
-
}
|
2291
|
-
}
|
2292
|
-
|
2293
|
-
return timer || null;
|
2294
|
-
},
|
2295
|
-
|
2296
|
-
callTimer: function (timer) {
|
2297
|
-
if (typeof timer.interval == "number") {
|
2298
|
-
this.timeouts[timer.id].callAt += timer.interval;
|
2299
|
-
} else {
|
2300
|
-
delete this.timeouts[timer.id];
|
2301
|
-
}
|
2302
|
-
|
2303
|
-
try {
|
2304
|
-
if (typeof timer.func == "function") {
|
2305
|
-
timer.func.apply(null, timer.invokeArgs);
|
2306
|
-
} else {
|
2307
|
-
eval(timer.func);
|
2308
|
-
}
|
2309
|
-
} catch (e) {
|
2310
|
-
var exception = e;
|
2311
|
-
}
|
2312
|
-
|
2313
|
-
if (!this.timeouts[timer.id]) {
|
2314
|
-
if (exception) {
|
2315
|
-
throw exception;
|
2316
|
-
}
|
2317
|
-
return;
|
2318
|
-
}
|
2319
|
-
|
2320
|
-
if (exception) {
|
2321
|
-
throw exception;
|
2322
|
-
}
|
2323
|
-
},
|
2324
|
-
|
2325
|
-
reset: function reset() {
|
2326
|
-
this.timeouts = {};
|
2327
|
-
},
|
2328
|
-
|
2329
|
-
Date: (function () {
|
2330
|
-
var NativeDate = Date;
|
2331
|
-
|
2332
|
-
function ClockDate(year, month, date, hour, minute, second, ms) {
|
2333
|
-
// Defensive and verbose to avoid potential harm in passing
|
2334
|
-
// explicit undefined when user does not pass argument
|
2335
|
-
switch (arguments.length) {
|
2336
|
-
case 0:
|
2337
|
-
return new NativeDate(ClockDate.clock.now);
|
2338
|
-
case 1:
|
2339
|
-
return new NativeDate(year);
|
2340
|
-
case 2:
|
2341
|
-
return new NativeDate(year, month);
|
2342
|
-
case 3:
|
2343
|
-
return new NativeDate(year, month, date);
|
2344
|
-
case 4:
|
2345
|
-
return new NativeDate(year, month, date, hour);
|
2346
|
-
case 5:
|
2347
|
-
return new NativeDate(year, month, date, hour, minute);
|
2348
|
-
case 6:
|
2349
|
-
return new NativeDate(year, month, date, hour, minute, second);
|
2350
|
-
default:
|
2351
|
-
return new NativeDate(year, month, date, hour, minute, second, ms);
|
2352
|
-
}
|
2353
|
-
}
|
2354
|
-
|
2355
|
-
return mirrorDateProperties(ClockDate, NativeDate);
|
2356
|
-
}())
|
2357
|
-
};
|
2358
|
-
|
2359
|
-
function mirrorDateProperties(target, source) {
|
2360
|
-
if (source.now) {
|
2361
|
-
target.now = function now() {
|
2362
|
-
return target.clock.now;
|
2363
|
-
};
|
2364
|
-
} else {
|
2365
|
-
delete target.now;
|
2366
|
-
}
|
2367
|
-
|
2368
|
-
if (source.toSource) {
|
2369
|
-
target.toSource = function toSource() {
|
2370
|
-
return source.toSource();
|
2371
|
-
};
|
2372
|
-
} else {
|
2373
|
-
delete target.toSource;
|
2374
|
-
}
|
2375
|
-
|
2376
|
-
target.toString = function toString() {
|
2377
|
-
return source.toString();
|
2378
|
-
};
|
2379
|
-
|
2380
|
-
target.prototype = source.prototype;
|
2381
|
-
target.parse = source.parse;
|
2382
|
-
target.UTC = source.UTC;
|
2383
|
-
target.prototype.toUTCString = source.prototype.toUTCString;
|
2384
|
-
return target;
|
2385
|
-
}
|
2386
|
-
|
2387
|
-
var methods = ["Date", "setTimeout", "setInterval",
|
2388
|
-
"clearTimeout", "clearInterval"];
|
2389
|
-
|
2390
|
-
function restore() {
|
2391
|
-
var method;
|
2392
|
-
|
2393
|
-
for (var i = 0, l = this.methods.length; i < l; i++) {
|
2394
|
-
method = this.methods[i];
|
2395
|
-
if (global[method].hadOwnProperty) {
|
2396
|
-
global[method] = this["_" + method];
|
2397
|
-
} else {
|
2398
|
-
delete global[method];
|
2399
|
-
}
|
2400
|
-
}
|
2401
|
-
|
2402
|
-
// Prevent multiple executions which will completely remove these props
|
2403
|
-
this.methods = [];
|
2404
|
-
}
|
2405
|
-
|
2406
|
-
function stubGlobal(method, clock) {
|
2407
|
-
clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(global, method);
|
2408
|
-
clock["_" + method] = global[method];
|
2409
|
-
|
2410
|
-
if (method == "Date") {
|
2411
|
-
var date = mirrorDateProperties(clock[method], global[method]);
|
2412
|
-
global[method] = date;
|
2413
|
-
} else {
|
2414
|
-
global[method] = function () {
|
2415
|
-
return clock[method].apply(clock, arguments);
|
2416
|
-
};
|
2417
|
-
|
2418
|
-
for (var prop in clock[method]) {
|
2419
|
-
if (clock[method].hasOwnProperty(prop)) {
|
2420
|
-
global[method][prop] = clock[method][prop];
|
2421
|
-
}
|
2422
|
-
}
|
2423
|
-
}
|
2424
|
-
|
2425
|
-
global[method].clock = clock;
|
2426
|
-
}
|
2427
|
-
|
2428
|
-
sinon.useFakeTimers = function useFakeTimers(now) {
|
2429
|
-
var clock = sinon.clock.create(now);
|
2430
|
-
clock.restore = restore;
|
2431
|
-
clock.methods = Array.prototype.slice.call(arguments,
|
2432
|
-
typeof now == "number" ? 1 : 0);
|
2433
|
-
|
2434
|
-
if (clock.methods.length === 0) {
|
2435
|
-
clock.methods = methods;
|
2436
|
-
}
|
2437
|
-
|
2438
|
-
for (var i = 0, l = clock.methods.length; i < l; i++) {
|
2439
|
-
stubGlobal(clock.methods[i], clock);
|
2440
|
-
}
|
2441
|
-
|
2442
|
-
return clock;
|
2443
|
-
};
|
2444
|
-
}(typeof global != "undefined" && typeof global !== "function" ? global : this));
|
2445
|
-
|
2446
|
-
sinon.timers = {
|
2447
|
-
setTimeout: setTimeout,
|
2448
|
-
clearTimeout: clearTimeout,
|
2449
|
-
setInterval: setInterval,
|
2450
|
-
clearInterval: clearInterval,
|
2451
|
-
Date: Date
|
2452
|
-
};
|
2453
|
-
|
2454
|
-
if (typeof module == "object" && typeof require == "function") {
|
2455
|
-
module.exports = sinon;
|
2456
|
-
}
|
2457
|
-
|
2458
|
-
/*jslint eqeqeq: false, onevar: false*/
|
2459
|
-
/*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
|
2460
|
-
/**
|
2461
|
-
* Minimal Event interface implementation
|
2462
|
-
*
|
2463
|
-
* Original implementation by Sven Fuchs: https://gist.github.com/995028
|
2464
|
-
* Modifications and tests by Christian Johansen.
|
2465
|
-
*
|
2466
|
-
* @author Sven Fuchs (svenfuchs@artweb-design.de)
|
2467
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
2468
|
-
* @license BSD
|
2469
|
-
*
|
2470
|
-
* Copyright (c) 2011 Sven Fuchs, Christian Johansen
|
2471
|
-
*/
|
2472
|
-
|
2473
|
-
if (typeof sinon == "undefined") {
|
2474
|
-
this.sinon = {};
|
2475
|
-
}
|
2476
|
-
|
2477
|
-
(function () {
|
2478
|
-
var push = [].push;
|
2479
|
-
|
2480
|
-
sinon.Event = function Event(type, bubbles, cancelable) {
|
2481
|
-
this.initEvent(type, bubbles, cancelable);
|
2482
|
-
};
|
2483
|
-
|
2484
|
-
sinon.Event.prototype = {
|
2485
|
-
initEvent: function(type, bubbles, cancelable) {
|
2486
|
-
this.type = type;
|
2487
|
-
this.bubbles = bubbles;
|
2488
|
-
this.cancelable = cancelable;
|
2489
|
-
},
|
2490
|
-
|
2491
|
-
stopPropagation: function () {},
|
2492
|
-
|
2493
|
-
preventDefault: function () {
|
2494
|
-
this.defaultPrevented = true;
|
2495
|
-
}
|
2496
|
-
};
|
2497
|
-
|
2498
|
-
sinon.EventTarget = {
|
2499
|
-
addEventListener: function addEventListener(event, listener, useCapture) {
|
2500
|
-
this.eventListeners = this.eventListeners || {};
|
2501
|
-
this.eventListeners[event] = this.eventListeners[event] || [];
|
2502
|
-
push.call(this.eventListeners[event], listener);
|
2503
|
-
},
|
2504
|
-
|
2505
|
-
removeEventListener: function removeEventListener(event, listener, useCapture) {
|
2506
|
-
var listeners = this.eventListeners && this.eventListeners[event] || [];
|
2507
|
-
|
2508
|
-
for (var i = 0, l = listeners.length; i < l; ++i) {
|
2509
|
-
if (listeners[i] == listener) {
|
2510
|
-
return listeners.splice(i, 1);
|
2511
|
-
}
|
2512
|
-
}
|
2513
|
-
},
|
2514
|
-
|
2515
|
-
dispatchEvent: function dispatchEvent(event) {
|
2516
|
-
var type = event.type;
|
2517
|
-
var listeners = this.eventListeners && this.eventListeners[type] || [];
|
2518
|
-
|
2519
|
-
for (var i = 0; i < listeners.length; i++) {
|
2520
|
-
if (typeof listeners[i] == "function") {
|
2521
|
-
listeners[i].call(this, event);
|
2522
|
-
} else {
|
2523
|
-
listeners[i].handleEvent(event);
|
2524
|
-
}
|
2525
|
-
}
|
2526
|
-
|
2527
|
-
return !!event.defaultPrevented;
|
2528
|
-
}
|
2529
|
-
};
|
2530
|
-
}());
|
2531
|
-
|
2532
|
-
/**
|
2533
|
-
* @depend ../../sinon.js
|
2534
|
-
* @depend event.js
|
2535
|
-
*/
|
2536
|
-
/*jslint eqeqeq: false, onevar: false*/
|
2537
|
-
/*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
|
2538
|
-
/**
|
2539
|
-
* Fake XMLHttpRequest object
|
2540
|
-
*
|
2541
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
2542
|
-
* @license BSD
|
2543
|
-
*
|
2544
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
2545
|
-
*/
|
2546
|
-
|
2547
|
-
if (typeof sinon == "undefined") {
|
2548
|
-
this.sinon = {};
|
2549
|
-
}
|
2550
|
-
sinon.xhr = { XMLHttpRequest: this.XMLHttpRequest };
|
2551
|
-
|
2552
|
-
// wrapper for global
|
2553
|
-
(function(global) {
|
2554
|
-
var xhr = sinon.xhr;
|
2555
|
-
xhr.GlobalXMLHttpRequest = global.XMLHttpRequest;
|
2556
|
-
xhr.GlobalActiveXObject = global.ActiveXObject;
|
2557
|
-
xhr.supportsActiveX = typeof xhr.GlobalActiveXObject != "undefined";
|
2558
|
-
xhr.supportsXHR = typeof xhr.GlobalXMLHttpRequest != "undefined";
|
2559
|
-
xhr.workingXHR = xhr.supportsXHR ? xhr.GlobalXMLHttpRequest : xhr.supportsActiveX
|
2560
|
-
? function() { return new xhr.GlobalActiveXObject("MSXML2.XMLHTTP.3.0") } : false;
|
2561
|
-
|
2562
|
-
/*jsl:ignore*/
|
2563
|
-
var unsafeHeaders = {
|
2564
|
-
"Accept-Charset": true,
|
2565
|
-
"Accept-Encoding": true,
|
2566
|
-
"Connection": true,
|
2567
|
-
"Content-Length": true,
|
2568
|
-
"Cookie": true,
|
2569
|
-
"Cookie2": true,
|
2570
|
-
"Content-Transfer-Encoding": true,
|
2571
|
-
"Date": true,
|
2572
|
-
"Expect": true,
|
2573
|
-
"Host": true,
|
2574
|
-
"Keep-Alive": true,
|
2575
|
-
"Referer": true,
|
2576
|
-
"TE": true,
|
2577
|
-
"Trailer": true,
|
2578
|
-
"Transfer-Encoding": true,
|
2579
|
-
"Upgrade": true,
|
2580
|
-
"User-Agent": true,
|
2581
|
-
"Via": true
|
2582
|
-
};
|
2583
|
-
/*jsl:end*/
|
2584
|
-
|
2585
|
-
function FakeXMLHttpRequest() {
|
2586
|
-
this.readyState = FakeXMLHttpRequest.UNSENT;
|
2587
|
-
this.requestHeaders = {};
|
2588
|
-
this.requestBody = null;
|
2589
|
-
this.status = 0;
|
2590
|
-
this.statusText = "";
|
2591
|
-
|
2592
|
-
if (typeof FakeXMLHttpRequest.onCreate == "function") {
|
2593
|
-
FakeXMLHttpRequest.onCreate(this);
|
2594
|
-
}
|
2595
|
-
}
|
2596
|
-
|
2597
|
-
function verifyState(xhr) {
|
2598
|
-
if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
|
2599
|
-
throw new Error("INVALID_STATE_ERR");
|
2600
|
-
}
|
2601
|
-
|
2602
|
-
if (xhr.sendFlag) {
|
2603
|
-
throw new Error("INVALID_STATE_ERR");
|
2604
|
-
}
|
2605
|
-
}
|
2606
|
-
|
2607
|
-
// filtering to enable a white-list version of Sinon FakeXhr,
|
2608
|
-
// where whitelisted requests are passed through to real XHR
|
2609
|
-
function each(collection, callback) {
|
2610
|
-
if (!collection) return;
|
2611
|
-
for (var i = 0, l = collection.length; i < l; i += 1) {
|
2612
|
-
callback(collection[i]);
|
2613
|
-
}
|
2614
|
-
}
|
2615
|
-
function some(collection, callback) {
|
2616
|
-
for (var index = 0; index < collection.length; index++) {
|
2617
|
-
if(callback(collection[index]) === true) return true;
|
2618
|
-
};
|
2619
|
-
return false;
|
2620
|
-
}
|
2621
|
-
// largest arity in XHR is 5 - XHR#open
|
2622
|
-
var apply = function(obj,method,args) {
|
2623
|
-
switch(args.length) {
|
2624
|
-
case 0: return obj[method]();
|
2625
|
-
case 1: return obj[method](args[0]);
|
2626
|
-
case 2: return obj[method](args[0],args[1]);
|
2627
|
-
case 3: return obj[method](args[0],args[1],args[2]);
|
2628
|
-
case 4: return obj[method](args[0],args[1],args[2],args[3]);
|
2629
|
-
case 5: return obj[method](args[0],args[1],args[2],args[3],args[4]);
|
2630
|
-
};
|
2631
|
-
};
|
2632
|
-
|
2633
|
-
FakeXMLHttpRequest.filters = [];
|
2634
|
-
FakeXMLHttpRequest.addFilter = function(fn) {
|
2635
|
-
this.filters.push(fn)
|
2636
|
-
};
|
2637
|
-
var IE6Re = /MSIE 6/;
|
2638
|
-
FakeXMLHttpRequest.defake = function(fakeXhr,xhrArgs) {
|
2639
|
-
var xhr = new sinon.xhr.workingXHR();
|
2640
|
-
each(["open","setRequestHeader","send","abort","getResponseHeader",
|
2641
|
-
"getAllResponseHeaders","addEventListener","overrideMimeType","removeEventListener"],
|
2642
|
-
function(method) {
|
2643
|
-
fakeXhr[method] = function() {
|
2644
|
-
return apply(xhr,method,arguments);
|
2645
|
-
};
|
2646
|
-
});
|
2647
|
-
|
2648
|
-
var copyAttrs = function(args) {
|
2649
|
-
each(args, function(attr) {
|
2650
|
-
try {
|
2651
|
-
fakeXhr[attr] = xhr[attr]
|
2652
|
-
} catch(e) {
|
2653
|
-
if(!IE6Re.test(navigator.userAgent)) throw e;
|
2654
|
-
}
|
2655
|
-
});
|
2656
|
-
};
|
2657
|
-
|
2658
|
-
var stateChange = function() {
|
2659
|
-
fakeXhr.readyState = xhr.readyState;
|
2660
|
-
if(xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) {
|
2661
|
-
copyAttrs(["status","statusText"]);
|
2662
|
-
}
|
2663
|
-
if(xhr.readyState >= FakeXMLHttpRequest.LOADING) {
|
2664
|
-
copyAttrs(["responseText"]);
|
2665
|
-
}
|
2666
|
-
if(xhr.readyState === FakeXMLHttpRequest.DONE) {
|
2667
|
-
copyAttrs(["responseXML"]);
|
2668
|
-
}
|
2669
|
-
if(fakeXhr.onreadystatechange) fakeXhr.onreadystatechange.call(fakeXhr);
|
2670
|
-
};
|
2671
|
-
if(xhr.addEventListener) {
|
2672
|
-
for(var event in fakeXhr.eventListeners) {
|
2673
|
-
if(fakeXhr.eventListeners.hasOwnProperty(event)) {
|
2674
|
-
each(fakeXhr.eventListeners[event],function(handler) {
|
2675
|
-
xhr.addEventListener(event, handler);
|
2676
|
-
});
|
2677
|
-
}
|
2678
|
-
}
|
2679
|
-
xhr.addEventListener("readystatechange",stateChange);
|
2680
|
-
} else {
|
2681
|
-
xhr.onreadystatechange = stateChange;
|
2682
|
-
}
|
2683
|
-
apply(xhr,"open",xhrArgs);
|
2684
|
-
};
|
2685
|
-
FakeXMLHttpRequest.useFilters = false;
|
2686
|
-
|
2687
|
-
function verifyRequestSent(xhr) {
|
2688
|
-
if (xhr.readyState == FakeXMLHttpRequest.DONE) {
|
2689
|
-
throw new Error("Request done");
|
2690
|
-
}
|
2691
|
-
}
|
2692
|
-
|
2693
|
-
function verifyHeadersReceived(xhr) {
|
2694
|
-
if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
|
2695
|
-
throw new Error("No headers received");
|
2696
|
-
}
|
2697
|
-
}
|
2698
|
-
|
2699
|
-
function verifyResponseBodyType(body) {
|
2700
|
-
if (typeof body != "string") {
|
2701
|
-
var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
|
2702
|
-
body + ", which is not a string.");
|
2703
|
-
error.name = "InvalidBodyException";
|
2704
|
-
throw error;
|
2705
|
-
}
|
2706
|
-
}
|
2707
|
-
|
2708
|
-
sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
|
2709
|
-
async: true,
|
2710
|
-
|
2711
|
-
open: function open(method, url, async, username, password) {
|
2712
|
-
this.method = method;
|
2713
|
-
this.url = url;
|
2714
|
-
this.async = typeof async == "boolean" ? async : true;
|
2715
|
-
this.username = username;
|
2716
|
-
this.password = password;
|
2717
|
-
this.responseText = null;
|
2718
|
-
this.responseXML = null;
|
2719
|
-
this.requestHeaders = {};
|
2720
|
-
this.sendFlag = false;
|
2721
|
-
if(sinon.FakeXMLHttpRequest.useFilters === true) {
|
2722
|
-
var xhrArgs = arguments;
|
2723
|
-
var defake = some(FakeXMLHttpRequest.filters,function(filter) {
|
2724
|
-
return filter.apply(this,xhrArgs)
|
2725
|
-
});
|
2726
|
-
if (defake) {
|
2727
|
-
return sinon.FakeXMLHttpRequest.defake(this,arguments);
|
2728
|
-
}
|
2729
|
-
}
|
2730
|
-
this.readyStateChange(FakeXMLHttpRequest.OPENED);
|
2731
|
-
},
|
2732
|
-
|
2733
|
-
readyStateChange: function readyStateChange(state) {
|
2734
|
-
this.readyState = state;
|
2735
|
-
|
2736
|
-
if (typeof this.onreadystatechange == "function") {
|
2737
|
-
try {
|
2738
|
-
this.onreadystatechange();
|
2739
|
-
} catch (e) {
|
2740
|
-
sinon.logError("Fake XHR onreadystatechange handler", e);
|
2741
|
-
}
|
2742
|
-
}
|
2743
|
-
|
2744
|
-
this.dispatchEvent(new sinon.Event("readystatechange"));
|
2745
|
-
},
|
2746
|
-
|
2747
|
-
setRequestHeader: function setRequestHeader(header, value) {
|
2748
|
-
verifyState(this);
|
2749
|
-
|
2750
|
-
if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
|
2751
|
-
throw new Error("Refused to set unsafe header \"" + header + "\"");
|
2752
|
-
}
|
2753
|
-
|
2754
|
-
if (this.requestHeaders[header]) {
|
2755
|
-
this.requestHeaders[header] += "," + value;
|
2756
|
-
} else {
|
2757
|
-
this.requestHeaders[header] = value;
|
2758
|
-
}
|
2759
|
-
},
|
2760
|
-
|
2761
|
-
// Helps testing
|
2762
|
-
setResponseHeaders: function setResponseHeaders(headers) {
|
2763
|
-
this.responseHeaders = {};
|
2764
|
-
|
2765
|
-
for (var header in headers) {
|
2766
|
-
if (headers.hasOwnProperty(header)) {
|
2767
|
-
this.responseHeaders[header] = headers[header];
|
2768
|
-
}
|
2769
|
-
}
|
2770
|
-
|
2771
|
-
if (this.async) {
|
2772
|
-
this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
|
2773
|
-
}
|
2774
|
-
},
|
2775
|
-
|
2776
|
-
// Currently treats ALL data as a DOMString (i.e. no Document)
|
2777
|
-
send: function send(data) {
|
2778
|
-
verifyState(this);
|
2779
|
-
|
2780
|
-
if (!/^(get|head)$/i.test(this.method)) {
|
2781
|
-
if (this.requestHeaders["Content-Type"]) {
|
2782
|
-
var value = this.requestHeaders["Content-Type"].split(";");
|
2783
|
-
this.requestHeaders["Content-Type"] = value[0] + ";charset=utf-8";
|
2784
|
-
} else {
|
2785
|
-
this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
|
2786
|
-
}
|
2787
|
-
|
2788
|
-
this.requestBody = data;
|
2789
|
-
}
|
2790
|
-
|
2791
|
-
this.errorFlag = false;
|
2792
|
-
this.sendFlag = this.async;
|
2793
|
-
this.readyStateChange(FakeXMLHttpRequest.OPENED);
|
2794
|
-
|
2795
|
-
if (typeof this.onSend == "function") {
|
2796
|
-
this.onSend(this);
|
2797
|
-
}
|
2798
|
-
},
|
2799
|
-
|
2800
|
-
abort: function abort() {
|
2801
|
-
this.aborted = true;
|
2802
|
-
this.responseText = null;
|
2803
|
-
this.errorFlag = true;
|
2804
|
-
this.requestHeaders = {};
|
2805
|
-
|
2806
|
-
if (this.readyState > sinon.FakeXMLHttpRequest.UNSENT && this.sendFlag) {
|
2807
|
-
this.readyStateChange(sinon.FakeXMLHttpRequest.DONE);
|
2808
|
-
this.sendFlag = false;
|
2809
|
-
}
|
2810
|
-
|
2811
|
-
this.readyState = sinon.FakeXMLHttpRequest.UNSENT;
|
2812
|
-
},
|
2813
|
-
|
2814
|
-
getResponseHeader: function getResponseHeader(header) {
|
2815
|
-
if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
|
2816
|
-
return null;
|
2817
|
-
}
|
2818
|
-
|
2819
|
-
if (/^Set-Cookie2?$/i.test(header)) {
|
2820
|
-
return null;
|
2821
|
-
}
|
2822
|
-
|
2823
|
-
header = header.toLowerCase();
|
2824
|
-
|
2825
|
-
for (var h in this.responseHeaders) {
|
2826
|
-
if (h.toLowerCase() == header) {
|
2827
|
-
return this.responseHeaders[h];
|
2828
|
-
}
|
2829
|
-
}
|
2830
|
-
|
2831
|
-
return null;
|
2832
|
-
},
|
2833
|
-
|
2834
|
-
getAllResponseHeaders: function getAllResponseHeaders() {
|
2835
|
-
if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
|
2836
|
-
return "";
|
2837
|
-
}
|
2838
|
-
|
2839
|
-
var headers = "";
|
2840
|
-
|
2841
|
-
for (var header in this.responseHeaders) {
|
2842
|
-
if (this.responseHeaders.hasOwnProperty(header) &&
|
2843
|
-
!/^Set-Cookie2?$/i.test(header)) {
|
2844
|
-
headers += header + ": " + this.responseHeaders[header] + "\r\n";
|
2845
|
-
}
|
2846
|
-
}
|
2847
|
-
|
2848
|
-
return headers;
|
2849
|
-
},
|
2850
|
-
|
2851
|
-
setResponseBody: function setResponseBody(body) {
|
2852
|
-
verifyRequestSent(this);
|
2853
|
-
verifyHeadersReceived(this);
|
2854
|
-
verifyResponseBodyType(body);
|
2855
|
-
|
2856
|
-
var chunkSize = this.chunkSize || 10;
|
2857
|
-
var index = 0;
|
2858
|
-
this.responseText = "";
|
2859
|
-
|
2860
|
-
do {
|
2861
|
-
if (this.async) {
|
2862
|
-
this.readyStateChange(FakeXMLHttpRequest.LOADING);
|
2863
|
-
}
|
2864
|
-
|
2865
|
-
this.responseText += body.substring(index, index + chunkSize);
|
2866
|
-
index += chunkSize;
|
2867
|
-
} while (index < body.length);
|
2868
|
-
|
2869
|
-
var type = this.getResponseHeader("Content-Type");
|
2870
|
-
|
2871
|
-
if (this.responseText &&
|
2872
|
-
(!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
|
2873
|
-
try {
|
2874
|
-
this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText);
|
2875
|
-
} catch (e) {
|
2876
|
-
// Unable to parse XML - no biggie
|
2877
|
-
}
|
2878
|
-
}
|
2879
|
-
|
2880
|
-
if (this.async) {
|
2881
|
-
this.readyStateChange(FakeXMLHttpRequest.DONE);
|
2882
|
-
} else {
|
2883
|
-
this.readyState = FakeXMLHttpRequest.DONE;
|
2884
|
-
}
|
2885
|
-
},
|
2886
|
-
|
2887
|
-
respond: function respond(status, headers, body) {
|
2888
|
-
this.setResponseHeaders(headers || {});
|
2889
|
-
this.status = typeof status == "number" ? status : 200;
|
2890
|
-
this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
|
2891
|
-
this.setResponseBody(body || "");
|
2892
|
-
}
|
2893
|
-
});
|
2894
|
-
|
2895
|
-
sinon.extend(FakeXMLHttpRequest, {
|
2896
|
-
UNSENT: 0,
|
2897
|
-
OPENED: 1,
|
2898
|
-
HEADERS_RECEIVED: 2,
|
2899
|
-
LOADING: 3,
|
2900
|
-
DONE: 4
|
2901
|
-
});
|
2902
|
-
|
2903
|
-
// Borrowed from JSpec
|
2904
|
-
FakeXMLHttpRequest.parseXML = function parseXML(text) {
|
2905
|
-
var xmlDoc;
|
2906
|
-
|
2907
|
-
if (typeof DOMParser != "undefined") {
|
2908
|
-
var parser = new DOMParser();
|
2909
|
-
xmlDoc = parser.parseFromString(text, "text/xml");
|
2910
|
-
} else {
|
2911
|
-
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
|
2912
|
-
xmlDoc.async = "false";
|
2913
|
-
xmlDoc.loadXML(text);
|
2914
|
-
}
|
2915
|
-
|
2916
|
-
return xmlDoc;
|
2917
|
-
};
|
2918
|
-
|
2919
|
-
FakeXMLHttpRequest.statusCodes = {
|
2920
|
-
100: "Continue",
|
2921
|
-
101: "Switching Protocols",
|
2922
|
-
200: "OK",
|
2923
|
-
201: "Created",
|
2924
|
-
202: "Accepted",
|
2925
|
-
203: "Non-Authoritative Information",
|
2926
|
-
204: "No Content",
|
2927
|
-
205: "Reset Content",
|
2928
|
-
206: "Partial Content",
|
2929
|
-
300: "Multiple Choice",
|
2930
|
-
301: "Moved Permanently",
|
2931
|
-
302: "Found",
|
2932
|
-
303: "See Other",
|
2933
|
-
304: "Not Modified",
|
2934
|
-
305: "Use Proxy",
|
2935
|
-
307: "Temporary Redirect",
|
2936
|
-
400: "Bad Request",
|
2937
|
-
401: "Unauthorized",
|
2938
|
-
402: "Payment Required",
|
2939
|
-
403: "Forbidden",
|
2940
|
-
404: "Not Found",
|
2941
|
-
405: "Method Not Allowed",
|
2942
|
-
406: "Not Acceptable",
|
2943
|
-
407: "Proxy Authentication Required",
|
2944
|
-
408: "Request Timeout",
|
2945
|
-
409: "Conflict",
|
2946
|
-
410: "Gone",
|
2947
|
-
411: "Length Required",
|
2948
|
-
412: "Precondition Failed",
|
2949
|
-
413: "Request Entity Too Large",
|
2950
|
-
414: "Request-URI Too Long",
|
2951
|
-
415: "Unsupported Media Type",
|
2952
|
-
416: "Requested Range Not Satisfiable",
|
2953
|
-
417: "Expectation Failed",
|
2954
|
-
422: "Unprocessable Entity",
|
2955
|
-
500: "Internal Server Error",
|
2956
|
-
501: "Not Implemented",
|
2957
|
-
502: "Bad Gateway",
|
2958
|
-
503: "Service Unavailable",
|
2959
|
-
504: "Gateway Timeout",
|
2960
|
-
505: "HTTP Version Not Supported"
|
2961
|
-
};
|
2962
|
-
|
2963
|
-
sinon.useFakeXMLHttpRequest = function () {
|
2964
|
-
sinon.FakeXMLHttpRequest.restore = function restore(keepOnCreate) {
|
2965
|
-
if (xhr.supportsXHR) {
|
2966
|
-
global.XMLHttpRequest = xhr.GlobalXMLHttpRequest;
|
2967
|
-
}
|
2968
|
-
|
2969
|
-
if (xhr.supportsActiveX) {
|
2970
|
-
global.ActiveXObject = xhr.GlobalActiveXObject;
|
2971
|
-
}
|
2972
|
-
|
2973
|
-
delete sinon.FakeXMLHttpRequest.restore;
|
2974
|
-
|
2975
|
-
if (keepOnCreate !== true) {
|
2976
|
-
delete sinon.FakeXMLHttpRequest.onCreate;
|
2977
|
-
}
|
2978
|
-
};
|
2979
|
-
if (xhr.supportsXHR) {
|
2980
|
-
global.XMLHttpRequest = sinon.FakeXMLHttpRequest;
|
2981
|
-
}
|
2982
|
-
|
2983
|
-
if (xhr.supportsActiveX) {
|
2984
|
-
global.ActiveXObject = function ActiveXObject(objId) {
|
2985
|
-
if (objId == "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) {
|
2986
|
-
|
2987
|
-
return new sinon.FakeXMLHttpRequest();
|
2988
|
-
}
|
2989
|
-
|
2990
|
-
return new xhr.GlobalActiveXObject(objId);
|
2991
|
-
};
|
2992
|
-
}
|
2993
|
-
|
2994
|
-
return sinon.FakeXMLHttpRequest;
|
2995
|
-
};
|
2996
|
-
|
2997
|
-
sinon.FakeXMLHttpRequest = FakeXMLHttpRequest;
|
2998
|
-
})(this);
|
2999
|
-
|
3000
|
-
if (typeof module == "object" && typeof require == "function") {
|
3001
|
-
module.exports = sinon;
|
3002
|
-
}
|
3003
|
-
|
3004
|
-
/**
|
3005
|
-
* @depend fake_xml_http_request.js
|
3006
|
-
*/
|
3007
|
-
/*jslint eqeqeq: false, onevar: false, regexp: false, plusplus: false*/
|
3008
|
-
/*global module, require, window*/
|
3009
|
-
/**
|
3010
|
-
* The Sinon "server" mimics a web server that receives requests from
|
3011
|
-
* sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
|
3012
|
-
* both synchronously and asynchronously. To respond synchronuously, canned
|
3013
|
-
* answers have to be provided upfront.
|
3014
|
-
*
|
3015
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
3016
|
-
* @license BSD
|
3017
|
-
*
|
3018
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
3019
|
-
*/
|
3020
|
-
|
3021
|
-
if (typeof sinon == "undefined") {
|
3022
|
-
var sinon = {};
|
3023
|
-
}
|
3024
|
-
|
3025
|
-
sinon.fakeServer = (function () {
|
3026
|
-
var push = [].push;
|
3027
|
-
function F() {}
|
3028
|
-
|
3029
|
-
function create(proto) {
|
3030
|
-
F.prototype = proto;
|
3031
|
-
return new F();
|
3032
|
-
}
|
3033
|
-
|
3034
|
-
function responseArray(handler) {
|
3035
|
-
var response = handler;
|
3036
|
-
|
3037
|
-
if (Object.prototype.toString.call(handler) != "[object Array]") {
|
3038
|
-
response = [200, {}, handler];
|
3039
|
-
}
|
3040
|
-
|
3041
|
-
if (typeof response[2] != "string") {
|
3042
|
-
throw new TypeError("Fake server response body should be string, but was " +
|
3043
|
-
typeof response[2]);
|
3044
|
-
}
|
3045
|
-
|
3046
|
-
return response;
|
3047
|
-
}
|
3048
|
-
|
3049
|
-
var wloc = typeof window !== "undefined" ? window.location : {};
|
3050
|
-
var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
|
3051
|
-
|
3052
|
-
function matchOne(response, reqMethod, reqUrl) {
|
3053
|
-
var rmeth = response.method;
|
3054
|
-
var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase();
|
3055
|
-
var url = response.url;
|
3056
|
-
var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl));
|
3057
|
-
|
3058
|
-
return matchMethod && matchUrl;
|
3059
|
-
}
|
3060
|
-
|
3061
|
-
function match(response, request) {
|
3062
|
-
var requestMethod = this.getHTTPMethod(request);
|
3063
|
-
var requestUrl = request.url;
|
3064
|
-
|
3065
|
-
if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
|
3066
|
-
requestUrl = requestUrl.replace(rCurrLoc, "");
|
3067
|
-
}
|
3068
|
-
|
3069
|
-
if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
|
3070
|
-
if (typeof response.response == "function") {
|
3071
|
-
var ru = response.url;
|
3072
|
-
var args = [request].concat(!ru ? [] : requestUrl.match(ru).slice(1));
|
3073
|
-
return response.response.apply(response, args);
|
3074
|
-
}
|
3075
|
-
|
3076
|
-
return true;
|
3077
|
-
}
|
3078
|
-
|
3079
|
-
return false;
|
3080
|
-
}
|
3081
|
-
|
3082
|
-
function log(response, request) {
|
3083
|
-
var str;
|
3084
|
-
|
3085
|
-
str = "Request:\n" + sinon.format(request) + "\n\n";
|
3086
|
-
str += "Response:\n" + sinon.format(response) + "\n\n";
|
3087
|
-
|
3088
|
-
sinon.log(str);
|
3089
|
-
}
|
3090
|
-
|
3091
|
-
return {
|
3092
|
-
create: function () {
|
3093
|
-
var server = create(this);
|
3094
|
-
this.xhr = sinon.useFakeXMLHttpRequest();
|
3095
|
-
server.requests = [];
|
3096
|
-
|
3097
|
-
this.xhr.onCreate = function (xhrObj) {
|
3098
|
-
server.addRequest(xhrObj);
|
3099
|
-
};
|
3100
|
-
|
3101
|
-
return server;
|
3102
|
-
},
|
3103
|
-
|
3104
|
-
addRequest: function addRequest(xhrObj) {
|
3105
|
-
var server = this;
|
3106
|
-
push.call(this.requests, xhrObj);
|
3107
|
-
|
3108
|
-
xhrObj.onSend = function () {
|
3109
|
-
server.handleRequest(this);
|
3110
|
-
};
|
3111
|
-
|
3112
|
-
if (this.autoRespond && !this.responding) {
|
3113
|
-
setTimeout(function () {
|
3114
|
-
server.responding = false;
|
3115
|
-
server.respond();
|
3116
|
-
}, this.autoRespondAfter || 10);
|
3117
|
-
|
3118
|
-
this.responding = true;
|
3119
|
-
}
|
3120
|
-
},
|
3121
|
-
|
3122
|
-
getHTTPMethod: function getHTTPMethod(request) {
|
3123
|
-
if (this.fakeHTTPMethods && /post/i.test(request.method)) {
|
3124
|
-
var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
|
3125
|
-
return !!matches ? matches[1] : request.method;
|
3126
|
-
}
|
3127
|
-
|
3128
|
-
return request.method;
|
3129
|
-
},
|
3130
|
-
|
3131
|
-
handleRequest: function handleRequest(xhr) {
|
3132
|
-
if (xhr.async) {
|
3133
|
-
if (!this.queue) {
|
3134
|
-
this.queue = [];
|
3135
|
-
}
|
3136
|
-
|
3137
|
-
push.call(this.queue, xhr);
|
3138
|
-
} else {
|
3139
|
-
this.processRequest(xhr);
|
3140
|
-
}
|
3141
|
-
},
|
3142
|
-
|
3143
|
-
respondWith: function respondWith(method, url, body) {
|
3144
|
-
if (arguments.length == 1 && typeof method != "function") {
|
3145
|
-
this.response = responseArray(method);
|
3146
|
-
return;
|
3147
|
-
}
|
3148
|
-
|
3149
|
-
if (!this.responses) { this.responses = []; }
|
3150
|
-
|
3151
|
-
if (arguments.length == 1) {
|
3152
|
-
body = method;
|
3153
|
-
url = method = null;
|
3154
|
-
}
|
3155
|
-
|
3156
|
-
if (arguments.length == 2) {
|
3157
|
-
body = url;
|
3158
|
-
url = method;
|
3159
|
-
method = null;
|
3160
|
-
}
|
3161
|
-
|
3162
|
-
push.call(this.responses, {
|
3163
|
-
method: method,
|
3164
|
-
url: url,
|
3165
|
-
response: typeof body == "function" ? body : responseArray(body)
|
3166
|
-
});
|
3167
|
-
},
|
3168
|
-
|
3169
|
-
respond: function respond() {
|
3170
|
-
if (arguments.length > 0) this.respondWith.apply(this, arguments);
|
3171
|
-
var queue = this.queue || [];
|
3172
|
-
var request;
|
3173
|
-
|
3174
|
-
while(request = queue.shift()) {
|
3175
|
-
this.processRequest(request);
|
3176
|
-
}
|
3177
|
-
},
|
3178
|
-
|
3179
|
-
processRequest: function processRequest(request) {
|
3180
|
-
try {
|
3181
|
-
if (request.aborted) {
|
3182
|
-
return;
|
3183
|
-
}
|
3184
|
-
|
3185
|
-
var response = this.response || [404, {}, ""];
|
3186
|
-
|
3187
|
-
if (this.responses) {
|
3188
|
-
for (var i = 0, l = this.responses.length; i < l; i++) {
|
3189
|
-
if (match.call(this, this.responses[i], request)) {
|
3190
|
-
response = this.responses[i].response;
|
3191
|
-
break;
|
3192
|
-
}
|
3193
|
-
}
|
3194
|
-
}
|
3195
|
-
|
3196
|
-
if (request.readyState != 4) {
|
3197
|
-
log(response, request);
|
3198
|
-
|
3199
|
-
request.respond(response[0], response[1], response[2]);
|
3200
|
-
}
|
3201
|
-
} catch (e) {
|
3202
|
-
sinon.logError("Fake server request processing", e);
|
3203
|
-
}
|
3204
|
-
},
|
3205
|
-
|
3206
|
-
restore: function restore() {
|
3207
|
-
return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
|
3208
|
-
}
|
3209
|
-
};
|
3210
|
-
}());
|
3211
|
-
|
3212
|
-
if (typeof module == "object" && typeof require == "function") {
|
3213
|
-
module.exports = sinon;
|
3214
|
-
}
|
3215
|
-
|
3216
|
-
/**
|
3217
|
-
* @depend fake_server.js
|
3218
|
-
* @depend fake_timers.js
|
3219
|
-
*/
|
3220
|
-
/*jslint browser: true, eqeqeq: false, onevar: false*/
|
3221
|
-
/*global sinon*/
|
3222
|
-
/**
|
3223
|
-
* Add-on for sinon.fakeServer that automatically handles a fake timer along with
|
3224
|
-
* the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
|
3225
|
-
* 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
|
3226
|
-
* it polls the object for completion with setInterval. Dispite the direct
|
3227
|
-
* motivation, there is nothing jQuery-specific in this file, so it can be used
|
3228
|
-
* in any environment where the ajax implementation depends on setInterval or
|
3229
|
-
* setTimeout.
|
3230
|
-
*
|
3231
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
3232
|
-
* @license BSD
|
3233
|
-
*
|
3234
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
3235
|
-
*/
|
3236
|
-
|
3237
|
-
(function () {
|
3238
|
-
function Server() {}
|
3239
|
-
Server.prototype = sinon.fakeServer;
|
3240
|
-
|
3241
|
-
sinon.fakeServerWithClock = new Server();
|
3242
|
-
|
3243
|
-
sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
|
3244
|
-
if (xhr.async) {
|
3245
|
-
if (typeof setTimeout.clock == "object") {
|
3246
|
-
this.clock = setTimeout.clock;
|
3247
|
-
} else {
|
3248
|
-
this.clock = sinon.useFakeTimers();
|
3249
|
-
this.resetClock = true;
|
3250
|
-
}
|
3251
|
-
|
3252
|
-
if (!this.longestTimeout) {
|
3253
|
-
var clockSetTimeout = this.clock.setTimeout;
|
3254
|
-
var clockSetInterval = this.clock.setInterval;
|
3255
|
-
var server = this;
|
3256
|
-
|
3257
|
-
this.clock.setTimeout = function (fn, timeout) {
|
3258
|
-
server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
|
3259
|
-
|
3260
|
-
return clockSetTimeout.apply(this, arguments);
|
3261
|
-
};
|
3262
|
-
|
3263
|
-
this.clock.setInterval = function (fn, timeout) {
|
3264
|
-
server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
|
3265
|
-
|
3266
|
-
return clockSetInterval.apply(this, arguments);
|
3267
|
-
};
|
3268
|
-
}
|
3269
|
-
}
|
3270
|
-
|
3271
|
-
return sinon.fakeServer.addRequest.call(this, xhr);
|
3272
|
-
};
|
3273
|
-
|
3274
|
-
sinon.fakeServerWithClock.respond = function respond() {
|
3275
|
-
var returnVal = sinon.fakeServer.respond.apply(this, arguments);
|
3276
|
-
|
3277
|
-
if (this.clock) {
|
3278
|
-
this.clock.tick(this.longestTimeout || 0);
|
3279
|
-
this.longestTimeout = 0;
|
3280
|
-
|
3281
|
-
if (this.resetClock) {
|
3282
|
-
this.clock.restore();
|
3283
|
-
this.resetClock = false;
|
3284
|
-
}
|
3285
|
-
}
|
3286
|
-
|
3287
|
-
return returnVal;
|
3288
|
-
};
|
3289
|
-
|
3290
|
-
sinon.fakeServerWithClock.restore = function restore() {
|
3291
|
-
if (this.clock) {
|
3292
|
-
this.clock.restore();
|
3293
|
-
}
|
3294
|
-
|
3295
|
-
return sinon.fakeServer.restore.apply(this, arguments);
|
3296
|
-
};
|
3297
|
-
}());
|
3298
|
-
|
3299
|
-
/**
|
3300
|
-
* @depend ../sinon.js
|
3301
|
-
* @depend collection.js
|
3302
|
-
* @depend util/fake_timers.js
|
3303
|
-
* @depend util/fake_server_with_clock.js
|
3304
|
-
*/
|
3305
|
-
/*jslint eqeqeq: false, onevar: false, plusplus: false*/
|
3306
|
-
/*global require, module*/
|
3307
|
-
/**
|
3308
|
-
* Manages fake collections as well as fake utilities such as Sinon's
|
3309
|
-
* timers and fake XHR implementation in one convenient object.
|
3310
|
-
*
|
3311
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
3312
|
-
* @license BSD
|
3313
|
-
*
|
3314
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
3315
|
-
*/
|
3316
|
-
|
3317
|
-
if (typeof module == "object" && typeof require == "function") {
|
3318
|
-
var sinon = require("../sinon");
|
3319
|
-
sinon.extend(sinon, require("./util/fake_timers"));
|
3320
|
-
}
|
3321
|
-
|
3322
|
-
(function () {
|
3323
|
-
var push = [].push;
|
3324
|
-
|
3325
|
-
function exposeValue(sandbox, config, key, value) {
|
3326
|
-
if (!value) {
|
3327
|
-
return;
|
3328
|
-
}
|
3329
|
-
|
3330
|
-
if (config.injectInto) {
|
3331
|
-
config.injectInto[key] = value;
|
3332
|
-
} else {
|
3333
|
-
push.call(sandbox.args, value);
|
3334
|
-
}
|
3335
|
-
}
|
3336
|
-
|
3337
|
-
function prepareSandboxFromConfig(config) {
|
3338
|
-
var sandbox = sinon.create(sinon.sandbox);
|
3339
|
-
|
3340
|
-
if (config.useFakeServer) {
|
3341
|
-
if (typeof config.useFakeServer == "object") {
|
3342
|
-
sandbox.serverPrototype = config.useFakeServer;
|
3343
|
-
}
|
3344
|
-
|
3345
|
-
sandbox.useFakeServer();
|
3346
|
-
}
|
3347
|
-
|
3348
|
-
if (config.useFakeTimers) {
|
3349
|
-
if (typeof config.useFakeTimers == "object") {
|
3350
|
-
sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers);
|
3351
|
-
} else {
|
3352
|
-
sandbox.useFakeTimers();
|
3353
|
-
}
|
3354
|
-
}
|
3355
|
-
|
3356
|
-
return sandbox;
|
3357
|
-
}
|
3358
|
-
|
3359
|
-
sinon.sandbox = sinon.extend(sinon.create(sinon.collection), {
|
3360
|
-
useFakeTimers: function useFakeTimers() {
|
3361
|
-
this.clock = sinon.useFakeTimers.apply(sinon, arguments);
|
3362
|
-
|
3363
|
-
return this.add(this.clock);
|
3364
|
-
},
|
3365
|
-
|
3366
|
-
serverPrototype: sinon.fakeServer,
|
3367
|
-
|
3368
|
-
useFakeServer: function useFakeServer() {
|
3369
|
-
var proto = this.serverPrototype || sinon.fakeServer;
|
3370
|
-
|
3371
|
-
if (!proto || !proto.create) {
|
3372
|
-
return null;
|
3373
|
-
}
|
3374
|
-
|
3375
|
-
this.server = proto.create();
|
3376
|
-
return this.add(this.server);
|
3377
|
-
},
|
3378
|
-
|
3379
|
-
inject: function (obj) {
|
3380
|
-
sinon.collection.inject.call(this, obj);
|
3381
|
-
|
3382
|
-
if (this.clock) {
|
3383
|
-
obj.clock = this.clock;
|
3384
|
-
}
|
3385
|
-
|
3386
|
-
if (this.server) {
|
3387
|
-
obj.server = this.server;
|
3388
|
-
obj.requests = this.server.requests;
|
3389
|
-
}
|
3390
|
-
|
3391
|
-
return obj;
|
3392
|
-
},
|
3393
|
-
|
3394
|
-
create: function (config) {
|
3395
|
-
if (!config) {
|
3396
|
-
return sinon.create(sinon.sandbox);
|
3397
|
-
}
|
3398
|
-
|
3399
|
-
var sandbox = prepareSandboxFromConfig(config);
|
3400
|
-
sandbox.args = sandbox.args || [];
|
3401
|
-
var prop, value, exposed = sandbox.inject({});
|
3402
|
-
|
3403
|
-
if (config.properties) {
|
3404
|
-
for (var i = 0, l = config.properties.length; i < l; i++) {
|
3405
|
-
prop = config.properties[i];
|
3406
|
-
value = exposed[prop] || prop == "sandbox" && sandbox;
|
3407
|
-
exposeValue(sandbox, config, prop, value);
|
3408
|
-
}
|
3409
|
-
} else {
|
3410
|
-
exposeValue(sandbox, config, "sandbox", value);
|
3411
|
-
}
|
3412
|
-
|
3413
|
-
return sandbox;
|
3414
|
-
}
|
3415
|
-
});
|
3416
|
-
|
3417
|
-
sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;
|
3418
|
-
|
3419
|
-
if (typeof module == "object" && typeof require == "function") {
|
3420
|
-
module.exports = sinon.sandbox;
|
3421
|
-
}
|
3422
|
-
}());
|
3423
|
-
|
3424
|
-
/**
|
3425
|
-
* @depend ../sinon.js
|
3426
|
-
* @depend stub.js
|
3427
|
-
* @depend mock.js
|
3428
|
-
* @depend sandbox.js
|
3429
|
-
*/
|
3430
|
-
/*jslint eqeqeq: false, onevar: false, forin: true, plusplus: false*/
|
3431
|
-
/*global module, require, sinon*/
|
3432
|
-
/**
|
3433
|
-
* Test function, sandboxes fakes
|
3434
|
-
*
|
3435
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
3436
|
-
* @license BSD
|
3437
|
-
*
|
3438
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
3439
|
-
*/
|
3440
|
-
|
3441
|
-
(function (sinon) {
|
3442
|
-
var commonJSModule = typeof module == "object" && typeof require == "function";
|
3443
|
-
|
3444
|
-
if (!sinon && commonJSModule) {
|
3445
|
-
sinon = require("../sinon");
|
3446
|
-
}
|
3447
|
-
|
3448
|
-
if (!sinon) {
|
3449
|
-
return;
|
3450
|
-
}
|
3451
|
-
|
3452
|
-
function test(callback) {
|
3453
|
-
var type = typeof callback;
|
3454
|
-
|
3455
|
-
if (type != "function") {
|
3456
|
-
throw new TypeError("sinon.test needs to wrap a test function, got " + type);
|
3457
|
-
}
|
3458
|
-
|
3459
|
-
return function () {
|
3460
|
-
var config = sinon.getConfig(sinon.config);
|
3461
|
-
config.injectInto = config.injectIntoThis && this || config.injectInto;
|
3462
|
-
var sandbox = sinon.sandbox.create(config);
|
3463
|
-
var exception, result;
|
3464
|
-
var args = Array.prototype.slice.call(arguments).concat(sandbox.args);
|
3465
|
-
|
3466
|
-
try {
|
3467
|
-
result = callback.apply(this, args);
|
3468
|
-
} catch (e) {
|
3469
|
-
exception = e;
|
3470
|
-
}
|
3471
|
-
|
3472
|
-
if (typeof exception !== "undefined") {
|
3473
|
-
sandbox.restore();
|
3474
|
-
throw exception;
|
3475
|
-
}
|
3476
|
-
else {
|
3477
|
-
sandbox.verifyAndRestore();
|
3478
|
-
}
|
3479
|
-
|
3480
|
-
return result;
|
3481
|
-
};
|
3482
|
-
}
|
3483
|
-
|
3484
|
-
test.config = {
|
3485
|
-
injectIntoThis: true,
|
3486
|
-
injectInto: null,
|
3487
|
-
properties: ["spy", "stub", "mock", "clock", "server", "requests"],
|
3488
|
-
useFakeTimers: true,
|
3489
|
-
useFakeServer: true
|
3490
|
-
};
|
3491
|
-
|
3492
|
-
if (commonJSModule) {
|
3493
|
-
module.exports = test;
|
3494
|
-
} else {
|
3495
|
-
sinon.test = test;
|
3496
|
-
}
|
3497
|
-
}(typeof sinon == "object" && sinon || null));
|
3498
|
-
|
3499
|
-
/**
|
3500
|
-
* @depend ../sinon.js
|
3501
|
-
* @depend test.js
|
3502
|
-
*/
|
3503
|
-
/*jslint eqeqeq: false, onevar: false, eqeqeq: false*/
|
3504
|
-
/*global module, require, sinon*/
|
3505
|
-
/**
|
3506
|
-
* Test case, sandboxes all test functions
|
3507
|
-
*
|
3508
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
3509
|
-
* @license BSD
|
3510
|
-
*
|
3511
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
3512
|
-
*/
|
3513
|
-
|
3514
|
-
(function (sinon) {
|
3515
|
-
var commonJSModule = typeof module == "object" && typeof require == "function";
|
3516
|
-
|
3517
|
-
if (!sinon && commonJSModule) {
|
3518
|
-
sinon = require("../sinon");
|
3519
|
-
}
|
3520
|
-
|
3521
|
-
if (!sinon || !Object.prototype.hasOwnProperty) {
|
3522
|
-
return;
|
3523
|
-
}
|
3524
|
-
|
3525
|
-
function createTest(property, setUp, tearDown) {
|
3526
|
-
return function () {
|
3527
|
-
if (setUp) {
|
3528
|
-
setUp.apply(this, arguments);
|
3529
|
-
}
|
3530
|
-
|
3531
|
-
var exception, result;
|
3532
|
-
|
3533
|
-
try {
|
3534
|
-
result = property.apply(this, arguments);
|
3535
|
-
} catch (e) {
|
3536
|
-
exception = e;
|
3537
|
-
}
|
3538
|
-
|
3539
|
-
if (tearDown) {
|
3540
|
-
tearDown.apply(this, arguments);
|
3541
|
-
}
|
3542
|
-
|
3543
|
-
if (exception) {
|
3544
|
-
throw exception;
|
3545
|
-
}
|
3546
|
-
|
3547
|
-
return result;
|
3548
|
-
};
|
3549
|
-
}
|
3550
|
-
|
3551
|
-
function testCase(tests, prefix) {
|
3552
|
-
/*jsl:ignore*/
|
3553
|
-
if (!tests || typeof tests != "object") {
|
3554
|
-
throw new TypeError("sinon.testCase needs an object with test functions");
|
3555
|
-
}
|
3556
|
-
/*jsl:end*/
|
3557
|
-
|
3558
|
-
prefix = prefix || "test";
|
3559
|
-
var rPrefix = new RegExp("^" + prefix);
|
3560
|
-
var methods = {}, testName, property, method;
|
3561
|
-
var setUp = tests.setUp;
|
3562
|
-
var tearDown = tests.tearDown;
|
3563
|
-
|
3564
|
-
for (testName in tests) {
|
3565
|
-
if (tests.hasOwnProperty(testName)) {
|
3566
|
-
property = tests[testName];
|
3567
|
-
|
3568
|
-
if (/^(setUp|tearDown)$/.test(testName)) {
|
3569
|
-
continue;
|
3570
|
-
}
|
3571
|
-
|
3572
|
-
if (typeof property == "function" && rPrefix.test(testName)) {
|
3573
|
-
method = property;
|
3574
|
-
|
3575
|
-
if (setUp || tearDown) {
|
3576
|
-
method = createTest(property, setUp, tearDown);
|
3577
|
-
}
|
3578
|
-
|
3579
|
-
methods[testName] = sinon.test(method);
|
3580
|
-
} else {
|
3581
|
-
methods[testName] = tests[testName];
|
3582
|
-
}
|
3583
|
-
}
|
3584
|
-
}
|
3585
|
-
|
3586
|
-
return methods;
|
3587
|
-
}
|
3588
|
-
|
3589
|
-
if (commonJSModule) {
|
3590
|
-
module.exports = testCase;
|
3591
|
-
} else {
|
3592
|
-
sinon.testCase = testCase;
|
3593
|
-
}
|
3594
|
-
}(typeof sinon == "object" && sinon || null));
|
3595
|
-
|
3596
|
-
/**
|
3597
|
-
* @depend ../sinon.js
|
3598
|
-
* @depend stub.js
|
3599
|
-
*/
|
3600
|
-
/*jslint eqeqeq: false, onevar: false, nomen: false, plusplus: false*/
|
3601
|
-
/*global module, require, sinon*/
|
3602
|
-
/**
|
3603
|
-
* Assertions matching the test spy retrieval interface.
|
3604
|
-
*
|
3605
|
-
* @author Christian Johansen (christian@cjohansen.no)
|
3606
|
-
* @license BSD
|
3607
|
-
*
|
3608
|
-
* Copyright (c) 2010-2011 Christian Johansen
|
3609
|
-
*/
|
3610
|
-
|
3611
|
-
(function (sinon, global) {
|
3612
|
-
var commonJSModule = typeof module == "object" && typeof require == "function";
|
3613
|
-
var slice = Array.prototype.slice;
|
3614
|
-
var assert;
|
3615
|
-
|
3616
|
-
if (!sinon && commonJSModule) {
|
3617
|
-
sinon = require("../sinon");
|
3618
|
-
}
|
3619
|
-
|
3620
|
-
if (!sinon) {
|
3621
|
-
return;
|
3622
|
-
}
|
3623
|
-
|
3624
|
-
function verifyIsStub() {
|
3625
|
-
var method;
|
3626
|
-
|
3627
|
-
for (var i = 0, l = arguments.length; i < l; ++i) {
|
3628
|
-
method = arguments[i];
|
3629
|
-
|
3630
|
-
if (!method) {
|
3631
|
-
assert.fail("fake is not a spy");
|
3632
|
-
}
|
3633
|
-
|
3634
|
-
if (typeof method != "function") {
|
3635
|
-
assert.fail(method + " is not a function");
|
3636
|
-
}
|
3637
|
-
|
3638
|
-
if (typeof method.getCall != "function") {
|
3639
|
-
assert.fail(method + " is not stubbed");
|
3640
|
-
}
|
3641
|
-
}
|
3642
|
-
}
|
3643
|
-
|
3644
|
-
function failAssertion(object, msg) {
|
3645
|
-
object = object || global;
|
3646
|
-
var failMethod = object.fail || assert.fail;
|
3647
|
-
failMethod.call(object, msg);
|
3648
|
-
}
|
3649
|
-
|
3650
|
-
function mirrorPropAsAssertion(name, method, message) {
|
3651
|
-
if (arguments.length == 2) {
|
3652
|
-
message = method;
|
3653
|
-
method = name;
|
3654
|
-
}
|
3655
|
-
|
3656
|
-
assert[name] = function (fake) {
|
3657
|
-
verifyIsStub(fake);
|
3658
|
-
|
3659
|
-
var args = slice.call(arguments, 1);
|
3660
|
-
var failed = false;
|
3661
|
-
|
3662
|
-
if (typeof method == "function") {
|
3663
|
-
failed = !method(fake);
|
3664
|
-
} else {
|
3665
|
-
failed = typeof fake[method] == "function" ?
|
3666
|
-
!fake[method].apply(fake, args) : !fake[method];
|
3667
|
-
}
|
3668
|
-
|
3669
|
-
if (failed) {
|
3670
|
-
failAssertion(this, fake.printf.apply(fake, [message].concat(args)));
|
3671
|
-
} else {
|
3672
|
-
assert.pass(name);
|
3673
|
-
}
|
3674
|
-
};
|
3675
|
-
}
|
3676
|
-
|
3677
|
-
function exposedName(prefix, prop) {
|
3678
|
-
return !prefix || /^fail/.test(prop) ? prop :
|
3679
|
-
prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
|
3680
|
-
};
|
3681
|
-
|
3682
|
-
assert = {
|
3683
|
-
failException: "AssertError",
|
3684
|
-
|
3685
|
-
fail: function fail(message) {
|
3686
|
-
var error = new Error(message);
|
3687
|
-
error.name = this.failException || assert.failException;
|
3688
|
-
|
3689
|
-
throw error;
|
3690
|
-
},
|
3691
|
-
|
3692
|
-
pass: function pass(assertion) {},
|
3693
|
-
|
3694
|
-
callOrder: function assertCallOrder() {
|
3695
|
-
verifyIsStub.apply(null, arguments);
|
3696
|
-
var expected = "", actual = "";
|
3697
|
-
|
3698
|
-
if (!sinon.calledInOrder(arguments)) {
|
3699
|
-
try {
|
3700
|
-
expected = [].join.call(arguments, ", ");
|
3701
|
-
actual = sinon.orderByFirstCall(slice.call(arguments)).join(", ");
|
3702
|
-
} catch (e) {
|
3703
|
-
// If this fails, we'll just fall back to the blank string
|
3704
|
-
}
|
3705
|
-
|
3706
|
-
failAssertion(this, "expected " + expected + " to be " +
|
3707
|
-
"called in order but were called as " + actual);
|
3708
|
-
} else {
|
3709
|
-
assert.pass("callOrder");
|
3710
|
-
}
|
3711
|
-
},
|
3712
|
-
|
3713
|
-
callCount: function assertCallCount(method, count) {
|
3714
|
-
verifyIsStub(method);
|
3715
|
-
|
3716
|
-
if (method.callCount != count) {
|
3717
|
-
var msg = "expected %n to be called " + sinon.timesInWords(count) +
|
3718
|
-
" but was called %c%C";
|
3719
|
-
failAssertion(this, method.printf(msg));
|
3720
|
-
} else {
|
3721
|
-
assert.pass("callCount");
|
3722
|
-
}
|
3723
|
-
},
|
3724
|
-
|
3725
|
-
expose: function expose(target, options) {
|
3726
|
-
if (!target) {
|
3727
|
-
throw new TypeError("target is null or undefined");
|
3728
|
-
}
|
3729
|
-
|
3730
|
-
var o = options || {};
|
3731
|
-
var prefix = typeof o.prefix == "undefined" && "assert" || o.prefix;
|
3732
|
-
var includeFail = typeof o.includeFail == "undefined" || !!o.includeFail;
|
3733
|
-
|
3734
|
-
for (var method in this) {
|
3735
|
-
if (method != "export" && (includeFail || !/^(fail)/.test(method))) {
|
3736
|
-
target[exposedName(prefix, method)] = this[method];
|
3737
|
-
}
|
3738
|
-
}
|
3739
|
-
|
3740
|
-
return target;
|
3741
|
-
}
|
3742
|
-
};
|
3743
|
-
|
3744
|
-
mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
|
3745
|
-
mirrorPropAsAssertion("notCalled", function (spy) { return !spy.called; },
|
3746
|
-
"expected %n to not have been called but was called %c%C");
|
3747
|
-
mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
|
3748
|
-
mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
|
3749
|
-
mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
|
3750
|
-
mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
|
3751
|
-
mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
|
3752
|
-
mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
|
3753
|
-
mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
|
3754
|
-
mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
|
3755
|
-
mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
|
3756
|
-
mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
|
3757
|
-
mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
|
3758
|
-
mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
|
3759
|
-
mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
|
3760
|
-
mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
|
3761
|
-
mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
|
3762
|
-
mirrorPropAsAssertion("threw", "%n did not throw exception%C");
|
3763
|
-
mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
|
3764
|
-
|
3765
|
-
if (commonJSModule) {
|
3766
|
-
module.exports = assert;
|
3767
|
-
} else {
|
3768
|
-
sinon.assert = assert;
|
3769
|
-
}
|
3770
|
-
}(typeof sinon == "object" && sinon || null, typeof window != "undefined" ? window : global));
|
3771
|
-
|