ember-data-factory-guy 0.7.5 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4287 +0,0 @@
1
- /**
2
- * Sinon.JS 1.6.0, 2013/02/18
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
- var sinon = (function () {
37
- "use strict";
38
-
39
- var buster = (function (setTimeout, B) {
40
- var isNode = typeof require == "function" && typeof module == "object";
41
- var div = typeof document != "undefined" && document.createElement("div");
42
- var F = function () {
43
- };
44
-
45
- var buster = {
46
- bind: function bind(obj, methOrProp) {
47
- var method = typeof methOrProp == "string" ? obj[methOrProp] : methOrProp;
48
- var args = Array.prototype.slice.call(arguments, 2);
49
- return function () {
50
- var allArgs = args.concat(Array.prototype.slice.call(arguments));
51
- return method.apply(obj, allArgs);
52
- };
53
- },
54
-
55
- partial: function partial(fn) {
56
- var args = [].slice.call(arguments, 1);
57
- return function () {
58
- return fn.apply(this, args.concat([].slice.call(arguments)));
59
- };
60
- },
61
-
62
- create: function create(object) {
63
- F.prototype = object;
64
- return new F();
65
- },
66
-
67
- extend: function extend(target) {
68
- if (!target) {
69
- return;
70
- }
71
- for (var i = 1, l = arguments.length, prop; i < l; ++i) {
72
- for (prop in arguments[i]) {
73
- target[prop] = arguments[i][prop];
74
- }
75
- }
76
- return target;
77
- },
78
-
79
- nextTick: function nextTick(callback) {
80
- if (typeof process != "undefined" && process.nextTick) {
81
- return process.nextTick(callback);
82
- }
83
- setTimeout(callback, 0);
84
- },
85
-
86
- functionName: function functionName(func) {
87
- if (!func) return "";
88
- if (func.displayName) return func.displayName;
89
- if (func.name) return func.name;
90
- var matches = func.toString().match(/function\s+([^\(]+)/m);
91
- return matches && matches[1] || "";
92
- },
93
-
94
- isNode: function isNode(obj) {
95
- if (!div) return false;
96
- try {
97
- obj.appendChild(div);
98
- obj.removeChild(div);
99
- } catch (e) {
100
- return false;
101
- }
102
- return true;
103
- },
104
-
105
- isElement: function isElement(obj) {
106
- return obj && obj.nodeType === 1 && buster.isNode(obj);
107
- },
108
-
109
- isArray: function isArray(arr) {
110
- return Object.prototype.toString.call(arr) == "[object Array]";
111
- },
112
-
113
- flatten: function flatten(arr) {
114
- var result = [], arr = arr || [];
115
- for (var i = 0, l = arr.length; i < l; ++i) {
116
- result = result.concat(buster.isArray(arr[i]) ? flatten(arr[i]) : arr[i]);
117
- }
118
- return result;
119
- },
120
-
121
- each: function each(arr, callback) {
122
- for (var i = 0, l = arr.length; i < l; ++i) {
123
- callback(arr[i]);
124
- }
125
- },
126
-
127
- map: function map(arr, callback) {
128
- var results = [];
129
- for (var i = 0, l = arr.length; i < l; ++i) {
130
- results.push(callback(arr[i]));
131
- }
132
- return results;
133
- },
134
-
135
- parallel: function parallel(fns, callback) {
136
- function cb(err, res) {
137
- if (typeof callback == "function") {
138
- callback(err, res);
139
- callback = null;
140
- }
141
- }
142
-
143
- if (fns.length == 0) {
144
- return cb(null, []);
145
- }
146
- var remaining = fns.length, results = [];
147
-
148
- function makeDone(num) {
149
- return function done(err, result) {
150
- if (err) {
151
- return cb(err);
152
- }
153
- results[num] = result;
154
- if (--remaining == 0) {
155
- cb(null, results);
156
- }
157
- };
158
- }
159
-
160
- for (var i = 0, l = fns.length; i < l; ++i) {
161
- fns[i](makeDone(i));
162
- }
163
- },
164
-
165
- series: function series(fns, callback) {
166
- function cb(err, res) {
167
- if (typeof callback == "function") {
168
- callback(err, res);
169
- }
170
- }
171
-
172
- var remaining = fns.slice();
173
- var results = [];
174
-
175
- function callNext() {
176
- if (remaining.length == 0) return cb(null, results);
177
- var promise = remaining.shift()(next);
178
- if (promise && typeof promise.then == "function") {
179
- promise.then(buster.partial(next, null), next);
180
- }
181
- }
182
-
183
- function next(err, result) {
184
- if (err) return cb(err);
185
- results.push(result);
186
- callNext();
187
- }
188
-
189
- callNext();
190
- },
191
-
192
- countdown: function countdown(num, done) {
193
- return function () {
194
- if (--num == 0) done();
195
- };
196
- }
197
- };
198
-
199
- if (typeof process === "object" &&
200
- typeof require === "function" && typeof module === "object") {
201
- var crypto = require("crypto");
202
- var path = require("path");
203
-
204
- buster.tmpFile = function (fileName) {
205
- var hashed = crypto.createHash("sha1");
206
- hashed.update(fileName);
207
- var tmpfileName = hashed.digest("hex");
208
-
209
- if (process.platform == "win32") {
210
- return path.join(process.env["TEMP"], tmpfileName);
211
- } else {
212
- return path.join("/tmp", tmpfileName);
213
- }
214
- };
215
- }
216
-
217
- if (Array.prototype.some) {
218
- buster.some = function (arr, fn, thisp) {
219
- return arr.some(fn, thisp);
220
- };
221
- } else {
222
- // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
223
- buster.some = function (arr, fun, thisp) {
224
- if (arr == null) {
225
- throw new TypeError();
226
- }
227
- arr = Object(arr);
228
- var len = arr.length >>> 0;
229
- if (typeof fun !== "function") {
230
- throw new TypeError();
231
- }
232
-
233
- for (var i = 0; i < len; i++) {
234
- if (arr.hasOwnProperty(i) && fun.call(thisp, arr[i], i, arr)) {
235
- return true;
236
- }
237
- }
238
-
239
- return false;
240
- };
241
- }
242
-
243
- if (Array.prototype.filter) {
244
- buster.filter = function (arr, fn, thisp) {
245
- return arr.filter(fn, thisp);
246
- };
247
- } else {
248
- // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
249
- buster.filter = function (fn, thisp) {
250
- if (this == null) {
251
- throw new TypeError();
252
- }
253
-
254
- var t = Object(this);
255
- var len = t.length >>> 0;
256
- if (typeof fn != "function") {
257
- throw new TypeError();
258
- }
259
-
260
- var res = [];
261
- for (var i = 0; i < len; i++) {
262
- if (i in t) {
263
- var val = t[i]; // in case fun mutates this
264
- if (fn.call(thisp, val, i, t)) {
265
- res.push(val);
266
- }
267
- }
268
- }
269
-
270
- return res;
271
- };
272
- }
273
-
274
- if (isNode) {
275
- module.exports = buster;
276
- buster.eventEmitter = require("./buster-event-emitter");
277
- Object.defineProperty(buster, "defineVersionGetter", {
278
- get: function () {
279
- return require("./define-version-getter");
280
- }
281
- });
282
- }
283
-
284
- return buster.extend(B || {}, buster);
285
- }(setTimeout, buster));
286
- if (typeof buster === "undefined") {
287
- var buster = {};
288
- }
289
-
290
- if (typeof module === "object" && typeof require === "function") {
291
- buster = require("buster-core");
292
- }
293
-
294
- buster.format = buster.format || {};
295
- buster.format.excludeConstructors = ["Object", /^.$/];
296
- buster.format.quoteStrings = true;
297
-
298
- buster.format.ascii = (function () {
299
-
300
- var hasOwn = Object.prototype.hasOwnProperty;
301
-
302
- var specialObjects = [];
303
- if (typeof global != "undefined") {
304
- specialObjects.push({ obj: global, value: "[object global]" });
305
- }
306
- if (typeof document != "undefined") {
307
- specialObjects.push({ obj: document, value: "[object HTMLDocument]" });
308
- }
309
- if (typeof window != "undefined") {
310
- specialObjects.push({ obj: window, value: "[object Window]" });
311
- }
312
-
313
- function keys(object) {
314
- var k = Object.keys && Object.keys(object) || [];
315
-
316
- if (k.length == 0) {
317
- for (var prop in object) {
318
- if (hasOwn.call(object, prop)) {
319
- k.push(prop);
320
- }
321
- }
322
- }
323
-
324
- return k.sort();
325
- }
326
-
327
- function isCircular(object, objects) {
328
- if (typeof object != "object") {
329
- return false;
330
- }
331
-
332
- for (var i = 0, l = objects.length; i < l; ++i) {
333
- if (objects[i] === object) {
334
- return true;
335
- }
336
- }
337
-
338
- return false;
339
- }
340
-
341
- function ascii(object, processed, indent) {
342
- if (typeof object == "string") {
343
- var quote = typeof this.quoteStrings != "boolean" || this.quoteStrings;
344
- return processed || quote ? '"' + object + '"' : object;
345
- }
346
-
347
- if (typeof object == "function" && !(object instanceof RegExp)) {
348
- return ascii.func(object);
349
- }
350
-
351
- processed = processed || [];
352
-
353
- if (isCircular(object, processed)) {
354
- return "[Circular]";
355
- }
356
-
357
- if (Object.prototype.toString.call(object) == "[object Array]") {
358
- return ascii.array.call(this, object, processed);
359
- }
360
-
361
- if (!object) {
362
- return "" + object;
363
- }
364
-
365
- if (buster.isElement(object)) {
366
- return ascii.element(object);
367
- }
368
-
369
- if (typeof object.toString == "function" &&
370
- object.toString !== Object.prototype.toString) {
371
- return object.toString();
372
- }
373
-
374
- for (var i = 0, l = specialObjects.length; i < l; i++) {
375
- if (object === specialObjects[i].obj) {
376
- return specialObjects[i].value;
377
- }
378
- }
379
-
380
- return ascii.object.call(this, object, processed, indent);
381
- }
382
-
383
- ascii.func = function (func) {
384
- return "function " + buster.functionName(func) + "() {}";
385
- };
386
-
387
- ascii.array = function (array, processed) {
388
- processed = processed || [];
389
- processed.push(array);
390
- var pieces = [];
391
-
392
- for (var i = 0, l = array.length; i < l; ++i) {
393
- pieces.push(ascii.call(this, array[i], processed));
394
- }
395
-
396
- return "[" + pieces.join(", ") + "]";
397
- };
398
-
399
- ascii.object = function (object, processed, indent) {
400
- processed = processed || [];
401
- processed.push(object);
402
- indent = indent || 0;
403
- var pieces = [], properties = keys(object), prop, str, obj;
404
- var is = "";
405
- var length = 3;
406
-
407
- for (var i = 0, l = indent; i < l; ++i) {
408
- is += " ";
409
- }
410
-
411
- for (i = 0, l = properties.length; i < l; ++i) {
412
- prop = properties[i];
413
- obj = object[prop];
414
-
415
- if (isCircular(obj, processed)) {
416
- str = "[Circular]";
417
- } else {
418
- str = ascii.call(this, obj, processed, indent + 2);
419
- }
420
-
421
- str = (/\s/.test(prop) ? '"' + prop + '"' : prop) + ": " + str;
422
- length += str.length;
423
- pieces.push(str);
424
- }
425
-
426
- var cons = ascii.constructorName.call(this, object);
427
- var prefix = cons ? "[" + cons + "] " : ""
428
-
429
- return (length + indent) > 80 ?
430
- prefix + "{\n " + is + pieces.join(",\n " + is) + "\n" + is + "}" :
431
- prefix + "{ " + pieces.join(", ") + " }";
432
- };
433
-
434
- ascii.element = function (element) {
435
- var tagName = element.tagName.toLowerCase();
436
- var attrs = element.attributes, attribute, pairs = [], attrName;
437
-
438
- for (var i = 0, l = attrs.length; i < l; ++i) {
439
- attribute = attrs.item(i);
440
- attrName = attribute.nodeName.toLowerCase().replace("html:", "");
441
-
442
- if (attrName == "contenteditable" && attribute.nodeValue == "inherit") {
443
- continue;
444
- }
445
-
446
- if (!!attribute.nodeValue) {
447
- pairs.push(attrName + "=\"" + attribute.nodeValue + "\"");
448
- }
449
- }
450
-
451
- var formatted = "<" + tagName + (pairs.length > 0 ? " " : "");
452
- var content = element.innerHTML;
453
-
454
- if (content.length > 20) {
455
- content = content.substr(0, 20) + "[...]";
456
- }
457
-
458
- var res = formatted + pairs.join(" ") + ">" + content + "</" + tagName + ">";
459
-
460
- return res.replace(/ contentEditable="inherit"/, "");
461
- };
462
-
463
- ascii.constructorName = function (object) {
464
- var name = buster.functionName(object && object.constructor);
465
- var excludes = this.excludeConstructors || buster.format.excludeConstructors || [];
466
-
467
- for (var i = 0, l = excludes.length; i < l; ++i) {
468
- if (typeof excludes[i] == "string" && excludes[i] == name) {
469
- return "";
470
- } else if (excludes[i].test && excludes[i].test(name)) {
471
- return "";
472
- }
473
- }
474
-
475
- return name;
476
- };
477
-
478
- return ascii;
479
- }());
480
-
481
- if (typeof module != "undefined") {
482
- module.exports = buster.format;
483
- }
484
- /*jslint eqeqeq: false, onevar: false, forin: true, nomen: false, regexp: false, plusplus: false*/
485
- /*global module, require, __dirname, document*/
486
- /**
487
- * Sinon core utilities. For internal use only.
488
- *
489
- * @author Christian Johansen (christian@cjohansen.no)
490
- * @license BSD
491
- *
492
- * Copyright (c) 2010-2013 Christian Johansen
493
- */
494
-
495
- var sinon = (function (buster) {
496
- var div = typeof document != "undefined" && document.createElement("div");
497
- var hasOwn = Object.prototype.hasOwnProperty;
498
-
499
- function isDOMNode(obj) {
500
- var success = false;
501
-
502
- try {
503
- obj.appendChild(div);
504
- success = div.parentNode == obj;
505
- } catch (e) {
506
- return false;
507
- } finally {
508
- try {
509
- obj.removeChild(div);
510
- } catch (e) {
511
- // Remove failed, not much we can do about that
512
- }
513
- }
514
-
515
- return success;
516
- }
517
-
518
- function isElement(obj) {
519
- return div && obj && obj.nodeType === 1 && isDOMNode(obj);
520
- }
521
-
522
- function isFunction(obj) {
523
- return typeof obj === "function" || !!(obj && obj.constructor && obj.call && obj.apply);
524
- }
525
-
526
- function mirrorProperties(target, source) {
527
- for (var prop in source) {
528
- if (!hasOwn.call(target, prop)) {
529
- target[prop] = source[prop];
530
- }
531
- }
532
- }
533
-
534
- var sinon = {
535
- wrapMethod: function wrapMethod(object, property, method) {
536
- if (!object) {
537
- throw new TypeError("Should wrap property of object");
538
- }
539
-
540
- if (typeof method != "function") {
541
- throw new TypeError("Method wrapper should be function");
542
- }
543
-
544
- var wrappedMethod = object[property];
545
-
546
- if (!isFunction(wrappedMethod)) {
547
- throw new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
548
- property + " as function");
549
- }
550
-
551
- if (wrappedMethod.restore && wrappedMethod.restore.sinon) {
552
- throw new TypeError("Attempted to wrap " + property + " which is already wrapped");
553
- }
554
-
555
- if (wrappedMethod.calledBefore) {
556
- var verb = !!wrappedMethod.returns ? "stubbed" : "spied on";
557
- throw new TypeError("Attempted to wrap " + property + " which is already " + verb);
558
- }
559
-
560
- // IE 8 does not support hasOwnProperty on the window object.
561
- var owned = hasOwn.call(object, property);
562
- object[property] = method;
563
- method.displayName = property;
564
-
565
- method.restore = function () {
566
- // For prototype properties try to reset by delete first.
567
- // If this fails (ex: localStorage on mobile safari) then force a reset
568
- // via direct assignment.
569
- if (!owned) {
570
- delete object[property];
571
- }
572
- if (object[property] === method) {
573
- object[property] = wrappedMethod;
574
- }
575
- };
576
-
577
- method.restore.sinon = true;
578
- mirrorProperties(method, wrappedMethod);
579
-
580
- return method;
581
- },
582
-
583
- extend: function extend(target) {
584
- for (var i = 1, l = arguments.length; i < l; i += 1) {
585
- for (var prop in arguments[i]) {
586
- if (arguments[i].hasOwnProperty(prop)) {
587
- target[prop] = arguments[i][prop];
588
- }
589
-
590
- // DONT ENUM bug, only care about toString
591
- if (arguments[i].hasOwnProperty("toString") &&
592
- arguments[i].toString != target.toString) {
593
- target.toString = arguments[i].toString;
594
- }
595
- }
596
- }
597
-
598
- return target;
599
- },
600
-
601
- create: function create(proto) {
602
- var F = function () {
603
- };
604
- F.prototype = proto;
605
- return new F();
606
- },
607
-
608
- deepEqual: function deepEqual(a, b) {
609
- if (sinon.match && sinon.match.isMatcher(a)) {
610
- return a.test(b);
611
- }
612
- if (typeof a != "object" || typeof b != "object") {
613
- return a === b;
614
- }
615
-
616
- if (isElement(a) || isElement(b)) {
617
- return a === b;
618
- }
619
-
620
- if (a === b) {
621
- return true;
622
- }
623
-
624
- if ((a === null && b !== null) || (a !== null && b === null)) {
625
- return false;
626
- }
627
-
628
- var aString = Object.prototype.toString.call(a);
629
- if (aString != Object.prototype.toString.call(b)) {
630
- return false;
631
- }
632
-
633
- if (aString == "[object Array]") {
634
- if (a.length !== b.length) {
635
- return false;
636
- }
637
-
638
- for (var i = 0, l = a.length; i < l; i += 1) {
639
- if (!deepEqual(a[i], b[i])) {
640
- return false;
641
- }
642
- }
643
-
644
- return true;
645
- }
646
-
647
- var prop, aLength = 0, bLength = 0;
648
-
649
- for (prop in a) {
650
- aLength += 1;
651
-
652
- if (!deepEqual(a[prop], b[prop])) {
653
- return false;
654
- }
655
- }
656
-
657
- for (prop in b) {
658
- bLength += 1;
659
- }
660
-
661
- if (aLength != bLength) {
662
- return false;
663
- }
664
-
665
- return true;
666
- },
667
-
668
- functionName: function functionName(func) {
669
- var name = func.displayName || func.name;
670
-
671
- // Use function decomposition as a last resort to get function
672
- // name. Does not rely on function decomposition to work - if it
673
- // doesn't debugging will be slightly less informative
674
- // (i.e. toString will say 'spy' rather than 'myFunc').
675
- if (!name) {
676
- var matches = func.toString().match(/function ([^\s\(]+)/);
677
- name = matches && matches[1];
678
- }
679
-
680
- return name;
681
- },
682
-
683
- functionToString: function toString() {
684
- if (this.getCall && this.callCount) {
685
- var thisValue, prop, i = this.callCount;
686
-
687
- while (i--) {
688
- thisValue = this.getCall(i).thisValue;
689
-
690
- for (prop in thisValue) {
691
- if (thisValue[prop] === this) {
692
- return prop;
693
- }
694
- }
695
- }
696
- }
697
-
698
- return this.displayName || "sinon fake";
699
- },
700
-
701
- getConfig: function (custom) {
702
- var config = {};
703
- custom = custom || {};
704
- var defaults = sinon.defaultConfig;
705
-
706
- for (var prop in defaults) {
707
- if (defaults.hasOwnProperty(prop)) {
708
- config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaults[prop];
709
- }
710
- }
711
-
712
- return config;
713
- },
714
-
715
- format: function (val) {
716
- return "" + val;
717
- },
718
-
719
- defaultConfig: {
720
- injectIntoThis: true,
721
- injectInto: null,
722
- properties: ["spy", "stub", "mock", "clock", "server", "requests"],
723
- useFakeTimers: true,
724
- useFakeServer: true
725
- },
726
-
727
- timesInWords: function timesInWords(count) {
728
- return count == 1 && "once" ||
729
- count == 2 && "twice" ||
730
- count == 3 && "thrice" ||
731
- (count || 0) + " times";
732
- },
733
-
734
- calledInOrder: function (spies) {
735
- for (var i = 1, l = spies.length; i < l; i++) {
736
- if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) {
737
- return false;
738
- }
739
- }
740
-
741
- return true;
742
- },
743
-
744
- orderByFirstCall: function (spies) {
745
- return spies.sort(function (a, b) {
746
- // uuid, won't ever be equal
747
- var aCall = a.getCall(0);
748
- var bCall = b.getCall(0);
749
- var aId = aCall && aCall.callId || -1;
750
- var bId = bCall && bCall.callId || -1;
751
-
752
- return aId < bId ? -1 : 1;
753
- });
754
- },
755
-
756
- log: function () {
757
- },
758
-
759
- logError: function (label, err) {
760
- var msg = label + " threw exception: "
761
- sinon.log(msg + "[" + err.name + "] " + err.message);
762
- if (err.stack) {
763
- sinon.log(err.stack);
764
- }
765
-
766
- setTimeout(function () {
767
- err.message = msg + err.message;
768
- throw err;
769
- }, 0);
770
- },
771
-
772
- typeOf: function (value) {
773
- if (value === null) {
774
- return "null";
775
- }
776
- else if (value === undefined) {
777
- return "undefined";
778
- }
779
- var string = Object.prototype.toString.call(value);
780
- return string.substring(8, string.length - 1).toLowerCase();
781
- },
782
-
783
- createStubInstance: function (constructor) {
784
- if (typeof constructor !== "function") {
785
- throw new TypeError("The constructor should be a function.");
786
- }
787
- return sinon.stub(sinon.create(constructor.prototype));
788
- }
789
- };
790
-
791
- var isNode = typeof module == "object" && typeof require == "function";
792
-
793
- if (isNode) {
794
- try {
795
- buster = { format: require("buster-format") };
796
- } catch (e) {
797
- }
798
- module.exports = sinon;
799
- module.exports.spy = require("./sinon/spy");
800
- module.exports.stub = require("./sinon/stub");
801
- module.exports.mock = require("./sinon/mock");
802
- module.exports.collection = require("./sinon/collection");
803
- module.exports.assert = require("./sinon/assert");
804
- module.exports.sandbox = require("./sinon/sandbox");
805
- module.exports.test = require("./sinon/test");
806
- module.exports.testCase = require("./sinon/test_case");
807
- module.exports.assert = require("./sinon/assert");
808
- module.exports.match = require("./sinon/match");
809
- }
810
-
811
- if (buster) {
812
- var formatter = sinon.create(buster.format);
813
- formatter.quoteStrings = false;
814
- sinon.format = function () {
815
- return formatter.ascii.apply(formatter, arguments);
816
- };
817
- } else if (isNode) {
818
- try {
819
- var util = require("util");
820
- sinon.format = function (value) {
821
- return typeof value == "object" && value.toString === Object.prototype.toString ? util.inspect(value) : value;
822
- };
823
- } catch (e) {
824
- /* Node, but no util module - would be very old, but better safe than
825
- sorry */
826
- }
827
- }
828
-
829
- return sinon;
830
- }(typeof buster == "object" && buster));
831
-
832
- /* @depend ../sinon.js */
833
- /*jslint eqeqeq: false, onevar: false, plusplus: false*/
834
- /*global module, require, sinon*/
835
- /**
836
- * Match functions
837
- *
838
- * @author Maximilian Antoni (mail@maxantoni.de)
839
- * @license BSD
840
- *
841
- * Copyright (c) 2012 Maximilian Antoni
842
- */
843
-
844
- (function (sinon) {
845
- var commonJSModule = typeof module == "object" && typeof require == "function";
846
-
847
- if (!sinon && commonJSModule) {
848
- sinon = require("../sinon");
849
- }
850
-
851
- if (!sinon) {
852
- return;
853
- }
854
-
855
- function assertType(value, type, name) {
856
- var actual = sinon.typeOf(value);
857
- if (actual !== type) {
858
- throw new TypeError("Expected type of " + name + " to be " +
859
- type + ", but was " + actual);
860
- }
861
- }
862
-
863
- var matcher = {
864
- toString: function () {
865
- return this.message;
866
- }
867
- };
868
-
869
- function isMatcher(object) {
870
- return matcher.isPrototypeOf(object);
871
- }
872
-
873
- function matchObject(expectation, actual) {
874
- if (actual === null || actual === undefined) {
875
- return false;
876
- }
877
- for (var key in expectation) {
878
- if (expectation.hasOwnProperty(key)) {
879
- var exp = expectation[key];
880
- var act = actual[key];
881
- if (match.isMatcher(exp)) {
882
- if (!exp.test(act)) {
883
- return false;
884
- }
885
- } else if (sinon.typeOf(exp) === "object") {
886
- if (!matchObject(exp, act)) {
887
- return false;
888
- }
889
- } else if (!sinon.deepEqual(exp, act)) {
890
- return false;
891
- }
892
- }
893
- }
894
- return true;
895
- }
896
-
897
- matcher.or = function (m2) {
898
- if (!isMatcher(m2)) {
899
- throw new TypeError("Matcher expected");
900
- }
901
- var m1 = this;
902
- var or = sinon.create(matcher);
903
- or.test = function (actual) {
904
- return m1.test(actual) || m2.test(actual);
905
- };
906
- or.message = m1.message + ".or(" + m2.message + ")";
907
- return or;
908
- };
909
-
910
- matcher.and = function (m2) {
911
- if (!isMatcher(m2)) {
912
- throw new TypeError("Matcher expected");
913
- }
914
- var m1 = this;
915
- var and = sinon.create(matcher);
916
- and.test = function (actual) {
917
- return m1.test(actual) && m2.test(actual);
918
- };
919
- and.message = m1.message + ".and(" + m2.message + ")";
920
- return and;
921
- };
922
-
923
- var match = function (expectation, message) {
924
- var m = sinon.create(matcher);
925
- var type = sinon.typeOf(expectation);
926
- switch (type) {
927
- case "object":
928
- if (typeof expectation.test === "function") {
929
- m.test = function (actual) {
930
- return expectation.test(actual) === true;
931
- };
932
- m.message = "match(" + sinon.functionName(expectation.test) + ")";
933
- return m;
934
- }
935
- var str = [];
936
- for (var key in expectation) {
937
- if (expectation.hasOwnProperty(key)) {
938
- str.push(key + ": " + expectation[key]);
939
- }
940
- }
941
- m.test = function (actual) {
942
- return matchObject(expectation, actual);
943
- };
944
- m.message = "match(" + str.join(", ") + ")";
945
- break;
946
- case "number":
947
- m.test = function (actual) {
948
- return expectation == actual;
949
- };
950
- break;
951
- case "string":
952
- m.test = function (actual) {
953
- if (typeof actual !== "string") {
954
- return false;
955
- }
956
- return actual.indexOf(expectation) !== -1;
957
- };
958
- m.message = "match(\"" + expectation + "\")";
959
- break;
960
- case "regexp":
961
- m.test = function (actual) {
962
- if (typeof actual !== "string") {
963
- return false;
964
- }
965
- return expectation.test(actual);
966
- };
967
- break;
968
- case "function":
969
- m.test = expectation;
970
- if (message) {
971
- m.message = message;
972
- } else {
973
- m.message = "match(" + sinon.functionName(expectation) + ")";
974
- }
975
- break;
976
- default:
977
- m.test = function (actual) {
978
- return sinon.deepEqual(expectation, actual);
979
- };
980
- }
981
- if (!m.message) {
982
- m.message = "match(" + expectation + ")";
983
- }
984
- return m;
985
- };
986
-
987
- match.isMatcher = isMatcher;
988
-
989
- match.any = match(function () {
990
- return true;
991
- }, "any");
992
-
993
- match.defined = match(function (actual) {
994
- return actual !== null && actual !== undefined;
995
- }, "defined");
996
-
997
- match.truthy = match(function (actual) {
998
- return !!actual;
999
- }, "truthy");
1000
-
1001
- match.falsy = match(function (actual) {
1002
- return !actual;
1003
- }, "falsy");
1004
-
1005
- match.same = function (expectation) {
1006
- return match(function (actual) {
1007
- return expectation === actual;
1008
- }, "same(" + expectation + ")");
1009
- };
1010
-
1011
- match.typeOf = function (type) {
1012
- assertType(type, "string", "type");
1013
- return match(function (actual) {
1014
- return sinon.typeOf(actual) === type;
1015
- }, "typeOf(\"" + type + "\")");
1016
- };
1017
-
1018
- match.instanceOf = function (type) {
1019
- assertType(type, "function", "type");
1020
- return match(function (actual) {
1021
- return actual instanceof type;
1022
- }, "instanceOf(" + sinon.functionName(type) + ")");
1023
- };
1024
-
1025
- function createPropertyMatcher(propertyTest, messagePrefix) {
1026
- return function (property, value) {
1027
- assertType(property, "string", "property");
1028
- var onlyProperty = arguments.length === 1;
1029
- var message = messagePrefix + "(\"" + property + "\"";
1030
- if (!onlyProperty) {
1031
- message += ", " + value;
1032
- }
1033
- message += ")";
1034
- return match(function (actual) {
1035
- if (actual === undefined || actual === null ||
1036
- !propertyTest(actual, property)) {
1037
- return false;
1038
- }
1039
- return onlyProperty || sinon.deepEqual(value, actual[property]);
1040
- }, message);
1041
- };
1042
- }
1043
-
1044
- match.has = createPropertyMatcher(function (actual, property) {
1045
- if (typeof actual === "object") {
1046
- return property in actual;
1047
- }
1048
- return actual[property] !== undefined;
1049
- }, "has");
1050
-
1051
- match.hasOwn = createPropertyMatcher(function (actual, property) {
1052
- return actual.hasOwnProperty(property);
1053
- }, "hasOwn");
1054
-
1055
- match.bool = match.typeOf("boolean");
1056
- match.number = match.typeOf("number");
1057
- match.string = match.typeOf("string");
1058
- match.object = match.typeOf("object");
1059
- match.func = match.typeOf("function");
1060
- match.array = match.typeOf("array");
1061
- match.regexp = match.typeOf("regexp");
1062
- match.date = match.typeOf("date");
1063
-
1064
- if (commonJSModule) {
1065
- module.exports = match;
1066
- } else {
1067
- sinon.match = match;
1068
- }
1069
- }(typeof sinon == "object" && sinon || null));
1070
-
1071
- /**
1072
- * @depend ../sinon.js
1073
- * @depend match.js
1074
- */
1075
- /*jslint eqeqeq: false, onevar: false, plusplus: false*/
1076
- /*global module, require, sinon*/
1077
- /**
1078
- * Spy functions
1079
- *
1080
- * @author Christian Johansen (christian@cjohansen.no)
1081
- * @license BSD
1082
- *
1083
- * Copyright (c) 2010-2013 Christian Johansen
1084
- */
1085
-
1086
- (function (sinon) {
1087
- var commonJSModule = typeof module == "object" && typeof require == "function";
1088
- var spyCall;
1089
- var callId = 0;
1090
- var push = [].push;
1091
- var slice = Array.prototype.slice;
1092
-
1093
- if (!sinon && commonJSModule) {
1094
- sinon = require("../sinon");
1095
- }
1096
-
1097
- if (!sinon) {
1098
- return;
1099
- }
1100
-
1101
- function spy(object, property) {
1102
- if (!property && typeof object == "function") {
1103
- return spy.create(object);
1104
- }
1105
-
1106
- if (!object && !property) {
1107
- return spy.create(function () {
1108
- });
1109
- }
1110
-
1111
- var method = object[property];
1112
- return sinon.wrapMethod(object, property, spy.create(method));
1113
- }
1114
-
1115
- sinon.extend(spy, (function () {
1116
-
1117
- function delegateToCalls(api, method, matchAny, actual, notCalled) {
1118
- api[method] = function () {
1119
- if (!this.called) {
1120
- if (notCalled) {
1121
- return notCalled.apply(this, arguments);
1122
- }
1123
- return false;
1124
- }
1125
-
1126
- var currentCall;
1127
- var matches = 0;
1128
-
1129
- for (var i = 0, l = this.callCount; i < l; i += 1) {
1130
- currentCall = this.getCall(i);
1131
-
1132
- if (currentCall[actual || method].apply(currentCall, arguments)) {
1133
- matches += 1;
1134
-
1135
- if (matchAny) {
1136
- return true;
1137
- }
1138
- }
1139
- }
1140
-
1141
- return matches === this.callCount;
1142
- };
1143
- }
1144
-
1145
- function matchingFake(fakes, args, strict) {
1146
- if (!fakes) {
1147
- return;
1148
- }
1149
-
1150
- var alen = args.length;
1151
-
1152
- for (var i = 0, l = fakes.length; i < l; i++) {
1153
- if (fakes[i].matches(args, strict)) {
1154
- return fakes[i];
1155
- }
1156
- }
1157
- }
1158
-
1159
- function incrementCallCount() {
1160
- this.called = true;
1161
- this.callCount += 1;
1162
- this.notCalled = false;
1163
- this.calledOnce = this.callCount == 1;
1164
- this.calledTwice = this.callCount == 2;
1165
- this.calledThrice = this.callCount == 3;
1166
- }
1167
-
1168
- function createCallProperties() {
1169
- this.firstCall = this.getCall(0);
1170
- this.secondCall = this.getCall(1);
1171
- this.thirdCall = this.getCall(2);
1172
- this.lastCall = this.getCall(this.callCount - 1);
1173
- }
1174
-
1175
- var vars = "a,b,c,d,e,f,g,h,i,j,k,l";
1176
-
1177
- function createProxy(func) {
1178
- // Retain the function length:
1179
- var p;
1180
- if (func.length) {
1181
- eval("p = (function proxy(" + vars.substring(0, func.length * 2 - 1) +
1182
- ") { return p.invoke(func, this, slice.call(arguments)); });");
1183
- }
1184
- else {
1185
- p = function proxy() {
1186
- return p.invoke(func, this, slice.call(arguments));
1187
- };
1188
- }
1189
- return p;
1190
- }
1191
-
1192
- var uuid = 0;
1193
-
1194
- // Public API
1195
- var spyApi = {
1196
- reset: function () {
1197
- this.called = false;
1198
- this.notCalled = true;
1199
- this.calledOnce = false;
1200
- this.calledTwice = false;
1201
- this.calledThrice = false;
1202
- this.callCount = 0;
1203
- this.firstCall = null;
1204
- this.secondCall = null;
1205
- this.thirdCall = null;
1206
- this.lastCall = null;
1207
- this.args = [];
1208
- this.returnValues = [];
1209
- this.thisValues = [];
1210
- this.exceptions = [];
1211
- this.callIds = [];
1212
- if (this.fakes) {
1213
- for (var i = 0; i < this.fakes.length; i++) {
1214
- this.fakes[i].reset();
1215
- }
1216
- }
1217
- },
1218
-
1219
- create: function create(func) {
1220
- var name;
1221
-
1222
- if (typeof func != "function") {
1223
- func = function () {
1224
- };
1225
- } else {
1226
- name = sinon.functionName(func);
1227
- }
1228
-
1229
- var proxy = createProxy(func);
1230
-
1231
- sinon.extend(proxy, spy);
1232
- delete proxy.create;
1233
- sinon.extend(proxy, func);
1234
-
1235
- proxy.reset();
1236
- proxy.prototype = func.prototype;
1237
- proxy.displayName = name || "spy";
1238
- proxy.toString = sinon.functionToString;
1239
- proxy._create = sinon.spy.create;
1240
- proxy.id = "spy#" + uuid++;
1241
-
1242
- return proxy;
1243
- },
1244
-
1245
- invoke: function invoke(func, thisValue, args) {
1246
- var matching = matchingFake(this.fakes, args);
1247
- var exception, returnValue;
1248
-
1249
- incrementCallCount.call(this);
1250
- push.call(this.thisValues, thisValue);
1251
- push.call(this.args, args);
1252
- push.call(this.callIds, callId++);
1253
-
1254
- try {
1255
- if (matching) {
1256
- returnValue = matching.invoke(func, thisValue, args);
1257
- } else {
1258
- returnValue = (this.func || func).apply(thisValue, args);
1259
- }
1260
- } catch (e) {
1261
- push.call(this.returnValues, undefined);
1262
- exception = e;
1263
- throw e;
1264
- } finally {
1265
- push.call(this.exceptions, exception);
1266
- }
1267
-
1268
- push.call(this.returnValues, returnValue);
1269
-
1270
- createCallProperties.call(this);
1271
-
1272
- return returnValue;
1273
- },
1274
-
1275
- getCall: function getCall(i) {
1276
- if (i < 0 || i >= this.callCount) {
1277
- return null;
1278
- }
1279
-
1280
- return spyCall.create(this, this.thisValues[i], this.args[i],
1281
- this.returnValues[i], this.exceptions[i],
1282
- this.callIds[i]);
1283
- },
1284
-
1285
- calledBefore: function calledBefore(spyFn) {
1286
- if (!this.called) {
1287
- return false;
1288
- }
1289
-
1290
- if (!spyFn.called) {
1291
- return true;
1292
- }
1293
-
1294
- return this.callIds[0] < spyFn.callIds[spyFn.callIds.length - 1];
1295
- },
1296
-
1297
- calledAfter: function calledAfter(spyFn) {
1298
- if (!this.called || !spyFn.called) {
1299
- return false;
1300
- }
1301
-
1302
- return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1];
1303
- },
1304
-
1305
- withArgs: function () {
1306
- var args = slice.call(arguments);
1307
-
1308
- if (this.fakes) {
1309
- var match = matchingFake(this.fakes, args, true);
1310
-
1311
- if (match) {
1312
- return match;
1313
- }
1314
- } else {
1315
- this.fakes = [];
1316
- }
1317
-
1318
- var original = this;
1319
- var fake = this._create();
1320
- fake.matchingAguments = args;
1321
- push.call(this.fakes, fake);
1322
-
1323
- fake.withArgs = function () {
1324
- return original.withArgs.apply(original, arguments);
1325
- };
1326
-
1327
- for (var i = 0; i < this.args.length; i++) {
1328
- if (fake.matches(this.args[i])) {
1329
- incrementCallCount.call(fake);
1330
- push.call(fake.thisValues, this.thisValues[i]);
1331
- push.call(fake.args, this.args[i]);
1332
- push.call(fake.returnValues, this.returnValues[i]);
1333
- push.call(fake.exceptions, this.exceptions[i]);
1334
- push.call(fake.callIds, this.callIds[i]);
1335
- }
1336
- }
1337
- createCallProperties.call(fake);
1338
-
1339
- return fake;
1340
- },
1341
-
1342
- matches: function (args, strict) {
1343
- var margs = this.matchingAguments;
1344
-
1345
- if (margs.length <= args.length &&
1346
- sinon.deepEqual(margs, args.slice(0, margs.length))) {
1347
- return !strict || margs.length == args.length;
1348
- }
1349
- },
1350
-
1351
- printf: function (format) {
1352
- var spy = this;
1353
- var args = slice.call(arguments, 1);
1354
- var formatter;
1355
-
1356
- return (format || "").replace(/%(.)/g, function (match, specifyer) {
1357
- formatter = spyApi.formatters[specifyer];
1358
-
1359
- if (typeof formatter == "function") {
1360
- return formatter.call(null, spy, args);
1361
- } else if (!isNaN(parseInt(specifyer), 10)) {
1362
- return sinon.format(args[specifyer - 1]);
1363
- }
1364
-
1365
- return "%" + specifyer;
1366
- });
1367
- }
1368
- };
1369
-
1370
- delegateToCalls(spyApi, "calledOn", true);
1371
- delegateToCalls(spyApi, "alwaysCalledOn", false, "calledOn");
1372
- delegateToCalls(spyApi, "calledWith", true);
1373
- delegateToCalls(spyApi, "calledWithMatch", true);
1374
- delegateToCalls(spyApi, "alwaysCalledWith", false, "calledWith");
1375
- delegateToCalls(spyApi, "alwaysCalledWithMatch", false, "calledWithMatch");
1376
- delegateToCalls(spyApi, "calledWithExactly", true);
1377
- delegateToCalls(spyApi, "alwaysCalledWithExactly", false, "calledWithExactly");
1378
- delegateToCalls(spyApi, "neverCalledWith", false, "notCalledWith",
1379
- function () {
1380
- return true;
1381
- });
1382
- delegateToCalls(spyApi, "neverCalledWithMatch", false, "notCalledWithMatch",
1383
- function () {
1384
- return true;
1385
- });
1386
- delegateToCalls(spyApi, "threw", true);
1387
- delegateToCalls(spyApi, "alwaysThrew", false, "threw");
1388
- delegateToCalls(spyApi, "returned", true);
1389
- delegateToCalls(spyApi, "alwaysReturned", false, "returned");
1390
- delegateToCalls(spyApi, "calledWithNew", true);
1391
- delegateToCalls(spyApi, "alwaysCalledWithNew", false, "calledWithNew");
1392
- delegateToCalls(spyApi, "callArg", false, "callArgWith", function () {
1393
- throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
1394
- });
1395
- spyApi.callArgWith = spyApi.callArg;
1396
- delegateToCalls(spyApi, "callArgOn", false, "callArgOnWith", function () {
1397
- throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
1398
- });
1399
- spyApi.callArgOnWith = spyApi.callArgOn;
1400
- delegateToCalls(spyApi, "yield", false, "yield", function () {
1401
- throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
1402
- });
1403
- // "invokeCallback" is an alias for "yield" since "yield" is invalid in strict mode.
1404
- spyApi.invokeCallback = spyApi.yield;
1405
- delegateToCalls(spyApi, "yieldOn", false, "yieldOn", function () {
1406
- throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
1407
- });
1408
- delegateToCalls(spyApi, "yieldTo", false, "yieldTo", function (property) {
1409
- throw new Error(this.toString() + " cannot yield to '" + property +
1410
- "' since it was not yet invoked.");
1411
- });
1412
- delegateToCalls(spyApi, "yieldToOn", false, "yieldToOn", function (property) {
1413
- throw new Error(this.toString() + " cannot yield to '" + property +
1414
- "' since it was not yet invoked.");
1415
- });
1416
-
1417
- spyApi.formatters = {
1418
- "c": function (spy) {
1419
- return sinon.timesInWords(spy.callCount);
1420
- },
1421
-
1422
- "n": function (spy) {
1423
- return spy.toString();
1424
- },
1425
-
1426
- "C": function (spy) {
1427
- var calls = [];
1428
-
1429
- for (var i = 0, l = spy.callCount; i < l; ++i) {
1430
- var stringifiedCall = " " + spy.getCall(i).toString();
1431
- if (/\n/.test(calls[i - 1])) {
1432
- stringifiedCall = "\n" + stringifiedCall;
1433
- }
1434
- push.call(calls, stringifiedCall);
1435
- }
1436
-
1437
- return calls.length > 0 ? "\n" + calls.join("\n") : "";
1438
- },
1439
-
1440
- "t": function (spy) {
1441
- var objects = [];
1442
-
1443
- for (var i = 0, l = spy.callCount; i < l; ++i) {
1444
- push.call(objects, sinon.format(spy.thisValues[i]));
1445
- }
1446
-
1447
- return objects.join(", ");
1448
- },
1449
-
1450
- "*": function (spy, args) {
1451
- var formatted = [];
1452
-
1453
- for (var i = 0, l = args.length; i < l; ++i) {
1454
- push.call(formatted, sinon.format(args[i]));
1455
- }
1456
-
1457
- return formatted.join(", ");
1458
- }
1459
- };
1460
-
1461
- return spyApi;
1462
- }()));
1463
-
1464
- spyCall = (function () {
1465
-
1466
- function throwYieldError(proxy, text, args) {
1467
- var msg = sinon.functionName(proxy) + text;
1468
- if (args.length) {
1469
- msg += " Received [" + slice.call(args).join(", ") + "]";
1470
- }
1471
- throw new Error(msg);
1472
- }
1473
-
1474
- var callApi = {
1475
- create: function create(spy, thisValue, args, returnValue, exception, id) {
1476
- var proxyCall = sinon.create(spyCall);
1477
- delete proxyCall.create;
1478
- proxyCall.proxy = spy;
1479
- proxyCall.thisValue = thisValue;
1480
- proxyCall.args = args;
1481
- proxyCall.returnValue = returnValue;
1482
- proxyCall.exception = exception;
1483
- proxyCall.callId = typeof id == "number" && id || callId++;
1484
-
1485
- return proxyCall;
1486
- },
1487
-
1488
- calledOn: function calledOn(thisValue) {
1489
- if (sinon.match && sinon.match.isMatcher(thisValue)) {
1490
- return thisValue.test(this.thisValue);
1491
- }
1492
- return this.thisValue === thisValue;
1493
- },
1494
-
1495
- calledWith: function calledWith() {
1496
- for (var i = 0, l = arguments.length; i < l; i += 1) {
1497
- if (!sinon.deepEqual(arguments[i], this.args[i])) {
1498
- return false;
1499
- }
1500
- }
1501
-
1502
- return true;
1503
- },
1504
-
1505
- calledWithMatch: function calledWithMatch() {
1506
- for (var i = 0, l = arguments.length; i < l; i += 1) {
1507
- var actual = this.args[i];
1508
- var expectation = arguments[i];
1509
- if (!sinon.match || !sinon.match(expectation).test(actual)) {
1510
- return false;
1511
- }
1512
- }
1513
- return true;
1514
- },
1515
-
1516
- calledWithExactly: function calledWithExactly() {
1517
- return arguments.length == this.args.length &&
1518
- this.calledWith.apply(this, arguments);
1519
- },
1520
-
1521
- notCalledWith: function notCalledWith() {
1522
- return !this.calledWith.apply(this, arguments);
1523
- },
1524
-
1525
- notCalledWithMatch: function notCalledWithMatch() {
1526
- return !this.calledWithMatch.apply(this, arguments);
1527
- },
1528
-
1529
- returned: function returned(value) {
1530
- return sinon.deepEqual(value, this.returnValue);
1531
- },
1532
-
1533
- threw: function threw(error) {
1534
- if (typeof error == "undefined" || !this.exception) {
1535
- return !!this.exception;
1536
- }
1537
-
1538
- if (typeof error == "string") {
1539
- return this.exception.name == error;
1540
- }
1541
-
1542
- return this.exception === error;
1543
- },
1544
-
1545
- calledWithNew: function calledWithNew(thisValue) {
1546
- return this.thisValue instanceof this.proxy;
1547
- },
1548
-
1549
- calledBefore: function (other) {
1550
- return this.callId < other.callId;
1551
- },
1552
-
1553
- calledAfter: function (other) {
1554
- return this.callId > other.callId;
1555
- },
1556
-
1557
- callArg: function (pos) {
1558
- this.args[pos]();
1559
- },
1560
-
1561
- callArgOn: function (pos, thisValue) {
1562
- this.args[pos].apply(thisValue);
1563
- },
1564
-
1565
- callArgWith: function (pos) {
1566
- this.callArgOnWith.apply(this, [pos, null].concat(slice.call(arguments, 1)));
1567
- },
1568
-
1569
- callArgOnWith: function (pos, thisValue) {
1570
- var args = slice.call(arguments, 2);
1571
- this.args[pos].apply(thisValue, args);
1572
- },
1573
-
1574
- "yield": function () {
1575
- this.yieldOn.apply(this, [null].concat(slice.call(arguments, 0)));
1576
- },
1577
-
1578
- yieldOn: function (thisValue) {
1579
- var args = this.args;
1580
- for (var i = 0, l = args.length; i < l; ++i) {
1581
- if (typeof args[i] === "function") {
1582
- args[i].apply(thisValue, slice.call(arguments, 1));
1583
- return;
1584
- }
1585
- }
1586
- throwYieldError(this.proxy, " cannot yield since no callback was passed.", args);
1587
- },
1588
-
1589
- yieldTo: function (prop) {
1590
- this.yieldToOn.apply(this, [prop, null].concat(slice.call(arguments, 1)));
1591
- },
1592
-
1593
- yieldToOn: function (prop, thisValue) {
1594
- var args = this.args;
1595
- for (var i = 0, l = args.length; i < l; ++i) {
1596
- if (args[i] && typeof args[i][prop] === "function") {
1597
- args[i][prop].apply(thisValue, slice.call(arguments, 2));
1598
- return;
1599
- }
1600
- }
1601
- throwYieldError(this.proxy, " cannot yield to '" + prop +
1602
- "' since no callback was passed.", args);
1603
- },
1604
-
1605
- toString: function () {
1606
- var callStr = this.proxy.toString() + "(";
1607
- var args = [];
1608
-
1609
- for (var i = 0, l = this.args.length; i < l; ++i) {
1610
- push.call(args, sinon.format(this.args[i]));
1611
- }
1612
-
1613
- callStr = callStr + args.join(", ") + ")";
1614
-
1615
- if (typeof this.returnValue != "undefined") {
1616
- callStr += " => " + sinon.format(this.returnValue);
1617
- }
1618
-
1619
- if (this.exception) {
1620
- callStr += " !" + this.exception.name;
1621
-
1622
- if (this.exception.message) {
1623
- callStr += "(" + this.exception.message + ")";
1624
- }
1625
- }
1626
-
1627
- return callStr;
1628
- }
1629
- };
1630
- callApi.invokeCallback = callApi.yield;
1631
- return callApi;
1632
- }());
1633
-
1634
- spy.spyCall = spyCall;
1635
-
1636
- // This steps outside the module sandbox and will be removed
1637
- sinon.spyCall = 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
- };
2681
- F.prototype = object;
2682
- newObject = new F();
2683
- }
2684
-
2685
- newObject.Date.clock = newObject;
2686
- return newObject;
2687
- }
2688
-
2689
- sinon.clock = {
2690
- now: 0,
2691
-
2692
- create: function create(now) {
2693
- var clock = createObject(this);
2694
-
2695
- if (typeof now == "number") {
2696
- clock.now = now;
2697
- }
2698
-
2699
- if (!!now && typeof now == "object") {
2700
- throw new TypeError("now should be milliseconds since UNIX epoch");
2701
- }
2702
-
2703
- return clock;
2704
- },
2705
-
2706
- setTimeout: function setTimeout(callback, timeout) {
2707
- return addTimer.call(this, arguments, false);
2708
- },
2709
-
2710
- clearTimeout: function clearTimeout(timerId) {
2711
- if (!this.timeouts) {
2712
- this.timeouts = [];
2713
- }
2714
-
2715
- if (timerId in this.timeouts) {
2716
- delete this.timeouts[timerId];
2717
- }
2718
- },
2719
-
2720
- setInterval: function setInterval(callback, timeout) {
2721
- return addTimer.call(this, arguments, true);
2722
- },
2723
-
2724
- clearInterval: function clearInterval(timerId) {
2725
- this.clearTimeout(timerId);
2726
- },
2727
-
2728
- tick: function tick(ms) {
2729
- ms = typeof ms == "number" ? ms : parseTime(ms);
2730
- var tickFrom = this.now, tickTo = this.now + ms, previous = this.now;
2731
- var timer = this.firstTimerInRange(tickFrom, tickTo);
2732
-
2733
- var firstException;
2734
- while (timer && tickFrom <= tickTo) {
2735
- if (this.timeouts[timer.id]) {
2736
- tickFrom = this.now = timer.callAt;
2737
- try {
2738
- this.callTimer(timer);
2739
- } catch (e) {
2740
- firstException = firstException || e;
2741
- }
2742
- }
2743
-
2744
- timer = this.firstTimerInRange(previous, tickTo);
2745
- previous = tickFrom;
2746
- }
2747
-
2748
- this.now = tickTo;
2749
-
2750
- if (firstException) {
2751
- throw firstException;
2752
- }
2753
-
2754
- return this.now;
2755
- },
2756
-
2757
- firstTimerInRange: function (from, to) {
2758
- var timer, smallest, originalTimer;
2759
-
2760
- for (var id in this.timeouts) {
2761
- if (this.timeouts.hasOwnProperty(id)) {
2762
- if (this.timeouts[id].callAt < from || this.timeouts[id].callAt > to) {
2763
- continue;
2764
- }
2765
-
2766
- if (!smallest || this.timeouts[id].callAt < smallest) {
2767
- originalTimer = this.timeouts[id];
2768
- smallest = this.timeouts[id].callAt;
2769
-
2770
- timer = {
2771
- func: this.timeouts[id].func,
2772
- callAt: this.timeouts[id].callAt,
2773
- interval: this.timeouts[id].interval,
2774
- id: this.timeouts[id].id,
2775
- invokeArgs: this.timeouts[id].invokeArgs
2776
- };
2777
- }
2778
- }
2779
- }
2780
-
2781
- return timer || null;
2782
- },
2783
-
2784
- callTimer: function (timer) {
2785
- if (typeof timer.interval == "number") {
2786
- this.timeouts[timer.id].callAt += timer.interval;
2787
- } else {
2788
- delete this.timeouts[timer.id];
2789
- }
2790
-
2791
- try {
2792
- if (typeof timer.func == "function") {
2793
- timer.func.apply(null, timer.invokeArgs);
2794
- } else {
2795
- eval(timer.func);
2796
- }
2797
- } catch (e) {
2798
- var exception = e;
2799
- }
2800
-
2801
- if (!this.timeouts[timer.id]) {
2802
- if (exception) {
2803
- throw exception;
2804
- }
2805
- return;
2806
- }
2807
-
2808
- if (exception) {
2809
- throw exception;
2810
- }
2811
- },
2812
-
2813
- reset: function reset() {
2814
- this.timeouts = {};
2815
- },
2816
-
2817
- Date: (function () {
2818
- var NativeDate = Date;
2819
-
2820
- function ClockDate(year, month, date, hour, minute, second, ms) {
2821
- // Defensive and verbose to avoid potential harm in passing
2822
- // explicit undefined when user does not pass argument
2823
- switch (arguments.length) {
2824
- case 0:
2825
- return new NativeDate(ClockDate.clock.now);
2826
- case 1:
2827
- return new NativeDate(year);
2828
- case 2:
2829
- return new NativeDate(year, month);
2830
- case 3:
2831
- return new NativeDate(year, month, date);
2832
- case 4:
2833
- return new NativeDate(year, month, date, hour);
2834
- case 5:
2835
- return new NativeDate(year, month, date, hour, minute);
2836
- case 6:
2837
- return new NativeDate(year, month, date, hour, minute, second);
2838
- default:
2839
- return new NativeDate(year, month, date, hour, minute, second, ms);
2840
- }
2841
- }
2842
-
2843
- return mirrorDateProperties(ClockDate, NativeDate);
2844
- }())
2845
- };
2846
-
2847
- function mirrorDateProperties(target, source) {
2848
- if (source.now) {
2849
- target.now = function now() {
2850
- return target.clock.now;
2851
- };
2852
- } else {
2853
- delete target.now;
2854
- }
2855
-
2856
- if (source.toSource) {
2857
- target.toSource = function toSource() {
2858
- return source.toSource();
2859
- };
2860
- } else {
2861
- delete target.toSource;
2862
- }
2863
-
2864
- target.toString = function toString() {
2865
- return source.toString();
2866
- };
2867
-
2868
- target.prototype = source.prototype;
2869
- target.parse = source.parse;
2870
- target.UTC = source.UTC;
2871
- target.prototype.toUTCString = source.prototype.toUTCString;
2872
- return target;
2873
- }
2874
-
2875
- var methods = ["Date", "setTimeout", "setInterval",
2876
- "clearTimeout", "clearInterval"];
2877
-
2878
- function restore() {
2879
- var method;
2880
-
2881
- for (var i = 0, l = this.methods.length; i < l; i++) {
2882
- method = this.methods[i];
2883
- if (global[method].hadOwnProperty) {
2884
- global[method] = this["_" + method];
2885
- } else {
2886
- delete global[method];
2887
- }
2888
- }
2889
-
2890
- // Prevent multiple executions which will completely remove these props
2891
- this.methods = [];
2892
- }
2893
-
2894
- function stubGlobal(method, clock) {
2895
- clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(global, method);
2896
- clock["_" + method] = global[method];
2897
-
2898
- if (method == "Date") {
2899
- var date = mirrorDateProperties(clock[method], global[method]);
2900
- global[method] = date;
2901
- } else {
2902
- global[method] = function () {
2903
- return clock[method].apply(clock, arguments);
2904
- };
2905
-
2906
- for (var prop in clock[method]) {
2907
- if (clock[method].hasOwnProperty(prop)) {
2908
- global[method][prop] = clock[method][prop];
2909
- }
2910
- }
2911
- }
2912
-
2913
- global[method].clock = clock;
2914
- }
2915
-
2916
- sinon.useFakeTimers = function useFakeTimers(now) {
2917
- var clock = sinon.clock.create(now);
2918
- clock.restore = restore;
2919
- clock.methods = Array.prototype.slice.call(arguments,
2920
- typeof now == "number" ? 1 : 0);
2921
-
2922
- if (clock.methods.length === 0) {
2923
- clock.methods = methods;
2924
- }
2925
-
2926
- for (var i = 0, l = clock.methods.length; i < l; i++) {
2927
- stubGlobal(clock.methods[i], clock);
2928
- }
2929
-
2930
- return clock;
2931
- };
2932
- }(typeof global != "undefined" && typeof global !== "function" ? global : this));
2933
-
2934
- sinon.timers = {
2935
- setTimeout: setTimeout,
2936
- clearTimeout: clearTimeout,
2937
- setInterval: setInterval,
2938
- clearInterval: clearInterval,
2939
- Date: Date
2940
- };
2941
-
2942
- if (typeof module == "object" && typeof require == "function") {
2943
- module.exports = sinon;
2944
- }
2945
-
2946
- /*jslint eqeqeq: false, onevar: false*/
2947
- /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
2948
- /**
2949
- * Minimal Event interface implementation
2950
- *
2951
- * Original implementation by Sven Fuchs: https://gist.github.com/995028
2952
- * Modifications and tests by Christian Johansen.
2953
- *
2954
- * @author Sven Fuchs (svenfuchs@artweb-design.de)
2955
- * @author Christian Johansen (christian@cjohansen.no)
2956
- * @license BSD
2957
- *
2958
- * Copyright (c) 2011 Sven Fuchs, Christian Johansen
2959
- */
2960
-
2961
- if (typeof sinon == "undefined") {
2962
- this.sinon = {};
2963
- }
2964
-
2965
- (function () {
2966
- var push = [].push;
2967
-
2968
- sinon.Event = function Event(type, bubbles, cancelable) {
2969
- this.initEvent(type, bubbles, cancelable);
2970
- };
2971
-
2972
- sinon.Event.prototype = {
2973
- initEvent: function (type, bubbles, cancelable) {
2974
- this.type = type;
2975
- this.bubbles = bubbles;
2976
- this.cancelable = cancelable;
2977
- },
2978
-
2979
- stopPropagation: function () {
2980
- },
2981
-
2982
- preventDefault: function () {
2983
- this.defaultPrevented = true;
2984
- }
2985
- };
2986
-
2987
- sinon.EventTarget = {
2988
- addEventListener: function addEventListener(event, listener, useCapture) {
2989
- this.eventListeners = this.eventListeners || {};
2990
- this.eventListeners[event] = this.eventListeners[event] || [];
2991
- push.call(this.eventListeners[event], listener);
2992
- },
2993
-
2994
- removeEventListener: function removeEventListener(event, listener, useCapture) {
2995
- var listeners = this.eventListeners && this.eventListeners[event] || [];
2996
-
2997
- for (var i = 0, l = listeners.length; i < l; ++i) {
2998
- if (listeners[i] == listener) {
2999
- return listeners.splice(i, 1);
3000
- }
3001
- }
3002
- },
3003
-
3004
- dispatchEvent: function dispatchEvent(event) {
3005
- var type = event.type;
3006
- var listeners = this.eventListeners && this.eventListeners[type] || [];
3007
-
3008
- for (var i = 0; i < listeners.length; i++) {
3009
- if (typeof listeners[i] == "function") {
3010
- listeners[i].call(this, event);
3011
- } else {
3012
- listeners[i].handleEvent(event);
3013
- }
3014
- }
3015
-
3016
- return !!event.defaultPrevented;
3017
- }
3018
- };
3019
- }());
3020
-
3021
- /**
3022
- * @depend ../../sinon.js
3023
- * @depend event.js
3024
- */
3025
- /*jslint eqeqeq: false, onevar: false*/
3026
- /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
3027
- /**
3028
- * Fake XMLHttpRequest object
3029
- *
3030
- * @author Christian Johansen (christian@cjohansen.no)
3031
- * @license BSD
3032
- *
3033
- * Copyright (c) 2010-2013 Christian Johansen
3034
- */
3035
-
3036
- if (typeof sinon == "undefined") {
3037
- this.sinon = {};
3038
- }
3039
- sinon.xhr = { XMLHttpRequest: this.XMLHttpRequest };
3040
-
3041
- // wrapper for global
3042
- (function (global) {
3043
- var xhr = sinon.xhr;
3044
- xhr.GlobalXMLHttpRequest = global.XMLHttpRequest;
3045
- xhr.GlobalActiveXObject = global.ActiveXObject;
3046
- xhr.supportsActiveX = typeof xhr.GlobalActiveXObject != "undefined";
3047
- xhr.supportsXHR = typeof xhr.GlobalXMLHttpRequest != "undefined";
3048
- xhr.workingXHR = xhr.supportsXHR ? xhr.GlobalXMLHttpRequest : xhr.supportsActiveX
3049
- ? function () {
3050
- return new xhr.GlobalActiveXObject("MSXML2.XMLHTTP.3.0")
3051
- } : false;
3052
-
3053
- /*jsl:ignore*/
3054
- var unsafeHeaders = {
3055
- "Accept-Charset": true,
3056
- "Accept-Encoding": true,
3057
- "Connection": true,
3058
- "Content-Length": true,
3059
- "Cookie": true,
3060
- "Cookie2": true,
3061
- "Content-Transfer-Encoding": true,
3062
- "Date": true,
3063
- "Expect": true,
3064
- "Host": true,
3065
- "Keep-Alive": true,
3066
- "Referer": true,
3067
- "TE": true,
3068
- "Trailer": true,
3069
- "Transfer-Encoding": true,
3070
- "Upgrade": true,
3071
- "User-Agent": true,
3072
- "Via": true
3073
- };
3074
- /*jsl:end*/
3075
-
3076
- function FakeXMLHttpRequest() {
3077
- this.readyState = FakeXMLHttpRequest.UNSENT;
3078
- this.requestHeaders = {};
3079
- this.requestBody = null;
3080
- this.status = 0;
3081
- this.statusText = "";
3082
-
3083
- if (typeof FakeXMLHttpRequest.onCreate == "function") {
3084
- FakeXMLHttpRequest.onCreate(this);
3085
- }
3086
- }
3087
-
3088
- function verifyState(xhr) {
3089
- if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
3090
- throw new Error("INVALID_STATE_ERR");
3091
- }
3092
-
3093
- if (xhr.sendFlag) {
3094
- throw new Error("INVALID_STATE_ERR");
3095
- }
3096
- }
3097
-
3098
- // filtering to enable a white-list version of Sinon FakeXhr,
3099
- // where whitelisted requests are passed through to real XHR
3100
- function each(collection, callback) {
3101
- if (!collection) return;
3102
- for (var i = 0, l = collection.length; i < l; i += 1) {
3103
- callback(collection[i]);
3104
- }
3105
- }
3106
-
3107
- function some(collection, callback) {
3108
- for (var index = 0; index < collection.length; index++) {
3109
- if (callback(collection[index]) === true) return true;
3110
- }
3111
- ;
3112
- return false;
3113
- }
3114
-
3115
- // largest arity in XHR is 5 - XHR#open
3116
- var apply = function (obj, method, args) {
3117
- switch (args.length) {
3118
- case 0:
3119
- return obj[method]();
3120
- case 1:
3121
- return obj[method](args[0]);
3122
- case 2:
3123
- return obj[method](args[0], args[1]);
3124
- case 3:
3125
- return obj[method](args[0], args[1], args[2]);
3126
- case 4:
3127
- return obj[method](args[0], args[1], args[2], args[3]);
3128
- case 5:
3129
- return obj[method](args[0], args[1], args[2], args[3], args[4]);
3130
- }
3131
- ;
3132
- };
3133
-
3134
- FakeXMLHttpRequest.filters = [];
3135
- FakeXMLHttpRequest.addFilter = function (fn) {
3136
- this.filters.push(fn)
3137
- };
3138
- var IE6Re = /MSIE 6/;
3139
- FakeXMLHttpRequest.defake = function (fakeXhr, xhrArgs) {
3140
- var xhr = new sinon.xhr.workingXHR();
3141
- each(["open", "setRequestHeader", "send", "abort", "getResponseHeader",
3142
- "getAllResponseHeaders", "addEventListener", "overrideMimeType", "removeEventListener"],
3143
- function (method) {
3144
- fakeXhr[method] = function () {
3145
- return apply(xhr, method, arguments);
3146
- };
3147
- });
3148
-
3149
- var copyAttrs = function (args) {
3150
- each(args, function (attr) {
3151
- try {
3152
- fakeXhr[attr] = xhr[attr]
3153
- } catch (e) {
3154
- if (!IE6Re.test(navigator.userAgent)) throw e;
3155
- }
3156
- });
3157
- };
3158
-
3159
- var stateChange = function () {
3160
- fakeXhr.readyState = xhr.readyState;
3161
- if (xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) {
3162
- copyAttrs(["status", "statusText"]);
3163
- }
3164
- if (xhr.readyState >= FakeXMLHttpRequest.LOADING) {
3165
- copyAttrs(["responseText"]);
3166
- }
3167
- if (xhr.readyState === FakeXMLHttpRequest.DONE) {
3168
- copyAttrs(["responseXML"]);
3169
- }
3170
- if (fakeXhr.onreadystatechange) fakeXhr.onreadystatechange.call(fakeXhr);
3171
- };
3172
- if (xhr.addEventListener) {
3173
- for (var event in fakeXhr.eventListeners) {
3174
- if (fakeXhr.eventListeners.hasOwnProperty(event)) {
3175
- each(fakeXhr.eventListeners[event], function (handler) {
3176
- xhr.addEventListener(event, handler);
3177
- });
3178
- }
3179
- }
3180
- xhr.addEventListener("readystatechange", stateChange);
3181
- } else {
3182
- xhr.onreadystatechange = stateChange;
3183
- }
3184
- apply(xhr, "open", xhrArgs);
3185
- };
3186
- FakeXMLHttpRequest.useFilters = false;
3187
-
3188
- function verifyRequestSent(xhr) {
3189
- if (xhr.readyState == FakeXMLHttpRequest.DONE) {
3190
- throw new Error("Request done");
3191
- }
3192
- }
3193
-
3194
- function verifyHeadersReceived(xhr) {
3195
- if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
3196
- throw new Error("No headers received");
3197
- }
3198
- }
3199
-
3200
- function verifyResponseBodyType(body) {
3201
- if (typeof body != "string") {
3202
- var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
3203
- body + ", which is not a string.");
3204
- error.name = "InvalidBodyException";
3205
- throw error;
3206
- }
3207
- }
3208
-
3209
- sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
3210
- async: true,
3211
-
3212
- open: function open(method, url, async, username, password) {
3213
- this.method = method;
3214
- this.url = url;
3215
- this.async = typeof async == "boolean" ? async : true;
3216
- this.username = username;
3217
- this.password = password;
3218
- this.responseText = null;
3219
- this.responseXML = null;
3220
- this.requestHeaders = {};
3221
- this.sendFlag = false;
3222
- if (sinon.FakeXMLHttpRequest.useFilters === true) {
3223
- var xhrArgs = arguments;
3224
- var defake = some(FakeXMLHttpRequest.filters, function (filter) {
3225
- return filter.apply(this, xhrArgs)
3226
- });
3227
- if (defake) {
3228
- return sinon.FakeXMLHttpRequest.defake(this, arguments);
3229
- }
3230
- }
3231
- this.readyStateChange(FakeXMLHttpRequest.OPENED);
3232
- },
3233
-
3234
- readyStateChange: function readyStateChange(state) {
3235
- this.readyState = state;
3236
-
3237
- if (typeof this.onreadystatechange == "function") {
3238
- try {
3239
- this.onreadystatechange();
3240
- } catch (e) {
3241
- sinon.logError("Fake XHR onreadystatechange handler", e);
3242
- }
3243
- }
3244
-
3245
- this.dispatchEvent(new sinon.Event("readystatechange"));
3246
- },
3247
-
3248
- setRequestHeader: function setRequestHeader(header, value) {
3249
- verifyState(this);
3250
-
3251
- if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
3252
- throw new Error("Refused to set unsafe header \"" + header + "\"");
3253
- }
3254
-
3255
- if (this.requestHeaders[header]) {
3256
- this.requestHeaders[header] += "," + value;
3257
- } else {
3258
- this.requestHeaders[header] = value;
3259
- }
3260
- },
3261
-
3262
- // Helps testing
3263
- setResponseHeaders: function setResponseHeaders(headers) {
3264
- this.responseHeaders = {};
3265
-
3266
- for (var header in headers) {
3267
- if (headers.hasOwnProperty(header)) {
3268
- this.responseHeaders[header] = headers[header];
3269
- }
3270
- }
3271
-
3272
- if (this.async) {
3273
- this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
3274
- } else {
3275
- this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
3276
- }
3277
- },
3278
-
3279
- // Currently treats ALL data as a DOMString (i.e. no Document)
3280
- send: function send(data) {
3281
- verifyState(this);
3282
-
3283
- if (!/^(get|head)$/i.test(this.method)) {
3284
- if (this.requestHeaders["Content-Type"]) {
3285
- var value = this.requestHeaders["Content-Type"].split(";");
3286
- this.requestHeaders["Content-Type"] = value[0] + ";charset=utf-8";
3287
- } else {
3288
- this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
3289
- }
3290
-
3291
- this.requestBody = data;
3292
- }
3293
-
3294
- this.errorFlag = false;
3295
- this.sendFlag = this.async;
3296
- this.readyStateChange(FakeXMLHttpRequest.OPENED);
3297
-
3298
- if (typeof this.onSend == "function") {
3299
- this.onSend(this);
3300
- }
3301
- },
3302
-
3303
- abort: function abort() {
3304
- this.aborted = true;
3305
- this.responseText = null;
3306
- this.errorFlag = true;
3307
- this.requestHeaders = {};
3308
-
3309
- if (this.readyState > sinon.FakeXMLHttpRequest.UNSENT && this.sendFlag) {
3310
- this.readyStateChange(sinon.FakeXMLHttpRequest.DONE);
3311
- this.sendFlag = false;
3312
- }
3313
-
3314
- this.readyState = sinon.FakeXMLHttpRequest.UNSENT;
3315
- },
3316
-
3317
- getResponseHeader: function getResponseHeader(header) {
3318
- if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
3319
- return null;
3320
- }
3321
-
3322
- if (/^Set-Cookie2?$/i.test(header)) {
3323
- return null;
3324
- }
3325
-
3326
- header = header.toLowerCase();
3327
-
3328
- for (var h in this.responseHeaders) {
3329
- if (h.toLowerCase() == header) {
3330
- return this.responseHeaders[h];
3331
- }
3332
- }
3333
-
3334
- return null;
3335
- },
3336
-
3337
- getAllResponseHeaders: function getAllResponseHeaders() {
3338
- if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
3339
- return "";
3340
- }
3341
-
3342
- var headers = "";
3343
-
3344
- for (var header in this.responseHeaders) {
3345
- if (this.responseHeaders.hasOwnProperty(header) &&
3346
- !/^Set-Cookie2?$/i.test(header)) {
3347
- headers += header + ": " + this.responseHeaders[header] + "\r\n";
3348
- }
3349
- }
3350
-
3351
- return headers;
3352
- },
3353
-
3354
- setResponseBody: function setResponseBody(body) {
3355
- verifyRequestSent(this);
3356
- verifyHeadersReceived(this);
3357
- verifyResponseBodyType(body);
3358
-
3359
- var chunkSize = this.chunkSize || 10;
3360
- var index = 0;
3361
- this.responseText = "";
3362
-
3363
- do {
3364
- if (this.async) {
3365
- this.readyStateChange(FakeXMLHttpRequest.LOADING);
3366
- }
3367
-
3368
- this.responseText += body.substring(index, index + chunkSize);
3369
- index += chunkSize;
3370
- } while (index < body.length);
3371
-
3372
- var type = this.getResponseHeader("Content-Type");
3373
-
3374
- if (this.responseText &&
3375
- (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
3376
- try {
3377
- this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText);
3378
- } catch (e) {
3379
- // Unable to parse XML - no biggie
3380
- }
3381
- }
3382
-
3383
- if (this.async) {
3384
- this.readyStateChange(FakeXMLHttpRequest.DONE);
3385
- } else {
3386
- this.readyState = FakeXMLHttpRequest.DONE;
3387
- }
3388
- },
3389
-
3390
- respond: function respond(status, headers, body) {
3391
- this.setResponseHeaders(headers || {});
3392
- this.status = typeof status == "number" ? status : 200;
3393
- this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
3394
- this.setResponseBody(body || "");
3395
- }
3396
- });
3397
-
3398
- sinon.extend(FakeXMLHttpRequest, {
3399
- UNSENT: 0,
3400
- OPENED: 1,
3401
- HEADERS_RECEIVED: 2,
3402
- LOADING: 3,
3403
- DONE: 4
3404
- });
3405
-
3406
- // Borrowed from JSpec
3407
- FakeXMLHttpRequest.parseXML = function parseXML(text) {
3408
- var xmlDoc;
3409
-
3410
- if (typeof DOMParser != "undefined") {
3411
- var parser = new DOMParser();
3412
- xmlDoc = parser.parseFromString(text, "text/xml");
3413
- } else {
3414
- xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
3415
- xmlDoc.async = "false";
3416
- xmlDoc.loadXML(text);
3417
- }
3418
-
3419
- return xmlDoc;
3420
- };
3421
-
3422
- FakeXMLHttpRequest.statusCodes = {
3423
- 100: "Continue",
3424
- 101: "Switching Protocols",
3425
- 200: "OK",
3426
- 201: "Created",
3427
- 202: "Accepted",
3428
- 203: "Non-Authoritative Information",
3429
- 204: "No Content",
3430
- 205: "Reset Content",
3431
- 206: "Partial Content",
3432
- 300: "Multiple Choice",
3433
- 301: "Moved Permanently",
3434
- 302: "Found",
3435
- 303: "See Other",
3436
- 304: "Not Modified",
3437
- 305: "Use Proxy",
3438
- 307: "Temporary Redirect",
3439
- 400: "Bad Request",
3440
- 401: "Unauthorized",
3441
- 402: "Payment Required",
3442
- 403: "Forbidden",
3443
- 404: "Not Found",
3444
- 405: "Method Not Allowed",
3445
- 406: "Not Acceptable",
3446
- 407: "Proxy Authentication Required",
3447
- 408: "Request Timeout",
3448
- 409: "Conflict",
3449
- 410: "Gone",
3450
- 411: "Length Required",
3451
- 412: "Precondition Failed",
3452
- 413: "Request Entity Too Large",
3453
- 414: "Request-URI Too Long",
3454
- 415: "Unsupported Media Type",
3455
- 416: "Requested Range Not Satisfiable",
3456
- 417: "Expectation Failed",
3457
- 422: "Unprocessable Entity",
3458
- 500: "Internal Server Error",
3459
- 501: "Not Implemented",
3460
- 502: "Bad Gateway",
3461
- 503: "Service Unavailable",
3462
- 504: "Gateway Timeout",
3463
- 505: "HTTP Version Not Supported"
3464
- };
3465
-
3466
- sinon.useFakeXMLHttpRequest = function () {
3467
- sinon.FakeXMLHttpRequest.restore = function restore(keepOnCreate) {
3468
- if (xhr.supportsXHR) {
3469
- global.XMLHttpRequest = xhr.GlobalXMLHttpRequest;
3470
- }
3471
-
3472
- if (xhr.supportsActiveX) {
3473
- global.ActiveXObject = xhr.GlobalActiveXObject;
3474
- }
3475
-
3476
- delete sinon.FakeXMLHttpRequest.restore;
3477
-
3478
- if (keepOnCreate !== true) {
3479
- delete sinon.FakeXMLHttpRequest.onCreate;
3480
- }
3481
- };
3482
- if (xhr.supportsXHR) {
3483
- global.XMLHttpRequest = sinon.FakeXMLHttpRequest;
3484
- }
3485
-
3486
- if (xhr.supportsActiveX) {
3487
- global.ActiveXObject = function ActiveXObject(objId) {
3488
- if (objId == "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) {
3489
-
3490
- return new sinon.FakeXMLHttpRequest();
3491
- }
3492
-
3493
- return new xhr.GlobalActiveXObject(objId);
3494
- };
3495
- }
3496
-
3497
- return sinon.FakeXMLHttpRequest;
3498
- };
3499
-
3500
- sinon.FakeXMLHttpRequest = FakeXMLHttpRequest;
3501
- })(this);
3502
-
3503
- if (typeof module == "object" && typeof require == "function") {
3504
- module.exports = sinon;
3505
- }
3506
-
3507
- /**
3508
- * @depend fake_xml_http_request.js
3509
- */
3510
- /*jslint eqeqeq: false, onevar: false, regexp: false, plusplus: false*/
3511
- /*global module, require, window*/
3512
- /**
3513
- * The Sinon "server" mimics a web server that receives requests from
3514
- * sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
3515
- * both synchronously and asynchronously. To respond synchronuously, canned
3516
- * answers have to be provided upfront.
3517
- *
3518
- * @author Christian Johansen (christian@cjohansen.no)
3519
- * @license BSD
3520
- *
3521
- * Copyright (c) 2010-2013 Christian Johansen
3522
- */
3523
-
3524
- if (typeof sinon == "undefined") {
3525
- var sinon = {};
3526
- }
3527
-
3528
- sinon.fakeServer = (function () {
3529
- var push = [].push;
3530
-
3531
- function F() {
3532
- }
3533
-
3534
- function create(proto) {
3535
- F.prototype = proto;
3536
- return new F();
3537
- }
3538
-
3539
- function responseArray(handler) {
3540
- var response = handler;
3541
-
3542
- if (Object.prototype.toString.call(handler) != "[object Array]") {
3543
- response = [200, {}, handler];
3544
- }
3545
-
3546
- if (typeof response[2] != "string") {
3547
- throw new TypeError("Fake server response body should be string, but was " +
3548
- typeof response[2]);
3549
- }
3550
-
3551
- return response;
3552
- }
3553
-
3554
- var wloc = typeof window !== "undefined" ? window.location : {};
3555
- var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
3556
-
3557
- function matchOne(response, reqMethod, reqUrl) {
3558
- var rmeth = response.method;
3559
- var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase();
3560
- var url = response.url;
3561
- var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl));
3562
-
3563
- return matchMethod && matchUrl;
3564
- }
3565
-
3566
- function match(response, request) {
3567
- var requestMethod = this.getHTTPMethod(request);
3568
- var requestUrl = request.url;
3569
-
3570
- if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
3571
- requestUrl = requestUrl.replace(rCurrLoc, "");
3572
- }
3573
-
3574
- if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
3575
- if (typeof response.response == "function") {
3576
- var ru = response.url;
3577
- var args = [request].concat(!ru ? [] : requestUrl.match(ru).slice(1));
3578
- return response.response.apply(response, args);
3579
- }
3580
-
3581
- return true;
3582
- }
3583
-
3584
- return false;
3585
- }
3586
-
3587
- function log(response, request) {
3588
- var str;
3589
-
3590
- str = "Request:\n" + sinon.format(request) + "\n\n";
3591
- str += "Response:\n" + sinon.format(response) + "\n\n";
3592
-
3593
- sinon.log(str);
3594
- }
3595
-
3596
- return {
3597
- create: function () {
3598
- var server = create(this);
3599
- this.xhr = sinon.useFakeXMLHttpRequest();
3600
- server.requests = [];
3601
-
3602
- this.xhr.onCreate = function (xhrObj) {
3603
- server.addRequest(xhrObj);
3604
- };
3605
-
3606
- return server;
3607
- },
3608
-
3609
- addRequest: function addRequest(xhrObj) {
3610
- var server = this;
3611
- push.call(this.requests, xhrObj);
3612
-
3613
- xhrObj.onSend = function () {
3614
- server.handleRequest(this);
3615
- };
3616
-
3617
- if (this.autoRespond && !this.responding) {
3618
- setTimeout(function () {
3619
- server.responding = false;
3620
- server.respond();
3621
- }, this.autoRespondAfter || 10);
3622
-
3623
- this.responding = true;
3624
- }
3625
- },
3626
-
3627
- getHTTPMethod: function getHTTPMethod(request) {
3628
- if (this.fakeHTTPMethods && /post/i.test(request.method)) {
3629
- var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
3630
- return !!matches ? matches[1] : request.method;
3631
- }
3632
-
3633
- return request.method;
3634
- },
3635
-
3636
- handleRequest: function handleRequest(xhr) {
3637
- if (xhr.async) {
3638
- if (!this.queue) {
3639
- this.queue = [];
3640
- }
3641
-
3642
- push.call(this.queue, xhr);
3643
- } else {
3644
- this.processRequest(xhr);
3645
- }
3646
- },
3647
-
3648
- respondWith: function respondWith(method, url, body) {
3649
- if (arguments.length == 1 && typeof method != "function") {
3650
- this.response = responseArray(method);
3651
- return;
3652
- }
3653
-
3654
- if (!this.responses) {
3655
- this.responses = [];
3656
- }
3657
-
3658
- if (arguments.length == 1) {
3659
- body = method;
3660
- url = method = null;
3661
- }
3662
-
3663
- if (arguments.length == 2) {
3664
- body = url;
3665
- url = method;
3666
- method = null;
3667
- }
3668
-
3669
- push.call(this.responses, {
3670
- method: method,
3671
- url: url,
3672
- response: typeof body == "function" ? body : responseArray(body)
3673
- });
3674
- },
3675
-
3676
- respond: function respond() {
3677
- if (arguments.length > 0) this.respondWith.apply(this, arguments);
3678
- var queue = this.queue || [];
3679
- var request;
3680
-
3681
- while (request = queue.shift()) {
3682
- this.processRequest(request);
3683
- }
3684
- },
3685
-
3686
- processRequest: function processRequest(request) {
3687
- try {
3688
- if (request.aborted) {
3689
- return;
3690
- }
3691
-
3692
- var response = this.response || [404, {}, ""];
3693
-
3694
- if (this.responses) {
3695
- for (var i = 0, l = this.responses.length; i < l; i++) {
3696
- if (match.call(this, this.responses[i], request)) {
3697
- response = this.responses[i].response;
3698
- break;
3699
- }
3700
- }
3701
- }
3702
-
3703
- if (request.readyState != 4) {
3704
- log(response, request);
3705
-
3706
- request.respond(response[0], response[1], response[2]);
3707
- }
3708
- } catch (e) {
3709
- sinon.logError("Fake server request processing", e);
3710
- }
3711
- },
3712
-
3713
- restore: function restore() {
3714
- return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
3715
- }
3716
- };
3717
- }());
3718
-
3719
- if (typeof module == "object" && typeof require == "function") {
3720
- module.exports = sinon;
3721
- }
3722
-
3723
- /**
3724
- * @depend fake_server.js
3725
- * @depend fake_timers.js
3726
- */
3727
- /*jslint browser: true, eqeqeq: false, onevar: false*/
3728
- /*global sinon*/
3729
- /**
3730
- * Add-on for sinon.fakeServer that automatically handles a fake timer along with
3731
- * the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
3732
- * 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
3733
- * it polls the object for completion with setInterval. Dispite the direct
3734
- * motivation, there is nothing jQuery-specific in this file, so it can be used
3735
- * in any environment where the ajax implementation depends on setInterval or
3736
- * setTimeout.
3737
- *
3738
- * @author Christian Johansen (christian@cjohansen.no)
3739
- * @license BSD
3740
- *
3741
- * Copyright (c) 2010-2013 Christian Johansen
3742
- */
3743
-
3744
- (function () {
3745
- function Server() {
3746
- }
3747
-
3748
- Server.prototype = sinon.fakeServer;
3749
-
3750
- sinon.fakeServerWithClock = new Server();
3751
-
3752
- sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
3753
- if (xhr.async) {
3754
- if (typeof setTimeout.clock == "object") {
3755
- this.clock = setTimeout.clock;
3756
- } else {
3757
- this.clock = sinon.useFakeTimers();
3758
- this.resetClock = true;
3759
- }
3760
-
3761
- if (!this.longestTimeout) {
3762
- var clockSetTimeout = this.clock.setTimeout;
3763
- var clockSetInterval = this.clock.setInterval;
3764
- var server = this;
3765
-
3766
- this.clock.setTimeout = function (fn, timeout) {
3767
- server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
3768
-
3769
- return clockSetTimeout.apply(this, arguments);
3770
- };
3771
-
3772
- this.clock.setInterval = function (fn, timeout) {
3773
- server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
3774
-
3775
- return clockSetInterval.apply(this, arguments);
3776
- };
3777
- }
3778
- }
3779
-
3780
- return sinon.fakeServer.addRequest.call(this, xhr);
3781
- };
3782
-
3783
- sinon.fakeServerWithClock.respond = function respond() {
3784
- var returnVal = sinon.fakeServer.respond.apply(this, arguments);
3785
-
3786
- if (this.clock) {
3787
- this.clock.tick(this.longestTimeout || 0);
3788
- this.longestTimeout = 0;
3789
-
3790
- if (this.resetClock) {
3791
- this.clock.restore();
3792
- this.resetClock = false;
3793
- }
3794
- }
3795
-
3796
- return returnVal;
3797
- };
3798
-
3799
- sinon.fakeServerWithClock.restore = function restore() {
3800
- if (this.clock) {
3801
- this.clock.restore();
3802
- }
3803
-
3804
- return sinon.fakeServer.restore.apply(this, arguments);
3805
- };
3806
- }());
3807
-
3808
- /**
3809
- * @depend ../sinon.js
3810
- * @depend collection.js
3811
- * @depend util/fake_timers.js
3812
- * @depend util/fake_server_with_clock.js
3813
- */
3814
- /*jslint eqeqeq: false, onevar: false, plusplus: false*/
3815
- /*global require, module*/
3816
- /**
3817
- * Manages fake collections as well as fake utilities such as Sinon's
3818
- * timers and fake XHR implementation in one convenient object.
3819
- *
3820
- * @author Christian Johansen (christian@cjohansen.no)
3821
- * @license BSD
3822
- *
3823
- * Copyright (c) 2010-2013 Christian Johansen
3824
- */
3825
-
3826
- if (typeof module == "object" && typeof require == "function") {
3827
- var sinon = require("../sinon");
3828
- sinon.extend(sinon, require("./util/fake_timers"));
3829
- }
3830
-
3831
- (function () {
3832
- var push = [].push;
3833
-
3834
- function exposeValue(sandbox, config, key, value) {
3835
- if (!value) {
3836
- return;
3837
- }
3838
-
3839
- if (config.injectInto) {
3840
- config.injectInto[key] = value;
3841
- } else {
3842
- push.call(sandbox.args, value);
3843
- }
3844
- }
3845
-
3846
- function prepareSandboxFromConfig(config) {
3847
- var sandbox = sinon.create(sinon.sandbox);
3848
-
3849
- if (config.useFakeServer) {
3850
- if (typeof config.useFakeServer == "object") {
3851
- sandbox.serverPrototype = config.useFakeServer;
3852
- }
3853
-
3854
- sandbox.useFakeServer();
3855
- }
3856
-
3857
- if (config.useFakeTimers) {
3858
- if (typeof config.useFakeTimers == "object") {
3859
- sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers);
3860
- } else {
3861
- sandbox.useFakeTimers();
3862
- }
3863
- }
3864
-
3865
- return sandbox;
3866
- }
3867
-
3868
- sinon.sandbox = sinon.extend(sinon.create(sinon.collection), {
3869
- useFakeTimers: function useFakeTimers() {
3870
- this.clock = sinon.useFakeTimers.apply(sinon, arguments);
3871
-
3872
- return this.add(this.clock);
3873
- },
3874
-
3875
- serverPrototype: sinon.fakeServer,
3876
-
3877
- useFakeServer: function useFakeServer() {
3878
- var proto = this.serverPrototype || sinon.fakeServer;
3879
-
3880
- if (!proto || !proto.create) {
3881
- return null;
3882
- }
3883
-
3884
- this.server = proto.create();
3885
- return this.add(this.server);
3886
- },
3887
-
3888
- inject: function (obj) {
3889
- sinon.collection.inject.call(this, obj);
3890
-
3891
- if (this.clock) {
3892
- obj.clock = this.clock;
3893
- }
3894
-
3895
- if (this.server) {
3896
- obj.server = this.server;
3897
- obj.requests = this.server.requests;
3898
- }
3899
-
3900
- return obj;
3901
- },
3902
-
3903
- create: function (config) {
3904
- if (!config) {
3905
- return sinon.create(sinon.sandbox);
3906
- }
3907
-
3908
- var sandbox = prepareSandboxFromConfig(config);
3909
- sandbox.args = sandbox.args || [];
3910
- var prop, value, exposed = sandbox.inject({});
3911
-
3912
- if (config.properties) {
3913
- for (var i = 0, l = config.properties.length; i < l; i++) {
3914
- prop = config.properties[i];
3915
- value = exposed[prop] || prop == "sandbox" && sandbox;
3916
- exposeValue(sandbox, config, prop, value);
3917
- }
3918
- } else {
3919
- exposeValue(sandbox, config, "sandbox", value);
3920
- }
3921
-
3922
- return sandbox;
3923
- }
3924
- });
3925
-
3926
- sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;
3927
-
3928
- if (typeof module == "object" && typeof require == "function") {
3929
- module.exports = sinon.sandbox;
3930
- }
3931
- }());
3932
-
3933
- /**
3934
- * @depend ../sinon.js
3935
- * @depend stub.js
3936
- * @depend mock.js
3937
- * @depend sandbox.js
3938
- */
3939
- /*jslint eqeqeq: false, onevar: false, forin: true, plusplus: false*/
3940
- /*global module, require, sinon*/
3941
- /**
3942
- * Test function, sandboxes fakes
3943
- *
3944
- * @author Christian Johansen (christian@cjohansen.no)
3945
- * @license BSD
3946
- *
3947
- * Copyright (c) 2010-2013 Christian Johansen
3948
- */
3949
-
3950
- (function (sinon) {
3951
- var commonJSModule = typeof module == "object" && typeof require == "function";
3952
-
3953
- if (!sinon && commonJSModule) {
3954
- sinon = require("../sinon");
3955
- }
3956
-
3957
- if (!sinon) {
3958
- return;
3959
- }
3960
-
3961
- function test(callback) {
3962
- var type = typeof callback;
3963
-
3964
- if (type != "function") {
3965
- throw new TypeError("sinon.test needs to wrap a test function, got " + type);
3966
- }
3967
-
3968
- return function () {
3969
- var config = sinon.getConfig(sinon.config);
3970
- config.injectInto = config.injectIntoThis && this || config.injectInto;
3971
- var sandbox = sinon.sandbox.create(config);
3972
- var exception, result;
3973
- var args = Array.prototype.slice.call(arguments).concat(sandbox.args);
3974
-
3975
- try {
3976
- result = callback.apply(this, args);
3977
- } catch (e) {
3978
- exception = e;
3979
- }
3980
-
3981
- if (typeof exception !== "undefined") {
3982
- sandbox.restore();
3983
- throw exception;
3984
- }
3985
- else {
3986
- sandbox.verifyAndRestore();
3987
- }
3988
-
3989
- return result;
3990
- };
3991
- }
3992
-
3993
- test.config = {
3994
- injectIntoThis: true,
3995
- injectInto: null,
3996
- properties: ["spy", "stub", "mock", "clock", "server", "requests"],
3997
- useFakeTimers: true,
3998
- useFakeServer: true
3999
- };
4000
-
4001
- if (commonJSModule) {
4002
- module.exports = test;
4003
- } else {
4004
- sinon.test = test;
4005
- }
4006
- }(typeof sinon == "object" && sinon || null));
4007
-
4008
- /**
4009
- * @depend ../sinon.js
4010
- * @depend test.js
4011
- */
4012
- /*jslint eqeqeq: false, onevar: false, eqeqeq: false*/
4013
- /*global module, require, sinon*/
4014
- /**
4015
- * Test case, sandboxes all test functions
4016
- *
4017
- * @author Christian Johansen (christian@cjohansen.no)
4018
- * @license BSD
4019
- *
4020
- * Copyright (c) 2010-2013 Christian Johansen
4021
- */
4022
-
4023
- (function (sinon) {
4024
- var commonJSModule = typeof module == "object" && typeof require == "function";
4025
-
4026
- if (!sinon && commonJSModule) {
4027
- sinon = require("../sinon");
4028
- }
4029
-
4030
- if (!sinon || !Object.prototype.hasOwnProperty) {
4031
- return;
4032
- }
4033
-
4034
- function createTest(property, setUp, tearDown) {
4035
- return function () {
4036
- if (setUp) {
4037
- setUp.apply(this, arguments);
4038
- }
4039
-
4040
- var exception, result;
4041
-
4042
- try {
4043
- result = property.apply(this, arguments);
4044
- } catch (e) {
4045
- exception = e;
4046
- }
4047
-
4048
- if (tearDown) {
4049
- tearDown.apply(this, arguments);
4050
- }
4051
-
4052
- if (exception) {
4053
- throw exception;
4054
- }
4055
-
4056
- return result;
4057
- };
4058
- }
4059
-
4060
- function testCase(tests, prefix) {
4061
- /*jsl:ignore*/
4062
- if (!tests || typeof tests != "object") {
4063
- throw new TypeError("sinon.testCase needs an object with test functions");
4064
- }
4065
- /*jsl:end*/
4066
-
4067
- prefix = prefix || "test";
4068
- var rPrefix = new RegExp("^" + prefix);
4069
- var methods = {}, testName, property, method;
4070
- var setUp = tests.setUp;
4071
- var tearDown = tests.tearDown;
4072
-
4073
- for (testName in tests) {
4074
- if (tests.hasOwnProperty(testName)) {
4075
- property = tests[testName];
4076
-
4077
- if (/^(setUp|tearDown)$/.test(testName)) {
4078
- continue;
4079
- }
4080
-
4081
- if (typeof property == "function" && rPrefix.test(testName)) {
4082
- method = property;
4083
-
4084
- if (setUp || tearDown) {
4085
- method = createTest(property, setUp, tearDown);
4086
- }
4087
-
4088
- methods[testName] = sinon.test(method);
4089
- } else {
4090
- methods[testName] = tests[testName];
4091
- }
4092
- }
4093
- }
4094
-
4095
- return methods;
4096
- }
4097
-
4098
- if (commonJSModule) {
4099
- module.exports = testCase;
4100
- } else {
4101
- sinon.testCase = testCase;
4102
- }
4103
- }(typeof sinon == "object" && sinon || null));
4104
-
4105
- /**
4106
- * @depend ../sinon.js
4107
- * @depend stub.js
4108
- */
4109
- /*jslint eqeqeq: false, onevar: false, nomen: false, plusplus: false*/
4110
- /*global module, require, sinon*/
4111
- /**
4112
- * Assertions matching the test spy retrieval interface.
4113
- *
4114
- * @author Christian Johansen (christian@cjohansen.no)
4115
- * @license BSD
4116
- *
4117
- * Copyright (c) 2010-2013 Christian Johansen
4118
- */
4119
-
4120
- (function (sinon, global) {
4121
- var commonJSModule = typeof module == "object" && typeof require == "function";
4122
- var slice = Array.prototype.slice;
4123
- var assert;
4124
-
4125
- if (!sinon && commonJSModule) {
4126
- sinon = require("../sinon");
4127
- }
4128
-
4129
- if (!sinon) {
4130
- return;
4131
- }
4132
-
4133
- function verifyIsStub() {
4134
- var method;
4135
-
4136
- for (var i = 0, l = arguments.length; i < l; ++i) {
4137
- method = arguments[i];
4138
-
4139
- if (!method) {
4140
- assert.fail("fake is not a spy");
4141
- }
4142
-
4143
- if (typeof method != "function") {
4144
- assert.fail(method + " is not a function");
4145
- }
4146
-
4147
- if (typeof method.getCall != "function") {
4148
- assert.fail(method + " is not stubbed");
4149
- }
4150
- }
4151
- }
4152
-
4153
- function failAssertion(object, msg) {
4154
- object = object || global;
4155
- var failMethod = object.fail || assert.fail;
4156
- failMethod.call(object, msg);
4157
- }
4158
-
4159
- function mirrorPropAsAssertion(name, method, message) {
4160
- if (arguments.length == 2) {
4161
- message = method;
4162
- method = name;
4163
- }
4164
-
4165
- assert[name] = function (fake) {
4166
- verifyIsStub(fake);
4167
-
4168
- var args = slice.call(arguments, 1);
4169
- var failed = false;
4170
-
4171
- if (typeof method == "function") {
4172
- failed = !method(fake);
4173
- } else {
4174
- failed = typeof fake[method] == "function" ?
4175
- !fake[method].apply(fake, args) : !fake[method];
4176
- }
4177
-
4178
- if (failed) {
4179
- failAssertion(this, fake.printf.apply(fake, [message].concat(args)));
4180
- } else {
4181
- assert.pass(name);
4182
- }
4183
- };
4184
- }
4185
-
4186
- function exposedName(prefix, prop) {
4187
- return !prefix || /^fail/.test(prop) ? prop :
4188
- prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
4189
- }
4190
-
4191
- ;
4192
-
4193
- assert = {
4194
- failException: "AssertError",
4195
-
4196
- fail: function fail(message) {
4197
- var error = new Error(message);
4198
- error.name = this.failException || assert.failException;
4199
-
4200
- throw error;
4201
- },
4202
-
4203
- pass: function pass(assertion) {
4204
- },
4205
-
4206
- callOrder: function assertCallOrder() {
4207
- verifyIsStub.apply(null, arguments);
4208
- var expected = "", actual = "";
4209
-
4210
- if (!sinon.calledInOrder(arguments)) {
4211
- try {
4212
- expected = [].join.call(arguments, ", ");
4213
- actual = sinon.orderByFirstCall(slice.call(arguments)).join(", ");
4214
- } catch (e) {
4215
- // If this fails, we'll just fall back to the blank string
4216
- }
4217
-
4218
- failAssertion(this, "expected " + expected + " to be " +
4219
- "called in order but were called as " + actual);
4220
- } else {
4221
- assert.pass("callOrder");
4222
- }
4223
- },
4224
-
4225
- callCount: function assertCallCount(method, count) {
4226
- verifyIsStub(method);
4227
-
4228
- if (method.callCount != count) {
4229
- var msg = "expected %n to be called " + sinon.timesInWords(count) +
4230
- " but was called %c%C";
4231
- failAssertion(this, method.printf(msg));
4232
- } else {
4233
- assert.pass("callCount");
4234
- }
4235
- },
4236
-
4237
- expose: function expose(target, options) {
4238
- if (!target) {
4239
- throw new TypeError("target is null or undefined");
4240
- }
4241
-
4242
- var o = options || {};
4243
- var prefix = typeof o.prefix == "undefined" && "assert" || o.prefix;
4244
- var includeFail = typeof o.includeFail == "undefined" || !!o.includeFail;
4245
-
4246
- for (var method in this) {
4247
- if (method != "export" && (includeFail || !/^(fail)/.test(method))) {
4248
- target[exposedName(prefix, method)] = this[method];
4249
- }
4250
- }
4251
-
4252
- return target;
4253
- }
4254
- };
4255
-
4256
- mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
4257
- mirrorPropAsAssertion("notCalled", function (spy) {
4258
- return !spy.called;
4259
- },
4260
- "expected %n to not have been called but was called %c%C");
4261
- mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
4262
- mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
4263
- mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
4264
- mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
4265
- mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
4266
- mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
4267
- mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
4268
- mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
4269
- mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
4270
- mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
4271
- mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
4272
- mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
4273
- mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
4274
- mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
4275
- mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
4276
- mirrorPropAsAssertion("threw", "%n did not throw exception%C");
4277
- mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
4278
-
4279
- if (commonJSModule) {
4280
- module.exports = assert;
4281
- } else {
4282
- sinon.assert = assert;
4283
- }
4284
- }(typeof sinon == "object" && sinon || null, typeof window != "undefined" ? window : global));
4285
-
4286
- return sinon;
4287
- }.call(typeof window != 'undefined' && window || {}));