konacha-chai-matchers 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  module Konacha
2
2
  module Chai
3
3
  module Matchers
4
- VERSION = "0.0.9"
4
+ VERSION = "0.0.10"
5
5
  end
6
6
  end
7
7
  end
data/tasks/update.rb CHANGED
@@ -4,7 +4,7 @@ require 'json'
4
4
  desc 'updates all submodules'
5
5
  task :update do
6
6
  modules = collect_modules
7
- #update_modules modules
7
+ update_modules modules
8
8
  vendor_files modules
9
9
  end
10
10
 
@@ -1,3 +1,38 @@
1
+ /**
2
+ * Sinon.JS 1.5.2, 2013/01/20
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
+
1
36
  /*jslint eqeqeq: false, onevar: false, forin: true, nomen: false, regexp: false, plusplus: false*/
2
37
  /*global module, require, __dirname, document*/
3
38
  /**
@@ -8,7 +43,6 @@
8
43
  *
9
44
  * Copyright (c) 2010-2011 Christian Johansen
10
45
  */
11
- "use strict";
12
46
 
13
47
  var sinon = (function (buster) {
14
48
  var div = typeof document != "undefined" && document.createElement("div");
@@ -341,3 +375,3400 @@ var sinon = (function (buster) {
341
375
 
342
376
  return sinon;
343
377
  }(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 || "anonymous mock expectation",
1914
+ args: args
1915
+ });
1916
+
1917
+ var message = callStr.replace(", [...", "[, ...") + " " +
1918
+ expectedCallCountInWords(this);
1919
+
1920
+ if (this.met()) {
1921
+ return "Expectation met: " + message;
1922
+ }
1923
+
1924
+ return "Expected " + message + " (" +
1925
+ callCountInWords(this.callCount) + ")";
1926
+ },
1927
+
1928
+ verify: function verify() {
1929
+ if (!this.met()) {
1930
+ sinon.expectation.fail(this.toString());
1931
+ } else {
1932
+ sinon.expectation.pass(this.toString());
1933
+ }
1934
+
1935
+ return true;
1936
+ },
1937
+
1938
+ pass: function(message) {
1939
+ sinon.assert.pass(message);
1940
+ },
1941
+ fail: function (message) {
1942
+ var exception = new Error(message);
1943
+ exception.name = "ExpectationError";
1944
+
1945
+ throw exception;
1946
+ }
1947
+ };
1948
+ }());
1949
+
1950
+ if (commonJSModule) {
1951
+ module.exports = mock;
1952
+ } else {
1953
+ sinon.mock = mock;
1954
+ }
1955
+ }(typeof sinon == "object" && sinon || null));
1956
+
1957
+ /**
1958
+ * @depend ../sinon.js
1959
+ * @depend stub.js
1960
+ * @depend mock.js
1961
+ */
1962
+ /*jslint eqeqeq: false, onevar: false, forin: true*/
1963
+ /*global module, require, sinon*/
1964
+ /**
1965
+ * Collections of stubs, spies and mocks.
1966
+ *
1967
+ * @author Christian Johansen (christian@cjohansen.no)
1968
+ * @license BSD
1969
+ *
1970
+ * Copyright (c) 2010-2011 Christian Johansen
1971
+ */
1972
+
1973
+ (function (sinon) {
1974
+ var commonJSModule = typeof module == "object" && typeof require == "function";
1975
+ var push = [].push;
1976
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
1977
+
1978
+ if (!sinon && commonJSModule) {
1979
+ sinon = require("../sinon");
1980
+ }
1981
+
1982
+ if (!sinon) {
1983
+ return;
1984
+ }
1985
+
1986
+ function getFakes(fakeCollection) {
1987
+ if (!fakeCollection.fakes) {
1988
+ fakeCollection.fakes = [];
1989
+ }
1990
+
1991
+ return fakeCollection.fakes;
1992
+ }
1993
+
1994
+ function each(fakeCollection, method) {
1995
+ var fakes = getFakes(fakeCollection);
1996
+
1997
+ for (var i = 0, l = fakes.length; i < l; i += 1) {
1998
+ if (typeof fakes[i][method] == "function") {
1999
+ fakes[i][method]();
2000
+ }
2001
+ }
2002
+ }
2003
+
2004
+ function compact(fakeCollection) {
2005
+ var fakes = getFakes(fakeCollection);
2006
+ var i = 0;
2007
+ while (i < fakes.length) {
2008
+ fakes.splice(i, 1);
2009
+ }
2010
+ }
2011
+
2012
+ var collection = {
2013
+ verify: function resolve() {
2014
+ each(this, "verify");
2015
+ },
2016
+
2017
+ restore: function restore() {
2018
+ each(this, "restore");
2019
+ compact(this);
2020
+ },
2021
+
2022
+ verifyAndRestore: function verifyAndRestore() {
2023
+ var exception;
2024
+
2025
+ try {
2026
+ this.verify();
2027
+ } catch (e) {
2028
+ exception = e;
2029
+ }
2030
+
2031
+ this.restore();
2032
+
2033
+ if (exception) {
2034
+ throw exception;
2035
+ }
2036
+ },
2037
+
2038
+ add: function add(fake) {
2039
+ push.call(getFakes(this), fake);
2040
+ return fake;
2041
+ },
2042
+
2043
+ spy: function spy() {
2044
+ return this.add(sinon.spy.apply(sinon, arguments));
2045
+ },
2046
+
2047
+ stub: function stub(object, property, value) {
2048
+ if (property) {
2049
+ var original = object[property];
2050
+
2051
+ if (typeof original != "function") {
2052
+ if (!hasOwnProperty.call(object, property)) {
2053
+ throw new TypeError("Cannot stub non-existent own property " + property);
2054
+ }
2055
+
2056
+ object[property] = value;
2057
+
2058
+ return this.add({
2059
+ restore: function () {
2060
+ object[property] = original;
2061
+ }
2062
+ });
2063
+ }
2064
+ }
2065
+ if (!property && !!object && typeof object == "object") {
2066
+ var stubbedObj = sinon.stub.apply(sinon, arguments);
2067
+
2068
+ for (var prop in stubbedObj) {
2069
+ if (typeof stubbedObj[prop] === "function") {
2070
+ this.add(stubbedObj[prop]);
2071
+ }
2072
+ }
2073
+
2074
+ return stubbedObj;
2075
+ }
2076
+
2077
+ return this.add(sinon.stub.apply(sinon, arguments));
2078
+ },
2079
+
2080
+ mock: function mock() {
2081
+ return this.add(sinon.mock.apply(sinon, arguments));
2082
+ },
2083
+
2084
+ inject: function inject(obj) {
2085
+ var col = this;
2086
+
2087
+ obj.spy = function () {
2088
+ return col.spy.apply(col, arguments);
2089
+ };
2090
+
2091
+ obj.stub = function () {
2092
+ return col.stub.apply(col, arguments);
2093
+ };
2094
+
2095
+ obj.mock = function () {
2096
+ return col.mock.apply(col, arguments);
2097
+ };
2098
+
2099
+ return obj;
2100
+ }
2101
+ };
2102
+
2103
+ if (commonJSModule) {
2104
+ module.exports = collection;
2105
+ } else {
2106
+ sinon.collection = collection;
2107
+ }
2108
+ }(typeof sinon == "object" && sinon || null));
2109
+
2110
+ /*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/
2111
+ /*global module, require, window*/
2112
+ /**
2113
+ * Fake timer API
2114
+ * setTimeout
2115
+ * setInterval
2116
+ * clearTimeout
2117
+ * clearInterval
2118
+ * tick
2119
+ * reset
2120
+ * Date
2121
+ *
2122
+ * Inspired by jsUnitMockTimeOut from JsUnit
2123
+ *
2124
+ * @author Christian Johansen (christian@cjohansen.no)
2125
+ * @license BSD
2126
+ *
2127
+ * Copyright (c) 2010-2011 Christian Johansen
2128
+ */
2129
+
2130
+ if (typeof sinon == "undefined") {
2131
+ var sinon = {};
2132
+ }
2133
+
2134
+ (function (global) {
2135
+ var id = 1;
2136
+
2137
+ function addTimer(args, recurring) {
2138
+ if (args.length === 0) {
2139
+ throw new Error("Function requires at least 1 parameter");
2140
+ }
2141
+
2142
+ var toId = id++;
2143
+ var delay = args[1] || 0;
2144
+
2145
+ if (!this.timeouts) {
2146
+ this.timeouts = {};
2147
+ }
2148
+
2149
+ this.timeouts[toId] = {
2150
+ id: toId,
2151
+ func: args[0],
2152
+ callAt: this.now + delay,
2153
+ invokeArgs: Array.prototype.slice.call(args, 2)
2154
+ };
2155
+
2156
+ if (recurring === true) {
2157
+ this.timeouts[toId].interval = delay;
2158
+ }
2159
+
2160
+ return toId;
2161
+ }
2162
+
2163
+ function parseTime(str) {
2164
+ if (!str) {
2165
+ return 0;
2166
+ }
2167
+
2168
+ var strings = str.split(":");
2169
+ var l = strings.length, i = l;
2170
+ var ms = 0, parsed;
2171
+
2172
+ if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
2173
+ throw new Error("tick only understands numbers and 'h:m:s'");
2174
+ }
2175
+
2176
+ while (i--) {
2177
+ parsed = parseInt(strings[i], 10);
2178
+
2179
+ if (parsed >= 60) {
2180
+ throw new Error("Invalid time " + str);
2181
+ }
2182
+
2183
+ ms += parsed * Math.pow(60, (l - i - 1));
2184
+ }
2185
+
2186
+ return ms * 1000;
2187
+ }
2188
+
2189
+ function createObject(object) {
2190
+ var newObject;
2191
+
2192
+ if (Object.create) {
2193
+ newObject = Object.create(object);
2194
+ } else {
2195
+ var F = function () {};
2196
+ F.prototype = object;
2197
+ newObject = new F();
2198
+ }
2199
+
2200
+ newObject.Date.clock = newObject;
2201
+ return newObject;
2202
+ }
2203
+
2204
+ sinon.clock = {
2205
+ now: 0,
2206
+
2207
+ create: function create(now) {
2208
+ var clock = createObject(this);
2209
+
2210
+ if (typeof now == "number") {
2211
+ clock.now = now;
2212
+ }
2213
+
2214
+ if (!!now && typeof now == "object") {
2215
+ throw new TypeError("now should be milliseconds since UNIX epoch");
2216
+ }
2217
+
2218
+ return clock;
2219
+ },
2220
+
2221
+ setTimeout: function setTimeout(callback, timeout) {
2222
+ return addTimer.call(this, arguments, false);
2223
+ },
2224
+
2225
+ clearTimeout: function clearTimeout(timerId) {
2226
+ if (!this.timeouts) {
2227
+ this.timeouts = [];
2228
+ }
2229
+
2230
+ if (timerId in this.timeouts) {
2231
+ delete this.timeouts[timerId];
2232
+ }
2233
+ },
2234
+
2235
+ setInterval: function setInterval(callback, timeout) {
2236
+ return addTimer.call(this, arguments, true);
2237
+ },
2238
+
2239
+ clearInterval: function clearInterval(timerId) {
2240
+ this.clearTimeout(timerId);
2241
+ },
2242
+
2243
+ tick: function tick(ms) {
2244
+ ms = typeof ms == "number" ? ms : parseTime(ms);
2245
+ var tickFrom = this.now, tickTo = this.now + ms, previous = this.now;
2246
+ var timer = this.firstTimerInRange(tickFrom, tickTo);
2247
+
2248
+ var firstException;
2249
+ while (timer && tickFrom <= tickTo) {
2250
+ if (this.timeouts[timer.id]) {
2251
+ tickFrom = this.now = timer.callAt;
2252
+ try {
2253
+ this.callTimer(timer);
2254
+ } catch (e) {
2255
+ firstException = firstException || e;
2256
+ }
2257
+ }
2258
+
2259
+ timer = this.firstTimerInRange(previous, tickTo);
2260
+ previous = tickFrom;
2261
+ }
2262
+
2263
+ this.now = tickTo;
2264
+
2265
+ if (firstException) {
2266
+ throw firstException;
2267
+ }
2268
+
2269
+ return this.now;
2270
+ },
2271
+
2272
+ firstTimerInRange: function (from, to) {
2273
+ var timer, smallest, originalTimer;
2274
+
2275
+ for (var id in this.timeouts) {
2276
+ if (this.timeouts.hasOwnProperty(id)) {
2277
+ if (this.timeouts[id].callAt < from || this.timeouts[id].callAt > to) {
2278
+ continue;
2279
+ }
2280
+
2281
+ if (!smallest || this.timeouts[id].callAt < smallest) {
2282
+ originalTimer = this.timeouts[id];
2283
+ smallest = this.timeouts[id].callAt;
2284
+
2285
+ timer = {
2286
+ func: this.timeouts[id].func,
2287
+ callAt: this.timeouts[id].callAt,
2288
+ interval: this.timeouts[id].interval,
2289
+ id: this.timeouts[id].id,
2290
+ invokeArgs: this.timeouts[id].invokeArgs
2291
+ };
2292
+ }
2293
+ }
2294
+ }
2295
+
2296
+ return timer || null;
2297
+ },
2298
+
2299
+ callTimer: function (timer) {
2300
+ if (typeof timer.interval == "number") {
2301
+ this.timeouts[timer.id].callAt += timer.interval;
2302
+ } else {
2303
+ delete this.timeouts[timer.id];
2304
+ }
2305
+
2306
+ try {
2307
+ if (typeof timer.func == "function") {
2308
+ timer.func.apply(null, timer.invokeArgs);
2309
+ } else {
2310
+ eval(timer.func);
2311
+ }
2312
+ } catch (e) {
2313
+ var exception = e;
2314
+ }
2315
+
2316
+ if (!this.timeouts[timer.id]) {
2317
+ if (exception) {
2318
+ throw exception;
2319
+ }
2320
+ return;
2321
+ }
2322
+
2323
+ if (exception) {
2324
+ throw exception;
2325
+ }
2326
+ },
2327
+
2328
+ reset: function reset() {
2329
+ this.timeouts = {};
2330
+ },
2331
+
2332
+ Date: (function () {
2333
+ var NativeDate = Date;
2334
+
2335
+ function ClockDate(year, month, date, hour, minute, second, ms) {
2336
+ // Defensive and verbose to avoid potential harm in passing
2337
+ // explicit undefined when user does not pass argument
2338
+ switch (arguments.length) {
2339
+ case 0:
2340
+ return new NativeDate(ClockDate.clock.now);
2341
+ case 1:
2342
+ return new NativeDate(year);
2343
+ case 2:
2344
+ return new NativeDate(year, month);
2345
+ case 3:
2346
+ return new NativeDate(year, month, date);
2347
+ case 4:
2348
+ return new NativeDate(year, month, date, hour);
2349
+ case 5:
2350
+ return new NativeDate(year, month, date, hour, minute);
2351
+ case 6:
2352
+ return new NativeDate(year, month, date, hour, minute, second);
2353
+ default:
2354
+ return new NativeDate(year, month, date, hour, minute, second, ms);
2355
+ }
2356
+ }
2357
+
2358
+ return mirrorDateProperties(ClockDate, NativeDate);
2359
+ }())
2360
+ };
2361
+
2362
+ function mirrorDateProperties(target, source) {
2363
+ if (source.now) {
2364
+ target.now = function now() {
2365
+ return target.clock.now;
2366
+ };
2367
+ } else {
2368
+ delete target.now;
2369
+ }
2370
+
2371
+ if (source.toSource) {
2372
+ target.toSource = function toSource() {
2373
+ return source.toSource();
2374
+ };
2375
+ } else {
2376
+ delete target.toSource;
2377
+ }
2378
+
2379
+ target.toString = function toString() {
2380
+ return source.toString();
2381
+ };
2382
+
2383
+ target.prototype = source.prototype;
2384
+ target.parse = source.parse;
2385
+ target.UTC = source.UTC;
2386
+ target.prototype.toUTCString = source.prototype.toUTCString;
2387
+ return target;
2388
+ }
2389
+
2390
+ var methods = ["Date", "setTimeout", "setInterval",
2391
+ "clearTimeout", "clearInterval"];
2392
+
2393
+ function restore() {
2394
+ var method;
2395
+
2396
+ for (var i = 0, l = this.methods.length; i < l; i++) {
2397
+ method = this.methods[i];
2398
+ if (global[method].hadOwnProperty) {
2399
+ global[method] = this["_" + method];
2400
+ } else {
2401
+ delete global[method];
2402
+ }
2403
+ }
2404
+
2405
+ // Prevent multiple executions which will completely remove these props
2406
+ this.methods = [];
2407
+ }
2408
+
2409
+ function stubGlobal(method, clock) {
2410
+ clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(global, method);
2411
+ clock["_" + method] = global[method];
2412
+
2413
+ if (method == "Date") {
2414
+ var date = mirrorDateProperties(clock[method], global[method]);
2415
+ global[method] = date;
2416
+ } else {
2417
+ global[method] = function () {
2418
+ return clock[method].apply(clock, arguments);
2419
+ };
2420
+
2421
+ for (var prop in clock[method]) {
2422
+ if (clock[method].hasOwnProperty(prop)) {
2423
+ global[method][prop] = clock[method][prop];
2424
+ }
2425
+ }
2426
+ }
2427
+
2428
+ global[method].clock = clock;
2429
+ }
2430
+
2431
+ sinon.useFakeTimers = function useFakeTimers(now) {
2432
+ var clock = sinon.clock.create(now);
2433
+ clock.restore = restore;
2434
+ clock.methods = Array.prototype.slice.call(arguments,
2435
+ typeof now == "number" ? 1 : 0);
2436
+
2437
+ if (clock.methods.length === 0) {
2438
+ clock.methods = methods;
2439
+ }
2440
+
2441
+ for (var i = 0, l = clock.methods.length; i < l; i++) {
2442
+ stubGlobal(clock.methods[i], clock);
2443
+ }
2444
+
2445
+ return clock;
2446
+ };
2447
+ }(typeof global != "undefined" && typeof global !== "function" ? global : this));
2448
+
2449
+ sinon.timers = {
2450
+ setTimeout: setTimeout,
2451
+ clearTimeout: clearTimeout,
2452
+ setInterval: setInterval,
2453
+ clearInterval: clearInterval,
2454
+ Date: Date
2455
+ };
2456
+
2457
+ if (typeof module == "object" && typeof require == "function") {
2458
+ module.exports = sinon;
2459
+ }
2460
+
2461
+ /*jslint eqeqeq: false, onevar: false*/
2462
+ /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
2463
+ /**
2464
+ * Minimal Event interface implementation
2465
+ *
2466
+ * Original implementation by Sven Fuchs: https://gist.github.com/995028
2467
+ * Modifications and tests by Christian Johansen.
2468
+ *
2469
+ * @author Sven Fuchs (svenfuchs@artweb-design.de)
2470
+ * @author Christian Johansen (christian@cjohansen.no)
2471
+ * @license BSD
2472
+ *
2473
+ * Copyright (c) 2011 Sven Fuchs, Christian Johansen
2474
+ */
2475
+
2476
+ if (typeof sinon == "undefined") {
2477
+ this.sinon = {};
2478
+ }
2479
+
2480
+ (function () {
2481
+ var push = [].push;
2482
+
2483
+ sinon.Event = function Event(type, bubbles, cancelable) {
2484
+ this.initEvent(type, bubbles, cancelable);
2485
+ };
2486
+
2487
+ sinon.Event.prototype = {
2488
+ initEvent: function(type, bubbles, cancelable) {
2489
+ this.type = type;
2490
+ this.bubbles = bubbles;
2491
+ this.cancelable = cancelable;
2492
+ },
2493
+
2494
+ stopPropagation: function () {},
2495
+
2496
+ preventDefault: function () {
2497
+ this.defaultPrevented = true;
2498
+ }
2499
+ };
2500
+
2501
+ sinon.EventTarget = {
2502
+ addEventListener: function addEventListener(event, listener, useCapture) {
2503
+ this.eventListeners = this.eventListeners || {};
2504
+ this.eventListeners[event] = this.eventListeners[event] || [];
2505
+ push.call(this.eventListeners[event], listener);
2506
+ },
2507
+
2508
+ removeEventListener: function removeEventListener(event, listener, useCapture) {
2509
+ var listeners = this.eventListeners && this.eventListeners[event] || [];
2510
+
2511
+ for (var i = 0, l = listeners.length; i < l; ++i) {
2512
+ if (listeners[i] == listener) {
2513
+ return listeners.splice(i, 1);
2514
+ }
2515
+ }
2516
+ },
2517
+
2518
+ dispatchEvent: function dispatchEvent(event) {
2519
+ var type = event.type;
2520
+ var listeners = this.eventListeners && this.eventListeners[type] || [];
2521
+
2522
+ for (var i = 0; i < listeners.length; i++) {
2523
+ if (typeof listeners[i] == "function") {
2524
+ listeners[i].call(this, event);
2525
+ } else {
2526
+ listeners[i].handleEvent(event);
2527
+ }
2528
+ }
2529
+
2530
+ return !!event.defaultPrevented;
2531
+ }
2532
+ };
2533
+ }());
2534
+
2535
+ /**
2536
+ * @depend ../../sinon.js
2537
+ * @depend event.js
2538
+ */
2539
+ /*jslint eqeqeq: false, onevar: false*/
2540
+ /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
2541
+ /**
2542
+ * Fake XMLHttpRequest object
2543
+ *
2544
+ * @author Christian Johansen (christian@cjohansen.no)
2545
+ * @license BSD
2546
+ *
2547
+ * Copyright (c) 2010-2011 Christian Johansen
2548
+ */
2549
+
2550
+ if (typeof sinon == "undefined") {
2551
+ this.sinon = {};
2552
+ }
2553
+ sinon.xhr = { XMLHttpRequest: this.XMLHttpRequest };
2554
+
2555
+ // wrapper for global
2556
+ (function(global) {
2557
+ var xhr = sinon.xhr;
2558
+ xhr.GlobalXMLHttpRequest = global.XMLHttpRequest;
2559
+ xhr.GlobalActiveXObject = global.ActiveXObject;
2560
+ xhr.supportsActiveX = typeof xhr.GlobalActiveXObject != "undefined";
2561
+ xhr.supportsXHR = typeof xhr.GlobalXMLHttpRequest != "undefined";
2562
+ xhr.workingXHR = xhr.supportsXHR ? xhr.GlobalXMLHttpRequest : xhr.supportsActiveX
2563
+ ? function() { return new xhr.GlobalActiveXObject("MSXML2.XMLHTTP.3.0") } : false;
2564
+
2565
+ /*jsl:ignore*/
2566
+ var unsafeHeaders = {
2567
+ "Accept-Charset": true,
2568
+ "Accept-Encoding": true,
2569
+ "Connection": true,
2570
+ "Content-Length": true,
2571
+ "Cookie": true,
2572
+ "Cookie2": true,
2573
+ "Content-Transfer-Encoding": true,
2574
+ "Date": true,
2575
+ "Expect": true,
2576
+ "Host": true,
2577
+ "Keep-Alive": true,
2578
+ "Referer": true,
2579
+ "TE": true,
2580
+ "Trailer": true,
2581
+ "Transfer-Encoding": true,
2582
+ "Upgrade": true,
2583
+ "User-Agent": true,
2584
+ "Via": true
2585
+ };
2586
+ /*jsl:end*/
2587
+
2588
+ function FakeXMLHttpRequest() {
2589
+ this.readyState = FakeXMLHttpRequest.UNSENT;
2590
+ this.requestHeaders = {};
2591
+ this.requestBody = null;
2592
+ this.status = 0;
2593
+ this.statusText = "";
2594
+
2595
+ if (typeof FakeXMLHttpRequest.onCreate == "function") {
2596
+ FakeXMLHttpRequest.onCreate(this);
2597
+ }
2598
+ }
2599
+
2600
+ function verifyState(xhr) {
2601
+ if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
2602
+ throw new Error("INVALID_STATE_ERR");
2603
+ }
2604
+
2605
+ if (xhr.sendFlag) {
2606
+ throw new Error("INVALID_STATE_ERR");
2607
+ }
2608
+ }
2609
+
2610
+ // filtering to enable a white-list version of Sinon FakeXhr,
2611
+ // where whitelisted requests are passed through to real XHR
2612
+ function each(collection, callback) {
2613
+ if (!collection) return;
2614
+ for (var i = 0, l = collection.length; i < l; i += 1) {
2615
+ callback(collection[i]);
2616
+ }
2617
+ }
2618
+ function some(collection, callback) {
2619
+ for (var index = 0; index < collection.length; index++) {
2620
+ if(callback(collection[index]) === true) return true;
2621
+ };
2622
+ return false;
2623
+ }
2624
+ // largest arity in XHR is 5 - XHR#open
2625
+ var apply = function(obj,method,args) {
2626
+ switch(args.length) {
2627
+ case 0: return obj[method]();
2628
+ case 1: return obj[method](args[0]);
2629
+ case 2: return obj[method](args[0],args[1]);
2630
+ case 3: return obj[method](args[0],args[1],args[2]);
2631
+ case 4: return obj[method](args[0],args[1],args[2],args[3]);
2632
+ case 5: return obj[method](args[0],args[1],args[2],args[3],args[4]);
2633
+ };
2634
+ };
2635
+
2636
+ FakeXMLHttpRequest.filters = [];
2637
+ FakeXMLHttpRequest.addFilter = function(fn) {
2638
+ this.filters.push(fn)
2639
+ };
2640
+ var IE6Re = /MSIE 6/;
2641
+ FakeXMLHttpRequest.defake = function(fakeXhr,xhrArgs) {
2642
+ var xhr = new sinon.xhr.workingXHR();
2643
+ each(["open","setRequestHeader","send","abort","getResponseHeader",
2644
+ "getAllResponseHeaders","addEventListener","overrideMimeType","removeEventListener"],
2645
+ function(method) {
2646
+ fakeXhr[method] = function() {
2647
+ return apply(xhr,method,arguments);
2648
+ };
2649
+ });
2650
+
2651
+ var copyAttrs = function(args) {
2652
+ each(args, function(attr) {
2653
+ try {
2654
+ fakeXhr[attr] = xhr[attr]
2655
+ } catch(e) {
2656
+ if(!IE6Re.test(navigator.userAgent)) throw e;
2657
+ }
2658
+ });
2659
+ };
2660
+
2661
+ var stateChange = function() {
2662
+ fakeXhr.readyState = xhr.readyState;
2663
+ if(xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) {
2664
+ copyAttrs(["status","statusText"]);
2665
+ }
2666
+ if(xhr.readyState >= FakeXMLHttpRequest.LOADING) {
2667
+ copyAttrs(["responseText"]);
2668
+ }
2669
+ if(xhr.readyState === FakeXMLHttpRequest.DONE) {
2670
+ copyAttrs(["responseXML"]);
2671
+ }
2672
+ if(fakeXhr.onreadystatechange) fakeXhr.onreadystatechange.call(fakeXhr);
2673
+ };
2674
+ if(xhr.addEventListener) {
2675
+ for(var event in fakeXhr.eventListeners) {
2676
+ if(fakeXhr.eventListeners.hasOwnProperty(event)) {
2677
+ each(fakeXhr.eventListeners[event],function(handler) {
2678
+ xhr.addEventListener(event, handler);
2679
+ });
2680
+ }
2681
+ }
2682
+ xhr.addEventListener("readystatechange",stateChange);
2683
+ } else {
2684
+ xhr.onreadystatechange = stateChange;
2685
+ }
2686
+ apply(xhr,"open",xhrArgs);
2687
+ };
2688
+ FakeXMLHttpRequest.useFilters = false;
2689
+
2690
+ function verifyRequestSent(xhr) {
2691
+ if (xhr.readyState == FakeXMLHttpRequest.DONE) {
2692
+ throw new Error("Request done");
2693
+ }
2694
+ }
2695
+
2696
+ function verifyHeadersReceived(xhr) {
2697
+ if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
2698
+ throw new Error("No headers received");
2699
+ }
2700
+ }
2701
+
2702
+ function verifyResponseBodyType(body) {
2703
+ if (typeof body != "string") {
2704
+ var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
2705
+ body + ", which is not a string.");
2706
+ error.name = "InvalidBodyException";
2707
+ throw error;
2708
+ }
2709
+ }
2710
+
2711
+ sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
2712
+ async: true,
2713
+
2714
+ open: function open(method, url, async, username, password) {
2715
+ this.method = method;
2716
+ this.url = url;
2717
+ this.async = typeof async == "boolean" ? async : true;
2718
+ this.username = username;
2719
+ this.password = password;
2720
+ this.responseText = null;
2721
+ this.responseXML = null;
2722
+ this.requestHeaders = {};
2723
+ this.sendFlag = false;
2724
+ if(sinon.FakeXMLHttpRequest.useFilters === true) {
2725
+ var xhrArgs = arguments;
2726
+ var defake = some(FakeXMLHttpRequest.filters,function(filter) {
2727
+ return filter.apply(this,xhrArgs)
2728
+ });
2729
+ if (defake) {
2730
+ return sinon.FakeXMLHttpRequest.defake(this,arguments);
2731
+ }
2732
+ }
2733
+ this.readyStateChange(FakeXMLHttpRequest.OPENED);
2734
+ },
2735
+
2736
+ readyStateChange: function readyStateChange(state) {
2737
+ this.readyState = state;
2738
+
2739
+ if (typeof this.onreadystatechange == "function") {
2740
+ try {
2741
+ this.onreadystatechange();
2742
+ } catch (e) {
2743
+ sinon.logError("Fake XHR onreadystatechange handler", e);
2744
+ }
2745
+ }
2746
+
2747
+ this.dispatchEvent(new sinon.Event("readystatechange"));
2748
+ },
2749
+
2750
+ setRequestHeader: function setRequestHeader(header, value) {
2751
+ verifyState(this);
2752
+
2753
+ if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
2754
+ throw new Error("Refused to set unsafe header \"" + header + "\"");
2755
+ }
2756
+
2757
+ if (this.requestHeaders[header]) {
2758
+ this.requestHeaders[header] += "," + value;
2759
+ } else {
2760
+ this.requestHeaders[header] = value;
2761
+ }
2762
+ },
2763
+
2764
+ // Helps testing
2765
+ setResponseHeaders: function setResponseHeaders(headers) {
2766
+ this.responseHeaders = {};
2767
+
2768
+ for (var header in headers) {
2769
+ if (headers.hasOwnProperty(header)) {
2770
+ this.responseHeaders[header] = headers[header];
2771
+ }
2772
+ }
2773
+
2774
+ if (this.async) {
2775
+ this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
2776
+ }
2777
+ },
2778
+
2779
+ // Currently treats ALL data as a DOMString (i.e. no Document)
2780
+ send: function send(data) {
2781
+ verifyState(this);
2782
+
2783
+ if (!/^(get|head)$/i.test(this.method)) {
2784
+ if (this.requestHeaders["Content-Type"]) {
2785
+ var value = this.requestHeaders["Content-Type"].split(";");
2786
+ this.requestHeaders["Content-Type"] = value[0] + ";charset=utf-8";
2787
+ } else {
2788
+ this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
2789
+ }
2790
+
2791
+ this.requestBody = data;
2792
+ }
2793
+
2794
+ this.errorFlag = false;
2795
+ this.sendFlag = this.async;
2796
+ this.readyStateChange(FakeXMLHttpRequest.OPENED);
2797
+
2798
+ if (typeof this.onSend == "function") {
2799
+ this.onSend(this);
2800
+ }
2801
+ },
2802
+
2803
+ abort: function abort() {
2804
+ this.aborted = true;
2805
+ this.responseText = null;
2806
+ this.errorFlag = true;
2807
+ this.requestHeaders = {};
2808
+
2809
+ if (this.readyState > sinon.FakeXMLHttpRequest.UNSENT && this.sendFlag) {
2810
+ this.readyStateChange(sinon.FakeXMLHttpRequest.DONE);
2811
+ this.sendFlag = false;
2812
+ }
2813
+
2814
+ this.readyState = sinon.FakeXMLHttpRequest.UNSENT;
2815
+ },
2816
+
2817
+ getResponseHeader: function getResponseHeader(header) {
2818
+ if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
2819
+ return null;
2820
+ }
2821
+
2822
+ if (/^Set-Cookie2?$/i.test(header)) {
2823
+ return null;
2824
+ }
2825
+
2826
+ header = header.toLowerCase();
2827
+
2828
+ for (var h in this.responseHeaders) {
2829
+ if (h.toLowerCase() == header) {
2830
+ return this.responseHeaders[h];
2831
+ }
2832
+ }
2833
+
2834
+ return null;
2835
+ },
2836
+
2837
+ getAllResponseHeaders: function getAllResponseHeaders() {
2838
+ if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
2839
+ return "";
2840
+ }
2841
+
2842
+ var headers = "";
2843
+
2844
+ for (var header in this.responseHeaders) {
2845
+ if (this.responseHeaders.hasOwnProperty(header) &&
2846
+ !/^Set-Cookie2?$/i.test(header)) {
2847
+ headers += header + ": " + this.responseHeaders[header] + "\r\n";
2848
+ }
2849
+ }
2850
+
2851
+ return headers;
2852
+ },
2853
+
2854
+ setResponseBody: function setResponseBody(body) {
2855
+ verifyRequestSent(this);
2856
+ verifyHeadersReceived(this);
2857
+ verifyResponseBodyType(body);
2858
+
2859
+ var chunkSize = this.chunkSize || 10;
2860
+ var index = 0;
2861
+ this.responseText = "";
2862
+
2863
+ do {
2864
+ if (this.async) {
2865
+ this.readyStateChange(FakeXMLHttpRequest.LOADING);
2866
+ }
2867
+
2868
+ this.responseText += body.substring(index, index + chunkSize);
2869
+ index += chunkSize;
2870
+ } while (index < body.length);
2871
+
2872
+ var type = this.getResponseHeader("Content-Type");
2873
+
2874
+ if (this.responseText &&
2875
+ (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
2876
+ try {
2877
+ this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText);
2878
+ } catch (e) {
2879
+ // Unable to parse XML - no biggie
2880
+ }
2881
+ }
2882
+
2883
+ if (this.async) {
2884
+ this.readyStateChange(FakeXMLHttpRequest.DONE);
2885
+ } else {
2886
+ this.readyState = FakeXMLHttpRequest.DONE;
2887
+ }
2888
+ },
2889
+
2890
+ respond: function respond(status, headers, body) {
2891
+ this.setResponseHeaders(headers || {});
2892
+ this.status = typeof status == "number" ? status : 200;
2893
+ this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
2894
+ this.setResponseBody(body || "");
2895
+ }
2896
+ });
2897
+
2898
+ sinon.extend(FakeXMLHttpRequest, {
2899
+ UNSENT: 0,
2900
+ OPENED: 1,
2901
+ HEADERS_RECEIVED: 2,
2902
+ LOADING: 3,
2903
+ DONE: 4
2904
+ });
2905
+
2906
+ // Borrowed from JSpec
2907
+ FakeXMLHttpRequest.parseXML = function parseXML(text) {
2908
+ var xmlDoc;
2909
+
2910
+ if (typeof DOMParser != "undefined") {
2911
+ var parser = new DOMParser();
2912
+ xmlDoc = parser.parseFromString(text, "text/xml");
2913
+ } else {
2914
+ xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
2915
+ xmlDoc.async = "false";
2916
+ xmlDoc.loadXML(text);
2917
+ }
2918
+
2919
+ return xmlDoc;
2920
+ };
2921
+
2922
+ FakeXMLHttpRequest.statusCodes = {
2923
+ 100: "Continue",
2924
+ 101: "Switching Protocols",
2925
+ 200: "OK",
2926
+ 201: "Created",
2927
+ 202: "Accepted",
2928
+ 203: "Non-Authoritative Information",
2929
+ 204: "No Content",
2930
+ 205: "Reset Content",
2931
+ 206: "Partial Content",
2932
+ 300: "Multiple Choice",
2933
+ 301: "Moved Permanently",
2934
+ 302: "Found",
2935
+ 303: "See Other",
2936
+ 304: "Not Modified",
2937
+ 305: "Use Proxy",
2938
+ 307: "Temporary Redirect",
2939
+ 400: "Bad Request",
2940
+ 401: "Unauthorized",
2941
+ 402: "Payment Required",
2942
+ 403: "Forbidden",
2943
+ 404: "Not Found",
2944
+ 405: "Method Not Allowed",
2945
+ 406: "Not Acceptable",
2946
+ 407: "Proxy Authentication Required",
2947
+ 408: "Request Timeout",
2948
+ 409: "Conflict",
2949
+ 410: "Gone",
2950
+ 411: "Length Required",
2951
+ 412: "Precondition Failed",
2952
+ 413: "Request Entity Too Large",
2953
+ 414: "Request-URI Too Long",
2954
+ 415: "Unsupported Media Type",
2955
+ 416: "Requested Range Not Satisfiable",
2956
+ 417: "Expectation Failed",
2957
+ 422: "Unprocessable Entity",
2958
+ 500: "Internal Server Error",
2959
+ 501: "Not Implemented",
2960
+ 502: "Bad Gateway",
2961
+ 503: "Service Unavailable",
2962
+ 504: "Gateway Timeout",
2963
+ 505: "HTTP Version Not Supported"
2964
+ };
2965
+
2966
+ sinon.useFakeXMLHttpRequest = function () {
2967
+ sinon.FakeXMLHttpRequest.restore = function restore(keepOnCreate) {
2968
+ if (xhr.supportsXHR) {
2969
+ global.XMLHttpRequest = xhr.GlobalXMLHttpRequest;
2970
+ }
2971
+
2972
+ if (xhr.supportsActiveX) {
2973
+ global.ActiveXObject = xhr.GlobalActiveXObject;
2974
+ }
2975
+
2976
+ delete sinon.FakeXMLHttpRequest.restore;
2977
+
2978
+ if (keepOnCreate !== true) {
2979
+ delete sinon.FakeXMLHttpRequest.onCreate;
2980
+ }
2981
+ };
2982
+ if (xhr.supportsXHR) {
2983
+ global.XMLHttpRequest = sinon.FakeXMLHttpRequest;
2984
+ }
2985
+
2986
+ if (xhr.supportsActiveX) {
2987
+ global.ActiveXObject = function ActiveXObject(objId) {
2988
+ if (objId == "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) {
2989
+
2990
+ return new sinon.FakeXMLHttpRequest();
2991
+ }
2992
+
2993
+ return new xhr.GlobalActiveXObject(objId);
2994
+ };
2995
+ }
2996
+
2997
+ return sinon.FakeXMLHttpRequest;
2998
+ };
2999
+
3000
+ sinon.FakeXMLHttpRequest = FakeXMLHttpRequest;
3001
+ })(this);
3002
+
3003
+ if (typeof module == "object" && typeof require == "function") {
3004
+ module.exports = sinon;
3005
+ }
3006
+
3007
+ /**
3008
+ * @depend fake_xml_http_request.js
3009
+ */
3010
+ /*jslint eqeqeq: false, onevar: false, regexp: false, plusplus: false*/
3011
+ /*global module, require, window*/
3012
+ /**
3013
+ * The Sinon "server" mimics a web server that receives requests from
3014
+ * sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
3015
+ * both synchronously and asynchronously. To respond synchronuously, canned
3016
+ * answers have to be provided upfront.
3017
+ *
3018
+ * @author Christian Johansen (christian@cjohansen.no)
3019
+ * @license BSD
3020
+ *
3021
+ * Copyright (c) 2010-2011 Christian Johansen
3022
+ */
3023
+
3024
+ if (typeof sinon == "undefined") {
3025
+ var sinon = {};
3026
+ }
3027
+
3028
+ sinon.fakeServer = (function () {
3029
+ var push = [].push;
3030
+ function F() {}
3031
+
3032
+ function create(proto) {
3033
+ F.prototype = proto;
3034
+ return new F();
3035
+ }
3036
+
3037
+ function responseArray(handler) {
3038
+ var response = handler;
3039
+
3040
+ if (Object.prototype.toString.call(handler) != "[object Array]") {
3041
+ response = [200, {}, handler];
3042
+ }
3043
+
3044
+ if (typeof response[2] != "string") {
3045
+ throw new TypeError("Fake server response body should be string, but was " +
3046
+ typeof response[2]);
3047
+ }
3048
+
3049
+ return response;
3050
+ }
3051
+
3052
+ var wloc = typeof window !== "undefined" ? window.location : {};
3053
+ var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
3054
+
3055
+ function matchOne(response, reqMethod, reqUrl) {
3056
+ var rmeth = response.method;
3057
+ var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase();
3058
+ var url = response.url;
3059
+ var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl));
3060
+
3061
+ return matchMethod && matchUrl;
3062
+ }
3063
+
3064
+ function match(response, request) {
3065
+ var requestMethod = this.getHTTPMethod(request);
3066
+ var requestUrl = request.url;
3067
+
3068
+ if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
3069
+ requestUrl = requestUrl.replace(rCurrLoc, "");
3070
+ }
3071
+
3072
+ if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
3073
+ if (typeof response.response == "function") {
3074
+ var ru = response.url;
3075
+ var args = [request].concat(!ru ? [] : requestUrl.match(ru).slice(1));
3076
+ return response.response.apply(response, args);
3077
+ }
3078
+
3079
+ return true;
3080
+ }
3081
+
3082
+ return false;
3083
+ }
3084
+
3085
+ function log(response, request) {
3086
+ var str;
3087
+
3088
+ str = "Request:\n" + sinon.format(request) + "\n\n";
3089
+ str += "Response:\n" + sinon.format(response) + "\n\n";
3090
+
3091
+ sinon.log(str);
3092
+ }
3093
+
3094
+ return {
3095
+ create: function () {
3096
+ var server = create(this);
3097
+ this.xhr = sinon.useFakeXMLHttpRequest();
3098
+ server.requests = [];
3099
+
3100
+ this.xhr.onCreate = function (xhrObj) {
3101
+ server.addRequest(xhrObj);
3102
+ };
3103
+
3104
+ return server;
3105
+ },
3106
+
3107
+ addRequest: function addRequest(xhrObj) {
3108
+ var server = this;
3109
+ push.call(this.requests, xhrObj);
3110
+
3111
+ xhrObj.onSend = function () {
3112
+ server.handleRequest(this);
3113
+ };
3114
+
3115
+ if (this.autoRespond && !this.responding) {
3116
+ setTimeout(function () {
3117
+ server.responding = false;
3118
+ server.respond();
3119
+ }, this.autoRespondAfter || 10);
3120
+
3121
+ this.responding = true;
3122
+ }
3123
+ },
3124
+
3125
+ getHTTPMethod: function getHTTPMethod(request) {
3126
+ if (this.fakeHTTPMethods && /post/i.test(request.method)) {
3127
+ var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
3128
+ return !!matches ? matches[1] : request.method;
3129
+ }
3130
+
3131
+ return request.method;
3132
+ },
3133
+
3134
+ handleRequest: function handleRequest(xhr) {
3135
+ if (xhr.async) {
3136
+ if (!this.queue) {
3137
+ this.queue = [];
3138
+ }
3139
+
3140
+ push.call(this.queue, xhr);
3141
+ } else {
3142
+ this.processRequest(xhr);
3143
+ }
3144
+ },
3145
+
3146
+ respondWith: function respondWith(method, url, body) {
3147
+ if (arguments.length == 1 && typeof method != "function") {
3148
+ this.response = responseArray(method);
3149
+ return;
3150
+ }
3151
+
3152
+ if (!this.responses) { this.responses = []; }
3153
+
3154
+ if (arguments.length == 1) {
3155
+ body = method;
3156
+ url = method = null;
3157
+ }
3158
+
3159
+ if (arguments.length == 2) {
3160
+ body = url;
3161
+ url = method;
3162
+ method = null;
3163
+ }
3164
+
3165
+ push.call(this.responses, {
3166
+ method: method,
3167
+ url: url,
3168
+ response: typeof body == "function" ? body : responseArray(body)
3169
+ });
3170
+ },
3171
+
3172
+ respond: function respond() {
3173
+ if (arguments.length > 0) this.respondWith.apply(this, arguments);
3174
+ var queue = this.queue || [];
3175
+ var request;
3176
+
3177
+ while(request = queue.shift()) {
3178
+ this.processRequest(request);
3179
+ }
3180
+ },
3181
+
3182
+ processRequest: function processRequest(request) {
3183
+ try {
3184
+ if (request.aborted) {
3185
+ return;
3186
+ }
3187
+
3188
+ var response = this.response || [404, {}, ""];
3189
+
3190
+ if (this.responses) {
3191
+ for (var i = 0, l = this.responses.length; i < l; i++) {
3192
+ if (match.call(this, this.responses[i], request)) {
3193
+ response = this.responses[i].response;
3194
+ break;
3195
+ }
3196
+ }
3197
+ }
3198
+
3199
+ if (request.readyState != 4) {
3200
+ log(response, request);
3201
+
3202
+ request.respond(response[0], response[1], response[2]);
3203
+ }
3204
+ } catch (e) {
3205
+ sinon.logError("Fake server request processing", e);
3206
+ }
3207
+ },
3208
+
3209
+ restore: function restore() {
3210
+ return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
3211
+ }
3212
+ };
3213
+ }());
3214
+
3215
+ if (typeof module == "object" && typeof require == "function") {
3216
+ module.exports = sinon;
3217
+ }
3218
+
3219
+ /**
3220
+ * @depend fake_server.js
3221
+ * @depend fake_timers.js
3222
+ */
3223
+ /*jslint browser: true, eqeqeq: false, onevar: false*/
3224
+ /*global sinon*/
3225
+ /**
3226
+ * Add-on for sinon.fakeServer that automatically handles a fake timer along with
3227
+ * the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
3228
+ * 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
3229
+ * it polls the object for completion with setInterval. Dispite the direct
3230
+ * motivation, there is nothing jQuery-specific in this file, so it can be used
3231
+ * in any environment where the ajax implementation depends on setInterval or
3232
+ * setTimeout.
3233
+ *
3234
+ * @author Christian Johansen (christian@cjohansen.no)
3235
+ * @license BSD
3236
+ *
3237
+ * Copyright (c) 2010-2011 Christian Johansen
3238
+ */
3239
+
3240
+ (function () {
3241
+ function Server() {}
3242
+ Server.prototype = sinon.fakeServer;
3243
+
3244
+ sinon.fakeServerWithClock = new Server();
3245
+
3246
+ sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
3247
+ if (xhr.async) {
3248
+ if (typeof setTimeout.clock == "object") {
3249
+ this.clock = setTimeout.clock;
3250
+ } else {
3251
+ this.clock = sinon.useFakeTimers();
3252
+ this.resetClock = true;
3253
+ }
3254
+
3255
+ if (!this.longestTimeout) {
3256
+ var clockSetTimeout = this.clock.setTimeout;
3257
+ var clockSetInterval = this.clock.setInterval;
3258
+ var server = this;
3259
+
3260
+ this.clock.setTimeout = function (fn, timeout) {
3261
+ server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
3262
+
3263
+ return clockSetTimeout.apply(this, arguments);
3264
+ };
3265
+
3266
+ this.clock.setInterval = function (fn, timeout) {
3267
+ server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
3268
+
3269
+ return clockSetInterval.apply(this, arguments);
3270
+ };
3271
+ }
3272
+ }
3273
+
3274
+ return sinon.fakeServer.addRequest.call(this, xhr);
3275
+ };
3276
+
3277
+ sinon.fakeServerWithClock.respond = function respond() {
3278
+ var returnVal = sinon.fakeServer.respond.apply(this, arguments);
3279
+
3280
+ if (this.clock) {
3281
+ this.clock.tick(this.longestTimeout || 0);
3282
+ this.longestTimeout = 0;
3283
+
3284
+ if (this.resetClock) {
3285
+ this.clock.restore();
3286
+ this.resetClock = false;
3287
+ }
3288
+ }
3289
+
3290
+ return returnVal;
3291
+ };
3292
+
3293
+ sinon.fakeServerWithClock.restore = function restore() {
3294
+ if (this.clock) {
3295
+ this.clock.restore();
3296
+ }
3297
+
3298
+ return sinon.fakeServer.restore.apply(this, arguments);
3299
+ };
3300
+ }());
3301
+
3302
+ /**
3303
+ * @depend ../sinon.js
3304
+ * @depend collection.js
3305
+ * @depend util/fake_timers.js
3306
+ * @depend util/fake_server_with_clock.js
3307
+ */
3308
+ /*jslint eqeqeq: false, onevar: false, plusplus: false*/
3309
+ /*global require, module*/
3310
+ /**
3311
+ * Manages fake collections as well as fake utilities such as Sinon's
3312
+ * timers and fake XHR implementation in one convenient object.
3313
+ *
3314
+ * @author Christian Johansen (christian@cjohansen.no)
3315
+ * @license BSD
3316
+ *
3317
+ * Copyright (c) 2010-2011 Christian Johansen
3318
+ */
3319
+
3320
+ if (typeof module == "object" && typeof require == "function") {
3321
+ var sinon = require("../sinon");
3322
+ sinon.extend(sinon, require("./util/fake_timers"));
3323
+ }
3324
+
3325
+ (function () {
3326
+ var push = [].push;
3327
+
3328
+ function exposeValue(sandbox, config, key, value) {
3329
+ if (!value) {
3330
+ return;
3331
+ }
3332
+
3333
+ if (config.injectInto) {
3334
+ config.injectInto[key] = value;
3335
+ } else {
3336
+ push.call(sandbox.args, value);
3337
+ }
3338
+ }
3339
+
3340
+ function prepareSandboxFromConfig(config) {
3341
+ var sandbox = sinon.create(sinon.sandbox);
3342
+
3343
+ if (config.useFakeServer) {
3344
+ if (typeof config.useFakeServer == "object") {
3345
+ sandbox.serverPrototype = config.useFakeServer;
3346
+ }
3347
+
3348
+ sandbox.useFakeServer();
3349
+ }
3350
+
3351
+ if (config.useFakeTimers) {
3352
+ if (typeof config.useFakeTimers == "object") {
3353
+ sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers);
3354
+ } else {
3355
+ sandbox.useFakeTimers();
3356
+ }
3357
+ }
3358
+
3359
+ return sandbox;
3360
+ }
3361
+
3362
+ sinon.sandbox = sinon.extend(sinon.create(sinon.collection), {
3363
+ useFakeTimers: function useFakeTimers() {
3364
+ this.clock = sinon.useFakeTimers.apply(sinon, arguments);
3365
+
3366
+ return this.add(this.clock);
3367
+ },
3368
+
3369
+ serverPrototype: sinon.fakeServer,
3370
+
3371
+ useFakeServer: function useFakeServer() {
3372
+ var proto = this.serverPrototype || sinon.fakeServer;
3373
+
3374
+ if (!proto || !proto.create) {
3375
+ return null;
3376
+ }
3377
+
3378
+ this.server = proto.create();
3379
+ return this.add(this.server);
3380
+ },
3381
+
3382
+ inject: function (obj) {
3383
+ sinon.collection.inject.call(this, obj);
3384
+
3385
+ if (this.clock) {
3386
+ obj.clock = this.clock;
3387
+ }
3388
+
3389
+ if (this.server) {
3390
+ obj.server = this.server;
3391
+ obj.requests = this.server.requests;
3392
+ }
3393
+
3394
+ return obj;
3395
+ },
3396
+
3397
+ create: function (config) {
3398
+ if (!config) {
3399
+ return sinon.create(sinon.sandbox);
3400
+ }
3401
+
3402
+ var sandbox = prepareSandboxFromConfig(config);
3403
+ sandbox.args = sandbox.args || [];
3404
+ var prop, value, exposed = sandbox.inject({});
3405
+
3406
+ if (config.properties) {
3407
+ for (var i = 0, l = config.properties.length; i < l; i++) {
3408
+ prop = config.properties[i];
3409
+ value = exposed[prop] || prop == "sandbox" && sandbox;
3410
+ exposeValue(sandbox, config, prop, value);
3411
+ }
3412
+ } else {
3413
+ exposeValue(sandbox, config, "sandbox", value);
3414
+ }
3415
+
3416
+ return sandbox;
3417
+ }
3418
+ });
3419
+
3420
+ sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;
3421
+
3422
+ if (typeof module == "object" && typeof require == "function") {
3423
+ module.exports = sinon.sandbox;
3424
+ }
3425
+ }());
3426
+
3427
+ /**
3428
+ * @depend ../sinon.js
3429
+ * @depend stub.js
3430
+ * @depend mock.js
3431
+ * @depend sandbox.js
3432
+ */
3433
+ /*jslint eqeqeq: false, onevar: false, forin: true, plusplus: false*/
3434
+ /*global module, require, sinon*/
3435
+ /**
3436
+ * Test function, sandboxes fakes
3437
+ *
3438
+ * @author Christian Johansen (christian@cjohansen.no)
3439
+ * @license BSD
3440
+ *
3441
+ * Copyright (c) 2010-2011 Christian Johansen
3442
+ */
3443
+
3444
+ (function (sinon) {
3445
+ var commonJSModule = typeof module == "object" && typeof require == "function";
3446
+
3447
+ if (!sinon && commonJSModule) {
3448
+ sinon = require("../sinon");
3449
+ }
3450
+
3451
+ if (!sinon) {
3452
+ return;
3453
+ }
3454
+
3455
+ function test(callback) {
3456
+ var type = typeof callback;
3457
+
3458
+ if (type != "function") {
3459
+ throw new TypeError("sinon.test needs to wrap a test function, got " + type);
3460
+ }
3461
+
3462
+ return function () {
3463
+ var config = sinon.getConfig(sinon.config);
3464
+ config.injectInto = config.injectIntoThis && this || config.injectInto;
3465
+ var sandbox = sinon.sandbox.create(config);
3466
+ var exception, result;
3467
+ var args = Array.prototype.slice.call(arguments).concat(sandbox.args);
3468
+
3469
+ try {
3470
+ result = callback.apply(this, args);
3471
+ } catch (e) {
3472
+ exception = e;
3473
+ }
3474
+
3475
+ if (typeof exception !== "undefined") {
3476
+ sandbox.restore();
3477
+ throw exception;
3478
+ }
3479
+ else {
3480
+ sandbox.verifyAndRestore();
3481
+ }
3482
+
3483
+ return result;
3484
+ };
3485
+ }
3486
+
3487
+ test.config = {
3488
+ injectIntoThis: true,
3489
+ injectInto: null,
3490
+ properties: ["spy", "stub", "mock", "clock", "server", "requests"],
3491
+ useFakeTimers: true,
3492
+ useFakeServer: true
3493
+ };
3494
+
3495
+ if (commonJSModule) {
3496
+ module.exports = test;
3497
+ } else {
3498
+ sinon.test = test;
3499
+ }
3500
+ }(typeof sinon == "object" && sinon || null));
3501
+
3502
+ /**
3503
+ * @depend ../sinon.js
3504
+ * @depend test.js
3505
+ */
3506
+ /*jslint eqeqeq: false, onevar: false, eqeqeq: false*/
3507
+ /*global module, require, sinon*/
3508
+ /**
3509
+ * Test case, sandboxes all test functions
3510
+ *
3511
+ * @author Christian Johansen (christian@cjohansen.no)
3512
+ * @license BSD
3513
+ *
3514
+ * Copyright (c) 2010-2011 Christian Johansen
3515
+ */
3516
+
3517
+ (function (sinon) {
3518
+ var commonJSModule = typeof module == "object" && typeof require == "function";
3519
+
3520
+ if (!sinon && commonJSModule) {
3521
+ sinon = require("../sinon");
3522
+ }
3523
+
3524
+ if (!sinon || !Object.prototype.hasOwnProperty) {
3525
+ return;
3526
+ }
3527
+
3528
+ function createTest(property, setUp, tearDown) {
3529
+ return function () {
3530
+ if (setUp) {
3531
+ setUp.apply(this, arguments);
3532
+ }
3533
+
3534
+ var exception, result;
3535
+
3536
+ try {
3537
+ result = property.apply(this, arguments);
3538
+ } catch (e) {
3539
+ exception = e;
3540
+ }
3541
+
3542
+ if (tearDown) {
3543
+ tearDown.apply(this, arguments);
3544
+ }
3545
+
3546
+ if (exception) {
3547
+ throw exception;
3548
+ }
3549
+
3550
+ return result;
3551
+ };
3552
+ }
3553
+
3554
+ function testCase(tests, prefix) {
3555
+ /*jsl:ignore*/
3556
+ if (!tests || typeof tests != "object") {
3557
+ throw new TypeError("sinon.testCase needs an object with test functions");
3558
+ }
3559
+ /*jsl:end*/
3560
+
3561
+ prefix = prefix || "test";
3562
+ var rPrefix = new RegExp("^" + prefix);
3563
+ var methods = {}, testName, property, method;
3564
+ var setUp = tests.setUp;
3565
+ var tearDown = tests.tearDown;
3566
+
3567
+ for (testName in tests) {
3568
+ if (tests.hasOwnProperty(testName)) {
3569
+ property = tests[testName];
3570
+
3571
+ if (/^(setUp|tearDown)$/.test(testName)) {
3572
+ continue;
3573
+ }
3574
+
3575
+ if (typeof property == "function" && rPrefix.test(testName)) {
3576
+ method = property;
3577
+
3578
+ if (setUp || tearDown) {
3579
+ method = createTest(property, setUp, tearDown);
3580
+ }
3581
+
3582
+ methods[testName] = sinon.test(method);
3583
+ } else {
3584
+ methods[testName] = tests[testName];
3585
+ }
3586
+ }
3587
+ }
3588
+
3589
+ return methods;
3590
+ }
3591
+
3592
+ if (commonJSModule) {
3593
+ module.exports = testCase;
3594
+ } else {
3595
+ sinon.testCase = testCase;
3596
+ }
3597
+ }(typeof sinon == "object" && sinon || null));
3598
+
3599
+ /**
3600
+ * @depend ../sinon.js
3601
+ * @depend stub.js
3602
+ */
3603
+ /*jslint eqeqeq: false, onevar: false, nomen: false, plusplus: false*/
3604
+ /*global module, require, sinon*/
3605
+ /**
3606
+ * Assertions matching the test spy retrieval interface.
3607
+ *
3608
+ * @author Christian Johansen (christian@cjohansen.no)
3609
+ * @license BSD
3610
+ *
3611
+ * Copyright (c) 2010-2011 Christian Johansen
3612
+ */
3613
+
3614
+ (function (sinon, global) {
3615
+ var commonJSModule = typeof module == "object" && typeof require == "function";
3616
+ var slice = Array.prototype.slice;
3617
+ var assert;
3618
+
3619
+ if (!sinon && commonJSModule) {
3620
+ sinon = require("../sinon");
3621
+ }
3622
+
3623
+ if (!sinon) {
3624
+ return;
3625
+ }
3626
+
3627
+ function verifyIsStub() {
3628
+ var method;
3629
+
3630
+ for (var i = 0, l = arguments.length; i < l; ++i) {
3631
+ method = arguments[i];
3632
+
3633
+ if (!method) {
3634
+ assert.fail("fake is not a spy");
3635
+ }
3636
+
3637
+ if (typeof method != "function") {
3638
+ assert.fail(method + " is not a function");
3639
+ }
3640
+
3641
+ if (typeof method.getCall != "function") {
3642
+ assert.fail(method + " is not stubbed");
3643
+ }
3644
+ }
3645
+ }
3646
+
3647
+ function failAssertion(object, msg) {
3648
+ object = object || global;
3649
+ var failMethod = object.fail || assert.fail;
3650
+ failMethod.call(object, msg);
3651
+ }
3652
+
3653
+ function mirrorPropAsAssertion(name, method, message) {
3654
+ if (arguments.length == 2) {
3655
+ message = method;
3656
+ method = name;
3657
+ }
3658
+
3659
+ assert[name] = function (fake) {
3660
+ verifyIsStub(fake);
3661
+
3662
+ var args = slice.call(arguments, 1);
3663
+ var failed = false;
3664
+
3665
+ if (typeof method == "function") {
3666
+ failed = !method(fake);
3667
+ } else {
3668
+ failed = typeof fake[method] == "function" ?
3669
+ !fake[method].apply(fake, args) : !fake[method];
3670
+ }
3671
+
3672
+ if (failed) {
3673
+ failAssertion(this, fake.printf.apply(fake, [message].concat(args)));
3674
+ } else {
3675
+ assert.pass(name);
3676
+ }
3677
+ };
3678
+ }
3679
+
3680
+ function exposedName(prefix, prop) {
3681
+ return !prefix || /^fail/.test(prop) ? prop :
3682
+ prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
3683
+ };
3684
+
3685
+ assert = {
3686
+ failException: "AssertError",
3687
+
3688
+ fail: function fail(message) {
3689
+ var error = new Error(message);
3690
+ error.name = this.failException || assert.failException;
3691
+
3692
+ throw error;
3693
+ },
3694
+
3695
+ pass: function pass(assertion) {},
3696
+
3697
+ callOrder: function assertCallOrder() {
3698
+ verifyIsStub.apply(null, arguments);
3699
+ var expected = "", actual = "";
3700
+
3701
+ if (!sinon.calledInOrder(arguments)) {
3702
+ try {
3703
+ expected = [].join.call(arguments, ", ");
3704
+ actual = sinon.orderByFirstCall(slice.call(arguments)).join(", ");
3705
+ } catch (e) {
3706
+ // If this fails, we'll just fall back to the blank string
3707
+ }
3708
+
3709
+ failAssertion(this, "expected " + expected + " to be " +
3710
+ "called in order but were called as " + actual);
3711
+ } else {
3712
+ assert.pass("callOrder");
3713
+ }
3714
+ },
3715
+
3716
+ callCount: function assertCallCount(method, count) {
3717
+ verifyIsStub(method);
3718
+
3719
+ if (method.callCount != count) {
3720
+ var msg = "expected %n to be called " + sinon.timesInWords(count) +
3721
+ " but was called %c%C";
3722
+ failAssertion(this, method.printf(msg));
3723
+ } else {
3724
+ assert.pass("callCount");
3725
+ }
3726
+ },
3727
+
3728
+ expose: function expose(target, options) {
3729
+ if (!target) {
3730
+ throw new TypeError("target is null or undefined");
3731
+ }
3732
+
3733
+ var o = options || {};
3734
+ var prefix = typeof o.prefix == "undefined" && "assert" || o.prefix;
3735
+ var includeFail = typeof o.includeFail == "undefined" || !!o.includeFail;
3736
+
3737
+ for (var method in this) {
3738
+ if (method != "export" && (includeFail || !/^(fail)/.test(method))) {
3739
+ target[exposedName(prefix, method)] = this[method];
3740
+ }
3741
+ }
3742
+
3743
+ return target;
3744
+ }
3745
+ };
3746
+
3747
+ mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
3748
+ mirrorPropAsAssertion("notCalled", function (spy) { return !spy.called; },
3749
+ "expected %n to not have been called but was called %c%C");
3750
+ mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
3751
+ mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
3752
+ mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
3753
+ mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
3754
+ mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
3755
+ mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
3756
+ mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
3757
+ mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
3758
+ mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
3759
+ mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
3760
+ mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
3761
+ mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
3762
+ mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
3763
+ mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
3764
+ mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
3765
+ mirrorPropAsAssertion("threw", "%n did not throw exception%C");
3766
+ mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
3767
+
3768
+ if (commonJSModule) {
3769
+ module.exports = assert;
3770
+ } else {
3771
+ sinon.assert = assert;
3772
+ }
3773
+ }(typeof sinon == "object" && sinon || null, typeof window != "undefined" ? window : global));
3774
+