2 * Sinon.JS 1.15.4, 2015/06/27
4 * @author Christian Johansen (christian@cjohansen.no)
5 * @author Contributors: https://github.com/cjohansen/Sinon.JS/blob/master/AUTHORS
9 * Copyright (c) 2010-2014, Christian Johansen, christian@cjohansen.no
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without modification,
13 * are permitted provided that the following conditions are met:
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.
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.
36 (function (root
, factory
) {
38 if (typeof define
=== 'function' && define
.amd
) {
39 define('sinon', [], function () {
40 return (root
.sinon
= factory());
42 } else if (typeof exports
=== 'object') {
43 module
.exports
= factory();
45 root
.sinon
= factory();
49 var samsam
, formatio
, lolex
;
51 function define(mod
, deps
, fn
) {
52 if (mod
== "samsam") {
54 } else if (typeof deps
=== "function" && mod
.length
=== 0) {
56 } else if (typeof fn
=== "function") {
57 formatio
= fn(samsam
);
61 ((typeof define
=== "function" && define
.amd
&& function (m
) { define("samsam", m
); }) ||
62 (typeof module
=== "object" &&
63 function (m
) { module
.exports
= m(); }) || // Node
64 function (m
) { this.samsam
= m(); } // Browser globals
66 var o
= Object
.prototype;
67 var div
= typeof document
!== "undefined" && document
.createElement("div");
69 function isNaN(value
) {
70 // Unlike global isNaN, this avoids type coercion
71 // typeof check avoids IE host object issues, hat tip to
73 var val
= value
; // JsLint thinks value !== value is "weird"
74 return typeof value
=== "number" && value
!== val
;
77 function getClass(value
) {
78 // Returns the internal [[Class]] by calling Object.prototype.toString
79 // with the provided value as this. Return value is a string, naming the
80 // internal class, e.g. "Array"
81 return o
.toString
.call(value
).split(/[ \]]/)[1];
85 * @name samsam.isArguments
86 * @param Object object
88 * Returns ``true`` if ``object`` is an ``arguments`` object,
89 * ``false`` otherwise.
91 function isArguments(object
) {
92 if (getClass(object
) === 'Arguments') { return true; }
93 if (typeof object
!== "object" || typeof object
.length
!== "number" ||
94 getClass(object
) === "Array") {
97 if (typeof object
.callee
== "function") { return true; }
99 object
[object
.length
] = 6;
100 delete object
[object
.length
];
108 * @name samsam.isElement
109 * @param Object object
111 * Returns ``true`` if ``object`` is a DOM element node. Unlike
112 * Underscore.js/lodash, this function will return ``false`` if ``object``
113 * is an *element-like* object, i.e. a regular object with a ``nodeType``
114 * property that holds the value ``1``.
116 function isElement(object
) {
117 if (!object
|| object
.nodeType
!== 1 || !div
) { return false; }
119 object
.appendChild(div
);
120 object
.removeChild(div
);
129 * @param Object object
131 * Return an array of own property names.
133 function keys(object
) {
135 for (prop
in object
) {
136 if (o
.hasOwnProperty
.call(object
, prop
)) { ks
.push(prop
); }
142 * @name samsam.isDate
143 * @param Object value
145 * Returns true if the object is a ``Date``, or *date-like*. Duck typing
146 * of date objects work by checking that the object has a ``getTime``
147 * function whose return value equals the return value from the object's
150 function isDate(value
) {
151 return typeof value
.getTime
== "function" &&
152 value
.getTime() == value
.valueOf();
156 * @name samsam.isNegZero
157 * @param Object value
159 * Returns ``true`` if ``value`` is ``-0``.
161 function isNegZero(value
) {
162 return value
=== 0 && 1 / value
=== -Infinity
;
170 * Returns ``true`` if two objects are strictly equal. Compared to
171 * ``===`` there are two exceptions:
173 * - NaN is considered equal to NaN
174 * - -0 and +0 are not considered equal
176 function identical(obj1
, obj2
) {
177 if (obj1
=== obj2
|| (isNaN(obj1
) && isNaN(obj2
))) {
178 return obj1
!== 0 || isNegZero(obj1
) === isNegZero(obj2
);
184 * @name samsam.deepEqual
188 * Deep equal comparison. Two values are "deep equal" if:
190 * - They are equal, according to samsam.identical
191 * - They are both date objects representing the same time
192 * - They are both arrays containing elements that are all deepEqual
193 * - They are objects with the same set of properties, and each property
194 * in ``obj1`` is deepEqual to the corresponding property in ``obj2``
196 * Supports cyclic objects.
198 function deepEqualCyclic(obj1
, obj2
) {
200 // used for cyclic comparison
201 // contain already visited objects
204 // contain pathes (position in the object structure)
205 // of the already visited objects
206 // indexes same as in objects arrays
209 // contains combinations of already compared objects
210 // in the manner: { "$1['ref']$2['ref']": true }
214 * used to check, if the value of a property is an object
215 * (cyclic logic is only needed for objects)
216 * only needed for cyclic logic
218 function isObject(value
) {
220 if (typeof value
=== 'object' && value
!== null &&
221 !(value
instanceof Boolean
) &&
222 !(value
instanceof Date
) &&
223 !(value
instanceof Number
) &&
224 !(value
instanceof RegExp
) &&
225 !(value
instanceof String
)) {
234 * returns the index of the given object in the
235 * given objects array, -1 if not contained
236 * only needed for cyclic logic
238 function getIndex(objects
, obj
) {
241 for (i
= 0; i
< objects
.length
; i
++) {
242 if (objects
[i
] === obj
) {
250 // does the recursion for the deep equal check
251 return (function deepEqual(obj1
, obj2
, path1
, path2
) {
252 var type1
= typeof obj1
;
253 var type2
= typeof obj2
;
255 // == null also matches undefined
257 isNaN(obj1
) || isNaN(obj2
) ||
258 obj1
== null || obj2
== null ||
259 type1
!== "object" || type2
!== "object") {
261 return identical(obj1
, obj2
);
264 // Elements are only equal if identical(expected, actual)
265 if (isElement(obj1
) || isElement(obj2
)) { return false; }
267 var isDate1
= isDate(obj1
), isDate2
= isDate(obj2
);
268 if (isDate1
|| isDate2
) {
269 if (!isDate1
|| !isDate2
|| obj1
.getTime() !== obj2
.getTime()) {
274 if (obj1
instanceof RegExp
&& obj2
instanceof RegExp
) {
275 if (obj1
.toString() !== obj2
.toString()) { return false; }
278 var class1
= getClass(obj1
);
279 var class2
= getClass(obj2
);
280 var keys1
= keys(obj1
);
281 var keys2
= keys(obj2
);
283 if (isArguments(obj1
) || isArguments(obj2
)) {
284 if (obj1
.length
!== obj2
.length
) { return false; }
286 if (type1
!== type2
|| class1
!== class2
||
287 keys1
.length
!== keys2
.length
) {
293 // following vars are used for the cyclic logic
295 isObject1
, isObject2
,
299 for (i
= 0, l
= keys1
.length
; i
< l
; i
++) {
301 if (!o
.hasOwnProperty
.call(obj2
, key
)) {
305 // Start of the cyclic logic
310 isObject1
= isObject(value1
);
311 isObject2
= isObject(value2
);
313 // determine, if the objects were already visited
314 // (it's faster to check for isObject first, than to
315 // get -1 from getIndex for non objects)
316 index1
= isObject1
? getIndex(objects1
, value1
) : -1;
317 index2
= isObject2
? getIndex(objects2
, value2
) : -1;
319 // determine the new pathes of the objects
320 // - for non cyclic objects the current path will be extended
321 // by current property name
322 // - for cyclic objects the stored path is taken
323 newPath1
= index1
!== -1
325 : path1
+ '[' + JSON
.stringify(key
) + ']';
326 newPath2
= index2
!== -1
328 : path2
+ '[' + JSON
.stringify(key
) + ']';
330 // stop recursion if current objects are already compared
331 if (compared
[newPath1
+ newPath2
]) {
335 // remember the current objects and their pathes
336 if (index1
=== -1 && isObject1
) {
337 objects1
.push(value1
);
338 paths1
.push(newPath1
);
340 if (index2
=== -1 && isObject2
) {
341 objects2
.push(value2
);
342 paths2
.push(newPath2
);
345 // remember that the current objects are already compared
346 if (isObject1
&& isObject2
) {
347 compared
[newPath1
+ newPath2
] = true;
350 // End of cyclic logic
352 // neither value1 nor value2 is a cycle
353 // continue with next level
354 if (!deepEqual(value1
, value2
, newPath1
, newPath2
)) {
361 }(obj1
, obj2
, '$1', '$2'));
366 function arrayContains(array
, subset
) {
367 if (subset
.length
=== 0) { return true; }
369 for (i
= 0, l
= array
.length
; i
< l
; ++i
) {
370 if (match(array
[i
], subset
[0])) {
371 for (j
= 0, k
= subset
.length
; j
< k
; ++j
) {
372 if (!match(array
[i
+ j
], subset
[j
])) { return false; }
382 * @param Object object
383 * @param Object matcher
385 * Compare arbitrary value ``object`` with matcher.
387 match
= function match(object
, matcher
) {
388 if (matcher
&& typeof matcher
.test
=== "function") {
389 return matcher
.test(object
);
392 if (typeof matcher
=== "function") {
393 return matcher(object
) === true;
396 if (typeof matcher
=== "string") {
397 matcher
= matcher
.toLowerCase();
398 var notNull
= typeof object
=== "string" || !!object
;
400 (String(object
)).toLowerCase().indexOf(matcher
) >= 0;
403 if (typeof matcher
=== "number") {
404 return matcher
=== object
;
407 if (typeof matcher
=== "boolean") {
408 return matcher
=== object
;
411 if (typeof(matcher
) === "undefined") {
412 return typeof(object
) === "undefined";
415 if (matcher
=== null) {
416 return object
=== null;
419 if (getClass(object
) === "Array" && getClass(matcher
) === "Array") {
420 return arrayContains(object
, matcher
);
423 if (matcher
&& typeof matcher
=== "object") {
424 if (matcher
=== object
) {
428 for (prop
in matcher
) {
429 var value
= object
[prop
];
430 if (typeof value
=== "undefined" &&
431 typeof object
.getAttribute
=== "function") {
432 value
= object
.getAttribute(prop
);
434 if (matcher
[prop
] === null || typeof matcher
[prop
] === 'undefined') {
435 if (value
!== matcher
[prop
]) {
438 } else if (typeof value
=== "undefined" || !match(value
, matcher
[prop
])) {
445 throw new Error("Matcher was not a string, a number, a " +
446 "function, a boolean or an object");
450 isArguments
: isArguments
,
451 isElement
: isElement
,
453 isNegZero
: isNegZero
,
454 identical
: identical
,
455 deepEqual
: deepEqualCyclic
,
460 ((typeof define
=== "function" && define
.amd
&& function (m
) {
461 define("formatio", ["samsam"], m
);
462 }) || (typeof module
=== "object" && function (m
) {
463 module
.exports
= m(require("samsam"));
464 }) || function (m
) { this.formatio
= m(this.samsam
); }
465 )(function (samsam
) {
468 excludeConstructors
: ["Object", /^.$/],
470 limitChildrenCount
: 0
473 var hasOwn
= Object
.prototype.hasOwnProperty
;
475 var specialObjects
= [];
476 if (typeof global
!== "undefined") {
477 specialObjects
.push({ object
: global
, value
: "[object global]" });
479 if (typeof document
!== "undefined") {
480 specialObjects
.push({
482 value
: "[object HTMLDocument]"
485 if (typeof window
!== "undefined") {
486 specialObjects
.push({ object
: window
, value
: "[object Window]" });
489 function functionName(func
) {
490 if (!func
) { return ""; }
491 if (func
.displayName
) { return func
.displayName
; }
492 if (func
.name
) { return func
.name
; }
493 var matches
= func
.toString().match(/function\s+([^\(]+)/m);
494 return (matches
&& matches
[1]) || "";
497 function constructorName(f
, object
) {
498 var name
= functionName(object
&& object
.constructor);
499 var excludes
= f
.excludeConstructors
||
500 formatio
.excludeConstructors
|| [];
503 for (i
= 0, l
= excludes
.length
; i
< l
; ++i
) {
504 if (typeof excludes
[i
] === "string" && excludes
[i
] === name
) {
506 } else if (excludes
[i
].test
&& excludes
[i
].test(name
)) {
514 function isCircular(object
, objects
) {
515 if (typeof object
!== "object") { return false; }
517 for (i
= 0, l
= objects
.length
; i
< l
; ++i
) {
518 if (objects
[i
] === object
) { return true; }
523 function ascii(f
, object
, processed
, indent
) {
524 if (typeof object
=== "string") {
525 var qs
= f
.quoteStrings
;
526 var quote
= typeof qs
!== "boolean" || qs
;
527 return processed
|| quote
? '"' + object
+ '"' : object
;
530 if (typeof object
=== "function" && !(object
instanceof RegExp
)) {
531 return ascii
.func(object
);
534 processed
= processed
|| [];
536 if (isCircular(object
, processed
)) { return "[Circular]"; }
538 if (Object
.prototype.toString
.call(object
) === "[object Array]") {
539 return ascii
.array
.call(f
, object
, processed
);
542 if (!object
) { return String((1/object
) === -Infinity
? "-0" : object
); }
543 if (samsam
.isElement(object
)) { return ascii
.element(object
); }
545 if (typeof object
.toString
=== "function" &&
546 object
.toString
!== Object
.prototype.toString
) {
547 return object
.toString();
551 for (i
= 0, l
= specialObjects
.length
; i
< l
; i
++) {
552 if (object
=== specialObjects
[i
].object
) {
553 return specialObjects
[i
].value
;
557 return ascii
.object
.call(f
, object
, processed
, indent
);
560 ascii
.func = function (func
) {
561 return "function " + functionName(func
) + "() {}";
564 ascii
.array = function (array
, processed
) {
565 processed
= processed
|| [];
566 processed
.push(array
);
569 l
= (this.limitChildrenCount
> 0) ?
570 Math
.min(this.limitChildrenCount
, array
.length
) : array
.length
;
572 for (i
= 0; i
< l
; ++i
) {
573 pieces
.push(ascii(this, array
[i
], processed
));
577 pieces
.push("[... " + (array
.length
- l
) + " more elements]");
579 return "[" + pieces
.join(", ") + "]";
582 ascii
.object = function (object
, processed
, indent
) {
583 processed
= processed
|| [];
584 processed
.push(object
);
585 indent
= indent
|| 0;
586 var pieces
= [], properties
= samsam
.keys(object
).sort();
588 var prop
, str
, obj
, i
, k
, l
;
589 l
= (this.limitChildrenCount
> 0) ?
590 Math
.min(this.limitChildrenCount
, properties
.length
) : properties
.length
;
592 for (i
= 0; i
< l
; ++i
) {
593 prop
= properties
[i
];
596 if (isCircular(obj
, processed
)) {
599 str
= ascii(this, obj
, processed
, indent
+ 2);
602 str
= (/\s/.test(prop
) ? '"' + prop
+ '"' : prop
) + ": " + str
;
603 length
+= str
.length
;
607 var cons
= constructorName(this, object
);
608 var prefix
= cons
? "[" + cons
+ "] " : "";
610 for (i
= 0, k
= indent
; i
< k
; ++i
) { is
+= " "; }
612 if(l
< properties
.length
)
613 pieces
.push("[... " + (properties
.length
- l
) + " more elements]");
615 if (length
+ indent
> 80) {
616 return prefix
+ "{\n " + is
+ pieces
.join(",\n " + is
) + "\n" +
619 return prefix
+ "{ " + pieces
.join(", ") + " }";
622 ascii
.element = function (element
) {
623 var tagName
= element
.tagName
.toLowerCase();
624 var attrs
= element
.attributes
, attr
, pairs
= [], attrName
, i
, l
, val
;
626 for (i
= 0, l
= attrs
.length
; i
< l
; ++i
) {
627 attr
= attrs
.item(i
);
628 attrName
= attr
.nodeName
.toLowerCase().replace("html:", "");
629 val
= attr
.nodeValue
;
630 if (attrName
!== "contenteditable" || val
!== "inherit") {
631 if (!!val
) { pairs
.push(attrName
+ "=\"" + val
+ "\""); }
635 var formatted
= "<" + tagName
+ (pairs
.length
> 0 ? " " : "");
636 var content
= element
.innerHTML
;
638 if (content
.length
> 20) {
639 content
= content
.substr(0, 20) + "[...]";
642 var res
= formatted
+ pairs
.join(" ") + ">" + content
+
643 "</" + tagName
+ ">";
645 return res
.replace(/ contentEditable
="inherit"/, "");
648 function Formatio(options
) {
649 for (var opt
in options
) {
650 this[opt
] = options
[opt
];
654 Formatio
.prototype = {
655 functionName
: functionName
,
657 configure: function (options
) {
658 return new Formatio(options
);
661 constructorName: function (object
) {
662 return constructorName(this, object
);
665 ascii: function (object
, processed
, indent
) {
666 return ascii(this, object
, processed
, indent
);
670 return Formatio
.prototype;
672 !function(e
){if("object"==typeof exports
&&"undefined"!=typeof module
)module
.exports
=e();else if("function"==typeof define
&&define
.amd
)define([],e
);else{var f
;"undefined"!=typeof window
?f
=window
:"undefined"!=typeof global
?f
=global
:"undefined"!=typeof self
&&(f
=self
),f
.lolex
=e()}}(function(){var define
,module
,exports
;return (function e(t
,n
,r
){function s(o
,u
){if(!n
[o
]){if(!t
[o
]){var a
=typeof require
=="function"&&require
;if(!u
&&a
)return a(o
,!0);if(i
)return i(o
,!0);var f
=new Error("Cannot find module '"+o
+"'");throw f
.code
="MODULE_NOT_FOUND",f
}var l
=n
[o
]={exports
:{}};t
[o
][0].call(l
.exports
,function(e
){var n
=t
[o
][1][e
];return s(n
?n
:e
)},l
,l
.exports
,e
,t
,n
,r
)}return n
[o
].exports
}var i
=typeof require
=="function"&&require
;for(var o
=0;o
<r
.length
;o
++)s(r
[o
]);return s
})({1:[function(require
,module
,exports
){
674 /*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/
677 * @author Christian Johansen (christian@cjohansen.no) and contributors
680 * Copyright (c) 2010-2014 Christian Johansen
683 // node expects setTimeout/setInterval to return a fn object w/ .ref()/.unref()
684 // browsers, a number.
685 // see https://github.com/cjohansen/Sinon.JS/pull/436
686 var timeoutResult
= setTimeout(function() {}, 0);
687 var addTimerReturnsObject
= typeof timeoutResult
=== "object";
688 clearTimeout(timeoutResult
);
690 var NativeDate
= Date
;
694 * Parse strings like "01:10:00" (meaning 1 hour, 10 minutes, 0 seconds) into
695 * number of milliseconds. This is used to support human-readable strings passed
698 function parseTime(str
) {
703 var strings
= str
.split(":");
704 var l
= strings
.length
, i
= l
;
707 if (l
> 3 || !/^(\d\d:){0,2}\d\d?$/.test(str
)) {
708 throw new Error("tick only understands numbers and 'h:m:s'");
712 parsed
= parseInt(strings
[i
], 10);
715 throw new Error("Invalid time " + str
);
718 ms
+= parsed
* Math
.pow(60, (l
- i
- 1));
725 * Used to grok the `now` parameter to createClock.
727 function getEpoch(epoch
) {
728 if (!epoch
) { return 0; }
729 if (typeof epoch
.getTime
=== "function") { return epoch
.getTime(); }
730 if (typeof epoch
=== "number") { return epoch
; }
731 throw new TypeError("now should be milliseconds since UNIX epoch");
734 function inRange(from, to
, timer
) {
735 return timer
&& timer
.callAt
>= from && timer
.callAt
<= to
;
738 function mirrorDateProperties(target
, source
) {
740 target
.now
= function now() {
741 return target
.clock
.now
;
747 if (source
.toSource
) {
748 target
.toSource
= function toSource() {
749 return source
.toSource();
752 delete target
.toSource
;
755 target
.toString
= function toString() {
756 return source
.toString();
759 target
.prototype = source
.prototype;
760 target
.parse
= source
.parse
;
761 target
.UTC
= source
.UTC
;
762 target
.prototype.toUTCString
= source
.prototype.toUTCString
;
764 for (var prop
in source
) {
765 if (source
.hasOwnProperty(prop
)) {
766 target
[prop
] = source
[prop
];
773 function createDate() {
774 function ClockDate(year
, month
, date
, hour
, minute
, second
, ms
) {
775 // Defensive and verbose to avoid potential harm in passing
776 // explicit undefined when user does not pass argument
777 switch (arguments
.length
) {
779 return new NativeDate(ClockDate
.clock
.now
);
781 return new NativeDate(year
);
783 return new NativeDate(year
, month
);
785 return new NativeDate(year
, month
, date
);
787 return new NativeDate(year
, month
, date
, hour
);
789 return new NativeDate(year
, month
, date
, hour
, minute
);
791 return new NativeDate(year
, month
, date
, hour
, minute
, second
);
793 return new NativeDate(year
, month
, date
, hour
, minute
, second
, ms
);
797 return mirrorDateProperties(ClockDate
, NativeDate
);
800 function addTimer(clock
, timer
) {
801 if (typeof timer
.func
=== "undefined") {
802 throw new Error("Callback must be provided to timer calls");
810 timer
.createdAt
= clock
.now
;
811 timer
.callAt
= clock
.now
+ (timer
.delay
|| 0);
813 clock
.timers
[timer
.id
] = timer
;
815 if (addTimerReturnsObject
) {
827 function firstTimerInRange(clock
, from, to
) {
828 var timers
= clock
.timers
, timer
= null;
830 for (var id
in timers
) {
831 if (!inRange(from, to
, timers
[id
])) {
835 if (!timer
|| ~compareTimers(timer
, timers
[id
])) {
843 function compareTimers(a
, b
) {
844 // Sort first by absolute timing
845 if (a
.callAt
< b
.callAt
) {
848 if (a
.callAt
> b
.callAt
) {
852 // Sort next by immediate, immediate timers take precedence
853 if (a
.immediate
&& !b
.immediate
) {
856 if (!a
.immediate
&& b
.immediate
) {
860 // Sort next by creation time, earlier-created timers take precedence
861 if (a
.createdAt
< b
.createdAt
) {
864 if (a
.createdAt
> b
.createdAt
) {
868 // Sort next by id, lower-id timers take precedence
876 // As timer ids are unique, no fallback `0` is necessary
879 function callTimer(clock
, timer
) {
880 if (typeof timer
.interval
== "number") {
881 clock
.timers
[timer
.id
].callAt
+= timer
.interval
;
883 delete clock
.timers
[timer
.id
];
887 if (typeof timer
.func
== "function") {
888 timer
.func
.apply(null, timer
.args
);
896 if (!clock
.timers
[timer
.id
]) {
908 function uninstall(clock
, target
) {
911 for (var i
= 0, l
= clock
.methods
.length
; i
< l
; i
++) {
912 method
= clock
.methods
[i
];
914 if (target
[method
].hadOwnProperty
) {
915 target
[method
] = clock
["_" + method
];
918 delete target
[method
];
923 // Prevent multiple executions which will completely remove these props
927 function hijackMethod(target
, method
, clock
) {
928 clock
[method
].hadOwnProperty
= Object
.prototype.hasOwnProperty
.call(target
, method
);
929 clock
["_" + method
] = target
[method
];
931 if (method
== "Date") {
932 var date
= mirrorDateProperties(clock
[method
], target
[method
]);
933 target
[method
] = date
;
935 target
[method
] = function () {
936 return clock
[method
].apply(clock
, arguments
);
939 for (var prop
in clock
[method
]) {
940 if (clock
[method
].hasOwnProperty(prop
)) {
941 target
[method
][prop
] = clock
[method
][prop
];
946 target
[method
].clock
= clock
;
950 setTimeout
: setTimeout
,
951 clearTimeout
: clearTimeout
,
952 setImmediate
: (typeof setImmediate
!== "undefined" ? setImmediate
: undefined),
953 clearImmediate
: (typeof clearImmediate
!== "undefined" ? clearImmediate
: undefined),
954 setInterval
: setInterval
,
955 clearInterval
: clearInterval
,
959 var keys
= Object
.keys
|| function (obj
) {
961 for (var key
in obj
) {
967 exports
.timers
= timers
;
969 var createClock
= exports
.createClock = function (now
) {
976 clock
.Date
.clock
= clock
;
978 clock
.setTimeout
= function setTimeout(func
, timeout
) {
979 return addTimer(clock
, {
981 args
: Array
.prototype.slice
.call(arguments
, 2),
986 clock
.clearTimeout
= function clearTimeout(timerId
) {
988 // null appears to be allowed in most browsers, and appears to be
989 // relied upon by some libraries, like Bootstrap carousel
995 // in Node, timerId is an object with .ref()/.unref(), and
996 // its .id field is the actual timer id.
997 if (typeof timerId
=== "object") {
1000 if (timerId
in clock
.timers
) {
1001 delete clock
.timers
[timerId
];
1005 clock
.setInterval
= function setInterval(func
, timeout
) {
1006 return addTimer(clock
, {
1008 args
: Array
.prototype.slice
.call(arguments
, 2),
1014 clock
.clearInterval
= function clearInterval(timerId
) {
1015 clock
.clearTimeout(timerId
);
1018 clock
.setImmediate
= function setImmediate(func
) {
1019 return addTimer(clock
, {
1021 args
: Array
.prototype.slice
.call(arguments
, 1),
1026 clock
.clearImmediate
= function clearImmediate(timerId
) {
1027 clock
.clearTimeout(timerId
);
1030 clock
.tick
= function tick(ms
) {
1031 ms
= typeof ms
== "number" ? ms
: parseTime(ms
);
1032 var tickFrom
= clock
.now
, tickTo
= clock
.now
+ ms
, previous
= clock
.now
;
1033 var timer
= firstTimerInRange(clock
, tickFrom
, tickTo
);
1036 while (timer
&& tickFrom
<= tickTo
) {
1037 if (clock
.timers
[timer
.id
]) {
1038 tickFrom
= clock
.now
= timer
.callAt
;
1040 callTimer(clock
, timer
);
1042 firstException
= firstException
|| e
;
1046 timer
= firstTimerInRange(clock
, previous
, tickTo
);
1047 previous
= tickFrom
;
1052 if (firstException
) {
1053 throw firstException
;
1059 clock
.reset
= function reset() {
1066 exports
.install
= function install(target
, now
, toFake
) {
1067 if (typeof target
=== "number") {
1077 var clock
= createClock(now
);
1079 clock
.uninstall = function () {
1080 uninstall(clock
, target
);
1083 clock
.methods
= toFake
|| [];
1085 if (clock
.methods
.length
=== 0) {
1086 clock
.methods
= keys(timers
);
1089 for (var i
= 0, l
= clock
.methods
.length
; i
< l
; i
++) {
1090 hijackMethod(target
, clock
.methods
[i
], clock
);
1096 }).call(this,typeof global
!== "undefined" ? global
: typeof self
!== "undefined" ? self
: typeof window
!== "undefined" ? window
: {})
1102 * Sinon core utilities. For internal use only.
1104 * @author Christian Johansen (christian@cjohansen.no)
1107 * Copyright (c) 2010-2013 Christian Johansen
1110 var sinon
= (function () {
1114 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
=== "function";
1115 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
1117 function loadDependencies(require
, exports
, module
) {
1118 sinon
= module
.exports
= require("./sinon/util/core");
1119 require("./sinon/extend");
1120 require("./sinon/typeOf");
1121 require("./sinon/times_in_words");
1122 require("./sinon/spy");
1123 require("./sinon/call");
1124 require("./sinon/behavior");
1125 require("./sinon/stub");
1126 require("./sinon/mock");
1127 require("./sinon/collection");
1128 require("./sinon/assert");
1129 require("./sinon/sandbox");
1130 require("./sinon/test");
1131 require("./sinon/test_case");
1132 require("./sinon/match");
1133 require("./sinon/format");
1134 require("./sinon/log_error");
1138 define(loadDependencies
);
1139 } else if (isNode
) {
1140 loadDependencies(require
, module
.exports
, module
);
1141 sinon
= module
.exports
;
1150 * @depend ../../sinon.js
1153 * Sinon core utilities. For internal use only.
1155 * @author Christian Johansen (christian@cjohansen.no)
1158 * Copyright (c) 2010-2013 Christian Johansen
1162 var div
= typeof document
!= "undefined" && document
.createElement("div");
1163 var hasOwn
= Object
.prototype.hasOwnProperty
;
1165 function isDOMNode(obj
) {
1166 var success
= false;
1169 obj
.appendChild(div
);
1170 success
= div
.parentNode
== obj
;
1175 obj
.removeChild(div
);
1177 // Remove failed, not much we can do about that
1184 function isElement(obj
) {
1185 return div
&& obj
&& obj
.nodeType
=== 1 && isDOMNode(obj
);
1188 function isFunction(obj
) {
1189 return typeof obj
=== "function" || !!(obj
&& obj
.constructor && obj
.call
&& obj
.apply
);
1192 function isReallyNaN(val
) {
1193 return typeof val
=== "number" && isNaN(val
);
1196 function mirrorProperties(target
, source
) {
1197 for (var prop
in source
) {
1198 if (!hasOwn
.call(target
, prop
)) {
1199 target
[prop
] = source
[prop
];
1204 function isRestorable(obj
) {
1205 return typeof obj
=== "function" && typeof obj
.restore
=== "function" && obj
.restore
.sinon
;
1208 // Cheap way to detect if we have ES5 support.
1209 var hasES5Support
= "keys" in Object
;
1211 function makeApi(sinon
) {
1212 sinon
.wrapMethod
= function wrapMethod(object
, property
, method
) {
1214 throw new TypeError("Should wrap property of object");
1217 if (typeof method
!= "function" && typeof method
!= "object") {
1218 throw new TypeError("Method wrapper should be a function or a property descriptor");
1221 function checkWrappedMethod(wrappedMethod
) {
1222 if (!isFunction(wrappedMethod
)) {
1223 error
= new TypeError("Attempted to wrap " + (typeof wrappedMethod
) + " property " +
1224 property
+ " as function");
1225 } else if (wrappedMethod
.restore
&& wrappedMethod
.restore
.sinon
) {
1226 error
= new TypeError("Attempted to wrap " + property
+ " which is already wrapped");
1227 } else if (wrappedMethod
.calledBefore
) {
1228 var verb
= !!wrappedMethod
.returns
? "stubbed" : "spied on";
1229 error
= new TypeError("Attempted to wrap " + property
+ " which is already " + verb
);
1233 if (wrappedMethod
&& wrappedMethod
.stackTrace
) {
1234 error
.stack
+= "\n--------------\n" + wrappedMethod
.stackTrace
;
1240 var error
, wrappedMethod
;
1242 // IE 8 does not support hasOwnProperty on the window object and Firefox has a problem
1243 // when using hasOwn.call on objects from other frames.
1244 var owned
= object
.hasOwnProperty
? object
.hasOwnProperty(property
) : hasOwn
.call(object
, property
);
1246 if (hasES5Support
) {
1247 var methodDesc
= (typeof method
== "function") ? {value
: method
} : method
,
1248 wrappedMethodDesc
= sinon
.getPropertyDescriptor(object
, property
),
1251 if (!wrappedMethodDesc
) {
1252 error
= new TypeError("Attempted to wrap " + (typeof wrappedMethod
) + " property " +
1253 property
+ " as function");
1254 } else if (wrappedMethodDesc
.restore
&& wrappedMethodDesc
.restore
.sinon
) {
1255 error
= new TypeError("Attempted to wrap " + property
+ " which is already wrapped");
1258 if (wrappedMethodDesc
&& wrappedMethodDesc
.stackTrace
) {
1259 error
.stack
+= "\n--------------\n" + wrappedMethodDesc
.stackTrace
;
1264 var types
= sinon
.objectKeys(methodDesc
);
1265 for (i
= 0; i
< types
.length
; i
++) {
1266 wrappedMethod
= wrappedMethodDesc
[types
[i
]];
1267 checkWrappedMethod(wrappedMethod
);
1270 mirrorProperties(methodDesc
, wrappedMethodDesc
);
1271 for (i
= 0; i
< types
.length
; i
++) {
1272 mirrorProperties(methodDesc
[types
[i
]], wrappedMethodDesc
[types
[i
]]);
1274 Object
.defineProperty(object
, property
, methodDesc
);
1276 wrappedMethod
= object
[property
];
1277 checkWrappedMethod(wrappedMethod
);
1278 object
[property
] = method
;
1279 method
.displayName
= property
;
1282 method
.displayName
= property
;
1284 // Set up a stack trace which can be used later to find what line of
1285 // code the original method was created on.
1286 method
.stackTrace
= (new Error("Stack Trace for original")).stack
;
1288 method
.restore = function () {
1289 // For prototype properties try to reset by delete first.
1290 // If this fails (ex: localStorage on mobile safari) then force a reset
1291 // via direct assignment.
1293 // In some cases `delete` may throw an error
1295 delete object
[property
];
1297 // For native code functions `delete` fails without throwing an error
1298 // on Chrome < 43, PhantomJS, etc.
1299 } else if (hasES5Support
) {
1300 Object
.defineProperty(object
, property
, wrappedMethodDesc
);
1303 // Use strict equality comparison to check failures then force a reset
1304 // via direct assignment.
1305 if (object
[property
] === method
) {
1306 object
[property
] = wrappedMethod
;
1310 method
.restore
.sinon
= true;
1312 if (!hasES5Support
) {
1313 mirrorProperties(method
, wrappedMethod
);
1319 sinon
.create
= function create(proto
) {
1320 var F = function () {};
1321 F
.prototype = proto
;
1325 sinon
.deepEqual
= function deepEqual(a
, b
) {
1326 if (sinon
.match
&& sinon
.match
.isMatcher(a
)) {
1330 if (typeof a
!= "object" || typeof b
!= "object") {
1331 if (isReallyNaN(a
) && isReallyNaN(b
)) {
1338 if (isElement(a
) || isElement(b
)) {
1346 if ((a
=== null && b
!== null) || (a
!== null && b
=== null)) {
1350 if (a
instanceof RegExp
&& b
instanceof RegExp
) {
1351 return (a
.source
=== b
.source
) && (a
.global
=== b
.global
) &&
1352 (a
.ignoreCase
=== b
.ignoreCase
) && (a
.multiline
=== b
.multiline
);
1355 var aString
= Object
.prototype.toString
.call(a
);
1356 if (aString
!= Object
.prototype.toString
.call(b
)) {
1360 if (aString
== "[object Date]") {
1361 return a
.valueOf() === b
.valueOf();
1364 var prop
, aLength
= 0, bLength
= 0;
1366 if (aString
== "[object Array]" && a
.length
!== b
.length
) {
1377 if (!deepEqual(a
[prop
], b
[prop
])) {
1386 return aLength
== bLength
;
1389 sinon
.functionName
= function functionName(func
) {
1390 var name
= func
.displayName
|| func
.name
;
1392 // Use function decomposition as a last resort to get function
1393 // name. Does not rely on function decomposition to work - if it
1394 // doesn't debugging will be slightly less informative
1395 // (i.e. toString will say 'spy' rather than 'myFunc').
1397 var matches
= func
.toString().match(/function ([^\s\(]+)/);
1398 name
= matches
&& matches
[1];
1404 sinon
.functionToString
= function toString() {
1405 if (this.getCall
&& this.callCount
) {
1406 var thisValue
, prop
, i
= this.callCount
;
1409 thisValue
= this.getCall(i
).thisValue
;
1411 for (prop
in thisValue
) {
1412 if (thisValue
[prop
] === this) {
1419 return this.displayName
|| "sinon fake";
1422 sinon
.objectKeys
= function objectKeys(obj
) {
1423 if (obj
!== Object(obj
)) {
1424 throw new TypeError("sinon.objectKeys called on a non-object");
1430 if (hasOwn
.call(obj
, key
)) {
1438 sinon
.getPropertyDescriptor
= function getPropertyDescriptor(object
, property
) {
1439 var proto
= object
, descriptor
;
1440 while (proto
&& !(descriptor
= Object
.getOwnPropertyDescriptor(proto
, property
))) {
1441 proto
= Object
.getPrototypeOf(proto
);
1446 sinon
.getConfig = function (custom
) {
1448 custom
= custom
|| {};
1449 var defaults
= sinon
.defaultConfig
;
1451 for (var prop
in defaults
) {
1452 if (defaults
.hasOwnProperty(prop
)) {
1453 config
[prop
] = custom
.hasOwnProperty(prop
) ? custom
[prop
] : defaults
[prop
];
1460 sinon
.defaultConfig
= {
1461 injectIntoThis
: true,
1463 properties
: ["spy", "stub", "mock", "clock", "server", "requests"],
1464 useFakeTimers
: true,
1468 sinon
.timesInWords
= function timesInWords(count
) {
1469 return count
== 1 && "once" ||
1470 count
== 2 && "twice" ||
1471 count
== 3 && "thrice" ||
1472 (count
|| 0) + " times";
1475 sinon
.calledInOrder = function (spies
) {
1476 for (var i
= 1, l
= spies
.length
; i
< l
; i
++) {
1477 if (!spies
[i
- 1].calledBefore(spies
[i
]) || !spies
[i
].called
) {
1485 sinon
.orderByFirstCall = function (spies
) {
1486 return spies
.sort(function (a
, b
) {
1487 // uuid, won't ever be equal
1488 var aCall
= a
.getCall(0);
1489 var bCall
= b
.getCall(0);
1490 var aId
= aCall
&& aCall
.callId
|| -1;
1491 var bId
= bCall
&& bCall
.callId
|| -1;
1493 return aId
< bId
? -1 : 1;
1497 sinon
.createStubInstance = function (constructor) {
1498 if (typeof constructor !== "function") {
1499 throw new TypeError("The constructor should be a function.");
1501 return sinon
.stub(sinon
.create(constructor.prototype));
1504 sinon
.restore = function (object
) {
1505 if (object
!== null && typeof object
=== "object") {
1506 for (var prop
in object
) {
1507 if (isRestorable(object
[prop
])) {
1508 object
[prop
].restore();
1511 } else if (isRestorable(object
)) {
1519 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
1520 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
1522 function loadDependencies(require
, exports
) {
1527 define(loadDependencies
);
1528 } else if (isNode
) {
1529 loadDependencies(require
, module
.exports
);
1530 } else if (!sinon
) {
1535 }(typeof sinon
== "object" && sinon
|| null));
1538 * @depend util/core.js
1542 function makeApi(sinon
) {
1544 // Adapted from https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
1545 var hasDontEnumBug
= (function () {
1547 constructor: function () {
1550 toString: function () {
1553 valueOf: function () {
1556 toLocaleString: function () {
1559 prototype: function () {
1562 isPrototypeOf: function () {
1565 propertyIsEnumerable: function () {
1568 hasOwnProperty: function () {
1571 length: function () {
1574 unique: function () {
1580 for (var prop
in obj
) {
1581 result
.push(obj
[prop
]());
1583 return result
.join("") !== "0123456789";
1586 /* Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will
1587 * override properties in previous sources.
1589 * target - The Object to extend
1590 * sources - Objects to copy properties from.
1592 * Returns the extended target
1594 function extend(target
/*, sources */) {
1595 var sources
= Array
.prototype.slice
.call(arguments
, 1),
1598 for (i
= 0; i
< sources
.length
; i
++) {
1599 source
= sources
[i
];
1601 for (prop
in source
) {
1602 if (source
.hasOwnProperty(prop
)) {
1603 target
[prop
] = source
[prop
];
1607 // Make sure we copy (own) toString method even when in JScript with DontEnum bug
1608 // See https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
1609 if (hasDontEnumBug
&& source
.hasOwnProperty("toString") && source
.toString
!== target
.toString
) {
1610 target
.toString
= source
.toString
;
1617 sinon
.extend
= extend
;
1618 return sinon
.extend
;
1621 function loadDependencies(require
, exports
, module
) {
1622 var sinon
= require("./util/core");
1623 module
.exports
= makeApi(sinon
);
1626 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
1627 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
1630 define(loadDependencies
);
1631 } else if (isNode
) {
1632 loadDependencies(require
, module
.exports
, module
);
1633 } else if (!sinon
) {
1638 }(typeof sinon
== "object" && sinon
|| null));
1641 * @depend util/core.js
1645 function makeApi(sinon
) {
1647 function timesInWords(count
) {
1656 return (count
|| 0) + " times";
1660 sinon
.timesInWords
= timesInWords
;
1661 return sinon
.timesInWords
;
1664 function loadDependencies(require
, exports
, module
) {
1665 var sinon
= require("./util/core");
1666 module
.exports
= makeApi(sinon
);
1669 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
1670 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
1673 define(loadDependencies
);
1674 } else if (isNode
) {
1675 loadDependencies(require
, module
.exports
, module
);
1676 } else if (!sinon
) {
1681 }(typeof sinon
== "object" && sinon
|| null));
1684 * @depend util/core.js
1689 * @author Christian Johansen (christian@cjohansen.no)
1692 * Copyright (c) 2010-2014 Christian Johansen
1695 (function (sinon
, formatio
) {
1696 function makeApi(sinon
) {
1697 function typeOf(value
) {
1698 if (value
=== null) {
1700 } else if (value
=== undefined) {
1703 var string
= Object
.prototype.toString
.call(value
);
1704 return string
.substring(8, string
.length
- 1).toLowerCase();
1707 sinon
.typeOf
= typeOf
;
1708 return sinon
.typeOf
;
1711 function loadDependencies(require
, exports
, module
) {
1712 var sinon
= require("./util/core");
1713 module
.exports
= makeApi(sinon
);
1716 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
1717 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
1720 define(loadDependencies
);
1721 } else if (isNode
) {
1722 loadDependencies(require
, module
.exports
, module
);
1723 } else if (!sinon
) {
1729 (typeof sinon
== "object" && sinon
|| null),
1730 (typeof formatio
== "object" && formatio
)
1734 * @depend util/core.js
1737 /*jslint eqeqeq: false, onevar: false, plusplus: false*/
1738 /*global module, require, sinon*/
1742 * @author Maximilian Antoni (mail@maxantoni.de)
1745 * Copyright (c) 2012 Maximilian Antoni
1749 function makeApi(sinon
) {
1750 function assertType(value
, type
, name
) {
1751 var actual
= sinon
.typeOf(value
);
1752 if (actual
!== type
) {
1753 throw new TypeError("Expected type of " + name
+ " to be " +
1754 type
+ ", but was " + actual
);
1759 toString: function () {
1760 return this.message
;
1764 function isMatcher(object
) {
1765 return matcher
.isPrototypeOf(object
);
1768 function matchObject(expectation
, actual
) {
1769 if (actual
=== null || actual
=== undefined) {
1772 for (var key
in expectation
) {
1773 if (expectation
.hasOwnProperty(key
)) {
1774 var exp
= expectation
[key
];
1775 var act
= actual
[key
];
1776 if (match
.isMatcher(exp
)) {
1777 if (!exp
.test(act
)) {
1780 } else if (sinon
.typeOf(exp
) === "object") {
1781 if (!matchObject(exp
, act
)) {
1784 } else if (!sinon
.deepEqual(exp
, act
)) {
1792 matcher
.or = function (m2
) {
1793 if (!arguments
.length
) {
1794 throw new TypeError("Matcher expected");
1795 } else if (!isMatcher(m2
)) {
1799 var or
= sinon
.create(matcher
);
1800 or
.test = function (actual
) {
1801 return m1
.test(actual
) || m2
.test(actual
);
1803 or
.message
= m1
.message
+ ".or(" + m2
.message
+ ")";
1807 matcher
.and = function (m2
) {
1808 if (!arguments
.length
) {
1809 throw new TypeError("Matcher expected");
1810 } else if (!isMatcher(m2
)) {
1814 var and
= sinon
.create(matcher
);
1815 and
.test = function (actual
) {
1816 return m1
.test(actual
) && m2
.test(actual
);
1818 and
.message
= m1
.message
+ ".and(" + m2
.message
+ ")";
1822 var match = function (expectation
, message
) {
1823 var m
= sinon
.create(matcher
);
1824 var type
= sinon
.typeOf(expectation
);
1827 if (typeof expectation
.test
=== "function") {
1828 m
.test = function (actual
) {
1829 return expectation
.test(actual
) === true;
1831 m
.message
= "match(" + sinon
.functionName(expectation
.test
) + ")";
1835 for (var key
in expectation
) {
1836 if (expectation
.hasOwnProperty(key
)) {
1837 str
.push(key
+ ": " + expectation
[key
]);
1840 m
.test = function (actual
) {
1841 return matchObject(expectation
, actual
);
1843 m
.message
= "match(" + str
.join(", ") + ")";
1846 m
.test = function (actual
) {
1847 return expectation
== actual
;
1851 m
.test = function (actual
) {
1852 if (typeof actual
!== "string") {
1855 return actual
.indexOf(expectation
) !== -1;
1857 m
.message
= "match(\"" + expectation
+ "\")";
1860 m
.test = function (actual
) {
1861 if (typeof actual
!== "string") {
1864 return expectation
.test(actual
);
1868 m
.test
= expectation
;
1870 m
.message
= message
;
1872 m
.message
= "match(" + sinon
.functionName(expectation
) + ")";
1876 m
.test = function (actual
) {
1877 return sinon
.deepEqual(expectation
, actual
);
1881 m
.message
= "match(" + expectation
+ ")";
1886 match
.isMatcher
= isMatcher
;
1888 match
.any
= match(function () {
1892 match
.defined
= match(function (actual
) {
1893 return actual
!== null && actual
!== undefined;
1896 match
.truthy
= match(function (actual
) {
1900 match
.falsy
= match(function (actual
) {
1904 match
.same = function (expectation
) {
1905 return match(function (actual
) {
1906 return expectation
=== actual
;
1907 }, "same(" + expectation
+ ")");
1910 match
.typeOf = function (type
) {
1911 assertType(type
, "string", "type");
1912 return match(function (actual
) {
1913 return sinon
.typeOf(actual
) === type
;
1914 }, "typeOf(\"" + type
+ "\")");
1917 match
.instanceOf = function (type
) {
1918 assertType(type
, "function", "type");
1919 return match(function (actual
) {
1920 return actual
instanceof type
;
1921 }, "instanceOf(" + sinon
.functionName(type
) + ")");
1924 function createPropertyMatcher(propertyTest
, messagePrefix
) {
1925 return function (property
, value
) {
1926 assertType(property
, "string", "property");
1927 var onlyProperty
= arguments
.length
=== 1;
1928 var message
= messagePrefix
+ "(\"" + property
+ "\"";
1929 if (!onlyProperty
) {
1930 message
+= ", " + value
;
1933 return match(function (actual
) {
1934 if (actual
=== undefined || actual
=== null ||
1935 !propertyTest(actual
, property
)) {
1938 return onlyProperty
|| sinon
.deepEqual(value
, actual
[property
]);
1943 match
.has
= createPropertyMatcher(function (actual
, property
) {
1944 if (typeof actual
=== "object") {
1945 return property
in actual
;
1947 return actual
[property
] !== undefined;
1950 match
.hasOwn
= createPropertyMatcher(function (actual
, property
) {
1951 return actual
.hasOwnProperty(property
);
1954 match
.bool
= match
.typeOf("boolean");
1955 match
.number
= match
.typeOf("number");
1956 match
.string
= match
.typeOf("string");
1957 match
.object
= match
.typeOf("object");
1958 match
.func
= match
.typeOf("function");
1959 match
.array
= match
.typeOf("array");
1960 match
.regexp
= match
.typeOf("regexp");
1961 match
.date
= match
.typeOf("date");
1963 sinon
.match
= match
;
1967 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
1968 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
1970 function loadDependencies(require
, exports
, module
) {
1971 var sinon
= require("./util/core");
1972 require("./typeOf");
1973 module
.exports
= makeApi(sinon
);
1977 define(loadDependencies
);
1978 } else if (isNode
) {
1979 loadDependencies(require
, module
.exports
, module
);
1980 } else if (!sinon
) {
1985 }(typeof sinon
== "object" && sinon
|| null));
1988 * @depend util/core.js
1993 * @author Christian Johansen (christian@cjohansen.no)
1996 * Copyright (c) 2010-2014 Christian Johansen
1999 (function (sinon
, formatio
) {
2000 function makeApi(sinon
) {
2001 function valueFormatter(value
) {
2005 function getFormatioFormatter() {
2006 var formatter
= formatio
.configure({
2007 quoteStrings
: false,
2008 limitChildrenCount
: 250
2012 return formatter
.ascii
.apply(formatter
, arguments
);
2018 function getNodeFormatter(value
) {
2019 function format(value
) {
2020 return typeof value
== "object" && value
.toString
=== Object
.prototype.toString
? util
.inspect(value
) : value
;
2024 var util
= require("util");
2026 /* Node, but no util module - would be very old, but better safe than sorry */
2029 return util
? format
: valueFormatter
;
2032 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function",
2037 formatio
= require("formatio");
2042 formatter
= getFormatioFormatter()
2043 } else if (isNode
) {
2044 formatter
= getNodeFormatter();
2046 formatter
= valueFormatter
;
2049 sinon
.format
= formatter
;
2050 return sinon
.format
;
2053 function loadDependencies(require
, exports
, module
) {
2054 var sinon
= require("./util/core");
2055 module
.exports
= makeApi(sinon
);
2058 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
2059 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
2062 define(loadDependencies
);
2063 } else if (isNode
) {
2064 loadDependencies(require
, module
.exports
, module
);
2065 } else if (!sinon
) {
2071 (typeof sinon
== "object" && sinon
|| null),
2072 (typeof formatio
== "object" && formatio
)
2076 * @depend util/core.js
2083 * @author Christian Johansen (christian@cjohansen.no)
2084 * @author Maximilian Antoni (mail@maxantoni.de)
2087 * Copyright (c) 2010-2013 Christian Johansen
2088 * Copyright (c) 2013 Maximilian Antoni
2092 function makeApi(sinon
) {
2093 function throwYieldError(proxy
, text
, args
) {
2094 var msg
= sinon
.functionName(proxy
) + text
;
2096 msg
+= " Received [" + slice
.call(args
).join(", ") + "]";
2098 throw new Error(msg
);
2101 var slice
= Array
.prototype.slice
;
2104 calledOn
: function calledOn(thisValue
) {
2105 if (sinon
.match
&& sinon
.match
.isMatcher(thisValue
)) {
2106 return thisValue
.test(this.thisValue
);
2108 return this.thisValue
=== thisValue
;
2111 calledWith
: function calledWith() {
2112 var l
= arguments
.length
;
2113 if (l
> this.args
.length
) {
2116 for (var i
= 0; i
< l
; i
+= 1) {
2117 if (!sinon
.deepEqual(arguments
[i
], this.args
[i
])) {
2125 calledWithMatch
: function calledWithMatch() {
2126 var l
= arguments
.length
;
2127 if (l
> this.args
.length
) {
2130 for (var i
= 0; i
< l
; i
+= 1) {
2131 var actual
= this.args
[i
];
2132 var expectation
= arguments
[i
];
2133 if (!sinon
.match
|| !sinon
.match(expectation
).test(actual
)) {
2140 calledWithExactly
: function calledWithExactly() {
2141 return arguments
.length
== this.args
.length
&&
2142 this.calledWith
.apply(this, arguments
);
2145 notCalledWith
: function notCalledWith() {
2146 return !this.calledWith
.apply(this, arguments
);
2149 notCalledWithMatch
: function notCalledWithMatch() {
2150 return !this.calledWithMatch
.apply(this, arguments
);
2153 returned
: function returned(value
) {
2154 return sinon
.deepEqual(value
, this.returnValue
);
2157 threw
: function threw(error
) {
2158 if (typeof error
=== "undefined" || !this.exception
) {
2159 return !!this.exception
;
2162 return this.exception
=== error
|| this.exception
.name
=== error
;
2165 calledWithNew
: function calledWithNew() {
2166 return this.proxy
.prototype && this.thisValue
instanceof this.proxy
;
2169 calledBefore: function (other
) {
2170 return this.callId
< other
.callId
;
2173 calledAfter: function (other
) {
2174 return this.callId
> other
.callId
;
2177 callArg: function (pos
) {
2181 callArgOn: function (pos
, thisValue
) {
2182 this.args
[pos
].apply(thisValue
);
2185 callArgWith: function (pos
) {
2186 this.callArgOnWith
.apply(this, [pos
, null].concat(slice
.call(arguments
, 1)));
2189 callArgOnWith: function (pos
, thisValue
) {
2190 var args
= slice
.call(arguments
, 2);
2191 this.args
[pos
].apply(thisValue
, args
);
2194 yield: function () {
2195 this.yieldOn
.apply(this, [null].concat(slice
.call(arguments
, 0)));
2198 yieldOn: function (thisValue
) {
2199 var args
= this.args
;
2200 for (var i
= 0, l
= args
.length
; i
< l
; ++i
) {
2201 if (typeof args
[i
] === "function") {
2202 args
[i
].apply(thisValue
, slice
.call(arguments
, 1));
2206 throwYieldError(this.proxy
, " cannot yield since no callback was passed.", args
);
2209 yieldTo: function (prop
) {
2210 this.yieldToOn
.apply(this, [prop
, null].concat(slice
.call(arguments
, 1)));
2213 yieldToOn: function (prop
, thisValue
) {
2214 var args
= this.args
;
2215 for (var i
= 0, l
= args
.length
; i
< l
; ++i
) {
2216 if (args
[i
] && typeof args
[i
][prop
] === "function") {
2217 args
[i
][prop
].apply(thisValue
, slice
.call(arguments
, 2));
2221 throwYieldError(this.proxy
, " cannot yield to '" + prop
+
2222 "' since no callback was passed.", args
);
2225 toString: function () {
2226 var callStr
= this.proxy
.toString() + "(";
2229 for (var i
= 0, l
= this.args
.length
; i
< l
; ++i
) {
2230 args
.push(sinon
.format(this.args
[i
]));
2233 callStr
= callStr
+ args
.join(", ") + ")";
2235 if (typeof this.returnValue
!= "undefined") {
2236 callStr
+= " => " + sinon
.format(this.returnValue
);
2239 if (this.exception
) {
2240 callStr
+= " !" + this.exception
.name
;
2242 if (this.exception
.message
) {
2243 callStr
+= "(" + this.exception
.message
+ ")";
2251 callProto
.invokeCallback
= callProto
.yield;
2253 function createSpyCall(spy
, thisValue
, args
, returnValue
, exception
, id
) {
2254 if (typeof id
!== "number") {
2255 throw new TypeError("Call id is not a number");
2257 var proxyCall
= sinon
.create(callProto
);
2258 proxyCall
.proxy
= spy
;
2259 proxyCall
.thisValue
= thisValue
;
2260 proxyCall
.args
= args
;
2261 proxyCall
.returnValue
= returnValue
;
2262 proxyCall
.exception
= exception
;
2263 proxyCall
.callId
= id
;
2267 createSpyCall
.toString
= callProto
.toString
; // used by mocks
2269 sinon
.spyCall
= createSpyCall
;
2270 return createSpyCall
;
2273 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
2274 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
2276 function loadDependencies(require
, exports
, module
) {
2277 var sinon
= require("./util/core");
2279 require("./format");
2280 module
.exports
= makeApi(sinon
);
2284 define(loadDependencies
);
2285 } else if (isNode
) {
2286 loadDependencies(require
, module
.exports
, module
);
2287 } else if (!sinon
) {
2292 }(typeof sinon
== "object" && sinon
|| null));
2295 * @depend times_in_words.js
2296 * @depend util/core.js
2304 * @author Christian Johansen (christian@cjohansen.no)
2307 * Copyright (c) 2010-2013 Christian Johansen
2312 function makeApi(sinon
) {
2313 var push
= Array
.prototype.push
;
2314 var slice
= Array
.prototype.slice
;
2317 function spy(object
, property
, types
) {
2318 if (!property
&& typeof object
== "function") {
2319 return spy
.create(object
);
2322 if (!object
&& !property
) {
2323 return spy
.create(function () { });
2327 var methodDesc
= sinon
.getPropertyDescriptor(object
, property
);
2328 for (var i
= 0; i
< types
.length
; i
++) {
2329 methodDesc
[types
[i
]] = spy
.create(methodDesc
[types
[i
]]);
2331 return sinon
.wrapMethod(object
, property
, methodDesc
);
2333 var method
= object
[property
];
2334 return sinon
.wrapMethod(object
, property
, spy
.create(method
));
2338 function matchingFake(fakes
, args
, strict
) {
2343 for (var i
= 0, l
= fakes
.length
; i
< l
; i
++) {
2344 if (fakes
[i
].matches(args
, strict
)) {
2350 function incrementCallCount() {
2352 this.callCount
+= 1;
2353 this.notCalled
= false;
2354 this.calledOnce
= this.callCount
== 1;
2355 this.calledTwice
= this.callCount
== 2;
2356 this.calledThrice
= this.callCount
== 3;
2359 function createCallProperties() {
2360 this.firstCall
= this.getCall(0);
2361 this.secondCall
= this.getCall(1);
2362 this.thirdCall
= this.getCall(2);
2363 this.lastCall
= this.getCall(this.callCount
- 1);
2366 var vars
= "a,b,c,d,e,f,g,h,i,j,k,l";
2367 function createProxy(func
, proxyLength
) {
2368 // Retain the function length:
2371 eval("p = (function proxy(" + vars
.substring(0, proxyLength
* 2 - 1) +
2372 ") { return p.invoke(func, this, slice.call(arguments)); });");
2374 p
= function proxy() {
2375 return p
.invoke(func
, this, slice
.call(arguments
));
2378 p
.isSinonProxy
= true;
2386 reset: function () {
2387 if (this.invoking
) {
2388 var err
= new Error("Cannot reset Sinon function while invoking it. " +
2389 "Move the call to .reset outside of the callback.");
2390 err
.name
= "InvalidResetException";
2394 this.called
= false;
2395 this.notCalled
= true;
2396 this.calledOnce
= false;
2397 this.calledTwice
= false;
2398 this.calledThrice
= false;
2400 this.firstCall
= null;
2401 this.secondCall
= null;
2402 this.thirdCall
= null;
2403 this.lastCall
= null;
2405 this.returnValues
= [];
2406 this.thisValues
= [];
2407 this.exceptions
= [];
2410 for (var i
= 0; i
< this.fakes
.length
; i
++) {
2411 this.fakes
[i
].reset();
2418 create
: function create(func
, spyLength
) {
2421 if (typeof func
!= "function") {
2422 func = function () { };
2424 name
= sinon
.functionName(func
);
2428 spyLength
= func
.length
;
2431 var proxy
= createProxy(func
, spyLength
);
2433 sinon
.extend(proxy
, spy
);
2434 delete proxy
.create
;
2435 sinon
.extend(proxy
, func
);
2438 proxy
.prototype = func
.prototype;
2439 proxy
.displayName
= name
|| "spy";
2440 proxy
.toString
= sinon
.functionToString
;
2441 proxy
.instantiateFake
= sinon
.spy
.create
;
2442 proxy
.id
= "spy#" + uuid
++;
2447 invoke
: function invoke(func
, thisValue
, args
) {
2448 var matching
= matchingFake(this.fakes
, args
);
2449 var exception
, returnValue
;
2451 incrementCallCount
.call(this);
2452 push
.call(this.thisValues
, thisValue
);
2453 push
.call(this.args
, args
);
2454 push
.call(this.callIds
, callId
++);
2456 // Make call properties available from within the spied function:
2457 createCallProperties
.call(this);
2460 this.invoking
= true;
2463 returnValue
= matching
.invoke(func
, thisValue
, args
);
2465 returnValue
= (this.func
|| func
).apply(thisValue
, args
);
2468 var thisCall
= this.getCall(this.callCount
- 1);
2469 if (thisCall
.calledWithNew() && typeof returnValue
!== "object") {
2470 returnValue
= thisValue
;
2475 delete this.invoking
;
2478 push
.call(this.exceptions
, exception
);
2479 push
.call(this.returnValues
, returnValue
);
2481 // Make return value and exception available in the calls:
2482 createCallProperties
.call(this);
2484 if (exception
!== undefined) {
2491 named
: function named(name
) {
2492 this.displayName
= name
;
2496 getCall
: function getCall(i
) {
2497 if (i
< 0 || i
>= this.callCount
) {
2501 return sinon
.spyCall(this, this.thisValues
[i
], this.args
[i
],
2502 this.returnValues
[i
], this.exceptions
[i
],
2506 getCalls: function () {
2510 for (i
= 0; i
< this.callCount
; i
++) {
2511 calls
.push(this.getCall(i
));
2517 calledBefore
: function calledBefore(spyFn
) {
2522 if (!spyFn
.called
) {
2526 return this.callIds
[0] < spyFn
.callIds
[spyFn
.callIds
.length
- 1];
2529 calledAfter
: function calledAfter(spyFn
) {
2530 if (!this.called
|| !spyFn
.called
) {
2534 return this.callIds
[this.callCount
- 1] > spyFn
.callIds
[spyFn
.callCount
- 1];
2537 withArgs: function () {
2538 var args
= slice
.call(arguments
);
2541 var match
= matchingFake(this.fakes
, args
, true);
2550 var original
= this;
2551 var fake
= this.instantiateFake();
2552 fake
.matchingAguments
= args
;
2554 push
.call(this.fakes
, fake
);
2556 fake
.withArgs = function () {
2557 return original
.withArgs
.apply(original
, arguments
);
2560 for (var i
= 0; i
< this.args
.length
; i
++) {
2561 if (fake
.matches(this.args
[i
])) {
2562 incrementCallCount
.call(fake
);
2563 push
.call(fake
.thisValues
, this.thisValues
[i
]);
2564 push
.call(fake
.args
, this.args
[i
]);
2565 push
.call(fake
.returnValues
, this.returnValues
[i
]);
2566 push
.call(fake
.exceptions
, this.exceptions
[i
]);
2567 push
.call(fake
.callIds
, this.callIds
[i
]);
2570 createCallProperties
.call(fake
);
2575 matches: function (args
, strict
) {
2576 var margs
= this.matchingAguments
;
2578 if (margs
.length
<= args
.length
&&
2579 sinon
.deepEqual(margs
, args
.slice(0, margs
.length
))) {
2580 return !strict
|| margs
.length
== args
.length
;
2584 printf: function (format
) {
2586 var args
= slice
.call(arguments
, 1);
2589 return (format
|| "").replace(/%(.)/g, function (match
, specifyer
) {
2590 formatter
= spyApi
.formatters
[specifyer
];
2592 if (typeof formatter
== "function") {
2593 return formatter
.call(null, spy
, args
);
2594 } else if (!isNaN(parseInt(specifyer
, 10))) {
2595 return sinon
.format(args
[specifyer
- 1]);
2598 return "%" + specifyer
;
2603 function delegateToCalls(method
, matchAny
, actual
, notCalled
) {
2604 spyApi
[method
] = function () {
2607 return notCalled
.apply(this, arguments
);
2615 for (var i
= 0, l
= this.callCount
; i
< l
; i
+= 1) {
2616 currentCall
= this.getCall(i
);
2618 if (currentCall
[actual
|| method
].apply(currentCall
, arguments
)) {
2627 return matches
=== this.callCount
;
2631 delegateToCalls("calledOn", true);
2632 delegateToCalls("alwaysCalledOn", false, "calledOn");
2633 delegateToCalls("calledWith", true);
2634 delegateToCalls("calledWithMatch", true);
2635 delegateToCalls("alwaysCalledWith", false, "calledWith");
2636 delegateToCalls("alwaysCalledWithMatch", false, "calledWithMatch");
2637 delegateToCalls("calledWithExactly", true);
2638 delegateToCalls("alwaysCalledWithExactly", false, "calledWithExactly");
2639 delegateToCalls("neverCalledWith", false, "notCalledWith", function () {
2642 delegateToCalls("neverCalledWithMatch", false, "notCalledWithMatch", function () {
2645 delegateToCalls("threw", true);
2646 delegateToCalls("alwaysThrew", false, "threw");
2647 delegateToCalls("returned", true);
2648 delegateToCalls("alwaysReturned", false, "returned");
2649 delegateToCalls("calledWithNew", true);
2650 delegateToCalls("alwaysCalledWithNew", false, "calledWithNew");
2651 delegateToCalls("callArg", false, "callArgWith", function () {
2652 throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
2654 spyApi
.callArgWith
= spyApi
.callArg
;
2655 delegateToCalls("callArgOn", false, "callArgOnWith", function () {
2656 throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
2658 spyApi
.callArgOnWith
= spyApi
.callArgOn
;
2659 delegateToCalls("yield", false, "yield", function () {
2660 throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
2662 // "invokeCallback" is an alias for "yield" since "yield" is invalid in strict mode.
2663 spyApi
.invokeCallback
= spyApi
.yield;
2664 delegateToCalls("yieldOn", false, "yieldOn", function () {
2665 throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
2667 delegateToCalls("yieldTo", false, "yieldTo", function (property
) {
2668 throw new Error(this.toString() + " cannot yield to '" + property
+
2669 "' since it was not yet invoked.");
2671 delegateToCalls("yieldToOn", false, "yieldToOn", function (property
) {
2672 throw new Error(this.toString() + " cannot yield to '" + property
+
2673 "' since it was not yet invoked.");
2676 spyApi
.formatters
= {
2678 return sinon
.timesInWords(spy
.callCount
);
2682 return spy
.toString();
2688 for (var i
= 0, l
= spy
.callCount
; i
< l
; ++i
) {
2689 var stringifiedCall
= " " + spy
.getCall(i
).toString();
2690 if (/\n/.test(calls
[i
- 1])) {
2691 stringifiedCall
= "\n" + stringifiedCall
;
2693 push
.call(calls
, stringifiedCall
);
2696 return calls
.length
> 0 ? "\n" + calls
.join("\n") : "";
2702 for (var i
= 0, l
= spy
.callCount
; i
< l
; ++i
) {
2703 push
.call(objects
, sinon
.format(spy
.thisValues
[i
]));
2706 return objects
.join(", ");
2709 "*": function (spy
, args
) {
2712 for (var i
= 0, l
= args
.length
; i
< l
; ++i
) {
2713 push
.call(formatted
, sinon
.format(args
[i
]));
2716 return formatted
.join(", ");
2720 sinon
.extend(spy
, spyApi
);
2722 spy
.spyCall
= sinon
.spyCall
;
2728 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
2729 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
2731 function loadDependencies(require
, exports
, module
) {
2732 var sinon
= require("./util/core");
2734 require("./extend");
2735 require("./times_in_words");
2736 require("./format");
2737 module
.exports
= makeApi(sinon
);
2741 define(loadDependencies
);
2742 } else if (isNode
) {
2743 loadDependencies(require
, module
.exports
, module
);
2744 } else if (!sinon
) {
2749 }(typeof sinon
== "object" && sinon
|| null));
2752 * @depend util/core.js
2758 * @author Christian Johansen (christian@cjohansen.no)
2759 * @author Tim Fischbach (mail@timfischbach.de)
2762 * Copyright (c) 2010-2013 Christian Johansen
2766 var slice
= Array
.prototype.slice
;
2767 var join
= Array
.prototype.join
;
2768 var useLeftMostCallback
= -1;
2769 var useRightMostCallback
= -2;
2771 var nextTick
= (function () {
2772 if (typeof process
=== "object" && typeof process
.nextTick
=== "function") {
2773 return process
.nextTick
;
2774 } else if (typeof setImmediate
=== "function") {
2775 return setImmediate
;
2777 return function (callback
) {
2778 setTimeout(callback
, 0);
2783 function throwsException(error
, message
) {
2784 if (typeof error
== "string") {
2785 this.exception
= new Error(message
|| "");
2786 this.exception
.name
= error
;
2787 } else if (!error
) {
2788 this.exception
= new Error("Error");
2790 this.exception
= error
;
2796 function getCallback(behavior
, args
) {
2797 var callArgAt
= behavior
.callArgAt
;
2799 if (callArgAt
>= 0) {
2800 return args
[callArgAt
];
2805 if (callArgAt
=== useLeftMostCallback
) {
2806 argumentList
= args
;
2809 if (callArgAt
=== useRightMostCallback
) {
2810 argumentList
= slice
.call(args
).reverse();
2813 var callArgProp
= behavior
.callArgProp
;
2815 for (var i
= 0, l
= argumentList
.length
; i
< l
; ++i
) {
2816 if (!callArgProp
&& typeof argumentList
[i
] == "function") {
2817 return argumentList
[i
];
2820 if (callArgProp
&& argumentList
[i
] &&
2821 typeof argumentList
[i
][callArgProp
] == "function") {
2822 return argumentList
[i
][callArgProp
];
2829 function makeApi(sinon
) {
2830 function getCallbackError(behavior
, func
, args
) {
2831 if (behavior
.callArgAt
< 0) {
2834 if (behavior
.callArgProp
) {
2835 msg
= sinon
.functionName(behavior
.stub
) +
2836 " expected to yield to '" + behavior
.callArgProp
+
2837 "', but no object with such a property was passed.";
2839 msg
= sinon
.functionName(behavior
.stub
) +
2840 " expected to yield, but no callback was passed.";
2843 if (args
.length
> 0) {
2844 msg
+= " Received [" + join
.call(args
, ", ") + "]";
2850 return "argument at index " + behavior
.callArgAt
+ " is not a function: " + func
;
2853 function callCallback(behavior
, args
) {
2854 if (typeof behavior
.callArgAt
== "number") {
2855 var func
= getCallback(behavior
, args
);
2857 if (typeof func
!= "function") {
2858 throw new TypeError(getCallbackError(behavior
, func
, args
));
2861 if (behavior
.callbackAsync
) {
2862 nextTick(function () {
2863 func
.apply(behavior
.callbackContext
, behavior
.callbackArguments
);
2866 func
.apply(behavior
.callbackContext
, behavior
.callbackArguments
);
2872 create
: function create(stub
) {
2873 var behavior
= sinon
.extend({}, sinon
.behavior
);
2874 delete behavior
.create
;
2875 behavior
.stub
= stub
;
2880 isPresent
: function isPresent() {
2881 return (typeof this.callArgAt
== "number" ||
2883 typeof this.returnArgAt
== "number" ||
2885 this.returnValueDefined
);
2888 invoke
: function invoke(context
, args
) {
2889 callCallback(this, args
);
2891 if (this.exception
) {
2892 throw this.exception
;
2893 } else if (typeof this.returnArgAt
== "number") {
2894 return args
[this.returnArgAt
];
2895 } else if (this.returnThis
) {
2899 return this.returnValue
;
2902 onCall
: function onCall(index
) {
2903 return this.stub
.onCall(index
);
2906 onFirstCall
: function onFirstCall() {
2907 return this.stub
.onFirstCall();
2910 onSecondCall
: function onSecondCall() {
2911 return this.stub
.onSecondCall();
2914 onThirdCall
: function onThirdCall() {
2915 return this.stub
.onThirdCall();
2918 withArgs
: function withArgs(/* arguments */) {
2919 throw new Error("Defining a stub by invoking \"stub.onCall(...).withArgs(...)\" is not supported. " +
2920 "Use \"stub.withArgs(...).onCall(...)\" to define sequential behavior for calls with certain arguments.");
2923 callsArg
: function callsArg(pos
) {
2924 if (typeof pos
!= "number") {
2925 throw new TypeError("argument index is not number");
2928 this.callArgAt
= pos
;
2929 this.callbackArguments
= [];
2930 this.callbackContext
= undefined;
2931 this.callArgProp
= undefined;
2932 this.callbackAsync
= false;
2937 callsArgOn
: function callsArgOn(pos
, context
) {
2938 if (typeof pos
!= "number") {
2939 throw new TypeError("argument index is not number");
2941 if (typeof context
!= "object") {
2942 throw new TypeError("argument context is not an object");
2945 this.callArgAt
= pos
;
2946 this.callbackArguments
= [];
2947 this.callbackContext
= context
;
2948 this.callArgProp
= undefined;
2949 this.callbackAsync
= false;
2954 callsArgWith
: function callsArgWith(pos
) {
2955 if (typeof pos
!= "number") {
2956 throw new TypeError("argument index is not number");
2959 this.callArgAt
= pos
;
2960 this.callbackArguments
= slice
.call(arguments
, 1);
2961 this.callbackContext
= undefined;
2962 this.callArgProp
= undefined;
2963 this.callbackAsync
= false;
2968 callsArgOnWith
: function callsArgWith(pos
, context
) {
2969 if (typeof pos
!= "number") {
2970 throw new TypeError("argument index is not number");
2972 if (typeof context
!= "object") {
2973 throw new TypeError("argument context is not an object");
2976 this.callArgAt
= pos
;
2977 this.callbackArguments
= slice
.call(arguments
, 2);
2978 this.callbackContext
= context
;
2979 this.callArgProp
= undefined;
2980 this.callbackAsync
= false;
2985 yields: function () {
2986 this.callArgAt
= useLeftMostCallback
;
2987 this.callbackArguments
= slice
.call(arguments
, 0);
2988 this.callbackContext
= undefined;
2989 this.callArgProp
= undefined;
2990 this.callbackAsync
= false;
2995 yieldsRight: function () {
2996 this.callArgAt
= useRightMostCallback
;
2997 this.callbackArguments
= slice
.call(arguments
, 0);
2998 this.callbackContext
= undefined;
2999 this.callArgProp
= undefined;
3000 this.callbackAsync
= false;
3005 yieldsOn: function (context
) {
3006 if (typeof context
!= "object") {
3007 throw new TypeError("argument context is not an object");
3010 this.callArgAt
= useLeftMostCallback
;
3011 this.callbackArguments
= slice
.call(arguments
, 1);
3012 this.callbackContext
= context
;
3013 this.callArgProp
= undefined;
3014 this.callbackAsync
= false;
3019 yieldsTo: function (prop
) {
3020 this.callArgAt
= useLeftMostCallback
;
3021 this.callbackArguments
= slice
.call(arguments
, 1);
3022 this.callbackContext
= undefined;
3023 this.callArgProp
= prop
;
3024 this.callbackAsync
= false;
3029 yieldsToOn: function (prop
, context
) {
3030 if (typeof context
!= "object") {
3031 throw new TypeError("argument context is not an object");
3034 this.callArgAt
= useLeftMostCallback
;
3035 this.callbackArguments
= slice
.call(arguments
, 2);
3036 this.callbackContext
= context
;
3037 this.callArgProp
= prop
;
3038 this.callbackAsync
= false;
3043 throws: throwsException
,
3044 throwsException
: throwsException
,
3046 returns
: function returns(value
) {
3047 this.returnValue
= value
;
3048 this.returnValueDefined
= true;
3053 returnsArg
: function returnsArg(pos
) {
3054 if (typeof pos
!= "number") {
3055 throw new TypeError("argument index is not number");
3058 this.returnArgAt
= pos
;
3063 returnsThis
: function returnsThis() {
3064 this.returnThis
= true;
3070 // create asynchronous versions of callsArg* and yields* methods
3071 for (var method
in proto
) {
3072 // need to avoid creating anotherasync versions of the newly added async methods
3073 if (proto
.hasOwnProperty(method
) &&
3074 method
.match(/^(callsArg|yields)/) &&
3075 !method
.match(/Async/)) {
3076 proto
[method
+ "Async"] = (function (syncFnName
) {
3077 return function () {
3078 var result
= this[syncFnName
].apply(this, arguments
);
3079 this.callbackAsync
= true;
3086 sinon
.behavior
= proto
;
3090 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
3091 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
3093 function loadDependencies(require
, exports
, module
) {
3094 var sinon
= require("./util/core");
3095 require("./extend");
3096 module
.exports
= makeApi(sinon
);
3100 define(loadDependencies
);
3101 } else if (isNode
) {
3102 loadDependencies(require
, module
.exports
, module
);
3103 } else if (!sinon
) {
3108 }(typeof sinon
== "object" && sinon
|| null));
3111 * @depend util/core.js
3114 * @depend behavior.js
3119 * @author Christian Johansen (christian@cjohansen.no)
3122 * Copyright (c) 2010-2013 Christian Johansen
3126 function makeApi(sinon
) {
3127 function stub(object
, property
, func
) {
3128 if (!!func
&& typeof func
!= "function" && typeof func
!= "object") {
3129 throw new TypeError("Custom stub should be a function or a property descriptor");
3135 if (typeof func
== "function") {
3136 wrapper
= sinon
.spy
&& sinon
.spy
.create
? sinon
.spy
.create(func
) : func
;
3139 if (sinon
.spy
&& sinon
.spy
.create
) {
3140 var types
= sinon
.objectKeys(wrapper
);
3141 for (var i
= 0; i
< types
.length
; i
++) {
3142 wrapper
[types
[i
]] = sinon
.spy
.create(wrapper
[types
[i
]]);
3148 if (typeof object
== "object" && typeof object
[property
] == "function") {
3149 stubLength
= object
[property
].length
;
3151 wrapper
= stub
.create(stubLength
);
3154 if (!object
&& typeof property
=== "undefined") {
3155 return sinon
.stub
.create();
3158 if (typeof property
=== "undefined" && typeof object
== "object") {
3159 for (var prop
in object
) {
3160 if (typeof sinon
.getPropertyDescriptor(object
, prop
).value
=== "function") {
3168 return sinon
.wrapMethod(object
, property
, wrapper
);
3171 function getDefaultBehavior(stub
) {
3172 return stub
.defaultBehavior
|| getParentBehaviour(stub
) || sinon
.behavior
.create(stub
);
3175 function getParentBehaviour(stub
) {
3176 return (stub
.parent
&& getCurrentBehavior(stub
.parent
));
3179 function getCurrentBehavior(stub
) {
3180 var behavior
= stub
.behaviors
[stub
.callCount
- 1];
3181 return behavior
&& behavior
.isPresent() ? behavior
: getDefaultBehavior(stub
);
3187 create
: function create(stubLength
) {
3188 var functionStub = function () {
3189 return getCurrentBehavior(functionStub
).invoke(this, arguments
);
3192 functionStub
.id
= "stub#" + uuid
++;
3193 var orig
= functionStub
;
3194 functionStub
= sinon
.spy
.create(functionStub
, stubLength
);
3195 functionStub
.func
= orig
;
3197 sinon
.extend(functionStub
, stub
);
3198 functionStub
.instantiateFake
= sinon
.stub
.create
;
3199 functionStub
.displayName
= "stub";
3200 functionStub
.toString
= sinon
.functionToString
;
3202 functionStub
.defaultBehavior
= null;
3203 functionStub
.behaviors
= [];
3205 return functionStub
;
3208 resetBehavior: function () {
3211 this.defaultBehavior
= null;
3212 this.behaviors
= [];
3214 delete this.returnValue
;
3215 delete this.returnArgAt
;
3216 this.returnThis
= false;
3219 for (i
= 0; i
< this.fakes
.length
; i
++) {
3220 this.fakes
[i
].resetBehavior();
3225 onCall
: function onCall(index
) {
3226 if (!this.behaviors
[index
]) {
3227 this.behaviors
[index
] = sinon
.behavior
.create(this);
3230 return this.behaviors
[index
];
3233 onFirstCall
: function onFirstCall() {
3234 return this.onCall(0);
3237 onSecondCall
: function onSecondCall() {
3238 return this.onCall(1);
3241 onThirdCall
: function onThirdCall() {
3242 return this.onCall(2);
3246 for (var method
in sinon
.behavior
) {
3247 if (sinon
.behavior
.hasOwnProperty(method
) &&
3248 !proto
.hasOwnProperty(method
) &&
3249 method
!= "create" &&
3250 method
!= "withArgs" &&
3251 method
!= "invoke") {
3252 proto
[method
] = (function (behaviorMethod
) {
3253 return function () {
3254 this.defaultBehavior
= this.defaultBehavior
|| sinon
.behavior
.create(this);
3255 this.defaultBehavior
[behaviorMethod
].apply(this.defaultBehavior
, arguments
);
3262 sinon
.extend(stub
, proto
);
3268 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
3269 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
3271 function loadDependencies(require
, exports
, module
) {
3272 var sinon
= require("./util/core");
3273 require("./behavior");
3275 require("./extend");
3276 module
.exports
= makeApi(sinon
);
3280 define(loadDependencies
);
3281 } else if (isNode
) {
3282 loadDependencies(require
, module
.exports
, module
);
3283 } else if (!sinon
) {
3288 }(typeof sinon
== "object" && sinon
|| null));
3291 * @depend times_in_words.js
3292 * @depend util/core.js
3303 * @author Christian Johansen (christian@cjohansen.no)
3306 * Copyright (c) 2010-2013 Christian Johansen
3310 function makeApi(sinon
) {
3312 var match
= sinon
.match
;
3314 function mock(object
) {
3315 // if (typeof console !== undefined && console.warn) {
3316 // console.warn("mock will be removed from Sinon.JS v2.0");
3320 return sinon
.expectation
.create("Anonymous mock");
3323 return mock
.create(object
);
3326 function each(collection
, callback
) {
3331 for (var i
= 0, l
= collection
.length
; i
< l
; i
+= 1) {
3332 callback(collection
[i
]);
3336 sinon
.extend(mock
, {
3337 create
: function create(object
) {
3339 throw new TypeError("object is null");
3342 var mockObject
= sinon
.extend({}, mock
);
3343 mockObject
.object
= object
;
3344 delete mockObject
.create
;
3349 expects
: function expects(method
) {
3351 throw new TypeError("method is falsy");
3354 if (!this.expectations
) {
3355 this.expectations
= {};
3359 if (!this.expectations
[method
]) {
3360 this.expectations
[method
] = [];
3361 var mockObject
= this;
3363 sinon
.wrapMethod(this.object
, method
, function () {
3364 return mockObject
.invokeMethod(method
, this, arguments
);
3367 push
.call(this.proxies
, method
);
3370 var expectation
= sinon
.expectation
.create(method
);
3371 push
.call(this.expectations
[method
], expectation
);
3376 restore
: function restore() {
3377 var object
= this.object
;
3379 each(this.proxies
, function (proxy
) {
3380 if (typeof object
[proxy
].restore
== "function") {
3381 object
[proxy
].restore();
3386 verify
: function verify() {
3387 var expectations
= this.expectations
|| {};
3388 var messages
= [], met
= [];
3390 each(this.proxies
, function (proxy
) {
3391 each(expectations
[proxy
], function (expectation
) {
3392 if (!expectation
.met()) {
3393 push
.call(messages
, expectation
.toString());
3395 push
.call(met
, expectation
.toString());
3402 if (messages
.length
> 0) {
3403 sinon
.expectation
.fail(messages
.concat(met
).join("\n"));
3404 } else if (met
.length
> 0) {
3405 sinon
.expectation
.pass(messages
.concat(met
).join("\n"));
3411 invokeMethod
: function invokeMethod(method
, thisValue
, args
) {
3412 var expectations
= this.expectations
&& this.expectations
[method
];
3413 var length
= expectations
&& expectations
.length
|| 0, i
;
3415 for (i
= 0; i
< length
; i
+= 1) {
3416 if (!expectations
[i
].met() &&
3417 expectations
[i
].allowsCall(thisValue
, args
)) {
3418 return expectations
[i
].apply(thisValue
, args
);
3422 var messages
= [], available
, exhausted
= 0;
3424 for (i
= 0; i
< length
; i
+= 1) {
3425 if (expectations
[i
].allowsCall(thisValue
, args
)) {
3426 available
= available
|| expectations
[i
];
3430 push
.call(messages
, " " + expectations
[i
].toString());
3433 if (exhausted
=== 0) {
3434 return available
.apply(thisValue
, args
);
3437 messages
.unshift("Unexpected call: " + sinon
.spyCall
.toString
.call({
3442 sinon
.expectation
.fail(messages
.join("\n"));
3446 var times
= sinon
.timesInWords
;
3447 var slice
= Array
.prototype.slice
;
3449 function callCountInWords(callCount
) {
3450 if (callCount
== 0) {
3451 return "never called";
3453 return "called " + times(callCount
);
3457 function expectedCallCountInWords(expectation
) {
3458 var min
= expectation
.minCalls
;
3459 var max
= expectation
.maxCalls
;
3461 if (typeof min
== "number" && typeof max
== "number") {
3462 var str
= times(min
);
3465 str
= "at least " + str
+ " and at most " + times(max
);
3471 if (typeof min
== "number") {
3472 return "at least " + times(min
);
3475 return "at most " + times(max
);
3478 function receivedMinCalls(expectation
) {
3479 var hasMinLimit
= typeof expectation
.minCalls
== "number";
3480 return !hasMinLimit
|| expectation
.callCount
>= expectation
.minCalls
;
3483 function receivedMaxCalls(expectation
) {
3484 if (typeof expectation
.maxCalls
!= "number") {
3488 return expectation
.callCount
== expectation
.maxCalls
;
3491 function verifyMatcher(possibleMatcher
, arg
) {
3492 if (match
&& match
.isMatcher(possibleMatcher
)) {
3493 return possibleMatcher
.test(arg
);
3499 sinon
.expectation
= {
3503 create
: function create(methodName
) {
3504 var expectation
= sinon
.extend(sinon
.stub
.create(), sinon
.expectation
);
3505 delete expectation
.create
;
3506 expectation
.method
= methodName
;
3511 invoke
: function invoke(func
, thisValue
, args
) {
3512 this.verifyCallAllowed(thisValue
, args
);
3514 return sinon
.spy
.invoke
.apply(this, arguments
);
3517 atLeast
: function atLeast(num
) {
3518 if (typeof num
!= "number") {
3519 throw new TypeError("'" + num
+ "' is not number");
3522 if (!this.limitsSet
) {
3523 this.maxCalls
= null;
3524 this.limitsSet
= true;
3527 this.minCalls
= num
;
3532 atMost
: function atMost(num
) {
3533 if (typeof num
!= "number") {
3534 throw new TypeError("'" + num
+ "' is not number");
3537 if (!this.limitsSet
) {
3538 this.minCalls
= null;
3539 this.limitsSet
= true;
3542 this.maxCalls
= num
;
3547 never
: function never() {
3548 return this.exactly(0);
3551 once
: function once() {
3552 return this.exactly(1);
3555 twice
: function twice() {
3556 return this.exactly(2);
3559 thrice
: function thrice() {
3560 return this.exactly(3);
3563 exactly
: function exactly(num
) {
3564 if (typeof num
!= "number") {
3565 throw new TypeError("'" + num
+ "' is not a number");
3569 return this.atMost(num
);
3572 met
: function met() {
3573 return !this.failed
&& receivedMinCalls(this);
3576 verifyCallAllowed
: function verifyCallAllowed(thisValue
, args
) {
3577 if (receivedMaxCalls(this)) {
3579 sinon
.expectation
.fail(this.method
+ " already called " + times(this.maxCalls
));
3582 if ("expectedThis" in this && this.expectedThis
!== thisValue
) {
3583 sinon
.expectation
.fail(this.method
+ " called with " + thisValue
+ " as thisValue, expected " +
3587 if (!("expectedArguments" in this)) {
3592 sinon
.expectation
.fail(this.method
+ " received no arguments, expected " +
3593 sinon
.format(this.expectedArguments
));
3596 if (args
.length
< this.expectedArguments
.length
) {
3597 sinon
.expectation
.fail(this.method
+ " received too few arguments (" + sinon
.format(args
) +
3598 "), expected " + sinon
.format(this.expectedArguments
));
3601 if (this.expectsExactArgCount
&&
3602 args
.length
!= this.expectedArguments
.length
) {
3603 sinon
.expectation
.fail(this.method
+ " received too many arguments (" + sinon
.format(args
) +
3604 "), expected " + sinon
.format(this.expectedArguments
));
3607 for (var i
= 0, l
= this.expectedArguments
.length
; i
< l
; i
+= 1) {
3609 if (!verifyMatcher(this.expectedArguments
[i
], args
[i
])) {
3610 sinon
.expectation
.fail(this.method
+ " received wrong arguments " + sinon
.format(args
) +
3611 ", didn't match " + this.expectedArguments
.toString());
3614 if (!sinon
.deepEqual(this.expectedArguments
[i
], args
[i
])) {
3615 sinon
.expectation
.fail(this.method
+ " received wrong arguments " + sinon
.format(args
) +
3616 ", expected " + sinon
.format(this.expectedArguments
));
3621 allowsCall
: function allowsCall(thisValue
, args
) {
3622 if (this.met() && receivedMaxCalls(this)) {
3626 if ("expectedThis" in this && this.expectedThis
!== thisValue
) {
3630 if (!("expectedArguments" in this)) {
3636 if (args
.length
< this.expectedArguments
.length
) {
3640 if (this.expectsExactArgCount
&&
3641 args
.length
!= this.expectedArguments
.length
) {
3645 for (var i
= 0, l
= this.expectedArguments
.length
; i
< l
; i
+= 1) {
3646 if (!verifyMatcher(this.expectedArguments
[i
], args
[i
])) {
3650 if (!sinon
.deepEqual(this.expectedArguments
[i
], args
[i
])) {
3658 withArgs
: function withArgs() {
3659 this.expectedArguments
= slice
.call(arguments
);
3663 withExactArgs
: function withExactArgs() {
3664 this.withArgs
.apply(this, arguments
);
3665 this.expectsExactArgCount
= true;
3669 on
: function on(thisValue
) {
3670 this.expectedThis
= thisValue
;
3674 toString: function () {
3675 var args
= (this.expectedArguments
|| []).slice();
3677 if (!this.expectsExactArgCount
) {
3678 push
.call(args
, "[...]");
3681 var callStr
= sinon
.spyCall
.toString
.call({
3682 proxy
: this.method
|| "anonymous mock expectation",
3686 var message
= callStr
.replace(", [...", "[, ...") + " " +
3687 expectedCallCountInWords(this);
3690 return "Expectation met: " + message
;
3693 return "Expected " + message
+ " (" +
3694 callCountInWords(this.callCount
) + ")";
3697 verify
: function verify() {
3699 sinon
.expectation
.fail(this.toString());
3701 sinon
.expectation
.pass(this.toString());
3707 pass
: function pass(message
) {
3708 sinon
.assert
.pass(message
);
3711 fail
: function fail(message
) {
3712 var exception
= new Error(message
);
3713 exception
.name
= "ExpectationError";
3723 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
3724 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
3726 function loadDependencies(require
, exports
, module
) {
3727 var sinon
= require("./util/core");
3728 require("./times_in_words");
3730 require("./extend");
3734 require("./format");
3736 module
.exports
= makeApi(sinon
);
3740 define(loadDependencies
);
3741 } else if (isNode
) {
3742 loadDependencies(require
, module
.exports
, module
);
3743 } else if (!sinon
) {
3748 }(typeof sinon
== "object" && sinon
|| null));
3751 * @depend util/core.js
3757 * Collections of stubs, spies and mocks.
3759 * @author Christian Johansen (christian@cjohansen.no)
3762 * Copyright (c) 2010-2013 Christian Johansen
3767 var hasOwnProperty
= Object
.prototype.hasOwnProperty
;
3769 function getFakes(fakeCollection
) {
3770 if (!fakeCollection
.fakes
) {
3771 fakeCollection
.fakes
= [];
3774 return fakeCollection
.fakes
;
3777 function each(fakeCollection
, method
) {
3778 var fakes
= getFakes(fakeCollection
);
3780 for (var i
= 0, l
= fakes
.length
; i
< l
; i
+= 1) {
3781 if (typeof fakes
[i
][method
] == "function") {
3787 function compact(fakeCollection
) {
3788 var fakes
= getFakes(fakeCollection
);
3790 while (i
< fakes
.length
) {
3795 function makeApi(sinon
) {
3797 verify
: function resolve() {
3798 each(this, "verify");
3801 restore
: function restore() {
3802 each(this, "restore");
3806 reset
: function restore() {
3807 each(this, "reset");
3810 verifyAndRestore
: function verifyAndRestore() {
3826 add
: function add(fake
) {
3827 push
.call(getFakes(this), fake
);
3831 spy
: function spy() {
3832 return this.add(sinon
.spy
.apply(sinon
, arguments
));
3835 stub
: function stub(object
, property
, value
) {
3837 var original
= object
[property
];
3839 if (typeof original
!= "function") {
3840 if (!hasOwnProperty
.call(object
, property
)) {
3841 throw new TypeError("Cannot stub non-existent own property " + property
);
3844 object
[property
] = value
;
3847 restore: function () {
3848 object
[property
] = original
;
3853 if (!property
&& !!object
&& typeof object
== "object") {
3854 var stubbedObj
= sinon
.stub
.apply(sinon
, arguments
);
3856 for (var prop
in stubbedObj
) {
3857 if (typeof stubbedObj
[prop
] === "function") {
3858 this.add(stubbedObj
[prop
]);
3865 return this.add(sinon
.stub
.apply(sinon
, arguments
));
3868 mock
: function mock() {
3869 return this.add(sinon
.mock
.apply(sinon
, arguments
));
3872 inject
: function inject(obj
) {
3875 obj
.spy = function () {
3876 return col
.spy
.apply(col
, arguments
);
3879 obj
.stub = function () {
3880 return col
.stub
.apply(col
, arguments
);
3883 obj
.mock = function () {
3884 return col
.mock
.apply(col
, arguments
);
3891 sinon
.collection
= collection
;
3895 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
3896 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
3898 function loadDependencies(require
, exports
, module
) {
3899 var sinon
= require("./util/core");
3903 module
.exports
= makeApi(sinon
);
3907 define(loadDependencies
);
3908 } else if (isNode
) {
3909 loadDependencies(require
, module
.exports
, module
);
3910 } else if (!sinon
) {
3915 }(typeof sinon
== "object" && sinon
|| null));
3929 * Inspired by jsUnitMockTimeOut from JsUnit
3931 * @author Christian Johansen (christian@cjohansen.no)
3934 * Copyright (c) 2010-2013 Christian Johansen
3937 if (typeof sinon
== "undefined") {
3941 (function (global
) {
3942 function makeApi(sinon
, lol
) {
3943 var llx
= typeof lolex
!== "undefined" ? lolex
: lol
;
3945 sinon
.useFakeTimers = function () {
3946 var now
, methods
= Array
.prototype.slice
.call(arguments
);
3948 if (typeof methods
[0] === "string") {
3951 now
= methods
.shift();
3954 var clock
= llx
.install(now
|| 0, methods
);
3955 clock
.restore
= clock
.uninstall
;
3960 create: function (now
) {
3961 return llx
.createClock(now
);
3966 setTimeout
: setTimeout
,
3967 clearTimeout
: clearTimeout
,
3968 setImmediate
: (typeof setImmediate
!== "undefined" ? setImmediate
: undefined),
3969 clearImmediate
: (typeof clearImmediate
!== "undefined" ? clearImmediate
: undefined),
3970 setInterval
: setInterval
,
3971 clearInterval
: clearInterval
,
3976 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
3977 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
3979 function loadDependencies(require
, epxorts
, module
, lolex
) {
3980 var sinon
= require("./core");
3981 makeApi(sinon
, lolex
);
3982 module
.exports
= sinon
;
3986 define(loadDependencies
);
3987 } else if (isNode
) {
3988 loadDependencies(require
, module
.exports
, module
, require("lolex"));
3992 }(typeof global
!= "undefined" && typeof global
!== "function" ? global
: this));
3995 * Minimal Event interface implementation
3997 * Original implementation by Sven Fuchs: https://gist.github.com/995028
3998 * Modifications and tests by Christian Johansen.
4000 * @author Sven Fuchs (svenfuchs@artweb-design.de)
4001 * @author Christian Johansen (christian@cjohansen.no)
4004 * Copyright (c) 2011 Sven Fuchs, Christian Johansen
4007 if (typeof sinon
== "undefined") {
4014 function makeApi(sinon
) {
4015 sinon
.Event
= function Event(type
, bubbles
, cancelable
, target
) {
4016 this.initEvent(type
, bubbles
, cancelable
, target
);
4019 sinon
.Event
.prototype = {
4020 initEvent: function (type
, bubbles
, cancelable
, target
) {
4022 this.bubbles
= bubbles
;
4023 this.cancelable
= cancelable
;
4024 this.target
= target
;
4027 stopPropagation: function () {},
4029 preventDefault: function () {
4030 this.defaultPrevented
= true;
4034 sinon
.ProgressEvent
= function ProgressEvent(type
, progressEventRaw
, target
) {
4035 this.initEvent(type
, false, false, target
);
4036 this.loaded
= progressEventRaw
.loaded
|| null;
4037 this.total
= progressEventRaw
.total
|| null;
4038 this.lengthComputable
= !!progressEventRaw
.total
;
4041 sinon
.ProgressEvent
.prototype = new sinon
.Event();
4043 sinon
.ProgressEvent
.prototype.constructor = sinon
.ProgressEvent
;
4045 sinon
.CustomEvent
= function CustomEvent(type
, customData
, target
) {
4046 this.initEvent(type
, false, false, target
);
4047 this.detail
= customData
.detail
|| null;
4050 sinon
.CustomEvent
.prototype = new sinon
.Event();
4052 sinon
.CustomEvent
.prototype.constructor = sinon
.CustomEvent
;
4054 sinon
.EventTarget
= {
4055 addEventListener
: function addEventListener(event
, listener
) {
4056 this.eventListeners
= this.eventListeners
|| {};
4057 this.eventListeners
[event
] = this.eventListeners
[event
] || [];
4058 push
.call(this.eventListeners
[event
], listener
);
4061 removeEventListener
: function removeEventListener(event
, listener
) {
4062 var listeners
= this.eventListeners
&& this.eventListeners
[event
] || [];
4064 for (var i
= 0, l
= listeners
.length
; i
< l
; ++i
) {
4065 if (listeners
[i
] == listener
) {
4066 return listeners
.splice(i
, 1);
4071 dispatchEvent
: function dispatchEvent(event
) {
4072 var type
= event
.type
;
4073 var listeners
= this.eventListeners
&& this.eventListeners
[type
] || [];
4075 for (var i
= 0; i
< listeners
.length
; i
++) {
4076 if (typeof listeners
[i
] == "function") {
4077 listeners
[i
].call(this, event
);
4079 listeners
[i
].handleEvent(event
);
4083 return !!event
.defaultPrevented
;
4088 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
4089 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
4091 function loadDependencies(require
) {
4092 var sinon
= require("./core");
4097 define(loadDependencies
);
4098 } else if (isNode
) {
4099 loadDependencies(require
);
4106 * @depend util/core.js
4111 * @author Christian Johansen (christian@cjohansen.no)
4114 * Copyright (c) 2010-2014 Christian Johansen
4118 // cache a reference to setTimeout, so that our reference won't be stubbed out
4119 // when using fake timers and errors will still get logged
4120 // https://github.com/cjohansen/Sinon.JS/issues/381
4121 var realSetTimeout
= setTimeout
;
4123 function makeApi(sinon
) {
4127 function logError(label
, err
) {
4128 var msg
= label
+ " threw exception: ";
4130 sinon
.log(msg
+ "[" + err
.name
+ "] " + err
.message
);
4133 sinon
.log(err
.stack
);
4136 logError
.setTimeout(function () {
4137 err
.message
= msg
+ err
.message
;
4142 // wrap realSetTimeout with something we can stub in tests
4143 logError
.setTimeout = function (func
, timeout
) {
4144 realSetTimeout(func
, timeout
);
4148 exports
.log
= sinon
.log
= log
;
4149 exports
.logError
= sinon
.logError
= logError
;
4154 function loadDependencies(require
, exports
, module
) {
4155 var sinon
= require("./util/core");
4156 module
.exports
= makeApi(sinon
);
4159 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
4160 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
4163 define(loadDependencies
);
4164 } else if (isNode
) {
4165 loadDependencies(require
, module
.exports
, module
);
4166 } else if (!sinon
) {
4171 }(typeof sinon
== "object" && sinon
|| null));
4175 * @depend ../extend.js
4177 * @depend ../log_error.js
4180 * Fake XDomainRequest object
4183 if (typeof sinon
== "undefined") {
4187 // wrapper for global
4188 (function (global
) {
4189 var xdr
= { XDomainRequest
: global
.XDomainRequest
};
4190 xdr
.GlobalXDomainRequest
= global
.XDomainRequest
;
4191 xdr
.supportsXDR
= typeof xdr
.GlobalXDomainRequest
!= "undefined";
4192 xdr
.workingXDR
= xdr
.supportsXDR
? xdr
.GlobalXDomainRequest
: false;
4194 function makeApi(sinon
) {
4197 function FakeXDomainRequest() {
4198 this.readyState
= FakeXDomainRequest
.UNSENT
;
4199 this.requestBody
= null;
4200 this.requestHeaders
= {};
4202 this.timeout
= null;
4204 if (typeof FakeXDomainRequest
.onCreate
== "function") {
4205 FakeXDomainRequest
.onCreate(this);
4209 function verifyState(xdr
) {
4210 if (xdr
.readyState
!== FakeXDomainRequest
.OPENED
) {
4211 throw new Error("INVALID_STATE_ERR");
4215 throw new Error("INVALID_STATE_ERR");
4219 function verifyRequestSent(xdr
) {
4220 if (xdr
.readyState
== FakeXDomainRequest
.UNSENT
) {
4221 throw new Error("Request not sent");
4223 if (xdr
.readyState
== FakeXDomainRequest
.DONE
) {
4224 throw new Error("Request done");
4228 function verifyResponseBodyType(body
) {
4229 if (typeof body
!= "string") {
4230 var error
= new Error("Attempted to respond to fake XDomainRequest with " +
4231 body
+ ", which is not a string.");
4232 error
.name
= "InvalidBodyException";
4237 sinon
.extend(FakeXDomainRequest
.prototype, sinon
.EventTarget
, {
4238 open
: function open(method
, url
) {
4239 this.method
= method
;
4242 this.responseText
= null;
4243 this.sendFlag
= false;
4245 this.readyStateChange(FakeXDomainRequest
.OPENED
);
4248 readyStateChange
: function readyStateChange(state
) {
4249 this.readyState
= state
;
4251 switch (this.readyState
) {
4252 case FakeXDomainRequest
.UNSENT
:
4254 case FakeXDomainRequest
.OPENED
:
4256 case FakeXDomainRequest
.LOADING
:
4257 if (this.sendFlag
) {
4258 //raise the progress event
4259 eventName
= "onprogress";
4262 case FakeXDomainRequest
.DONE
:
4263 if (this.isTimeout
) {
4264 eventName
= "ontimeout"
4265 } else if (this.errorFlag
|| (this.status
< 200 || this.status
> 299)) {
4266 eventName
= "onerror";
4268 eventName
= "onload"
4273 // raising event (if defined)
4275 if (typeof this[eventName
] == "function") {
4279 sinon
.logError("Fake XHR " + eventName
+ " handler", e
);
4285 send
: function send(data
) {
4288 if (!/^(get|head)$/i.test(this.method
)) {
4289 this.requestBody
= data
;
4291 this.requestHeaders
["Content-Type"] = "text/plain;charset=utf-8";
4293 this.errorFlag
= false;
4294 this.sendFlag
= true;
4295 this.readyStateChange(FakeXDomainRequest
.OPENED
);
4297 if (typeof this.onSend
== "function") {
4302 abort
: function abort() {
4303 this.aborted
= true;
4304 this.responseText
= null;
4305 this.errorFlag
= true;
4307 if (this.readyState
> sinon
.FakeXDomainRequest
.UNSENT
&& this.sendFlag
) {
4308 this.readyStateChange(sinon
.FakeXDomainRequest
.DONE
);
4309 this.sendFlag
= false;
4313 setResponseBody
: function setResponseBody(body
) {
4314 verifyRequestSent(this);
4315 verifyResponseBodyType(body
);
4317 var chunkSize
= this.chunkSize
|| 10;
4319 this.responseText
= "";
4322 this.readyStateChange(FakeXDomainRequest
.LOADING
);
4323 this.responseText
+= body
.substring(index
, index
+ chunkSize
);
4325 } while (index
< body
.length
);
4327 this.readyStateChange(FakeXDomainRequest
.DONE
);
4330 respond
: function respond(status
, contentType
, body
) {
4331 // content-type ignored, since XDomainRequest does not carry this
4332 // we keep the same syntax for respond(...) as for FakeXMLHttpRequest to ease
4333 // test integration across browsers
4334 this.status
= typeof status
== "number" ? status
: 200;
4335 this.setResponseBody(body
|| "");
4338 simulatetimeout
: function simulatetimeout() {
4340 this.isTimeout
= true;
4341 // Access to this should actually throw an error
4342 this.responseText
= undefined;
4343 this.readyStateChange(FakeXDomainRequest
.DONE
);
4347 sinon
.extend(FakeXDomainRequest
, {
4354 sinon
.useFakeXDomainRequest
= function useFakeXDomainRequest() {
4355 sinon
.FakeXDomainRequest
.restore
= function restore(keepOnCreate
) {
4356 if (xdr
.supportsXDR
) {
4357 global
.XDomainRequest
= xdr
.GlobalXDomainRequest
;
4360 delete sinon
.FakeXDomainRequest
.restore
;
4362 if (keepOnCreate
!== true) {
4363 delete sinon
.FakeXDomainRequest
.onCreate
;
4366 if (xdr
.supportsXDR
) {
4367 global
.XDomainRequest
= sinon
.FakeXDomainRequest
;
4369 return sinon
.FakeXDomainRequest
;
4372 sinon
.FakeXDomainRequest
= FakeXDomainRequest
;
4375 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
4376 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
4378 function loadDependencies(require
, exports
, module
) {
4379 var sinon
= require("./core");
4380 require("../extend");
4382 require("../log_error");
4384 module
.exports
= sinon
;
4388 define(loadDependencies
);
4389 } else if (isNode
) {
4390 loadDependencies(require
, module
.exports
, module
);
4394 })(typeof global
!== "undefined" ? global
: self
);
4398 * @depend ../extend.js
4400 * @depend ../log_error.js
4403 * Fake XMLHttpRequest object
4405 * @author Christian Johansen (christian@cjohansen.no)
4408 * Copyright (c) 2010-2013 Christian Johansen
4411 (function (global
) {
4413 var supportsProgress
= typeof ProgressEvent
!== "undefined";
4414 var supportsCustomEvent
= typeof CustomEvent
!== "undefined";
4415 var supportsFormData
= typeof FormData
!== "undefined";
4416 var sinonXhr
= { XMLHttpRequest
: global
.XMLHttpRequest
};
4417 sinonXhr
.GlobalXMLHttpRequest
= global
.XMLHttpRequest
;
4418 sinonXhr
.GlobalActiveXObject
= global
.ActiveXObject
;
4419 sinonXhr
.supportsActiveX
= typeof sinonXhr
.GlobalActiveXObject
!= "undefined";
4420 sinonXhr
.supportsXHR
= typeof sinonXhr
.GlobalXMLHttpRequest
!= "undefined";
4421 sinonXhr
.workingXHR
= sinonXhr
.supportsXHR
? sinonXhr
.GlobalXMLHttpRequest
: sinonXhr
.supportsActiveX
4423 return new sinonXhr
.GlobalActiveXObject("MSXML2.XMLHTTP.3.0")
4425 sinonXhr
.supportsCORS
= sinonXhr
.supportsXHR
&& "withCredentials" in (new sinonXhr
.GlobalXMLHttpRequest());
4428 var unsafeHeaders
= {
4429 "Accept-Charset": true,
4430 "Accept-Encoding": true,
4432 "Content-Length": true,
4435 "Content-Transfer-Encoding": true,
4443 "Transfer-Encoding": true,
4450 // Note that for FakeXMLHttpRequest to work pre ES5
4451 // we lose some of the alignment with the spec.
4452 // To ensure as close a match as possible,
4453 // set responseType before calling open, send or respond;
4454 function FakeXMLHttpRequest() {
4455 this.readyState
= FakeXMLHttpRequest
.UNSENT
;
4456 this.requestHeaders
= {};
4457 this.requestBody
= null;
4459 this.statusText
= "";
4460 this.upload
= new UploadProgress();
4461 this.responseType
= "";
4463 if (sinonXhr
.supportsCORS
) {
4464 this.withCredentials
= false;
4468 var events
= ["loadstart", "load", "abort", "loadend"];
4470 function addEventListener(eventName
) {
4471 xhr
.addEventListener(eventName
, function (event
) {
4472 var listener
= xhr
["on" + eventName
];
4474 if (listener
&& typeof listener
== "function") {
4475 listener
.call(this, event
);
4480 for (var i
= events
.length
- 1; i
>= 0; i
--) {
4481 addEventListener(events
[i
]);
4484 if (typeof FakeXMLHttpRequest
.onCreate
== "function") {
4485 FakeXMLHttpRequest
.onCreate(this);
4489 // An upload object is created for each
4490 // FakeXMLHttpRequest and allows upload
4491 // events to be simulated using uploadProgress
4493 function UploadProgress() {
4494 this.eventListeners
= {
4502 UploadProgress
.prototype.addEventListener
= function addEventListener(event
, listener
) {
4503 this.eventListeners
[event
].push(listener
);
4506 UploadProgress
.prototype.removeEventListener
= function removeEventListener(event
, listener
) {
4507 var listeners
= this.eventListeners
[event
] || [];
4509 for (var i
= 0, l
= listeners
.length
; i
< l
; ++i
) {
4510 if (listeners
[i
] == listener
) {
4511 return listeners
.splice(i
, 1);
4516 UploadProgress
.prototype.dispatchEvent
= function dispatchEvent(event
) {
4517 var listeners
= this.eventListeners
[event
.type
] || [];
4519 for (var i
= 0, listener
; (listener
= listeners
[i
]) != null; i
++) {
4524 function verifyState(xhr
) {
4525 if (xhr
.readyState
!== FakeXMLHttpRequest
.OPENED
) {
4526 throw new Error("INVALID_STATE_ERR");
4530 throw new Error("INVALID_STATE_ERR");
4534 function getHeader(headers
, header
) {
4535 header
= header
.toLowerCase();
4537 for (var h
in headers
) {
4538 if (h
.toLowerCase() == header
) {
4546 // filtering to enable a white-list version of Sinon FakeXhr,
4547 // where whitelisted requests are passed through to real XHR
4548 function each(collection
, callback
) {
4553 for (var i
= 0, l
= collection
.length
; i
< l
; i
+= 1) {
4554 callback(collection
[i
]);
4557 function some(collection
, callback
) {
4558 for (var index
= 0; index
< collection
.length
; index
++) {
4559 if (callback(collection
[index
]) === true) {
4565 // largest arity in XHR is 5 - XHR#open
4566 var apply = function (obj
, method
, args
) {
4567 switch (args
.length
) {
4568 case 0: return obj
[method
]();
4569 case 1: return obj
[method
](args
[0]);
4570 case 2: return obj
[method
](args
[0], args
[1]);
4571 case 3: return obj
[method
](args
[0], args
[1], args
[2]);
4572 case 4: return obj
[method
](args
[0], args
[1], args
[2], args
[3]);
4573 case 5: return obj
[method
](args
[0], args
[1], args
[2], args
[3], args
[4]);
4577 FakeXMLHttpRequest
.filters
= [];
4578 FakeXMLHttpRequest
.addFilter
= function addFilter(fn
) {
4579 this.filters
.push(fn
)
4581 var IE6Re
= /MSIE 6/;
4582 FakeXMLHttpRequest
.defake
= function defake(fakeXhr
, xhrArgs
) {
4583 var xhr
= new sinonXhr
.workingXHR();
4589 "getResponseHeader",
4590 "getAllResponseHeaders",
4593 "removeEventListener"
4594 ], function (method
) {
4595 fakeXhr
[method
] = function () {
4596 return apply(xhr
, method
, arguments
);
4600 var copyAttrs = function (args
) {
4601 each(args
, function (attr
) {
4603 fakeXhr
[attr
] = xhr
[attr
]
4605 if (!IE6Re
.test(navigator
.userAgent
)) {
4612 var stateChange
= function stateChange() {
4613 fakeXhr
.readyState
= xhr
.readyState
;
4614 if (xhr
.readyState
>= FakeXMLHttpRequest
.HEADERS_RECEIVED
) {
4615 copyAttrs(["status", "statusText"]);
4617 if (xhr
.readyState
>= FakeXMLHttpRequest
.LOADING
) {
4618 copyAttrs(["responseText", "response"]);
4620 if (xhr
.readyState
=== FakeXMLHttpRequest
.DONE
) {
4621 copyAttrs(["responseXML"]);
4623 if (fakeXhr
.onreadystatechange
) {
4624 fakeXhr
.onreadystatechange
.call(fakeXhr
, { target
: fakeXhr
});
4628 if (xhr
.addEventListener
) {
4629 for (var event
in fakeXhr
.eventListeners
) {
4630 if (fakeXhr
.eventListeners
.hasOwnProperty(event
)) {
4631 each(fakeXhr
.eventListeners
[event
], function (handler
) {
4632 xhr
.addEventListener(event
, handler
);
4636 xhr
.addEventListener("readystatechange", stateChange
);
4638 xhr
.onreadystatechange
= stateChange
;
4640 apply(xhr
, "open", xhrArgs
);
4642 FakeXMLHttpRequest
.useFilters
= false;
4644 function verifyRequestOpened(xhr
) {
4645 if (xhr
.readyState
!= FakeXMLHttpRequest
.OPENED
) {
4646 throw new Error("INVALID_STATE_ERR - " + xhr
.readyState
);
4650 function verifyRequestSent(xhr
) {
4651 if (xhr
.readyState
== FakeXMLHttpRequest
.DONE
) {
4652 throw new Error("Request done");
4656 function verifyHeadersReceived(xhr
) {
4657 if (xhr
.async
&& xhr
.readyState
!= FakeXMLHttpRequest
.HEADERS_RECEIVED
) {
4658 throw new Error("No headers received");
4662 function verifyResponseBodyType(body
) {
4663 if (typeof body
!= "string") {
4664 var error
= new Error("Attempted to respond to fake XMLHttpRequest with " +
4665 body
+ ", which is not a string.");
4666 error
.name
= "InvalidBodyException";
4671 FakeXMLHttpRequest
.parseXML
= function parseXML(text
) {
4674 if (typeof DOMParser
!= "undefined") {
4675 var parser
= new DOMParser();
4676 xmlDoc
= parser
.parseFromString(text
, "text/xml");
4678 xmlDoc
= new ActiveXObject("Microsoft.XMLDOM");
4679 xmlDoc
.async
= "false";
4680 xmlDoc
.loadXML(text
);
4686 FakeXMLHttpRequest
.statusCodes
= {
4688 101: "Switching Protocols",
4692 203: "Non-Authoritative Information",
4694 205: "Reset Content",
4695 206: "Partial Content",
4696 207: "Multi-Status",
4697 300: "Multiple Choice",
4698 301: "Moved Permanently",
4701 304: "Not Modified",
4703 307: "Temporary Redirect",
4705 401: "Unauthorized",
4706 402: "Payment Required",
4709 405: "Method Not Allowed",
4710 406: "Not Acceptable",
4711 407: "Proxy Authentication Required",
4712 408: "Request Timeout",
4715 411: "Length Required",
4716 412: "Precondition Failed",
4717 413: "Request Entity Too Large",
4718 414: "Request-URI Too Long",
4719 415: "Unsupported Media Type",
4720 416: "Requested Range Not Satisfiable",
4721 417: "Expectation Failed",
4722 422: "Unprocessable Entity",
4723 500: "Internal Server Error",
4724 501: "Not Implemented",
4726 503: "Service Unavailable",
4727 504: "Gateway Timeout",
4728 505: "HTTP Version Not Supported"
4731 function makeApi(sinon
) {
4732 sinon
.xhr
= sinonXhr
;
4734 sinon
.extend(FakeXMLHttpRequest
.prototype, sinon
.EventTarget
, {
4737 open
: function open(method
, url
, async
, username
, password
) {
4738 this.method
= method
;
4740 this.async
= typeof async
== "boolean" ? async
: true;
4741 this.username
= username
;
4742 this.password
= password
;
4743 this.responseText
= null;
4744 this.response
= this.responseType
=== "json" ? null : "";
4745 this.responseXML
= null;
4746 this.requestHeaders
= {};
4747 this.sendFlag
= false;
4749 if (FakeXMLHttpRequest
.useFilters
=== true) {
4750 var xhrArgs
= arguments
;
4751 var defake
= some(FakeXMLHttpRequest
.filters
, function (filter
) {
4752 return filter
.apply(this, xhrArgs
)
4755 return FakeXMLHttpRequest
.defake(this, arguments
);
4758 this.readyStateChange(FakeXMLHttpRequest
.OPENED
);
4761 readyStateChange
: function readyStateChange(state
) {
4762 this.readyState
= state
;
4764 if (typeof this.onreadystatechange
== "function") {
4766 this.onreadystatechange();
4768 sinon
.logError("Fake XHR onreadystatechange handler", e
);
4772 switch (this.readyState
) {
4773 case FakeXMLHttpRequest
.DONE
:
4774 if (supportsProgress
) {
4775 this.upload
.dispatchEvent(new sinon
.ProgressEvent("progress", {loaded
: 100, total
: 100}));
4776 this.dispatchEvent(new sinon
.ProgressEvent("progress", {loaded
: 100, total
: 100}));
4778 this.upload
.dispatchEvent(new sinon
.Event("load", false, false, this));
4779 this.dispatchEvent(new sinon
.Event("load", false, false, this));
4780 this.dispatchEvent(new sinon
.Event("loadend", false, false, this));
4784 this.dispatchEvent(new sinon
.Event("readystatechange"));
4787 setRequestHeader
: function setRequestHeader(header
, value
) {
4790 if (unsafeHeaders
[header
] || /^(Sec-|Proxy-)/.test(header
)) {
4791 throw new Error("Refused to set unsafe header \"" + header
+ "\"");
4794 if (this.requestHeaders
[header
]) {
4795 this.requestHeaders
[header
] += "," + value
;
4797 this.requestHeaders
[header
] = value
;
4802 setResponseHeaders
: function setResponseHeaders(headers
) {
4803 verifyRequestOpened(this);
4804 this.responseHeaders
= {};
4806 for (var header
in headers
) {
4807 if (headers
.hasOwnProperty(header
)) {
4808 this.responseHeaders
[header
] = headers
[header
];
4813 this.readyStateChange(FakeXMLHttpRequest
.HEADERS_RECEIVED
);
4815 this.readyState
= FakeXMLHttpRequest
.HEADERS_RECEIVED
;
4819 // Currently treats ALL data as a DOMString (i.e. no Document)
4820 send
: function send(data
) {
4823 if (!/^(get|head)$/i.test(this.method
)) {
4824 var contentType
= getHeader(this.requestHeaders
, "Content-Type");
4825 if (this.requestHeaders
[contentType
]) {
4826 var value
= this.requestHeaders
[contentType
].split(";");
4827 this.requestHeaders
[contentType
] = value
[0] + ";charset=utf-8";
4828 } else if (supportsFormData
&& !(data
instanceof FormData
)) {
4829 this.requestHeaders
["Content-Type"] = "text/plain;charset=utf-8";
4832 this.requestBody
= data
;
4835 this.errorFlag
= false;
4836 this.sendFlag
= this.async
;
4837 this.response
= this.responseType
=== "json" ? null : "";
4838 this.readyStateChange(FakeXMLHttpRequest
.OPENED
);
4840 if (typeof this.onSend
== "function") {
4844 this.dispatchEvent(new sinon
.Event("loadstart", false, false, this));
4847 abort
: function abort() {
4848 this.aborted
= true;
4849 this.responseText
= null;
4850 this.response
= this.responseType
=== "json" ? null : "";
4851 this.errorFlag
= true;
4852 this.requestHeaders
= {};
4853 this.responseHeaders
= {};
4855 if (this.readyState
> FakeXMLHttpRequest
.UNSENT
&& this.sendFlag
) {
4856 this.readyStateChange(FakeXMLHttpRequest
.DONE
);
4857 this.sendFlag
= false;
4860 this.readyState
= FakeXMLHttpRequest
.UNSENT
;
4862 this.dispatchEvent(new sinon
.Event("abort", false, false, this));
4864 this.upload
.dispatchEvent(new sinon
.Event("abort", false, false, this));
4866 if (typeof this.onerror
=== "function") {
4871 getResponseHeader
: function getResponseHeader(header
) {
4872 if (this.readyState
< FakeXMLHttpRequest
.HEADERS_RECEIVED
) {
4876 if (/^Set-Cookie2?$/i.test(header
)) {
4880 header
= getHeader(this.responseHeaders
, header
);
4882 return this.responseHeaders
[header
] || null;
4885 getAllResponseHeaders
: function getAllResponseHeaders() {
4886 if (this.readyState
< FakeXMLHttpRequest
.HEADERS_RECEIVED
) {
4892 for (var header
in this.responseHeaders
) {
4893 if (this.responseHeaders
.hasOwnProperty(header
) &&
4894 !/^Set-Cookie2?$/i.test(header
)) {
4895 headers
+= header
+ ": " + this.responseHeaders
[header
] + "\r\n";
4902 setResponseBody
: function setResponseBody(body
) {
4903 verifyRequestSent(this);
4904 verifyHeadersReceived(this);
4905 verifyResponseBodyType(body
);
4907 var chunkSize
= this.chunkSize
|| 10;
4909 this.responseText
= "";
4913 this.readyStateChange(FakeXMLHttpRequest
.LOADING
);
4916 this.responseText
+= body
.substring(index
, index
+ chunkSize
);
4918 } while (index
< body
.length
);
4920 var type
= this.getResponseHeader("Content-Type");
4922 if (this.responseText
&&
4923 (!type
|| /(text\/xml)|(application\/xml)|(\+xml)/.test(type
))) {
4925 this.responseXML
= FakeXMLHttpRequest
.parseXML(this.responseText
);
4927 // Unable to parse XML - no biggie
4931 this.response
= this.responseType
=== "json" ? JSON
.parse(this.responseText
) : this.responseText
;
4932 this.readyStateChange(FakeXMLHttpRequest
.DONE
);
4935 respond
: function respond(status
, headers
, body
) {
4936 this.status
= typeof status
== "number" ? status
: 200;
4937 this.statusText
= FakeXMLHttpRequest
.statusCodes
[this.status
];
4938 this.setResponseHeaders(headers
|| {});
4939 this.setResponseBody(body
|| "");
4942 uploadProgress
: function uploadProgress(progressEventRaw
) {
4943 if (supportsProgress
) {
4944 this.upload
.dispatchEvent(new sinon
.ProgressEvent("progress", progressEventRaw
));
4948 downloadProgress
: function downloadProgress(progressEventRaw
) {
4949 if (supportsProgress
) {
4950 this.dispatchEvent(new sinon
.ProgressEvent("progress", progressEventRaw
));
4954 uploadError
: function uploadError(error
) {
4955 if (supportsCustomEvent
) {
4956 this.upload
.dispatchEvent(new sinon
.CustomEvent("error", {detail
: error
}));
4961 sinon
.extend(FakeXMLHttpRequest
, {
4964 HEADERS_RECEIVED
: 2,
4969 sinon
.useFakeXMLHttpRequest = function () {
4970 FakeXMLHttpRequest
.restore
= function restore(keepOnCreate
) {
4971 if (sinonXhr
.supportsXHR
) {
4972 global
.XMLHttpRequest
= sinonXhr
.GlobalXMLHttpRequest
;
4975 if (sinonXhr
.supportsActiveX
) {
4976 global
.ActiveXObject
= sinonXhr
.GlobalActiveXObject
;
4979 delete FakeXMLHttpRequest
.restore
;
4981 if (keepOnCreate
!== true) {
4982 delete FakeXMLHttpRequest
.onCreate
;
4985 if (sinonXhr
.supportsXHR
) {
4986 global
.XMLHttpRequest
= FakeXMLHttpRequest
;
4989 if (sinonXhr
.supportsActiveX
) {
4990 global
.ActiveXObject
= function ActiveXObject(objId
) {
4991 if (objId
== "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId
)) {
4993 return new FakeXMLHttpRequest();
4996 return new sinonXhr
.GlobalActiveXObject(objId
);
5000 return FakeXMLHttpRequest
;
5003 sinon
.FakeXMLHttpRequest
= FakeXMLHttpRequest
;
5006 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
5007 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
5009 function loadDependencies(require
, exports
, module
) {
5010 var sinon
= require("./core");
5011 require("../extend");
5013 require("../log_error");
5015 module
.exports
= sinon
;
5019 define(loadDependencies
);
5020 } else if (isNode
) {
5021 loadDependencies(require
, module
.exports
, module
);
5022 } else if (typeof sinon
=== "undefined") {
5028 })(typeof global
!== "undefined" ? global
: self
);
5031 * @depend fake_xdomain_request.js
5032 * @depend fake_xml_http_request.js
5033 * @depend ../format.js
5034 * @depend ../log_error.js
5037 * The Sinon "server" mimics a web server that receives requests from
5038 * sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
5039 * both synchronously and asynchronously. To respond synchronuously, canned
5040 * answers have to be provided upfront.
5042 * @author Christian Johansen (christian@cjohansen.no)
5045 * Copyright (c) 2010-2013 Christian Johansen
5048 if (typeof sinon
== "undefined") {
5056 function create(proto
) {
5057 F
.prototype = proto
;
5061 function responseArray(handler
) {
5062 var response
= handler
;
5064 if (Object
.prototype.toString
.call(handler
) != "[object Array]") {
5065 response
= [200, {}, handler
];
5068 if (typeof response
[2] != "string") {
5069 throw new TypeError("Fake server response body should be string, but was " +
5070 typeof response
[2]);
5076 var wloc
= typeof window
!== "undefined" ? window
.location
: {};
5077 var rCurrLoc
= new RegExp("^" + wloc
.protocol
+ "//" + wloc
.host
);
5079 function matchOne(response
, reqMethod
, reqUrl
) {
5080 var rmeth
= response
.method
;
5081 var matchMethod
= !rmeth
|| rmeth
.toLowerCase() == reqMethod
.toLowerCase();
5082 var url
= response
.url
;
5083 var matchUrl
= !url
|| url
== reqUrl
|| (typeof url
.test
== "function" && url
.test(reqUrl
));
5085 return matchMethod
&& matchUrl
;
5088 function match(response
, request
) {
5089 var requestUrl
= request
.url
;
5091 if (!/^https?:\/\//.test(requestUrl
) || rCurrLoc
.test(requestUrl
)) {
5092 requestUrl
= requestUrl
.replace(rCurrLoc
, "");
5095 if (matchOne(response
, this.getHTTPMethod(request
), requestUrl
)) {
5096 if (typeof response
.response
== "function") {
5097 var ru
= response
.url
;
5098 var args
= [request
].concat(ru
&& typeof ru
.exec
== "function" ? ru
.exec(requestUrl
).slice(1) : []);
5099 return response
.response
.apply(response
, args
);
5108 function makeApi(sinon
) {
5109 sinon
.fakeServer
= {
5110 create: function () {
5111 var server
= create(this);
5112 if (!sinon
.xhr
.supportsCORS
) {
5113 this.xhr
= sinon
.useFakeXDomainRequest();
5115 this.xhr
= sinon
.useFakeXMLHttpRequest();
5117 server
.requests
= [];
5119 this.xhr
.onCreate = function (xhrObj
) {
5120 server
.addRequest(xhrObj
);
5126 addRequest
: function addRequest(xhrObj
) {
5128 push
.call(this.requests
, xhrObj
);
5130 xhrObj
.onSend = function () {
5131 server
.handleRequest(this);
5133 if (server
.respondImmediately
) {
5135 } else if (server
.autoRespond
&& !server
.responding
) {
5136 setTimeout(function () {
5137 server
.responding
= false;
5139 }, server
.autoRespondAfter
|| 10);
5141 server
.responding
= true;
5146 getHTTPMethod
: function getHTTPMethod(request
) {
5147 if (this.fakeHTTPMethods
&& /post/i.test(request
.method
)) {
5148 var matches
= (request
.requestBody
|| "").match(/_method=([^\b;]+)/);
5149 return !!matches
? matches
[1] : request
.method
;
5152 return request
.method
;
5155 handleRequest
: function handleRequest(xhr
) {
5161 push
.call(this.queue
, xhr
);
5163 this.processRequest(xhr
);
5167 log
: function log(response
, request
) {
5170 str
= "Request:\n" + sinon
.format(request
) + "\n\n";
5171 str
+= "Response:\n" + sinon
.format(response
) + "\n\n";
5176 respondWith
: function respondWith(method
, url
, body
) {
5177 if (arguments
.length
== 1 && typeof method
!= "function") {
5178 this.response
= responseArray(method
);
5182 if (!this.responses
) {
5183 this.responses
= [];
5186 if (arguments
.length
== 1) {
5188 url
= method
= null;
5191 if (arguments
.length
== 2) {
5197 push
.call(this.responses
, {
5200 response
: typeof body
== "function" ? body
: responseArray(body
)
5204 respond
: function respond() {
5205 if (arguments
.length
> 0) {
5206 this.respondWith
.apply(this, arguments
);
5209 var queue
= this.queue
|| [];
5210 var requests
= queue
.splice(0, queue
.length
);
5213 while (request
= requests
.shift()) {
5214 this.processRequest(request
);
5218 processRequest
: function processRequest(request
) {
5220 if (request
.aborted
) {
5224 var response
= this.response
|| [404, {}, ""];
5226 if (this.responses
) {
5227 for (var l
= this.responses
.length
, i
= l
- 1; i
>= 0; i
--) {
5228 if (match
.call(this, this.responses
[i
], request
)) {
5229 response
= this.responses
[i
].response
;
5235 if (request
.readyState
!= 4) {
5236 this.log(response
, request
);
5238 request
.respond(response
[0], response
[1], response
[2]);
5241 sinon
.logError("Fake server request processing", e
);
5245 restore
: function restore() {
5246 return this.xhr
.restore
&& this.xhr
.restore
.apply(this.xhr
, arguments
);
5251 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
5252 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
5254 function loadDependencies(require
, exports
, module
) {
5255 var sinon
= require("./core");
5256 require("./fake_xdomain_request");
5257 require("./fake_xml_http_request");
5258 require("../format");
5260 module
.exports
= sinon
;
5264 define(loadDependencies
);
5265 } else if (isNode
) {
5266 loadDependencies(require
, module
.exports
, module
);
5273 * @depend fake_server.js
5274 * @depend fake_timers.js
5277 * Add-on for sinon.fakeServer that automatically handles a fake timer along with
5278 * the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
5279 * 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
5280 * it polls the object for completion with setInterval. Dispite the direct
5281 * motivation, there is nothing jQuery-specific in this file, so it can be used
5282 * in any environment where the ajax implementation depends on setInterval or
5285 * @author Christian Johansen (christian@cjohansen.no)
5288 * Copyright (c) 2010-2013 Christian Johansen
5292 function makeApi(sinon
) {
5293 function Server() {}
5294 Server
.prototype = sinon
.fakeServer
;
5296 sinon
.fakeServerWithClock
= new Server();
5298 sinon
.fakeServerWithClock
.addRequest
= function addRequest(xhr
) {
5300 if (typeof setTimeout
.clock
== "object") {
5301 this.clock
= setTimeout
.clock
;
5303 this.clock
= sinon
.useFakeTimers();
5304 this.resetClock
= true;
5307 if (!this.longestTimeout
) {
5308 var clockSetTimeout
= this.clock
.setTimeout
;
5309 var clockSetInterval
= this.clock
.setInterval
;
5312 this.clock
.setTimeout = function (fn
, timeout
) {
5313 server
.longestTimeout
= Math
.max(timeout
, server
.longestTimeout
|| 0);
5315 return clockSetTimeout
.apply(this, arguments
);
5318 this.clock
.setInterval = function (fn
, timeout
) {
5319 server
.longestTimeout
= Math
.max(timeout
, server
.longestTimeout
|| 0);
5321 return clockSetInterval
.apply(this, arguments
);
5326 return sinon
.fakeServer
.addRequest
.call(this, xhr
);
5329 sinon
.fakeServerWithClock
.respond
= function respond() {
5330 var returnVal
= sinon
.fakeServer
.respond
.apply(this, arguments
);
5333 this.clock
.tick(this.longestTimeout
|| 0);
5334 this.longestTimeout
= 0;
5336 if (this.resetClock
) {
5337 this.clock
.restore();
5338 this.resetClock
= false;
5345 sinon
.fakeServerWithClock
.restore
= function restore() {
5347 this.clock
.restore();
5350 return sinon
.fakeServer
.restore
.apply(this, arguments
);
5354 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
5355 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
5357 function loadDependencies(require
) {
5358 var sinon
= require("./core");
5359 require("./fake_server");
5360 require("./fake_timers");
5365 define(loadDependencies
);
5366 } else if (isNode
) {
5367 loadDependencies(require
);
5374 * @depend util/core.js
5376 * @depend collection.js
5377 * @depend util/fake_timers.js
5378 * @depend util/fake_server_with_clock.js
5381 * Manages fake collections as well as fake utilities such as Sinon's
5382 * timers and fake XHR implementation in one convenient object.
5384 * @author Christian Johansen (christian@cjohansen.no)
5387 * Copyright (c) 2010-2013 Christian Johansen
5391 function makeApi(sinon
) {
5394 function exposeValue(sandbox
, config
, key
, value
) {
5399 if (config
.injectInto
&& !(key
in config
.injectInto
)) {
5400 config
.injectInto
[key
] = value
;
5401 sandbox
.injectedKeys
.push(key
);
5403 push
.call(sandbox
.args
, value
);
5407 function prepareSandboxFromConfig(config
) {
5408 var sandbox
= sinon
.create(sinon
.sandbox
);
5410 if (config
.useFakeServer
) {
5411 if (typeof config
.useFakeServer
== "object") {
5412 sandbox
.serverPrototype
= config
.useFakeServer
;
5415 sandbox
.useFakeServer();
5418 if (config
.useFakeTimers
) {
5419 if (typeof config
.useFakeTimers
== "object") {
5420 sandbox
.useFakeTimers
.apply(sandbox
, config
.useFakeTimers
);
5422 sandbox
.useFakeTimers();
5429 sinon
.sandbox
= sinon
.extend(sinon
.create(sinon
.collection
), {
5430 useFakeTimers
: function useFakeTimers() {
5431 this.clock
= sinon
.useFakeTimers
.apply(sinon
, arguments
);
5433 return this.add(this.clock
);
5436 serverPrototype
: sinon
.fakeServer
,
5438 useFakeServer
: function useFakeServer() {
5439 var proto
= this.serverPrototype
|| sinon
.fakeServer
;
5441 if (!proto
|| !proto
.create
) {
5445 this.server
= proto
.create();
5446 return this.add(this.server
);
5449 inject: function (obj
) {
5450 sinon
.collection
.inject
.call(this, obj
);
5453 obj
.clock
= this.clock
;
5457 obj
.server
= this.server
;
5458 obj
.requests
= this.server
.requests
;
5461 obj
.match
= sinon
.match
;
5466 restore: function () {
5467 sinon
.collection
.restore
.apply(this, arguments
);
5468 this.restoreContext();
5471 restoreContext: function () {
5472 if (this.injectedKeys
) {
5473 for (var i
= 0, j
= this.injectedKeys
.length
; i
< j
; i
++) {
5474 delete this.injectInto
[this.injectedKeys
[i
]];
5476 this.injectedKeys
= [];
5480 create: function (config
) {
5482 return sinon
.create(sinon
.sandbox
);
5485 var sandbox
= prepareSandboxFromConfig(config
);
5486 sandbox
.args
= sandbox
.args
|| [];
5487 sandbox
.injectedKeys
= [];
5488 sandbox
.injectInto
= config
.injectInto
;
5489 var prop
, value
, exposed
= sandbox
.inject({});
5491 if (config
.properties
) {
5492 for (var i
= 0, l
= config
.properties
.length
; i
< l
; i
++) {
5493 prop
= config
.properties
[i
];
5494 value
= exposed
[prop
] || prop
== "sandbox" && sandbox
;
5495 exposeValue(sandbox
, config
, prop
, value
);
5498 exposeValue(sandbox
, config
, "sandbox", value
);
5507 sinon
.sandbox
.useFakeXMLHttpRequest
= sinon
.sandbox
.useFakeServer
;
5509 return sinon
.sandbox
;
5512 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
5513 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
5515 function loadDependencies(require
, exports
, module
) {
5516 var sinon
= require("./util/core");
5517 require("./extend");
5518 require("./util/fake_server_with_clock");
5519 require("./util/fake_timers");
5520 require("./collection");
5521 module
.exports
= makeApi(sinon
);
5525 define(loadDependencies
);
5526 } else if (isNode
) {
5527 loadDependencies(require
, module
.exports
, module
);
5528 } else if (!sinon
) {
5536 * @depend util/core.js
5537 * @depend sandbox.js
5540 * Test function, sandboxes fakes
5542 * @author Christian Johansen (christian@cjohansen.no)
5545 * Copyright (c) 2010-2013 Christian Johansen
5549 function makeApi(sinon
) {
5550 var slice
= Array
.prototype.slice
;
5552 function test(callback
) {
5553 var type
= typeof callback
;
5555 if (type
!= "function") {
5556 throw new TypeError("sinon.test needs to wrap a test function, got " + type
);
5559 function sinonSandboxedTest() {
5560 var config
= sinon
.getConfig(sinon
.config
);
5561 config
.injectInto
= config
.injectIntoThis
&& this || config
.injectInto
;
5562 var sandbox
= sinon
.sandbox
.create(config
);
5563 var args
= slice
.call(arguments
);
5564 var oldDone
= args
.length
&& args
[args
.length
- 1];
5565 var exception
, result
;
5567 if (typeof oldDone
== "function") {
5568 args
[args
.length
- 1] = function sinonDone(result
) {
5573 sandbox
.verifyAndRestore();
5580 result
= callback
.apply(this, args
.concat(sandbox
.args
));
5585 if (typeof oldDone
!= "function") {
5586 if (typeof exception
!== "undefined") {
5590 sandbox
.verifyAndRestore();
5597 if (callback
.length
) {
5598 return function sinonAsyncSandboxedTest(callback
) {
5599 return sinonSandboxedTest
.apply(this, arguments
);
5603 return sinonSandboxedTest
;
5607 injectIntoThis
: true,
5609 properties
: ["spy", "stub", "mock", "clock", "server", "requests"],
5610 useFakeTimers
: true,
5618 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
5619 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
5621 function loadDependencies(require
, exports
, module
) {
5622 var sinon
= require("./util/core");
5623 require("./sandbox");
5624 module
.exports
= makeApi(sinon
);
5628 define(loadDependencies
);
5629 } else if (isNode
) {
5630 loadDependencies(require
, module
.exports
, module
);
5634 }(typeof sinon
== "object" && sinon
|| null));
5637 * @depend util/core.js
5641 * Test case, sandboxes all test functions
5643 * @author Christian Johansen (christian@cjohansen.no)
5646 * Copyright (c) 2010-2013 Christian Johansen
5650 function createTest(property
, setUp
, tearDown
) {
5651 return function () {
5653 setUp
.apply(this, arguments
);
5656 var exception
, result
;
5659 result
= property
.apply(this, arguments
);
5665 tearDown
.apply(this, arguments
);
5676 function makeApi(sinon
) {
5677 function testCase(tests
, prefix
) {
5678 if (!tests
|| typeof tests
!= "object") {
5679 throw new TypeError("sinon.testCase needs an object with test functions");
5682 prefix
= prefix
|| "test";
5683 var rPrefix
= new RegExp("^" + prefix
);
5684 var methods
= {}, testName
, property
, method
;
5685 var setUp
= tests
.setUp
;
5686 var tearDown
= tests
.tearDown
;
5688 for (testName
in tests
) {
5689 if (tests
.hasOwnProperty(testName
) && !/^(setUp|tearDown)$/.test(testName
)) {
5690 property
= tests
[testName
];
5692 if (typeof property
== "function" && rPrefix
.test(testName
)) {
5695 if (setUp
|| tearDown
) {
5696 method
= createTest(property
, setUp
, tearDown
);
5699 methods
[testName
] = sinon
.test(method
);
5701 methods
[testName
] = tests
[testName
];
5709 sinon
.testCase
= testCase
;
5713 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
5714 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
5716 function loadDependencies(require
, exports
, module
) {
5717 var sinon
= require("./util/core");
5719 module
.exports
= makeApi(sinon
);
5723 define(loadDependencies
);
5724 } else if (isNode
) {
5725 loadDependencies(require
, module
.exports
, module
);
5726 } else if (!sinon
) {
5731 }(typeof sinon
== "object" && sinon
|| null));
5734 * @depend times_in_words.js
5735 * @depend util/core.js
5740 * Assertions matching the test spy retrieval interface.
5742 * @author Christian Johansen (christian@cjohansen.no)
5745 * Copyright (c) 2010-2013 Christian Johansen
5748 (function (sinon
, global
) {
5749 var slice
= Array
.prototype.slice
;
5751 function makeApi(sinon
) {
5754 function verifyIsStub() {
5757 for (var i
= 0, l
= arguments
.length
; i
< l
; ++i
) {
5758 method
= arguments
[i
];
5761 assert
.fail("fake is not a spy");
5764 if (method
.proxy
&& method
.proxy
.isSinonProxy
) {
5765 verifyIsStub(method
.proxy
);
5767 if (typeof method
!= "function") {
5768 assert
.fail(method
+ " is not a function");
5771 if (typeof method
.getCall
!= "function") {
5772 assert
.fail(method
+ " is not stubbed");
5779 function failAssertion(object
, msg
) {
5780 object
= object
|| global
;
5781 var failMethod
= object
.fail
|| assert
.fail
;
5782 failMethod
.call(object
, msg
);
5785 function mirrorPropAsAssertion(name
, method
, message
) {
5786 if (arguments
.length
== 2) {
5791 assert
[name
] = function (fake
) {
5794 var args
= slice
.call(arguments
, 1);
5797 if (typeof method
== "function") {
5798 failed
= !method(fake
);
5800 failed
= typeof fake
[method
] == "function" ?
5801 !fake
[method
].apply(fake
, args
) : !fake
[method
];
5805 failAssertion(this, (fake
.printf
|| fake
.proxy
.printf
).apply(fake
, [message
].concat(args
)));
5812 function exposedName(prefix
, prop
) {
5813 return !prefix
|| /^fail/.test(prop
) ? prop
:
5814 prefix
+ prop
.slice(0, 1).toUpperCase() + prop
.slice(1);
5818 failException
: "AssertError",
5820 fail
: function fail(message
) {
5821 var error
= new Error(message
);
5822 error
.name
= this.failException
|| assert
.failException
;
5827 pass
: function pass(assertion
) {},
5829 callOrder
: function assertCallOrder() {
5830 verifyIsStub
.apply(null, arguments
);
5831 var expected
= "", actual
= "";
5833 if (!sinon
.calledInOrder(arguments
)) {
5835 expected
= [].join
.call(arguments
, ", ");
5836 var calls
= slice
.call(arguments
);
5837 var i
= calls
.length
;
5839 if (!calls
[--i
].called
) {
5843 actual
= sinon
.orderByFirstCall(calls
).join(", ");
5845 // If this fails, we'll just fall back to the blank string
5848 failAssertion(this, "expected " + expected
+ " to be " +
5849 "called in order but were called as " + actual
);
5851 assert
.pass("callOrder");
5855 callCount
: function assertCallCount(method
, count
) {
5856 verifyIsStub(method
);
5858 if (method
.callCount
!= count
) {
5859 var msg
= "expected %n to be called " + sinon
.timesInWords(count
) +
5860 " but was called %c%C";
5861 failAssertion(this, method
.printf(msg
));
5863 assert
.pass("callCount");
5867 expose
: function expose(target
, options
) {
5869 throw new TypeError("target is null or undefined");
5872 var o
= options
|| {};
5873 var prefix
= typeof o
.prefix
== "undefined" && "assert" || o
.prefix
;
5874 var includeFail
= typeof o
.includeFail
== "undefined" || !!o
.includeFail
;
5876 for (var method
in this) {
5877 if (method
!= "expose" && (includeFail
|| !/^(fail)/.test(method
))) {
5878 target
[exposedName(prefix
, method
)] = this[method
];
5885 match
: function match(actual
, expectation
) {
5886 var matcher
= sinon
.match(expectation
);
5887 if (matcher
.test(actual
)) {
5888 assert
.pass("match");
5891 "expected value to match",
5892 " expected = " + sinon
.format(expectation
),
5893 " actual = " + sinon
.format(actual
)
5895 failAssertion(this, formatted
.join("\n"));
5900 mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
5901 mirrorPropAsAssertion("notCalled", function (spy
) {
5903 }, "expected %n to not have been called but was called %c%C");
5904 mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
5905 mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
5906 mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
5907 mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
5908 mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
5909 mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
5910 mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
5911 mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
5912 mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
5913 mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
5914 mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
5915 mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
5916 mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
5917 mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
5918 mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
5919 mirrorPropAsAssertion("threw", "%n did not throw exception%C");
5920 mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
5922 sinon
.assert
= assert
;
5926 var isNode
= typeof module
!== "undefined" && module
.exports
&& typeof require
== "function";
5927 var isAMD
= typeof define
=== "function" && typeof define
.amd
=== "object" && define
.amd
;
5929 function loadDependencies(require
, exports
, module
) {
5930 var sinon
= require("./util/core");
5932 require("./format");
5933 module
.exports
= makeApi(sinon
);
5937 define(loadDependencies
);
5938 } else if (isNode
) {
5939 loadDependencies(require
, module
.exports
, module
);
5940 } else if (!sinon
) {
5946 }(typeof sinon
== "object" && sinon
|| null, typeof window
!= "undefined" ? window
: (typeof self
!= "undefined") ? self
: global
));