transponder 0.0.5 → 0.0.6

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