1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | (function(root, factory) {
|
37 | if (typeof define === 'function' && define.amd) {
|
38 | define([], function() {
|
39 | return (root.sinon = factory());
|
40 | });
|
41 | } else if (typeof exports === 'object') {
|
42 | module.exports = factory();
|
43 | } else {
|
44 | root.sinon = factory();
|
45 | }
|
46 | }(this, function() {
|
47 | var samsam, formatio;
|
48 | (function() {
|
49 | function define(mod, deps, fn) {
|
50 | if (mod == "samsam") {
|
51 | samsam = deps();
|
52 | } else if (typeof deps === "function" && mod.length === 0) {
|
53 | lolex = deps();
|
54 | } else if (typeof fn === "function") {
|
55 | formatio = fn(samsam);
|
56 | }
|
57 | }
|
58 | define.amd = {};
|
59 | ((typeof define === "function" && define.amd && function(m) {
|
60 | define("samsam", m);
|
61 | }) ||
|
62 | (typeof module === "object" &&
|
63 | function(m) {
|
64 | module.exports = m();
|
65 | }) ||
|
66 |
|
67 | function(m) {
|
68 | this.samsam = m();
|
69 | }
|
70 | )(function() {
|
71 | var o = Object.prototype;
|
72 | var div = typeof document !== "undefined" && document.createElement("div");
|
73 |
|
74 | function isNaN(value) {
|
75 |
|
76 |
|
77 |
|
78 | var val = value;
|
79 | return typeof value === "number" && value !== val;
|
80 | }
|
81 |
|
82 | function getClass(value) {
|
83 |
|
84 |
|
85 |
|
86 | return o.toString.call(value).split(/[ \]]/)[1];
|
87 | }
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | function isArguments(object) {
|
98 | if (getClass(object) === 'Arguments') {
|
99 | return true;
|
100 | }
|
101 | if (typeof object !== "object" || typeof object.length !== "number" ||
|
102 | getClass(object) === "Array") {
|
103 | return false;
|
104 | }
|
105 | if (typeof object.callee == "function") {
|
106 | return true;
|
107 | }
|
108 | try {
|
109 | object[object.length] = 6;
|
110 | delete object[object.length];
|
111 | } catch (e) {
|
112 | return true;
|
113 | }
|
114 | return false;
|
115 | }
|
116 |
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function isElement(object) {
|
128 | if (!object || object.nodeType !== 1 || !div) {
|
129 | return false;
|
130 | }
|
131 | try {
|
132 | object.appendChild(div);
|
133 | object.removeChild(div);
|
134 | } catch (e) {
|
135 | return false;
|
136 | }
|
137 | return true;
|
138 | }
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | function keys(object) {
|
148 | var ks = [],
|
149 | prop;
|
150 | for (prop in object) {
|
151 | if (o.hasOwnProperty.call(object, prop)) {
|
152 | ks.push(prop);
|
153 | }
|
154 | }
|
155 | return ks;
|
156 | }
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function isDate(value) {
|
169 | return typeof value.getTime == "function" &&
|
170 | value.getTime() == value.valueOf();
|
171 | }
|
172 |
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | function isNegZero(value) {
|
181 | return value === 0 && 1 / value === -Infinity;
|
182 | }
|
183 |
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | function identical(obj1, obj2) {
|
197 | if (obj1 === obj2 || (isNaN(obj1) && isNaN(obj2))) {
|
198 | return obj1 !== 0 || isNegZero(obj1) === isNegZero(obj2);
|
199 | }
|
200 | }
|
201 |
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | function deepEqualCyclic(obj1, obj2) {
|
220 |
|
221 |
|
222 |
|
223 | var objects1 = [],
|
224 | objects2 = [],
|
225 |
|
226 |
|
227 |
|
228 | paths1 = [],
|
229 | paths2 = [],
|
230 |
|
231 |
|
232 | compared = {};
|
233 |
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | function isObject(value) {
|
241 |
|
242 | if (typeof value === 'object' && value !== null && !(value instanceof Boolean) && !(value instanceof Date) && !(value instanceof Number) && !(value instanceof RegExp) && !(value instanceof String)) {
|
243 |
|
244 | return true;
|
245 | }
|
246 |
|
247 | return false;
|
248 | }
|
249 |
|
250 | |
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | function getIndex(objects, obj) {
|
257 |
|
258 | var i;
|
259 | for (i = 0; i < objects.length; i++) {
|
260 | if (objects[i] === obj) {
|
261 | return i;
|
262 | }
|
263 | }
|
264 |
|
265 | return -1;
|
266 | }
|
267 |
|
268 |
|
269 | return (function deepEqual(obj1, obj2, path1, path2) {
|
270 | var type1 = typeof obj1;
|
271 | var type2 = typeof obj2;
|
272 |
|
273 |
|
274 | if (obj1 === obj2 ||
|
275 | isNaN(obj1) || isNaN(obj2) ||
|
276 | obj1 == null || obj2 == null ||
|
277 | type1 !== "object" || type2 !== "object") {
|
278 |
|
279 | return identical(obj1, obj2);
|
280 | }
|
281 |
|
282 |
|
283 | if (isElement(obj1) || isElement(obj2)) {
|
284 | return false;
|
285 | }
|
286 |
|
287 | var isDate1 = isDate(obj1),
|
288 | isDate2 = isDate(obj2);
|
289 | if (isDate1 || isDate2) {
|
290 | if (!isDate1 || !isDate2 || obj1.getTime() !== obj2.getTime()) {
|
291 | return false;
|
292 | }
|
293 | }
|
294 |
|
295 | if (obj1 instanceof RegExp && obj2 instanceof RegExp) {
|
296 | if (obj1.toString() !== obj2.toString()) {
|
297 | return false;
|
298 | }
|
299 | }
|
300 |
|
301 | var class1 = getClass(obj1);
|
302 | var class2 = getClass(obj2);
|
303 | var keys1 = keys(obj1);
|
304 | var keys2 = keys(obj2);
|
305 |
|
306 | if (isArguments(obj1) || isArguments(obj2)) {
|
307 | if (obj1.length !== obj2.length) {
|
308 | return false;
|
309 | }
|
310 | } else {
|
311 | if (type1 !== type2 || class1 !== class2 ||
|
312 | keys1.length !== keys2.length) {
|
313 | return false;
|
314 | }
|
315 | }
|
316 |
|
317 | var key, i, l,
|
318 |
|
319 | value1, value2,
|
320 | isObject1, isObject2,
|
321 | index1, index2,
|
322 | newPath1, newPath2;
|
323 |
|
324 | for (i = 0, l = keys1.length; i < l; i++) {
|
325 | key = keys1[i];
|
326 | if (!o.hasOwnProperty.call(obj2, key)) {
|
327 | return false;
|
328 | }
|
329 |
|
330 |
|
331 |
|
332 | value1 = obj1[key];
|
333 | value2 = obj2[key];
|
334 |
|
335 | isObject1 = isObject(value1);
|
336 | isObject2 = isObject(value2);
|
337 |
|
338 |
|
339 |
|
340 |
|
341 | index1 = isObject1 ? getIndex(objects1, value1) : -1;
|
342 | index2 = isObject2 ? getIndex(objects2, value2) : -1;
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | newPath1 = index1 !== -1 ? paths1[index1] : path1 + '[' + JSON.stringify(key) + ']';
|
349 | newPath2 = index2 !== -1 ? paths2[index2] : path2 + '[' + JSON.stringify(key) + ']';
|
350 |
|
351 |
|
352 | if (compared[newPath1 + newPath2]) {
|
353 | return true;
|
354 | }
|
355 |
|
356 |
|
357 | if (index1 === -1 && isObject1) {
|
358 | objects1.push(value1);
|
359 | paths1.push(newPath1);
|
360 | }
|
361 | if (index2 === -1 && isObject2) {
|
362 | objects2.push(value2);
|
363 | paths2.push(newPath2);
|
364 | }
|
365 |
|
366 |
|
367 | if (isObject1 && isObject2) {
|
368 | compared[newPath1 + newPath2] = true;
|
369 | }
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | if (!deepEqual(value1, value2, newPath1, newPath2)) {
|
376 | return false;
|
377 | }
|
378 | }
|
379 |
|
380 | return true;
|
381 |
|
382 | }(obj1, obj2, '$1', '$2'));
|
383 | }
|
384 |
|
385 | var match;
|
386 |
|
387 | function arrayContains(array, subset) {
|
388 | if (subset.length === 0) {
|
389 | return true;
|
390 | }
|
391 | var i, l, j, k;
|
392 | for (i = 0, l = array.length; i < l; ++i) {
|
393 | if (match(array[i], subset[0])) {
|
394 | for (j = 0, k = subset.length; j < k; ++j) {
|
395 | if (!match(array[i + j], subset[j])) {
|
396 | return false;
|
397 | }
|
398 | }
|
399 | return true;
|
400 | }
|
401 | }
|
402 | return false;
|
403 | }
|
404 |
|
405 | |
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 | match = function match(object, matcher) {
|
413 | if (matcher && typeof matcher.test === "function") {
|
414 | return matcher.test(object);
|
415 | }
|
416 |
|
417 | if (typeof matcher === "function") {
|
418 | return matcher(object) === true;
|
419 | }
|
420 |
|
421 | if (typeof matcher === "string") {
|
422 | matcher = matcher.toLowerCase();
|
423 | var notNull = typeof object === "string" || !! object;
|
424 | return notNull &&
|
425 | (String(object)).toLowerCase().indexOf(matcher) >= 0;
|
426 | }
|
427 |
|
428 | if (typeof matcher === "number") {
|
429 | return matcher === object;
|
430 | }
|
431 |
|
432 | if (typeof matcher === "boolean") {
|
433 | return matcher === object;
|
434 | }
|
435 |
|
436 | if (getClass(object) === "Array" && getClass(matcher) === "Array") {
|
437 | return arrayContains(object, matcher);
|
438 | }
|
439 |
|
440 | if (matcher && typeof matcher === "object") {
|
441 | var prop;
|
442 | for (prop in matcher) {
|
443 | var value = object[prop];
|
444 | if (typeof value === "undefined" &&
|
445 | typeof object.getAttribute === "function") {
|
446 | value = object.getAttribute(prop);
|
447 | }
|
448 | if (typeof value === "undefined" || !match(value, matcher[prop])) {
|
449 | return false;
|
450 | }
|
451 | }
|
452 | return true;
|
453 | }
|
454 |
|
455 | throw new Error("Matcher was not a string, a number, a " +
|
456 | "function, a boolean or an object");
|
457 | };
|
458 |
|
459 | return {
|
460 | isArguments: isArguments,
|
461 | isElement: isElement,
|
462 | isDate: isDate,
|
463 | isNegZero: isNegZero,
|
464 | identical: identical,
|
465 | deepEqual: deepEqualCyclic,
|
466 | match: match,
|
467 | keys: keys
|
468 | };
|
469 | });
|
470 | ((typeof define === "function" && define.amd && function(m) {
|
471 | define("formatio", ["samsam"], m);
|
472 | }) || (typeof module === "object" && function(m) {
|
473 | module.exports = m(require("samsam"));
|
474 | }) || function(m) {
|
475 | this.formatio = m(this.samsam);
|
476 | })(function(samsam) {
|
477 |
|
478 | var formatio = {
|
479 | excludeConstructors: ["Object", /^.$/],
|
480 | quoteStrings: true,
|
481 | limitChildrenCount: 0
|
482 | };
|
483 |
|
484 | var hasOwn = Object.prototype.hasOwnProperty;
|
485 |
|
486 | var specialObjects = [];
|
487 | if (typeof global !== "undefined") {
|
488 | specialObjects.push({
|
489 | object: global,
|
490 | value: "[object global]"
|
491 | });
|
492 | }
|
493 | if (typeof document !== "undefined") {
|
494 | specialObjects.push({
|
495 | object: document,
|
496 | value: "[object HTMLDocument]"
|
497 | });
|
498 | }
|
499 | if (typeof window !== "undefined") {
|
500 | specialObjects.push({
|
501 | object: window,
|
502 | value: "[object Window]"
|
503 | });
|
504 | }
|
505 |
|
506 | function functionName(func) {
|
507 | if (!func) {
|
508 | return "";
|
509 | }
|
510 | if (func.displayName) {
|
511 | return func.displayName;
|
512 | }
|
513 | if (func.name) {
|
514 | return func.name;
|
515 | }
|
516 | var matches = func.toString().match(/function\s+([^\(]+)/m);
|
517 | return (matches && matches[1]) || "";
|
518 | }
|
519 |
|
520 | function constructorName(f, object) {
|
521 | var name = functionName(object && object.constructor);
|
522 | var excludes = f.excludeConstructors ||
|
523 | formatio.excludeConstructors || [];
|
524 |
|
525 | var i, l;
|
526 | for (i = 0, l = excludes.length; i < l; ++i) {
|
527 | if (typeof excludes[i] === "string" && excludes[i] === name) {
|
528 | return "";
|
529 | } else if (excludes[i].test && excludes[i].test(name)) {
|
530 | return "";
|
531 | }
|
532 | }
|
533 |
|
534 | return name;
|
535 | }
|
536 |
|
537 | function isCircular(object, objects) {
|
538 | if (typeof object !== "object") {
|
539 | return false;
|
540 | }
|
541 | var i, l;
|
542 | for (i = 0, l = objects.length; i < l; ++i) {
|
543 | if (objects[i] === object) {
|
544 | return true;
|
545 | }
|
546 | }
|
547 | return false;
|
548 | }
|
549 |
|
550 | function ascii(f, object, processed, indent) {
|
551 | if (typeof object === "string") {
|
552 | var qs = f.quoteStrings;
|
553 | var quote = typeof qs !== "boolean" || qs;
|
554 | return processed || quote ? '"' + object + '"' : object;
|
555 | }
|
556 |
|
557 | if (typeof object === "function" && !(object instanceof RegExp)) {
|
558 | return ascii.func(object);
|
559 | }
|
560 |
|
561 | processed = processed || [];
|
562 |
|
563 | if (isCircular(object, processed)) {
|
564 | return "[Circular]";
|
565 | }
|
566 |
|
567 | if (Object.prototype.toString.call(object) === "[object Array]") {
|
568 | return ascii.array.call(f, object, processed);
|
569 | }
|
570 |
|
571 | if (!object) {
|
572 | return String((1 / object) === -Infinity ? "-0" : object);
|
573 | }
|
574 | if (samsam.isElement(object)) {
|
575 | return ascii.element(object);
|
576 | }
|
577 |
|
578 | if (typeof object.toString === "function" &&
|
579 | object.toString !== Object.prototype.toString) {
|
580 | return object.toString();
|
581 | }
|
582 |
|
583 | var i, l;
|
584 | for (i = 0, l = specialObjects.length; i < l; i++) {
|
585 | if (object === specialObjects[i].object) {
|
586 | return specialObjects[i].value;
|
587 | }
|
588 | }
|
589 |
|
590 | return ascii.object.call(f, object, processed, indent);
|
591 | }
|
592 |
|
593 | ascii.func = function(func) {
|
594 | return "function " + functionName(func) + "() {}";
|
595 | };
|
596 |
|
597 | ascii.array = function(array, processed) {
|
598 | processed = processed || [];
|
599 | processed.push(array);
|
600 | var pieces = [];
|
601 | var i, l;
|
602 | l = (this.limitChildrenCount > 0) ?
|
603 | Math.min(this.limitChildrenCount, array.length) : array.length;
|
604 |
|
605 | for (i = 0; i < l; ++i) {
|
606 | pieces.push(ascii(this, array[i], processed));
|
607 | }
|
608 |
|
609 | if (l < array.length)
|
610 | pieces.push("[... " + (array.length - l) + " more elements]");
|
611 |
|
612 | return "[" + pieces.join(", ") + "]";
|
613 | };
|
614 |
|
615 | ascii.object = function(object, processed, indent) {
|
616 | processed = processed || [];
|
617 | processed.push(object);
|
618 | indent = indent || 0;
|
619 | var pieces = [],
|
620 | properties = samsam.keys(object).sort();
|
621 | var length = 3;
|
622 | var prop, str, obj, i, k, l;
|
623 | l = (this.limitChildrenCount > 0) ?
|
624 | Math.min(this.limitChildrenCount, properties.length) : properties.length;
|
625 |
|
626 | for (i = 0; i < l; ++i) {
|
627 | prop = properties[i];
|
628 | obj = object[prop];
|
629 |
|
630 | if (isCircular(obj, processed)) {
|
631 | str = "[Circular]";
|
632 | } else {
|
633 | str = ascii(this, obj, processed, indent + 2);
|
634 | }
|
635 |
|
636 | str = (/\s/.test(prop) ? '"' + prop + '"' : prop) + ": " + str;
|
637 | length += str.length;
|
638 | pieces.push(str);
|
639 | }
|
640 |
|
641 | var cons = constructorName(this, object);
|
642 | var prefix = cons ? "[" + cons + "] " : "";
|
643 | var is = "";
|
644 | for (i = 0, k = indent; i < k; ++i) {
|
645 | is += " ";
|
646 | }
|
647 |
|
648 | if (l < properties.length)
|
649 | pieces.push("[... " + (properties.length - l) + " more elements]");
|
650 |
|
651 | if (length + indent > 80) {
|
652 | return prefix + "{\n " + is + pieces.join(",\n " + is) + "\n" +
|
653 | is + "}";
|
654 | }
|
655 | return prefix + "{ " + pieces.join(", ") + " }";
|
656 | };
|
657 |
|
658 | ascii.element = function(element) {
|
659 | var tagName = element.tagName.toLowerCase();
|
660 | var attrs = element.attributes,
|
661 | attr, pairs = [],
|
662 | attrName, i, l, val;
|
663 |
|
664 | for (i = 0, l = attrs.length; i < l; ++i) {
|
665 | attr = attrs.item(i);
|
666 | attrName = attr.nodeName.toLowerCase().replace("html:", "");
|
667 | val = attr.nodeValue;
|
668 | if (attrName !== "contenteditable" || val !== "inherit") {
|
669 | if ( !! val) {
|
670 | pairs.push(attrName + "=\"" + val + "\"");
|
671 | }
|
672 | }
|
673 | }
|
674 |
|
675 | var formatted = "<" + tagName + (pairs.length > 0 ? " " : "");
|
676 | var content = element.innerHTML;
|
677 |
|
678 | if (content.length > 20) {
|
679 | content = content.substr(0, 20) + "[...]";
|
680 | }
|
681 |
|
682 | var res = formatted + pairs.join(" ") + ">" + content +
|
683 | "</" + tagName + ">";
|
684 |
|
685 | return res.replace(/ contentEditable="inherit"/, "");
|
686 | };
|
687 |
|
688 | function Formatio(options) {
|
689 | for (var opt in options) {
|
690 | this[opt] = options[opt];
|
691 | }
|
692 | }
|
693 |
|
694 | Formatio.prototype = {
|
695 | functionName: functionName,
|
696 |
|
697 | configure: function(options) {
|
698 | return new Formatio(options);
|
699 | },
|
700 |
|
701 | constructorName: function(object) {
|
702 | return constructorName(this, object);
|
703 | },
|
704 |
|
705 | ascii: function(object, processed, indent) {
|
706 | return ascii(this, object, processed, indent);
|
707 | }
|
708 | };
|
709 |
|
710 | return Formatio.prototype;
|
711 | });
|
712 | ! function(e) {
|
713 | if ("object" == typeof exports && "undefined" != typeof module) module.exports = e();
|
714 | else if ("function" == typeof define && define.amd) define([], e);
|
715 | else {
|
716 | var f;
|
717 | "undefined" != typeof window ? f = window : "undefined" != typeof global ? f = global : "undefined" != typeof self && (f = self), f.lolex = e()
|
718 | }
|
719 | }(function() {
|
720 | var define, module, exports;
|
721 | return (function e(t, n, r) {
|
722 | function s(o, u) {
|
723 | if (!n[o]) {
|
724 | if (!t[o]) {
|
725 | var a = typeof require == "function" && require;
|
726 | if (!u && a) return a(o, !0);
|
727 | if (i) return i(o, !0);
|
728 | var f = new Error("Cannot find module '" + o + "'");
|
729 | throw f.code = "MODULE_NOT_FOUND", f
|
730 | }
|
731 | var l = n[o] = {
|
732 | exports: {}
|
733 | };
|
734 | t[o][0].call(l.exports, function(e) {
|
735 | var n = t[o][1][e];
|
736 | return s(n ? n : e)
|
737 | }, l, l.exports, e, t, n, r)
|
738 | }
|
739 | return n[o].exports
|
740 | }
|
741 | var i = typeof require == "function" && require;
|
742 | for (var o = 0; o < r.length; o++) s(r[o]);
|
743 | return s
|
744 | })({
|
745 | 1: [
|
746 | function(require, module, exports) {
|
747 | (function(global) {
|
748 |
|
749 |
|
750 | |
751 |
|
752 |
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 | var timeoutResult = setTimeout(function() {}, 0);
|
761 | var addTimerReturnsObject = typeof timeoutResult === "object";
|
762 | clearTimeout(timeoutResult);
|
763 |
|
764 | var NativeDate = Date;
|
765 | var id = 1;
|
766 |
|
767 | |
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 | function parseTime(str) {
|
774 | if (!str) {
|
775 | return 0;
|
776 | }
|
777 |
|
778 | var strings = str.split(":");
|
779 | var l = strings.length,
|
780 | i = l;
|
781 | var ms = 0,
|
782 | parsed;
|
783 |
|
784 | if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
|
785 | throw new Error("tick only understands numbers and 'h:m:s'");
|
786 | }
|
787 |
|
788 | while (i--) {
|
789 | parsed = parseInt(strings[i], 10);
|
790 |
|
791 | if (parsed >= 60) {
|
792 | throw new Error("Invalid time " + str);
|
793 | }
|
794 |
|
795 | ms += parsed * Math.pow(60, (l - i - 1));
|
796 | }
|
797 |
|
798 | return ms * 1000;
|
799 | }
|
800 |
|
801 | |
802 |
|
803 |
|
804 |
|
805 | function getEpoch(epoch) {
|
806 | if (!epoch) {
|
807 | return 0;
|
808 | }
|
809 | if (typeof epoch.getTime === "function") {
|
810 | return epoch.getTime();
|
811 | }
|
812 | if (typeof epoch === "number") {
|
813 | return epoch;
|
814 | }
|
815 | throw new TypeError("now should be milliseconds since UNIX epoch");
|
816 | }
|
817 |
|
818 | function inRange(from, to, timer) {
|
819 | return timer && timer.callAt >= from && timer.callAt <= to;
|
820 | }
|
821 |
|
822 | function mirrorDateProperties(target, source) {
|
823 | if (source.now) {
|
824 | target.now = function now() {
|
825 | return target.clock.now;
|
826 | };
|
827 | } else {
|
828 | delete target.now;
|
829 | }
|
830 |
|
831 | if (source.toSource) {
|
832 | target.toSource = function toSource() {
|
833 | return source.toSource();
|
834 | };
|
835 | } else {
|
836 | delete target.toSource;
|
837 | }
|
838 |
|
839 | target.toString = function toString() {
|
840 | return source.toString();
|
841 | };
|
842 |
|
843 | target.prototype = source.prototype;
|
844 | target.parse = source.parse;
|
845 | target.UTC = source.UTC;
|
846 | target.prototype.toUTCString = source.prototype.toUTCString;
|
847 |
|
848 | for (var prop in source) {
|
849 | if (source.hasOwnProperty(prop)) {
|
850 | target[prop] = source[prop];
|
851 | }
|
852 | }
|
853 |
|
854 | return target;
|
855 | }
|
856 |
|
857 | function createDate() {
|
858 | function ClockDate(year, month, date, hour, minute, second, ms) {
|
859 |
|
860 |
|
861 | switch (arguments.length) {
|
862 | case 0:
|
863 | return new NativeDate(ClockDate.clock.now);
|
864 | case 1:
|
865 | return new NativeDate(year);
|
866 | case 2:
|
867 | return new NativeDate(year, month);
|
868 | case 3:
|
869 | return new NativeDate(year, month, date);
|
870 | case 4:
|
871 | return new NativeDate(year, month, date, hour);
|
872 | case 5:
|
873 | return new NativeDate(year, month, date, hour, minute);
|
874 | case 6:
|
875 | return new NativeDate(year, month, date, hour, minute, second);
|
876 | default:
|
877 | return new NativeDate(year, month, date, hour, minute, second, ms);
|
878 | }
|
879 | }
|
880 |
|
881 | return mirrorDateProperties(ClockDate, NativeDate);
|
882 | }
|
883 |
|
884 | function addTimer(clock, timer) {
|
885 | if (typeof timer.func === "undefined") {
|
886 | throw new Error("Callback must be provided to timer calls");
|
887 | }
|
888 |
|
889 | if (!clock.timers) {
|
890 | clock.timers = {};
|
891 | }
|
892 |
|
893 | timer.id = id++;
|
894 | timer.createdAt = clock.now;
|
895 | timer.callAt = clock.now + (timer.delay || 0);
|
896 |
|
897 | clock.timers[timer.id] = timer;
|
898 |
|
899 | if (addTimerReturnsObject) {
|
900 | return {
|
901 | id: timer.id,
|
902 | ref: function() {},
|
903 | unref: function() {}
|
904 | };
|
905 | } else {
|
906 | return timer.id;
|
907 | }
|
908 | }
|
909 |
|
910 | function firstTimerInRange(clock, from, to) {
|
911 | var timers = clock.timers,
|
912 | timer = null;
|
913 |
|
914 | for (var id in timers) {
|
915 | if (!inRange(from, to, timers[id])) {
|
916 | continue;
|
917 | }
|
918 |
|
919 | if (!timer || ~compareTimers(timer, timers[id])) {
|
920 | timer = timers[id];
|
921 | }
|
922 | }
|
923 |
|
924 | return timer;
|
925 | }
|
926 |
|
927 | function compareTimers(a, b) {
|
928 |
|
929 | if (a.callAt < b.callAt) {
|
930 | return -1;
|
931 | }
|
932 | if (a.callAt > b.callAt) {
|
933 | return 1;
|
934 | }
|
935 |
|
936 |
|
937 | if (a.immediate && !b.immediate) {
|
938 | return -1;
|
939 | }
|
940 | if (!a.immediate && b.immediate) {
|
941 | return 1;
|
942 | }
|
943 |
|
944 |
|
945 | if (a.createdAt < b.createdAt) {
|
946 | return -1;
|
947 | }
|
948 | if (a.createdAt > b.createdAt) {
|
949 | return 1;
|
950 | }
|
951 |
|
952 |
|
953 | if (a.id < b.id) {
|
954 | return -1;
|
955 | }
|
956 | if (a.id > b.id) {
|
957 | return 1;
|
958 | }
|
959 |
|
960 |
|
961 | }
|
962 |
|
963 | function callTimer(clock, timer) {
|
964 | if (typeof timer.interval == "number") {
|
965 | clock.timers[timer.id].callAt += timer.interval;
|
966 | } else {
|
967 | delete clock.timers[timer.id];
|
968 | }
|
969 |
|
970 | try {
|
971 | if (typeof timer.func == "function") {
|
972 | timer.func.apply(null, timer.args);
|
973 | } else {
|
974 | eval(timer.func);
|
975 | }
|
976 | } catch (e) {
|
977 | var exception = e;
|
978 | }
|
979 |
|
980 | if (!clock.timers[timer.id]) {
|
981 | if (exception) {
|
982 | throw exception;
|
983 | }
|
984 | return;
|
985 | }
|
986 |
|
987 | if (exception) {
|
988 | throw exception;
|
989 | }
|
990 | }
|
991 |
|
992 | function uninstall(clock, target) {
|
993 | var method;
|
994 |
|
995 | for (var i = 0, l = clock.methods.length; i < l; i++) {
|
996 | method = clock.methods[i];
|
997 |
|
998 | if (target[method].hadOwnProperty) {
|
999 | target[method] = clock["_" + method];
|
1000 | } else {
|
1001 | try {
|
1002 | delete target[method];
|
1003 | } catch (e) {}
|
1004 | }
|
1005 | }
|
1006 |
|
1007 |
|
1008 | clock.methods = [];
|
1009 | }
|
1010 |
|
1011 | function hijackMethod(target, method, clock) {
|
1012 | clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(target, method);
|
1013 | clock["_" + method] = target[method];
|
1014 |
|
1015 | if (method == "Date") {
|
1016 | var date = mirrorDateProperties(clock[method], target[method]);
|
1017 | target[method] = date;
|
1018 | } else {
|
1019 | target[method] = function() {
|
1020 | return clock[method].apply(clock, arguments);
|
1021 | };
|
1022 |
|
1023 | for (var prop in clock[method]) {
|
1024 | if (clock[method].hasOwnProperty(prop)) {
|
1025 | target[method][prop] = clock[method][prop];
|
1026 | }
|
1027 | }
|
1028 | }
|
1029 |
|
1030 | target[method].clock = clock;
|
1031 | }
|
1032 |
|
1033 | var timers = {
|
1034 | setTimeout: setTimeout,
|
1035 | clearTimeout: clearTimeout,
|
1036 | setImmediate: (typeof setImmediate !== "undefined" ? setImmediate : undefined),
|
1037 | clearImmediate: (typeof clearImmediate !== "undefined" ? clearImmediate : undefined),
|
1038 | setInterval: setInterval,
|
1039 | clearInterval: clearInterval,
|
1040 | Date: Date
|
1041 | };
|
1042 |
|
1043 | var keys = Object.keys || function(obj) {
|
1044 | var ks = [];
|
1045 | for (var key in obj) {
|
1046 | ks.push(key);
|
1047 | }
|
1048 | return ks;
|
1049 | };
|
1050 |
|
1051 | exports.timers = timers;
|
1052 |
|
1053 | var createClock = exports.createClock = function(now) {
|
1054 | var clock = {
|
1055 | now: getEpoch(now),
|
1056 | timeouts: {},
|
1057 | Date: createDate()
|
1058 | };
|
1059 |
|
1060 | clock.Date.clock = clock;
|
1061 |
|
1062 | clock.setTimeout = function setTimeout(func, timeout) {
|
1063 | return addTimer(clock, {
|
1064 | func: func,
|
1065 | args: Array.prototype.slice.call(arguments, 2),
|
1066 | delay: timeout
|
1067 | });
|
1068 | };
|
1069 |
|
1070 | clock.clearTimeout = function clearTimeout(timerId) {
|
1071 | if (!timerId) {
|
1072 |
|
1073 |
|
1074 | return;
|
1075 | }
|
1076 | if (!clock.timers) {
|
1077 | clock.timers = [];
|
1078 | }
|
1079 |
|
1080 |
|
1081 | if (typeof timerId === "object") {
|
1082 | timerId = timerId.id
|
1083 | }
|
1084 | if (timerId in clock.timers) {
|
1085 | delete clock.timers[timerId];
|
1086 | }
|
1087 | };
|
1088 |
|
1089 | clock.setInterval = function setInterval(func, timeout) {
|
1090 | return addTimer(clock, {
|
1091 | func: func,
|
1092 | args: Array.prototype.slice.call(arguments, 2),
|
1093 | delay: timeout,
|
1094 | interval: timeout
|
1095 | });
|
1096 | };
|
1097 |
|
1098 | clock.clearInterval = function clearInterval(timerId) {
|
1099 | clock.clearTimeout(timerId);
|
1100 | };
|
1101 |
|
1102 | clock.setImmediate = function setImmediate(func) {
|
1103 | return addTimer(clock, {
|
1104 | func: func,
|
1105 | args: Array.prototype.slice.call(arguments, 1),
|
1106 | immediate: true
|
1107 | });
|
1108 | };
|
1109 |
|
1110 | clock.clearImmediate = function clearImmediate(timerId) {
|
1111 | clock.clearTimeout(timerId);
|
1112 | };
|
1113 |
|
1114 | clock.tick = function tick(ms) {
|
1115 | ms = typeof ms == "number" ? ms : parseTime(ms);
|
1116 | var tickFrom = clock.now,
|
1117 | tickTo = clock.now + ms,
|
1118 | previous = clock.now;
|
1119 | var timer = firstTimerInRange(clock, tickFrom, tickTo);
|
1120 |
|
1121 | var firstException;
|
1122 | while (timer && tickFrom <= tickTo) {
|
1123 | if (clock.timers[timer.id]) {
|
1124 | tickFrom = clock.now = timer.callAt;
|
1125 | try {
|
1126 | callTimer(clock, timer);
|
1127 | } catch (e) {
|
1128 | firstException = firstException || e;
|
1129 | }
|
1130 | }
|
1131 |
|
1132 | timer = firstTimerInRange(clock, previous, tickTo);
|
1133 | previous = tickFrom;
|
1134 | }
|
1135 |
|
1136 | clock.now = tickTo;
|
1137 |
|
1138 | if (firstException) {
|
1139 | throw firstException;
|
1140 | }
|
1141 |
|
1142 | return clock.now;
|
1143 | };
|
1144 |
|
1145 | clock.reset = function reset() {
|
1146 | clock.timers = {};
|
1147 | };
|
1148 |
|
1149 | return clock;
|
1150 | };
|
1151 |
|
1152 | exports.install = function install(target, now, toFake) {
|
1153 | if (typeof target === "number") {
|
1154 | toFake = now;
|
1155 | now = target;
|
1156 | target = null;
|
1157 | }
|
1158 |
|
1159 | if (!target) {
|
1160 | target = global;
|
1161 | }
|
1162 |
|
1163 | var clock = createClock(now);
|
1164 |
|
1165 | clock.uninstall = function() {
|
1166 | uninstall(clock, target);
|
1167 | };
|
1168 |
|
1169 | clock.methods = toFake || [];
|
1170 |
|
1171 | if (clock.methods.length === 0) {
|
1172 | clock.methods = keys(timers);
|
1173 | }
|
1174 |
|
1175 | for (var i = 0, l = clock.methods.length; i < l; i++) {
|
1176 | hijackMethod(target, clock.methods[i], clock);
|
1177 | }
|
1178 |
|
1179 | return clock;
|
1180 | };
|
1181 |
|
1182 | }).call(this, typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
|
1183 | }, {}
|
1184 | ]
|
1185 | }, {}, [1])(1)
|
1186 | });
|
1187 | })();
|
1188 | var define;
|
1189 | |
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 | var sinon = (function() {
|
1199 | "use strict";
|
1200 |
|
1201 | var sinon;
|
1202 | var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
|
1203 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
1204 |
|
1205 | function loadDependencies(require, exports, module) {
|
1206 | sinon = module.exports = require("./sinon/util/core");
|
1207 | require("./sinon/extend");
|
1208 | require("./sinon/typeOf");
|
1209 | require("./sinon/times_in_words");
|
1210 | require("./sinon/spy");
|
1211 | require("./sinon/call");
|
1212 | require("./sinon/behavior");
|
1213 | require("./sinon/stub");
|
1214 | require("./sinon/mock");
|
1215 | require("./sinon/collection");
|
1216 | require("./sinon/assert");
|
1217 | require("./sinon/sandbox");
|
1218 | require("./sinon/test");
|
1219 | require("./sinon/test_case");
|
1220 | require("./sinon/match");
|
1221 | require("./sinon/format");
|
1222 | require("./sinon/log_error");
|
1223 | }
|
1224 |
|
1225 | if (isAMD) {
|
1226 | define(loadDependencies);
|
1227 | } else if (isNode) {
|
1228 | loadDependencies(require, module.exports, module);
|
1229 | sinon = module.exports;
|
1230 | } else {
|
1231 | sinon = {};
|
1232 | }
|
1233 |
|
1234 | return sinon;
|
1235 | }());
|
1236 |
|
1237 | |
1238 |
|
1239 |
|
1240 | |
1241 |
|
1242 |
|
1243 |
|
1244 |
|
1245 |
|
1246 |
|
1247 |
|
1248 |
|
1249 | (function(sinon) {
|
1250 | var div = typeof document != "undefined" && document.createElement("div");
|
1251 | var hasOwn = Object.prototype.hasOwnProperty;
|
1252 |
|
1253 | function isDOMNode(obj) {
|
1254 | var success = false;
|
1255 |
|
1256 | try {
|
1257 | obj.appendChild(div);
|
1258 | success = div.parentNode == obj;
|
1259 | } catch (e) {
|
1260 | return false;
|
1261 | } finally {
|
1262 | try {
|
1263 | obj.removeChild(div);
|
1264 | } catch (e) {
|
1265 |
|
1266 | }
|
1267 | }
|
1268 |
|
1269 | return success;
|
1270 | }
|
1271 |
|
1272 | function isElement(obj) {
|
1273 | return div && obj && obj.nodeType === 1 && isDOMNode(obj);
|
1274 | }
|
1275 |
|
1276 | function isFunction(obj) {
|
1277 | return typeof obj === "function" || !! (obj && obj.constructor && obj.call && obj.apply);
|
1278 | }
|
1279 |
|
1280 | function isReallyNaN(val) {
|
1281 | return typeof val === "number" && isNaN(val);
|
1282 | }
|
1283 |
|
1284 | function mirrorProperties(target, source) {
|
1285 | for (var prop in source) {
|
1286 | if (!hasOwn.call(target, prop)) {
|
1287 | target[prop] = source[prop];
|
1288 | }
|
1289 | }
|
1290 | }
|
1291 |
|
1292 | function isRestorable(obj) {
|
1293 | return typeof obj === "function" && typeof obj.restore === "function" && obj.restore.sinon;
|
1294 | }
|
1295 |
|
1296 | function makeApi(sinon) {
|
1297 | sinon.wrapMethod = function wrapMethod(object, property, method) {
|
1298 | if (!object) {
|
1299 | throw new TypeError("Should wrap property of object");
|
1300 | }
|
1301 |
|
1302 | if (typeof method != "function") {
|
1303 | throw new TypeError("Method wrapper should be function");
|
1304 | }
|
1305 |
|
1306 | var wrappedMethod = object[property],
|
1307 | error;
|
1308 |
|
1309 | if (!isFunction(wrappedMethod)) {
|
1310 | error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
|
1311 | property + " as function");
|
1312 | } else if (wrappedMethod.restore && wrappedMethod.restore.sinon) {
|
1313 | error = new TypeError("Attempted to wrap " + property + " which is already wrapped");
|
1314 | } else if (wrappedMethod.calledBefore) {
|
1315 | var verb = !! wrappedMethod.returns ? "stubbed" : "spied on";
|
1316 | error = new TypeError("Attempted to wrap " + property + " which is already " + verb);
|
1317 | }
|
1318 |
|
1319 | if (error) {
|
1320 | if (wrappedMethod && wrappedMethod.stackTrace) {
|
1321 | error.stack += "\n--------------\n" + wrappedMethod.stackTrace;
|
1322 | }
|
1323 | throw error;
|
1324 | }
|
1325 |
|
1326 |
|
1327 |
|
1328 | var owned = object.hasOwnProperty ? object.hasOwnProperty(property) : hasOwn.call(object, property);
|
1329 | object[property] = method;
|
1330 | method.displayName = property;
|
1331 |
|
1332 |
|
1333 | method.stackTrace = (new Error("Stack Trace for original")).stack;
|
1334 |
|
1335 | method.restore = function() {
|
1336 |
|
1337 |
|
1338 |
|
1339 | if (!owned) {
|
1340 | delete object[property];
|
1341 | }
|
1342 | if (object[property] === method) {
|
1343 | object[property] = wrappedMethod;
|
1344 | }
|
1345 | };
|
1346 |
|
1347 | method.restore.sinon = true;
|
1348 | mirrorProperties(method, wrappedMethod);
|
1349 |
|
1350 | return method;
|
1351 | };
|
1352 |
|
1353 | sinon.create = function create(proto) {
|
1354 | var F = function() {};
|
1355 | F.prototype = proto;
|
1356 | return new F();
|
1357 | };
|
1358 |
|
1359 | sinon.deepEqual = function deepEqual(a, b) {
|
1360 | if (sinon.match && sinon.match.isMatcher(a)) {
|
1361 | return a.test(b);
|
1362 | }
|
1363 |
|
1364 | if (typeof a != "object" || typeof b != "object") {
|
1365 | if (isReallyNaN(a) && isReallyNaN(b)) {
|
1366 | return true;
|
1367 | } else {
|
1368 | return a === b;
|
1369 | }
|
1370 | }
|
1371 |
|
1372 | if (isElement(a) || isElement(b)) {
|
1373 | return a === b;
|
1374 | }
|
1375 |
|
1376 | if (a === b) {
|
1377 | return true;
|
1378 | }
|
1379 |
|
1380 | if ((a === null && b !== null) || (a !== null && b === null)) {
|
1381 | return false;
|
1382 | }
|
1383 |
|
1384 | if (a instanceof RegExp && b instanceof RegExp) {
|
1385 | return (a.source === b.source) && (a.global === b.global) &&
|
1386 | (a.ignoreCase === b.ignoreCase) && (a.multiline === b.multiline);
|
1387 | }
|
1388 |
|
1389 | var aString = Object.prototype.toString.call(a);
|
1390 | if (aString != Object.prototype.toString.call(b)) {
|
1391 | return false;
|
1392 | }
|
1393 |
|
1394 | if (aString == "[object Date]") {
|
1395 | return a.valueOf() === b.valueOf();
|
1396 | }
|
1397 |
|
1398 | var prop, aLength = 0,
|
1399 | bLength = 0;
|
1400 |
|
1401 | if (aString == "[object Array]" && a.length !== b.length) {
|
1402 | return false;
|
1403 | }
|
1404 |
|
1405 | for (prop in a) {
|
1406 | aLength += 1;
|
1407 |
|
1408 | if (!(prop in b)) {
|
1409 | return false;
|
1410 | }
|
1411 |
|
1412 | if (!deepEqual(a[prop], b[prop])) {
|
1413 | return false;
|
1414 | }
|
1415 | }
|
1416 |
|
1417 | for (prop in b) {
|
1418 | bLength += 1;
|
1419 | }
|
1420 |
|
1421 | return aLength == bLength;
|
1422 | };
|
1423 |
|
1424 | sinon.functionName = function functionName(func) {
|
1425 | var name = func.displayName || func.name;
|
1426 |
|
1427 |
|
1428 |
|
1429 |
|
1430 |
|
1431 | if (!name) {
|
1432 | var matches = func.toString().match(/function ([^\s\(]+)/);
|
1433 | name = matches && matches[1];
|
1434 | }
|
1435 |
|
1436 | return name;
|
1437 | };
|
1438 |
|
1439 | sinon.functionToString = function toString() {
|
1440 | if (this.getCall && this.callCount) {
|
1441 | var thisValue, prop, i = this.callCount;
|
1442 |
|
1443 | while (i--) {
|
1444 | thisValue = this.getCall(i).thisValue;
|
1445 |
|
1446 | for (prop in thisValue) {
|
1447 | if (thisValue[prop] === this) {
|
1448 | return prop;
|
1449 | }
|
1450 | }
|
1451 | }
|
1452 | }
|
1453 |
|
1454 | return this.displayName || "sinon fake";
|
1455 | };
|
1456 |
|
1457 | sinon.getConfig = function(custom) {
|
1458 | var config = {};
|
1459 | custom = custom || {};
|
1460 | var defaults = sinon.defaultConfig;
|
1461 |
|
1462 | for (var prop in defaults) {
|
1463 | if (defaults.hasOwnProperty(prop)) {
|
1464 | config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaults[prop];
|
1465 | }
|
1466 | }
|
1467 |
|
1468 | return config;
|
1469 | };
|
1470 |
|
1471 | sinon.defaultConfig = {
|
1472 | injectIntoThis: true,
|
1473 | injectInto: null,
|
1474 | properties: ["spy", "stub", "mock", "clock", "server", "requests"],
|
1475 | useFakeTimers: true,
|
1476 | useFakeServer: true
|
1477 | };
|
1478 |
|
1479 | sinon.timesInWords = function timesInWords(count) {
|
1480 | return count == 1 && "once" ||
|
1481 | count == 2 && "twice" ||
|
1482 | count == 3 && "thrice" ||
|
1483 | (count || 0) + " times";
|
1484 | };
|
1485 |
|
1486 | sinon.calledInOrder = function(spies) {
|
1487 | for (var i = 1, l = spies.length; i < l; i++) {
|
1488 | if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) {
|
1489 | return false;
|
1490 | }
|
1491 | }
|
1492 |
|
1493 | return true;
|
1494 | };
|
1495 |
|
1496 | sinon.orderByFirstCall = function(spies) {
|
1497 | return spies.sort(function(a, b) {
|
1498 |
|
1499 | var aCall = a.getCall(0);
|
1500 | var bCall = b.getCall(0);
|
1501 | var aId = aCall && aCall.callId || -1;
|
1502 | var bId = bCall && bCall.callId || -1;
|
1503 |
|
1504 | return aId < bId ? -1 : 1;
|
1505 | });
|
1506 | };
|
1507 |
|
1508 | sinon.createStubInstance = function(constructor) {
|
1509 | if (typeof constructor !== "function") {
|
1510 | throw new TypeError("The constructor should be a function.");
|
1511 | }
|
1512 | return sinon.stub(sinon.create(constructor.prototype));
|
1513 | };
|
1514 |
|
1515 | sinon.restore = function(object) {
|
1516 | if (object !== null && typeof object === "object") {
|
1517 | for (var prop in object) {
|
1518 | if (isRestorable(object[prop])) {
|
1519 | object[prop].restore();
|
1520 | }
|
1521 | }
|
1522 | } else if (isRestorable(object)) {
|
1523 | object.restore();
|
1524 | }
|
1525 | };
|
1526 |
|
1527 | return sinon;
|
1528 | }
|
1529 |
|
1530 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
1531 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
1532 |
|
1533 | function loadDependencies(require, exports) {
|
1534 | makeApi(exports);
|
1535 | }
|
1536 |
|
1537 | if (isAMD) {
|
1538 | define(loadDependencies);
|
1539 | } else if (isNode) {
|
1540 | loadDependencies(require, module.exports);
|
1541 | } else if (!sinon) {
|
1542 | return;
|
1543 | } else {
|
1544 | makeApi(sinon);
|
1545 | }
|
1546 | }(typeof sinon == "object" && sinon || null));
|
1547 |
|
1548 | |
1549 |
|
1550 |
|
1551 |
|
1552 | (function(sinon) {
|
1553 | function makeApi(sinon) {
|
1554 |
|
1555 |
|
1556 | var hasDontEnumBug = (function() {
|
1557 | var obj = {
|
1558 | constructor: function() {
|
1559 | return "0";
|
1560 | },
|
1561 | toString: function() {
|
1562 | return "1";
|
1563 | },
|
1564 | valueOf: function() {
|
1565 | return "2";
|
1566 | },
|
1567 | toLocaleString: function() {
|
1568 | return "3";
|
1569 | },
|
1570 | prototype: function() {
|
1571 | return "4";
|
1572 | },
|
1573 | isPrototypeOf: function() {
|
1574 | return "5";
|
1575 | },
|
1576 | propertyIsEnumerable: function() {
|
1577 | return "6";
|
1578 | },
|
1579 | hasOwnProperty: function() {
|
1580 | return "7";
|
1581 | },
|
1582 | length: function() {
|
1583 | return "8";
|
1584 | },
|
1585 | unique: function() {
|
1586 | return "9"
|
1587 | }
|
1588 | };
|
1589 |
|
1590 | var result = [];
|
1591 | for (var prop in obj) {
|
1592 | result.push(obj[prop]());
|
1593 | }
|
1594 | return result.join("") !== "0123456789";
|
1595 | })();
|
1596 |
|
1597 | |
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 |
|
1603 |
|
1604 |
|
1605 |
|
1606 | function extend(target /*, sources */ ) {
|
1607 | var sources = Array.prototype.slice.call(arguments, 1),
|
1608 | source, i, prop;
|
1609 |
|
1610 | for (i = 0; i < sources.length; i++) {
|
1611 | source = sources[i];
|
1612 |
|
1613 | for (prop in source) {
|
1614 | if (source.hasOwnProperty(prop)) {
|
1615 | target[prop] = source[prop];
|
1616 | }
|
1617 | }
|
1618 |
|
1619 |
|
1620 |
|
1621 | if (hasDontEnumBug && source.hasOwnProperty("toString") && source.toString !== target.toString) {
|
1622 | target.toString = source.toString;
|
1623 | }
|
1624 | }
|
1625 |
|
1626 | return target;
|
1627 | };
|
1628 |
|
1629 | sinon.extend = extend;
|
1630 | return sinon.extend;
|
1631 | }
|
1632 |
|
1633 | function loadDependencies(require, exports, module) {
|
1634 | var sinon = require("./util/core");
|
1635 | module.exports = makeApi(sinon);
|
1636 | }
|
1637 |
|
1638 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
1639 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
1640 |
|
1641 | if (isAMD) {
|
1642 | define(loadDependencies);
|
1643 | } else if (isNode) {
|
1644 | loadDependencies(require, module.exports, module);
|
1645 | } else if (!sinon) {
|
1646 | return;
|
1647 | } else {
|
1648 | makeApi(sinon);
|
1649 | }
|
1650 | }(typeof sinon == "object" && sinon || null));
|
1651 |
|
1652 | |
1653 |
|
1654 |
|
1655 |
|
1656 | (function(sinon) {
|
1657 | function makeApi(sinon) {
|
1658 |
|
1659 | function timesInWords(count) {
|
1660 | switch (count) {
|
1661 | case 1:
|
1662 | return "once";
|
1663 | case 2:
|
1664 | return "twice";
|
1665 | case 3:
|
1666 | return "thrice";
|
1667 | default:
|
1668 | return (count || 0) + " times";
|
1669 | }
|
1670 | }
|
1671 |
|
1672 | sinon.timesInWords = timesInWords;
|
1673 | return sinon.timesInWords;
|
1674 | }
|
1675 |
|
1676 | function loadDependencies(require, exports, module) {
|
1677 | var sinon = require("./util/core");
|
1678 | module.exports = makeApi(sinon);
|
1679 | }
|
1680 |
|
1681 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
1682 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
1683 |
|
1684 | if (isAMD) {
|
1685 | define(loadDependencies);
|
1686 | } else if (isNode) {
|
1687 | loadDependencies(require, module.exports, module);
|
1688 | } else if (!sinon) {
|
1689 | return;
|
1690 | } else {
|
1691 | makeApi(sinon);
|
1692 | }
|
1693 | }(typeof sinon == "object" && sinon || null));
|
1694 |
|
1695 | |
1696 |
|
1697 |
|
1698 | |
1699 |
|
1700 |
|
1701 |
|
1702 |
|
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 | (function(sinon, formatio) {
|
1708 | function makeApi(sinon) {
|
1709 | function typeOf(value) {
|
1710 | if (value === null) {
|
1711 | return "null";
|
1712 | } else if (value === undefined) {
|
1713 | return "undefined";
|
1714 | }
|
1715 | var string = Object.prototype.toString.call(value);
|
1716 | return string.substring(8, string.length - 1).toLowerCase();
|
1717 | };
|
1718 |
|
1719 | sinon.typeOf = typeOf;
|
1720 | return sinon.typeOf;
|
1721 | }
|
1722 |
|
1723 | function loadDependencies(require, exports, module) {
|
1724 | var sinon = require("./util/core");
|
1725 | module.exports = makeApi(sinon);
|
1726 | }
|
1727 |
|
1728 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
1729 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
1730 |
|
1731 | if (isAMD) {
|
1732 | define(loadDependencies);
|
1733 | } else if (isNode) {
|
1734 | loadDependencies(require, module.exports, module);
|
1735 | } else if (!sinon) {
|
1736 | return;
|
1737 | } else {
|
1738 | makeApi(sinon);
|
1739 | }
|
1740 | }(
|
1741 | (typeof sinon == "object" && sinon || null), (typeof formatio == "object" && formatio)
|
1742 | ));
|
1743 |
|
1744 | |
1745 |
|
1746 |
|
1747 |
|
1748 |
|
1749 |
|
1750 | |
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 | (function(sinon) {
|
1760 | function makeApi(sinon) {
|
1761 | function assertType(value, type, name) {
|
1762 | var actual = sinon.typeOf(value);
|
1763 | if (actual !== type) {
|
1764 | throw new TypeError("Expected type of " + name + " to be " +
|
1765 | type + ", but was " + actual);
|
1766 | }
|
1767 | }
|
1768 |
|
1769 | var matcher = {
|
1770 | toString: function() {
|
1771 | return this.message;
|
1772 | }
|
1773 | };
|
1774 |
|
1775 | function isMatcher(object) {
|
1776 | return matcher.isPrototypeOf(object);
|
1777 | }
|
1778 |
|
1779 | function matchObject(expectation, actual) {
|
1780 | if (actual === null || actual === undefined) {
|
1781 | return false;
|
1782 | }
|
1783 | for (var key in expectation) {
|
1784 | if (expectation.hasOwnProperty(key)) {
|
1785 | var exp = expectation[key];
|
1786 | var act = actual[key];
|
1787 | if (match.isMatcher(exp)) {
|
1788 | if (!exp.test(act)) {
|
1789 | return false;
|
1790 | }
|
1791 | } else if (sinon.typeOf(exp) === "object") {
|
1792 | if (!matchObject(exp, act)) {
|
1793 | return false;
|
1794 | }
|
1795 | } else if (!sinon.deepEqual(exp, act)) {
|
1796 | return false;
|
1797 | }
|
1798 | }
|
1799 | }
|
1800 | return true;
|
1801 | }
|
1802 |
|
1803 | matcher.or = function(m2) {
|
1804 | if (!arguments.length) {
|
1805 | throw new TypeError("Matcher expected");
|
1806 | } else if (!isMatcher(m2)) {
|
1807 | m2 = match(m2);
|
1808 | }
|
1809 | var m1 = this;
|
1810 | var or = sinon.create(matcher);
|
1811 | or.test = function(actual) {
|
1812 | return m1.test(actual) || m2.test(actual);
|
1813 | };
|
1814 | or.message = m1.message + ".or(" + m2.message + ")";
|
1815 | return or;
|
1816 | };
|
1817 |
|
1818 | matcher.and = function(m2) {
|
1819 | if (!arguments.length) {
|
1820 | throw new TypeError("Matcher expected");
|
1821 | } else if (!isMatcher(m2)) {
|
1822 | m2 = match(m2);
|
1823 | }
|
1824 | var m1 = this;
|
1825 | var and = sinon.create(matcher);
|
1826 | and.test = function(actual) {
|
1827 | return m1.test(actual) && m2.test(actual);
|
1828 | };
|
1829 | and.message = m1.message + ".and(" + m2.message + ")";
|
1830 | return and;
|
1831 | };
|
1832 |
|
1833 | var match = function(expectation, message) {
|
1834 | var m = sinon.create(matcher);
|
1835 | var type = sinon.typeOf(expectation);
|
1836 | switch (type) {
|
1837 | case "object":
|
1838 | if (typeof expectation.test === "function") {
|
1839 | m.test = function(actual) {
|
1840 | return expectation.test(actual) === true;
|
1841 | };
|
1842 | m.message = "match(" + sinon.functionName(expectation.test) + ")";
|
1843 | return m;
|
1844 | }
|
1845 | var str = [];
|
1846 | for (var key in expectation) {
|
1847 | if (expectation.hasOwnProperty(key)) {
|
1848 | str.push(key + ": " + expectation[key]);
|
1849 | }
|
1850 | }
|
1851 | m.test = function(actual) {
|
1852 | return matchObject(expectation, actual);
|
1853 | };
|
1854 | m.message = "match(" + str.join(", ") + ")";
|
1855 | break;
|
1856 | case "number":
|
1857 | m.test = function(actual) {
|
1858 | return expectation == actual;
|
1859 | };
|
1860 | break;
|
1861 | case "string":
|
1862 | m.test = function(actual) {
|
1863 | if (typeof actual !== "string") {
|
1864 | return false;
|
1865 | }
|
1866 | return actual.indexOf(expectation) !== -1;
|
1867 | };
|
1868 | m.message = "match(\"" + expectation + "\")";
|
1869 | break;
|
1870 | case "regexp":
|
1871 | m.test = function(actual) {
|
1872 | if (typeof actual !== "string") {
|
1873 | return false;
|
1874 | }
|
1875 | return expectation.test(actual);
|
1876 | };
|
1877 | break;
|
1878 | case "function":
|
1879 | m.test = expectation;
|
1880 | if (message) {
|
1881 | m.message = message;
|
1882 | } else {
|
1883 | m.message = "match(" + sinon.functionName(expectation) + ")";
|
1884 | }
|
1885 | break;
|
1886 | default:
|
1887 | m.test = function(actual) {
|
1888 | return sinon.deepEqual(expectation, actual);
|
1889 | };
|
1890 | }
|
1891 | if (!m.message) {
|
1892 | m.message = "match(" + expectation + ")";
|
1893 | }
|
1894 | return m;
|
1895 | };
|
1896 |
|
1897 | match.isMatcher = isMatcher;
|
1898 |
|
1899 | match.any = match(function() {
|
1900 | return true;
|
1901 | }, "any");
|
1902 |
|
1903 | match.defined = match(function(actual) {
|
1904 | return actual !== null && actual !== undefined;
|
1905 | }, "defined");
|
1906 |
|
1907 | match.truthy = match(function(actual) {
|
1908 | return !!actual;
|
1909 | }, "truthy");
|
1910 |
|
1911 | match.falsy = match(function(actual) {
|
1912 | return !actual;
|
1913 | }, "falsy");
|
1914 |
|
1915 | match.same = function(expectation) {
|
1916 | return match(function(actual) {
|
1917 | return expectation === actual;
|
1918 | }, "same(" + expectation + ")");
|
1919 | };
|
1920 |
|
1921 | match.typeOf = function(type) {
|
1922 | assertType(type, "string", "type");
|
1923 | return match(function(actual) {
|
1924 | return sinon.typeOf(actual) === type;
|
1925 | }, "typeOf(\"" + type + "\")");
|
1926 | };
|
1927 |
|
1928 | match.instanceOf = function(type) {
|
1929 | assertType(type, "function", "type");
|
1930 | return match(function(actual) {
|
1931 | return actual instanceof type;
|
1932 | }, "instanceOf(" + sinon.functionName(type) + ")");
|
1933 | };
|
1934 |
|
1935 | function createPropertyMatcher(propertyTest, messagePrefix) {
|
1936 | return function(property, value) {
|
1937 | assertType(property, "string", "property");
|
1938 | var onlyProperty = arguments.length === 1;
|
1939 | var message = messagePrefix + "(\"" + property + "\"";
|
1940 | if (!onlyProperty) {
|
1941 | message += ", " + value;
|
1942 | }
|
1943 | message += ")";
|
1944 | return match(function(actual) {
|
1945 | if (actual === undefined || actual === null || !propertyTest(actual, property)) {
|
1946 | return false;
|
1947 | }
|
1948 | return onlyProperty || sinon.deepEqual(value, actual[property]);
|
1949 | }, message);
|
1950 | };
|
1951 | }
|
1952 |
|
1953 | match.has = createPropertyMatcher(function(actual, property) {
|
1954 | if (typeof actual === "object") {
|
1955 | return property in actual;
|
1956 | }
|
1957 | return actual[property] !== undefined;
|
1958 | }, "has");
|
1959 |
|
1960 | match.hasOwn = createPropertyMatcher(function(actual, property) {
|
1961 | return actual.hasOwnProperty(property);
|
1962 | }, "hasOwn");
|
1963 |
|
1964 | match.bool = match.typeOf("boolean");
|
1965 | match.number = match.typeOf("number");
|
1966 | match.string = match.typeOf("string");
|
1967 | match.object = match.typeOf("object");
|
1968 | match.func = match.typeOf("function");
|
1969 | match.array = match.typeOf("array");
|
1970 | match.regexp = match.typeOf("regexp");
|
1971 | match.date = match.typeOf("date");
|
1972 |
|
1973 | sinon.match = match;
|
1974 | return match;
|
1975 | }
|
1976 |
|
1977 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
1978 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
1979 |
|
1980 | function loadDependencies(require, exports, module) {
|
1981 | var sinon = require("./util/core");
|
1982 | module.exports = makeApi(sinon);
|
1983 | }
|
1984 |
|
1985 | if (isAMD) {
|
1986 | define(loadDependencies);
|
1987 | } else if (isNode) {
|
1988 | loadDependencies(require, module.exports, module);
|
1989 | } else if (!sinon) {
|
1990 | return;
|
1991 | } else {
|
1992 | makeApi(sinon);
|
1993 | }
|
1994 | }(typeof sinon == "object" && sinon || null));
|
1995 |
|
1996 | |
1997 |
|
1998 |
|
1999 | |
2000 |
|
2001 |
|
2002 |
|
2003 |
|
2004 |
|
2005 |
|
2006 |
|
2007 |
|
2008 | (function(sinon, formatio) {
|
2009 | function makeApi(sinon) {
|
2010 | function valueFormatter(value) {
|
2011 | return "" + value;
|
2012 | }
|
2013 |
|
2014 | function getFormatioFormatter() {
|
2015 | var formatter = formatio.configure({
|
2016 | quoteStrings: false,
|
2017 | limitChildrenCount: 250
|
2018 | });
|
2019 |
|
2020 | function format() {
|
2021 | return formatter.ascii.apply(formatter, arguments);
|
2022 | };
|
2023 |
|
2024 | return format;
|
2025 | }
|
2026 |
|
2027 | function getNodeFormatter(value) {
|
2028 | function format(value) {
|
2029 | return typeof value == "object" && value.toString === Object.prototype.toString ? util.inspect(value) : value;
|
2030 | };
|
2031 |
|
2032 | try {
|
2033 | var util = require("util");
|
2034 | } catch (e) {
|
2035 |
|
2036 | }
|
2037 |
|
2038 | return util ? format : valueFormatter;
|
2039 | }
|
2040 |
|
2041 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function",
|
2042 | formatter;
|
2043 |
|
2044 | if (isNode) {
|
2045 | try {
|
2046 | formatio = require("formatio");
|
2047 | } catch (e) {}
|
2048 | }
|
2049 |
|
2050 | if (formatio) {
|
2051 | formatter = getFormatioFormatter()
|
2052 | } else if (isNode) {
|
2053 | formatter = getNodeFormatter();
|
2054 | } else {
|
2055 | formatter = valueFormatter;
|
2056 | }
|
2057 |
|
2058 | sinon.format = formatter;
|
2059 | return sinon.format;
|
2060 | }
|
2061 |
|
2062 | function loadDependencies(require, exports, module) {
|
2063 | var sinon = require("./util/core");
|
2064 | module.exports = makeApi(sinon);
|
2065 | }
|
2066 |
|
2067 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
2068 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
2069 |
|
2070 | if (isAMD) {
|
2071 | define(loadDependencies);
|
2072 | } else if (isNode) {
|
2073 | loadDependencies(require, module.exports, module);
|
2074 | } else if (!sinon) {
|
2075 | return;
|
2076 | } else {
|
2077 | makeApi(sinon);
|
2078 | }
|
2079 | }(
|
2080 | (typeof sinon == "object" && sinon || null), (typeof formatio == "object" && formatio)
|
2081 | ));
|
2082 |
|
2083 | |
2084 |
|
2085 |
|
2086 |
|
2087 |
|
2088 | |
2089 |
|
2090 |
|
2091 |
|
2092 |
|
2093 |
|
2094 |
|
2095 |
|
2096 |
|
2097 |
|
2098 |
|
2099 | (function(sinon) {
|
2100 | function makeApi(sinon) {
|
2101 | function throwYieldError(proxy, text, args) {
|
2102 | var msg = sinon.functionName(proxy) + text;
|
2103 | if (args.length) {
|
2104 | msg += " Received [" + slice.call(args).join(", ") + "]";
|
2105 | }
|
2106 | throw new Error(msg);
|
2107 | }
|
2108 |
|
2109 | var slice = Array.prototype.slice;
|
2110 |
|
2111 | var callProto = {
|
2112 | calledOn: function calledOn(thisValue) {
|
2113 | if (sinon.match && sinon.match.isMatcher(thisValue)) {
|
2114 | return thisValue.test(this.thisValue);
|
2115 | }
|
2116 | return this.thisValue === thisValue;
|
2117 | },
|
2118 |
|
2119 | calledWith: function calledWith() {
|
2120 | for (var i = 0, l = arguments.length; i < l; i += 1) {
|
2121 | if (!sinon.deepEqual(arguments[i], this.args[i])) {
|
2122 | return false;
|
2123 | }
|
2124 | }
|
2125 |
|
2126 | return true;
|
2127 | },
|
2128 |
|
2129 | calledWithMatch: function calledWithMatch() {
|
2130 | for (var i = 0, l = arguments.length; i < l; i += 1) {
|
2131 | var actual = this.args[i];
|
2132 | var expectation = arguments[i];
|
2133 | if (!sinon.match || !sinon.match(expectation).test(actual)) {
|
2134 | return false;
|
2135 | }
|
2136 | }
|
2137 | return true;
|
2138 | },
|
2139 |
|
2140 | calledWithExactly: function calledWithExactly() {
|
2141 | return arguments.length == this.args.length &&
|
2142 | this.calledWith.apply(this, arguments);
|
2143 | },
|
2144 |
|
2145 | notCalledWith: function notCalledWith() {
|
2146 | return !this.calledWith.apply(this, arguments);
|
2147 | },
|
2148 |
|
2149 | notCalledWithMatch: function notCalledWithMatch() {
|
2150 | return !this.calledWithMatch.apply(this, arguments);
|
2151 | },
|
2152 |
|
2153 | returned: function returned(value) {
|
2154 | return sinon.deepEqual(value, this.returnValue);
|
2155 | },
|
2156 |
|
2157 | threw: function threw(error) {
|
2158 | if (typeof error === "undefined" || !this.exception) {
|
2159 | return !!this.exception;
|
2160 | }
|
2161 |
|
2162 | return this.exception === error || this.exception.name === error;
|
2163 | },
|
2164 |
|
2165 | calledWithNew: function calledWithNew() {
|
2166 | return this.proxy.prototype && this.thisValue instanceof this.proxy;
|
2167 | },
|
2168 |
|
2169 | calledBefore: function(other) {
|
2170 | return this.callId < other.callId;
|
2171 | },
|
2172 |
|
2173 | calledAfter: function(other) {
|
2174 | return this.callId > other.callId;
|
2175 | },
|
2176 |
|
2177 | callArg: function(pos) {
|
2178 | this.args[pos]();
|
2179 | },
|
2180 |
|
2181 | callArgOn: function(pos, thisValue) {
|
2182 | this.args[pos].apply(thisValue);
|
2183 | },
|
2184 |
|
2185 | callArgWith: function(pos) {
|
2186 | this.callArgOnWith.apply(this, [pos, null].concat(slice.call(arguments, 1)));
|
2187 | },
|
2188 |
|
2189 | callArgOnWith: function(pos, thisValue) {
|
2190 | var args = slice.call(arguments, 2);
|
2191 | this.args[pos].apply(thisValue, args);
|
2192 | },
|
2193 |
|
2194 | yield: function() {
|
2195 | this.yieldOn.apply(this, [null].concat(slice.call(arguments, 0)));
|
2196 | },
|
2197 |
|
2198 | yieldOn: function(thisValue) {
|
2199 | var args = this.args;
|
2200 | for (var i = 0, l = args.length; i < l; ++i) {
|
2201 | if (typeof args[i] === "function") {
|
2202 | args[i].apply(thisValue, slice.call(arguments, 1));
|
2203 | return;
|
2204 | }
|
2205 | }
|
2206 | throwYieldError(this.proxy, " cannot yield since no callback was passed.", args);
|
2207 | },
|
2208 |
|
2209 | yieldTo: function(prop) {
|
2210 | this.yieldToOn.apply(this, [prop, null].concat(slice.call(arguments, 1)));
|
2211 | },
|
2212 |
|
2213 | yieldToOn: function(prop, thisValue) {
|
2214 | var args = this.args;
|
2215 | for (var i = 0, l = args.length; i < l; ++i) {
|
2216 | if (args[i] && typeof args[i][prop] === "function") {
|
2217 | args[i][prop].apply(thisValue, slice.call(arguments, 2));
|
2218 | return;
|
2219 | }
|
2220 | }
|
2221 | throwYieldError(this.proxy, " cannot yield to '" + prop +
|
2222 | "' since no callback was passed.", args);
|
2223 | },
|
2224 |
|
2225 | toString: function() {
|
2226 | var callStr = this.proxy.toString() + "(";
|
2227 | var args = [];
|
2228 |
|
2229 | for (var i = 0, l = this.args.length; i < l; ++i) {
|
2230 | args.push(sinon.format(this.args[i]));
|
2231 | }
|
2232 |
|
2233 | callStr = callStr + args.join(", ") + ")";
|
2234 |
|
2235 | if (typeof this.returnValue != "undefined") {
|
2236 | callStr += " => " + sinon.format(this.returnValue);
|
2237 | }
|
2238 |
|
2239 | if (this.exception) {
|
2240 | callStr += " !" + this.exception.name;
|
2241 |
|
2242 | if (this.exception.message) {
|
2243 | callStr += "(" + this.exception.message + ")";
|
2244 | }
|
2245 | }
|
2246 |
|
2247 | return callStr;
|
2248 | }
|
2249 | };
|
2250 |
|
2251 | callProto.invokeCallback = callProto.yield;
|
2252 |
|
2253 | function createSpyCall(spy, thisValue, args, returnValue, exception, id) {
|
2254 | if (typeof id !== "number") {
|
2255 | throw new TypeError("Call id is not a number");
|
2256 | }
|
2257 | var proxyCall = sinon.create(callProto);
|
2258 | proxyCall.proxy = spy;
|
2259 | proxyCall.thisValue = thisValue;
|
2260 | proxyCall.args = args;
|
2261 | proxyCall.returnValue = returnValue;
|
2262 | proxyCall.exception = exception;
|
2263 | proxyCall.callId = id;
|
2264 |
|
2265 | return proxyCall;
|
2266 | }
|
2267 | createSpyCall.toString = callProto.toString;
|
2268 |
|
2269 | sinon.spyCall = createSpyCall;
|
2270 | return createSpyCall;
|
2271 | }
|
2272 |
|
2273 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
2274 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
2275 |
|
2276 | function loadDependencies(require, exports, module) {
|
2277 | var sinon = require("./util/core");
|
2278 | require("./match");
|
2279 | module.exports = makeApi(sinon);
|
2280 | }
|
2281 |
|
2282 | if (isAMD) {
|
2283 | define(loadDependencies);
|
2284 | } else if (isNode) {
|
2285 | loadDependencies(require, module.exports, module);
|
2286 | } else if (!sinon) {
|
2287 | return;
|
2288 | } else {
|
2289 | makeApi(sinon);
|
2290 | }
|
2291 | }(typeof sinon == "object" && sinon || null));
|
2292 |
|
2293 | |
2294 |
|
2295 |
|
2296 |
|
2297 |
|
2298 |
|
2299 |
|
2300 | |
2301 |
|
2302 |
|
2303 |
|
2304 |
|
2305 |
|
2306 |
|
2307 |
|
2308 |
|
2309 | (function(sinon) {
|
2310 | function makeApi(sinon) {
|
2311 | var push = Array.prototype.push;
|
2312 | var slice = Array.prototype.slice;
|
2313 | var callId = 0;
|
2314 |
|
2315 | function spy(object, property) {
|
2316 | if (!property && typeof object == "function") {
|
2317 | return spy.create(object);
|
2318 | }
|
2319 |
|
2320 | if (!object && !property) {
|
2321 | return spy.create(function() {});
|
2322 | }
|
2323 |
|
2324 | var method = object[property];
|
2325 | return sinon.wrapMethod(object, property, spy.create(method));
|
2326 | }
|
2327 |
|
2328 | function matchingFake(fakes, args, strict) {
|
2329 | if (!fakes) {
|
2330 | return;
|
2331 | }
|
2332 |
|
2333 | for (var i = 0, l = fakes.length; i < l; i++) {
|
2334 | if (fakes[i].matches(args, strict)) {
|
2335 | return fakes[i];
|
2336 | }
|
2337 | }
|
2338 | }
|
2339 |
|
2340 | function incrementCallCount() {
|
2341 | this.called = true;
|
2342 | this.callCount += 1;
|
2343 | this.notCalled = false;
|
2344 | this.calledOnce = this.callCount == 1;
|
2345 | this.calledTwice = this.callCount == 2;
|
2346 | this.calledThrice = this.callCount == 3;
|
2347 | }
|
2348 |
|
2349 | function createCallProperties() {
|
2350 | this.firstCall = this.getCall(0);
|
2351 | this.secondCall = this.getCall(1);
|
2352 | this.thirdCall = this.getCall(2);
|
2353 | this.lastCall = this.getCall(this.callCount - 1);
|
2354 | }
|
2355 |
|
2356 | var vars = "a,b,c,d,e,f,g,h,i,j,k,l";
|
2357 |
|
2358 | function createProxy(func) {
|
2359 |
|
2360 | var p;
|
2361 | if (func.length) {
|
2362 | eval("p = (function proxy(" + vars.substring(0, func.length * 2 - 1) +
|
2363 | ") { return p.invoke(func, this, slice.call(arguments)); });");
|
2364 | } else {
|
2365 | p = function proxy() {
|
2366 | return p.invoke(func, this, slice.call(arguments));
|
2367 | };
|
2368 | }
|
2369 | return p;
|
2370 | }
|
2371 |
|
2372 | var uuid = 0;
|
2373 |
|
2374 |
|
2375 | var spyApi = {
|
2376 | reset: function() {
|
2377 | if (this.invoking) {
|
2378 | var err = new Error("Cannot reset Sinon function while invoking it. " +
|
2379 | "Move the call to .reset outside of the callback.");
|
2380 | err.name = "InvalidResetException";
|
2381 | throw err;
|
2382 | }
|
2383 |
|
2384 | this.called = false;
|
2385 | this.notCalled = true;
|
2386 | this.calledOnce = false;
|
2387 | this.calledTwice = false;
|
2388 | this.calledThrice = false;
|
2389 | this.callCount = 0;
|
2390 | this.firstCall = null;
|
2391 | this.secondCall = null;
|
2392 | this.thirdCall = null;
|
2393 | this.lastCall = null;
|
2394 | this.args = [];
|
2395 | this.returnValues = [];
|
2396 | this.thisValues = [];
|
2397 | this.exceptions = [];
|
2398 | this.callIds = [];
|
2399 | if (this.fakes) {
|
2400 | for (var i = 0; i < this.fakes.length; i++) {
|
2401 | this.fakes[i].reset();
|
2402 | }
|
2403 | }
|
2404 | },
|
2405 |
|
2406 | create: function create(func) {
|
2407 | var name;
|
2408 |
|
2409 | if (typeof func != "function") {
|
2410 | func = function() {};
|
2411 | } else {
|
2412 | name = sinon.functionName(func);
|
2413 | }
|
2414 |
|
2415 | var proxy = createProxy(func);
|
2416 |
|
2417 | sinon.extend(proxy, spy);
|
2418 | delete proxy.create;
|
2419 | sinon.extend(proxy, func);
|
2420 |
|
2421 | proxy.reset();
|
2422 | proxy.prototype = func.prototype;
|
2423 | proxy.displayName = name || "spy";
|
2424 | proxy.toString = sinon.functionToString;
|
2425 | proxy.instantiateFake = sinon.spy.create;
|
2426 | proxy.id = "spy#" + uuid++;
|
2427 |
|
2428 | return proxy;
|
2429 | },
|
2430 |
|
2431 | invoke: function invoke(func, thisValue, args) {
|
2432 | var matching = matchingFake(this.fakes, args);
|
2433 | var exception, returnValue;
|
2434 |
|
2435 | incrementCallCount.call(this);
|
2436 | push.call(this.thisValues, thisValue);
|
2437 | push.call(this.args, args);
|
2438 | push.call(this.callIds, callId++);
|
2439 |
|
2440 |
|
2441 | createCallProperties.call(this);
|
2442 |
|
2443 | try {
|
2444 | this.invoking = true;
|
2445 |
|
2446 | if (matching) {
|
2447 | returnValue = matching.invoke(func, thisValue, args);
|
2448 | } else {
|
2449 | returnValue = (this.func || func).apply(thisValue, args);
|
2450 | }
|
2451 |
|
2452 | var thisCall = this.getCall(this.callCount - 1);
|
2453 | if (thisCall.calledWithNew() && typeof returnValue !== "object") {
|
2454 | returnValue = thisValue;
|
2455 | }
|
2456 | } catch (e) {
|
2457 | exception = e;
|
2458 | } finally {
|
2459 | delete this.invoking;
|
2460 | }
|
2461 |
|
2462 | push.call(this.exceptions, exception);
|
2463 | push.call(this.returnValues, returnValue);
|
2464 |
|
2465 |
|
2466 | createCallProperties.call(this);
|
2467 |
|
2468 | if (exception !== undefined) {
|
2469 | throw exception;
|
2470 | }
|
2471 |
|
2472 | return returnValue;
|
2473 | },
|
2474 |
|
2475 | named: function named(name) {
|
2476 | this.displayName = name;
|
2477 | return this;
|
2478 | },
|
2479 |
|
2480 | getCall: function getCall(i) {
|
2481 | if (i < 0 || i >= this.callCount) {
|
2482 | return null;
|
2483 | }
|
2484 |
|
2485 | return sinon.spyCall(this, this.thisValues[i], this.args[i],
|
2486 | this.returnValues[i], this.exceptions[i],
|
2487 | this.callIds[i]);
|
2488 | },
|
2489 |
|
2490 | getCalls: function() {
|
2491 | var calls = [];
|
2492 | var i;
|
2493 |
|
2494 | for (i = 0; i < this.callCount; i++) {
|
2495 | calls.push(this.getCall(i));
|
2496 | }
|
2497 |
|
2498 | return calls;
|
2499 | },
|
2500 |
|
2501 | calledBefore: function calledBefore(spyFn) {
|
2502 | if (!this.called) {
|
2503 | return false;
|
2504 | }
|
2505 |
|
2506 | if (!spyFn.called) {
|
2507 | return true;
|
2508 | }
|
2509 |
|
2510 | return this.callIds[0] < spyFn.callIds[spyFn.callIds.length - 1];
|
2511 | },
|
2512 |
|
2513 | calledAfter: function calledAfter(spyFn) {
|
2514 | if (!this.called || !spyFn.called) {
|
2515 | return false;
|
2516 | }
|
2517 |
|
2518 | return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1];
|
2519 | },
|
2520 |
|
2521 | withArgs: function() {
|
2522 | var args = slice.call(arguments);
|
2523 |
|
2524 | if (this.fakes) {
|
2525 | var match = matchingFake(this.fakes, args, true);
|
2526 |
|
2527 | if (match) {
|
2528 | return match;
|
2529 | }
|
2530 | } else {
|
2531 | this.fakes = [];
|
2532 | }
|
2533 |
|
2534 | var original = this;
|
2535 | var fake = this.instantiateFake();
|
2536 | fake.matchingAguments = args;
|
2537 | fake.parent = this;
|
2538 | push.call(this.fakes, fake);
|
2539 |
|
2540 | fake.withArgs = function() {
|
2541 | return original.withArgs.apply(original, arguments);
|
2542 | };
|
2543 |
|
2544 | for (var i = 0; i < this.args.length; i++) {
|
2545 | if (fake.matches(this.args[i])) {
|
2546 | incrementCallCount.call(fake);
|
2547 | push.call(fake.thisValues, this.thisValues[i]);
|
2548 | push.call(fake.args, this.args[i]);
|
2549 | push.call(fake.returnValues, this.returnValues[i]);
|
2550 | push.call(fake.exceptions, this.exceptions[i]);
|
2551 | push.call(fake.callIds, this.callIds[i]);
|
2552 | }
|
2553 | }
|
2554 | createCallProperties.call(fake);
|
2555 |
|
2556 | return fake;
|
2557 | },
|
2558 |
|
2559 | matches: function(args, strict) {
|
2560 | var margs = this.matchingAguments;
|
2561 |
|
2562 | if (margs.length <= args.length &&
|
2563 | sinon.deepEqual(margs, args.slice(0, margs.length))) {
|
2564 | return !strict || margs.length == args.length;
|
2565 | }
|
2566 | },
|
2567 |
|
2568 | printf: function(format) {
|
2569 | var spy = this;
|
2570 | var args = slice.call(arguments, 1);
|
2571 | var formatter;
|
2572 |
|
2573 | return (format || "").replace(/%(.)/g, function(match, specifyer) {
|
2574 | formatter = spyApi.formatters[specifyer];
|
2575 |
|
2576 | if (typeof formatter == "function") {
|
2577 | return formatter.call(null, spy, args);
|
2578 | } else if (!isNaN(parseInt(specifyer, 10))) {
|
2579 | return sinon.format(args[specifyer - 1]);
|
2580 | }
|
2581 |
|
2582 | return "%" + specifyer;
|
2583 | });
|
2584 | }
|
2585 | };
|
2586 |
|
2587 | function delegateToCalls(method, matchAny, actual, notCalled) {
|
2588 | spyApi[method] = function() {
|
2589 | if (!this.called) {
|
2590 | if (notCalled) {
|
2591 | return notCalled.apply(this, arguments);
|
2592 | }
|
2593 | return false;
|
2594 | }
|
2595 |
|
2596 | var currentCall;
|
2597 | var matches = 0;
|
2598 |
|
2599 | for (var i = 0, l = this.callCount; i < l; i += 1) {
|
2600 | currentCall = this.getCall(i);
|
2601 |
|
2602 | if (currentCall[actual || method].apply(currentCall, arguments)) {
|
2603 | matches += 1;
|
2604 |
|
2605 | if (matchAny) {
|
2606 | return true;
|
2607 | }
|
2608 | }
|
2609 | }
|
2610 |
|
2611 | return matches === this.callCount;
|
2612 | };
|
2613 | }
|
2614 |
|
2615 | delegateToCalls("calledOn", true);
|
2616 | delegateToCalls("alwaysCalledOn", false, "calledOn");
|
2617 | delegateToCalls("calledWith", true);
|
2618 | delegateToCalls("calledWithMatch", true);
|
2619 | delegateToCalls("alwaysCalledWith", false, "calledWith");
|
2620 | delegateToCalls("alwaysCalledWithMatch", false, "calledWithMatch");
|
2621 | delegateToCalls("calledWithExactly", true);
|
2622 | delegateToCalls("alwaysCalledWithExactly", false, "calledWithExactly");
|
2623 | delegateToCalls("neverCalledWith", false, "notCalledWith",
|
2624 | function() {
|
2625 | return true;
|
2626 | });
|
2627 | delegateToCalls("neverCalledWithMatch", false, "notCalledWithMatch",
|
2628 | function() {
|
2629 | return true;
|
2630 | });
|
2631 | delegateToCalls("threw", true);
|
2632 | delegateToCalls("alwaysThrew", false, "threw");
|
2633 | delegateToCalls("returned", true);
|
2634 | delegateToCalls("alwaysReturned", false, "returned");
|
2635 | delegateToCalls("calledWithNew", true);
|
2636 | delegateToCalls("alwaysCalledWithNew", false, "calledWithNew");
|
2637 | delegateToCalls("callArg", false, "callArgWith", function() {
|
2638 | throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
|
2639 | });
|
2640 | spyApi.callArgWith = spyApi.callArg;
|
2641 | delegateToCalls("callArgOn", false, "callArgOnWith", function() {
|
2642 | throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
|
2643 | });
|
2644 | spyApi.callArgOnWith = spyApi.callArgOn;
|
2645 | delegateToCalls("yield", false, "yield", function() {
|
2646 | throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
|
2647 | });
|
2648 |
|
2649 | spyApi.invokeCallback = spyApi.yield;
|
2650 | delegateToCalls("yieldOn", false, "yieldOn", function() {
|
2651 | throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
|
2652 | });
|
2653 | delegateToCalls("yieldTo", false, "yieldTo", function(property) {
|
2654 | throw new Error(this.toString() + " cannot yield to '" + property +
|
2655 | "' since it was not yet invoked.");
|
2656 | });
|
2657 | delegateToCalls("yieldToOn", false, "yieldToOn", function(property) {
|
2658 | throw new Error(this.toString() + " cannot yield to '" + property +
|
2659 | "' since it was not yet invoked.");
|
2660 | });
|
2661 |
|
2662 | spyApi.formatters = {
|
2663 | c: function(spy) {
|
2664 | return sinon.timesInWords(spy.callCount);
|
2665 | },
|
2666 |
|
2667 | n: function(spy) {
|
2668 | return spy.toString();
|
2669 | },
|
2670 |
|
2671 | C: function(spy) {
|
2672 | var calls = [];
|
2673 |
|
2674 | for (var i = 0, l = spy.callCount; i < l; ++i) {
|
2675 | var stringifiedCall = " " + spy.getCall(i).toString();
|
2676 | if (/\n/.test(calls[i - 1])) {
|
2677 | stringifiedCall = "\n" + stringifiedCall;
|
2678 | }
|
2679 | push.call(calls, stringifiedCall);
|
2680 | }
|
2681 |
|
2682 | return calls.length > 0 ? "\n" + calls.join("\n") : "";
|
2683 | },
|
2684 |
|
2685 | t: function(spy) {
|
2686 | var objects = [];
|
2687 |
|
2688 | for (var i = 0, l = spy.callCount; i < l; ++i) {
|
2689 | push.call(objects, sinon.format(spy.thisValues[i]));
|
2690 | }
|
2691 |
|
2692 | return objects.join(", ");
|
2693 | },
|
2694 |
|
2695 | "*": function(spy, args) {
|
2696 | var formatted = [];
|
2697 |
|
2698 | for (var i = 0, l = args.length; i < l; ++i) {
|
2699 | push.call(formatted, sinon.format(args[i]));
|
2700 | }
|
2701 |
|
2702 | return formatted.join(", ");
|
2703 | }
|
2704 | };
|
2705 |
|
2706 | sinon.extend(spy, spyApi);
|
2707 |
|
2708 | spy.spyCall = sinon.spyCall;
|
2709 | sinon.spy = spy;
|
2710 |
|
2711 | return spy;
|
2712 | }
|
2713 |
|
2714 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
2715 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
2716 |
|
2717 | function loadDependencies(require, exports, module) {
|
2718 | var sinon = require("./util/core");
|
2719 | require("./call");
|
2720 | module.exports = makeApi(sinon);
|
2721 | }
|
2722 |
|
2723 | if (isAMD) {
|
2724 | define(loadDependencies);
|
2725 | } else if (isNode) {
|
2726 | loadDependencies(require, module.exports, module);
|
2727 | } else if (!sinon) {
|
2728 | return;
|
2729 | } else {
|
2730 | makeApi(sinon);
|
2731 | }
|
2732 | }(typeof sinon == "object" && sinon || null));
|
2733 |
|
2734 | |
2735 |
|
2736 |
|
2737 |
|
2738 | |
2739 |
|
2740 |
|
2741 |
|
2742 |
|
2743 |
|
2744 |
|
2745 |
|
2746 |
|
2747 |
|
2748 | (function(sinon) {
|
2749 | var slice = Array.prototype.slice;
|
2750 | var join = Array.prototype.join;
|
2751 |
|
2752 | var nextTick = (function() {
|
2753 | if (typeof process === "object" && typeof process.nextTick === "function") {
|
2754 | return process.nextTick;
|
2755 | } else if (typeof setImmediate === "function") {
|
2756 | return setImmediate;
|
2757 | } else {
|
2758 | return function(callback) {
|
2759 | setTimeout(callback, 0);
|
2760 | };
|
2761 | }
|
2762 | })();
|
2763 |
|
2764 | function throwsException(error, message) {
|
2765 | if (typeof error == "string") {
|
2766 | this.exception = new Error(message || "");
|
2767 | this.exception.name = error;
|
2768 | } else if (!error) {
|
2769 | this.exception = new Error("Error");
|
2770 | } else {
|
2771 | this.exception = error;
|
2772 | }
|
2773 |
|
2774 | return this;
|
2775 | }
|
2776 |
|
2777 | function getCallback(behavior, args) {
|
2778 | var callArgAt = behavior.callArgAt;
|
2779 |
|
2780 | if (callArgAt < 0) {
|
2781 | var callArgProp = behavior.callArgProp;
|
2782 |
|
2783 | for (var i = 0, l = args.length; i < l; ++i) {
|
2784 | if (!callArgProp && typeof args[i] == "function") {
|
2785 | return args[i];
|
2786 | }
|
2787 |
|
2788 | if (callArgProp && args[i] &&
|
2789 | typeof args[i][callArgProp] == "function") {
|
2790 | return args[i][callArgProp];
|
2791 | }
|
2792 | }
|
2793 |
|
2794 | return null;
|
2795 | }
|
2796 |
|
2797 | return args[callArgAt];
|
2798 | }
|
2799 |
|
2800 | function makeApi(sinon) {
|
2801 | function getCallbackError(behavior, func, args) {
|
2802 | if (behavior.callArgAt < 0) {
|
2803 | var msg;
|
2804 |
|
2805 | if (behavior.callArgProp) {
|
2806 | msg = sinon.functionName(behavior.stub) +
|
2807 | " expected to yield to '" + behavior.callArgProp +
|
2808 | "', but no object with such a property was passed.";
|
2809 | } else {
|
2810 | msg = sinon.functionName(behavior.stub) +
|
2811 | " expected to yield, but no callback was passed.";
|
2812 | }
|
2813 |
|
2814 | if (args.length > 0) {
|
2815 | msg += " Received [" + join.call(args, ", ") + "]";
|
2816 | }
|
2817 |
|
2818 | return msg;
|
2819 | }
|
2820 |
|
2821 | return "argument at index " + behavior.callArgAt + " is not a function: " + func;
|
2822 | }
|
2823 |
|
2824 | function callCallback(behavior, args) {
|
2825 | if (typeof behavior.callArgAt == "number") {
|
2826 | var func = getCallback(behavior, args);
|
2827 |
|
2828 | if (typeof func != "function") {
|
2829 | throw new TypeError(getCallbackError(behavior, func, args));
|
2830 | }
|
2831 |
|
2832 | if (behavior.callbackAsync) {
|
2833 | nextTick(function() {
|
2834 | func.apply(behavior.callbackContext, behavior.callbackArguments);
|
2835 | });
|
2836 | } else {
|
2837 | func.apply(behavior.callbackContext, behavior.callbackArguments);
|
2838 | }
|
2839 | }
|
2840 | }
|
2841 |
|
2842 | var proto = {
|
2843 | create: function create(stub) {
|
2844 | var behavior = sinon.extend({}, sinon.behavior);
|
2845 | delete behavior.create;
|
2846 | behavior.stub = stub;
|
2847 |
|
2848 | return behavior;
|
2849 | },
|
2850 |
|
2851 | isPresent: function isPresent() {
|
2852 | return (typeof this.callArgAt == "number" ||
|
2853 | this.exception ||
|
2854 | typeof this.returnArgAt == "number" ||
|
2855 | this.returnThis ||
|
2856 | this.returnValueDefined);
|
2857 | },
|
2858 |
|
2859 | invoke: function invoke(context, args) {
|
2860 | callCallback(this, args);
|
2861 |
|
2862 | if (this.exception) {
|
2863 | throw this.exception;
|
2864 | } else if (typeof this.returnArgAt == "number") {
|
2865 | return args[this.returnArgAt];
|
2866 | } else if (this.returnThis) {
|
2867 | return context;
|
2868 | }
|
2869 |
|
2870 | return this.returnValue;
|
2871 | },
|
2872 |
|
2873 | onCall: function onCall(index) {
|
2874 | return this.stub.onCall(index);
|
2875 | },
|
2876 |
|
2877 | onFirstCall: function onFirstCall() {
|
2878 | return this.stub.onFirstCall();
|
2879 | },
|
2880 |
|
2881 | onSecondCall: function onSecondCall() {
|
2882 | return this.stub.onSecondCall();
|
2883 | },
|
2884 |
|
2885 | onThirdCall: function onThirdCall() {
|
2886 | return this.stub.onThirdCall();
|
2887 | },
|
2888 |
|
2889 | withArgs: function withArgs( /* arguments */ ) {
|
2890 | throw new Error("Defining a stub by invoking \"stub.onCall(...).withArgs(...)\" is not supported. " +
|
2891 | "Use \"stub.withArgs(...).onCall(...)\" to define sequential behavior for calls with certain arguments.");
|
2892 | },
|
2893 |
|
2894 | callsArg: function callsArg(pos) {
|
2895 | if (typeof pos != "number") {
|
2896 | throw new TypeError("argument index is not number");
|
2897 | }
|
2898 |
|
2899 | this.callArgAt = pos;
|
2900 | this.callbackArguments = [];
|
2901 | this.callbackContext = undefined;
|
2902 | this.callArgProp = undefined;
|
2903 | this.callbackAsync = false;
|
2904 |
|
2905 | return this;
|
2906 | },
|
2907 |
|
2908 | callsArgOn: function callsArgOn(pos, context) {
|
2909 | if (typeof pos != "number") {
|
2910 | throw new TypeError("argument index is not number");
|
2911 | }
|
2912 | if (typeof context != "object") {
|
2913 | throw new TypeError("argument context is not an object");
|
2914 | }
|
2915 |
|
2916 | this.callArgAt = pos;
|
2917 | this.callbackArguments = [];
|
2918 | this.callbackContext = context;
|
2919 | this.callArgProp = undefined;
|
2920 | this.callbackAsync = false;
|
2921 |
|
2922 | return this;
|
2923 | },
|
2924 |
|
2925 | callsArgWith: function callsArgWith(pos) {
|
2926 | if (typeof pos != "number") {
|
2927 | throw new TypeError("argument index is not number");
|
2928 | }
|
2929 |
|
2930 | this.callArgAt = pos;
|
2931 | this.callbackArguments = slice.call(arguments, 1);
|
2932 | this.callbackContext = undefined;
|
2933 | this.callArgProp = undefined;
|
2934 | this.callbackAsync = false;
|
2935 |
|
2936 | return this;
|
2937 | },
|
2938 |
|
2939 | callsArgOnWith: function callsArgWith(pos, context) {
|
2940 | if (typeof pos != "number") {
|
2941 | throw new TypeError("argument index is not number");
|
2942 | }
|
2943 | if (typeof context != "object") {
|
2944 | throw new TypeError("argument context is not an object");
|
2945 | }
|
2946 |
|
2947 | this.callArgAt = pos;
|
2948 | this.callbackArguments = slice.call(arguments, 2);
|
2949 | this.callbackContext = context;
|
2950 | this.callArgProp = undefined;
|
2951 | this.callbackAsync = false;
|
2952 |
|
2953 | return this;
|
2954 | },
|
2955 |
|
2956 | yields: function() {
|
2957 | this.callArgAt = -1;
|
2958 | this.callbackArguments = slice.call(arguments, 0);
|
2959 | this.callbackContext = undefined;
|
2960 | this.callArgProp = undefined;
|
2961 | this.callbackAsync = false;
|
2962 |
|
2963 | return this;
|
2964 | },
|
2965 |
|
2966 | yieldsOn: function(context) {
|
2967 | if (typeof context != "object") {
|
2968 | throw new TypeError("argument context is not an object");
|
2969 | }
|
2970 |
|
2971 | this.callArgAt = -1;
|
2972 | this.callbackArguments = slice.call(arguments, 1);
|
2973 | this.callbackContext = context;
|
2974 | this.callArgProp = undefined;
|
2975 | this.callbackAsync = false;
|
2976 |
|
2977 | return this;
|
2978 | },
|
2979 |
|
2980 | yieldsTo: function(prop) {
|
2981 | this.callArgAt = -1;
|
2982 | this.callbackArguments = slice.call(arguments, 1);
|
2983 | this.callbackContext = undefined;
|
2984 | this.callArgProp = prop;
|
2985 | this.callbackAsync = false;
|
2986 |
|
2987 | return this;
|
2988 | },
|
2989 |
|
2990 | yieldsToOn: function(prop, context) {
|
2991 | if (typeof context != "object") {
|
2992 | throw new TypeError("argument context is not an object");
|
2993 | }
|
2994 |
|
2995 | this.callArgAt = -1;
|
2996 | this.callbackArguments = slice.call(arguments, 2);
|
2997 | this.callbackContext = context;
|
2998 | this.callArgProp = prop;
|
2999 | this.callbackAsync = false;
|
3000 |
|
3001 | return this;
|
3002 | },
|
3003 |
|
3004 | throws: throwsException,
|
3005 | throwsException: throwsException,
|
3006 |
|
3007 | returns: function returns(value) {
|
3008 | this.returnValue = value;
|
3009 | this.returnValueDefined = true;
|
3010 |
|
3011 | return this;
|
3012 | },
|
3013 |
|
3014 | returnsArg: function returnsArg(pos) {
|
3015 | if (typeof pos != "number") {
|
3016 | throw new TypeError("argument index is not number");
|
3017 | }
|
3018 |
|
3019 | this.returnArgAt = pos;
|
3020 |
|
3021 | return this;
|
3022 | },
|
3023 |
|
3024 | returnsThis: function returnsThis() {
|
3025 | this.returnThis = true;
|
3026 |
|
3027 | return this;
|
3028 | }
|
3029 | };
|
3030 |
|
3031 |
|
3032 | for (var method in proto) {
|
3033 |
|
3034 | if (proto.hasOwnProperty(method) &&
|
3035 | method.match(/^(callsArg|yields)/) && !method.match(/Async/)) {
|
3036 | proto[method + "Async"] = (function(syncFnName) {
|
3037 | return function() {
|
3038 | var result = this[syncFnName].apply(this, arguments);
|
3039 | this.callbackAsync = true;
|
3040 | return result;
|
3041 | };
|
3042 | })(method);
|
3043 | }
|
3044 | }
|
3045 |
|
3046 | sinon.behavior = proto;
|
3047 | return proto;
|
3048 | }
|
3049 |
|
3050 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
3051 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
3052 |
|
3053 | function loadDependencies(require, exports, module) {
|
3054 | var sinon = require("./util/core");
|
3055 | module.exports = makeApi(sinon);
|
3056 | }
|
3057 |
|
3058 | if (isAMD) {
|
3059 | define(loadDependencies);
|
3060 | } else if (isNode) {
|
3061 | loadDependencies(require, module.exports, module);
|
3062 | } else if (!sinon) {
|
3063 | return;
|
3064 | } else {
|
3065 | makeApi(sinon);
|
3066 | }
|
3067 | }(typeof sinon == "object" && sinon || null));
|
3068 |
|
3069 | |
3070 |
|
3071 |
|
3072 |
|
3073 |
|
3074 |
|
3075 | |
3076 |
|
3077 |
|
3078 |
|
3079 |
|
3080 |
|
3081 |
|
3082 |
|
3083 |
|
3084 | (function(sinon) {
|
3085 | function makeApi(sinon) {
|
3086 | function stub(object, property, func) {
|
3087 | if ( !! func && typeof func != "function") {
|
3088 | throw new TypeError("Custom stub should be function");
|
3089 | }
|
3090 |
|
3091 | var wrapper;
|
3092 |
|
3093 | if (func) {
|
3094 | wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func;
|
3095 | } else {
|
3096 | wrapper = stub.create();
|
3097 | }
|
3098 |
|
3099 | if (!object && typeof property === "undefined") {
|
3100 | return sinon.stub.create();
|
3101 | }
|
3102 |
|
3103 | if (typeof property === "undefined" && typeof object == "object") {
|
3104 | for (var prop in object) {
|
3105 | if (typeof object[prop] === "function") {
|
3106 | stub(object, prop);
|
3107 | }
|
3108 | }
|
3109 |
|
3110 | return object;
|
3111 | }
|
3112 |
|
3113 | return sinon.wrapMethod(object, property, wrapper);
|
3114 | }
|
3115 |
|
3116 | function getDefaultBehavior(stub) {
|
3117 | return stub.defaultBehavior || getParentBehaviour(stub) || sinon.behavior.create(stub);
|
3118 | }
|
3119 |
|
3120 | function getParentBehaviour(stub) {
|
3121 | return (stub.parent && getCurrentBehavior(stub.parent));
|
3122 | }
|
3123 |
|
3124 | function getCurrentBehavior(stub) {
|
3125 | var behavior = stub.behaviors[stub.callCount - 1];
|
3126 | return behavior && behavior.isPresent() ? behavior : getDefaultBehavior(stub);
|
3127 | }
|
3128 |
|
3129 | var uuid = 0;
|
3130 |
|
3131 | var proto = {
|
3132 | create: function create() {
|
3133 | var functionStub = function() {
|
3134 | return getCurrentBehavior(functionStub).invoke(this, arguments);
|
3135 | };
|
3136 |
|
3137 | functionStub.id = "stub#" + uuid++;
|
3138 | var orig = functionStub;
|
3139 | functionStub = sinon.spy.create(functionStub);
|
3140 | functionStub.func = orig;
|
3141 |
|
3142 | sinon.extend(functionStub, stub);
|
3143 | functionStub.instantiateFake = sinon.stub.create;
|
3144 | functionStub.displayName = "stub";
|
3145 | functionStub.toString = sinon.functionToString;
|
3146 |
|
3147 | functionStub.defaultBehavior = null;
|
3148 | functionStub.behaviors = [];
|
3149 |
|
3150 | return functionStub;
|
3151 | },
|
3152 |
|
3153 | resetBehavior: function() {
|
3154 | var i;
|
3155 |
|
3156 | this.defaultBehavior = null;
|
3157 | this.behaviors = [];
|
3158 |
|
3159 | delete this.returnValue;
|
3160 | delete this.returnArgAt;
|
3161 | this.returnThis = false;
|
3162 |
|
3163 | if (this.fakes) {
|
3164 | for (i = 0; i < this.fakes.length; i++) {
|
3165 | this.fakes[i].resetBehavior();
|
3166 | }
|
3167 | }
|
3168 | },
|
3169 |
|
3170 | onCall: function onCall(index) {
|
3171 | if (!this.behaviors[index]) {
|
3172 | this.behaviors[index] = sinon.behavior.create(this);
|
3173 | }
|
3174 |
|
3175 | return this.behaviors[index];
|
3176 | },
|
3177 |
|
3178 | onFirstCall: function onFirstCall() {
|
3179 | return this.onCall(0);
|
3180 | },
|
3181 |
|
3182 | onSecondCall: function onSecondCall() {
|
3183 | return this.onCall(1);
|
3184 | },
|
3185 |
|
3186 | onThirdCall: function onThirdCall() {
|
3187 | return this.onCall(2);
|
3188 | }
|
3189 | };
|
3190 |
|
3191 | for (var method in sinon.behavior) {
|
3192 | if (sinon.behavior.hasOwnProperty(method) && !proto.hasOwnProperty(method) &&
|
3193 | method != "create" &&
|
3194 | method != "withArgs" &&
|
3195 | method != "invoke") {
|
3196 | proto[method] = (function(behaviorMethod) {
|
3197 | return function() {
|
3198 | this.defaultBehavior = this.defaultBehavior || sinon.behavior.create(this);
|
3199 | this.defaultBehavior[behaviorMethod].apply(this.defaultBehavior, arguments);
|
3200 | return this;
|
3201 | };
|
3202 | }(method));
|
3203 | }
|
3204 | }
|
3205 |
|
3206 | sinon.extend(stub, proto);
|
3207 | sinon.stub = stub;
|
3208 |
|
3209 | return stub;
|
3210 | }
|
3211 |
|
3212 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
3213 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
3214 |
|
3215 | function loadDependencies(require, exports, module) {
|
3216 | var sinon = require("./util/core");
|
3217 | require("./behavior");
|
3218 | require("./spy");
|
3219 | module.exports = makeApi(sinon);
|
3220 | }
|
3221 |
|
3222 | if (isAMD) {
|
3223 | define(loadDependencies);
|
3224 | } else if (isNode) {
|
3225 | loadDependencies(require, module.exports, module);
|
3226 | } else if (!sinon) {
|
3227 | return;
|
3228 | } else {
|
3229 | makeApi(sinon);
|
3230 | }
|
3231 | }(typeof sinon == "object" && sinon || null));
|
3232 |
|
3233 | |
3234 |
|
3235 |
|
3236 |
|
3237 |
|
3238 |
|
3239 |
|
3240 | |
3241 |
|
3242 |
|
3243 |
|
3244 |
|
3245 |
|
3246 |
|
3247 |
|
3248 |
|
3249 | (function(sinon) {
|
3250 | function makeApi(sinon) {
|
3251 | var push = [].push;
|
3252 | var match = sinon.match;
|
3253 |
|
3254 | function mock(object) {
|
3255 | if (!object) {
|
3256 | return sinon.expectation.create("Anonymous mock");
|
3257 | }
|
3258 |
|
3259 | return mock.create(object);
|
3260 | }
|
3261 |
|
3262 | function each(collection, callback) {
|
3263 | if (!collection) {
|
3264 | return;
|
3265 | }
|
3266 |
|
3267 | for (var i = 0, l = collection.length; i < l; i += 1) {
|
3268 | callback(collection[i]);
|
3269 | }
|
3270 | }
|
3271 |
|
3272 | sinon.extend(mock, {
|
3273 | create: function create(object) {
|
3274 | if (!object) {
|
3275 | throw new TypeError("object is null");
|
3276 | }
|
3277 |
|
3278 | var mockObject = sinon.extend({}, mock);
|
3279 | mockObject.object = object;
|
3280 | delete mockObject.create;
|
3281 |
|
3282 | return mockObject;
|
3283 | },
|
3284 |
|
3285 | expects: function expects(method) {
|
3286 | if (!method) {
|
3287 | throw new TypeError("method is falsy");
|
3288 | }
|
3289 |
|
3290 | if (!this.expectations) {
|
3291 | this.expectations = {};
|
3292 | this.proxies = [];
|
3293 | }
|
3294 |
|
3295 | if (!this.expectations[method]) {
|
3296 | this.expectations[method] = [];
|
3297 | var mockObject = this;
|
3298 |
|
3299 | sinon.wrapMethod(this.object, method, function() {
|
3300 | return mockObject.invokeMethod(method, this, arguments);
|
3301 | });
|
3302 |
|
3303 | push.call(this.proxies, method);
|
3304 | }
|
3305 |
|
3306 | var expectation = sinon.expectation.create(method);
|
3307 | push.call(this.expectations[method], expectation);
|
3308 |
|
3309 | return expectation;
|
3310 | },
|
3311 |
|
3312 | restore: function restore() {
|
3313 | var object = this.object;
|
3314 |
|
3315 | each(this.proxies, function(proxy) {
|
3316 | if (typeof object[proxy].restore == "function") {
|
3317 | object[proxy].restore();
|
3318 | }
|
3319 | });
|
3320 | },
|
3321 |
|
3322 | verify: function verify() {
|
3323 | var expectations = this.expectations || {};
|
3324 | var messages = [],
|
3325 | met = [];
|
3326 |
|
3327 | each(this.proxies, function(proxy) {
|
3328 | each(expectations[proxy], function(expectation) {
|
3329 | if (!expectation.met()) {
|
3330 | push.call(messages, expectation.toString());
|
3331 | } else {
|
3332 | push.call(met, expectation.toString());
|
3333 | }
|
3334 | });
|
3335 | });
|
3336 |
|
3337 | this.restore();
|
3338 |
|
3339 | if (messages.length > 0) {
|
3340 | sinon.expectation.fail(messages.concat(met).join("\n"));
|
3341 | } else if (met.length > 0) {
|
3342 | sinon.expectation.pass(messages.concat(met).join("\n"));
|
3343 | }
|
3344 |
|
3345 | return true;
|
3346 | },
|
3347 |
|
3348 | invokeMethod: function invokeMethod(method, thisValue, args) {
|
3349 | var expectations = this.expectations && this.expectations[method];
|
3350 | var length = expectations && expectations.length || 0,
|
3351 | i;
|
3352 |
|
3353 | for (i = 0; i < length; i += 1) {
|
3354 | if (!expectations[i].met() &&
|
3355 | expectations[i].allowsCall(thisValue, args)) {
|
3356 | return expectations[i].apply(thisValue, args);
|
3357 | }
|
3358 | }
|
3359 |
|
3360 | var messages = [],
|
3361 | available, exhausted = 0;
|
3362 |
|
3363 | for (i = 0; i < length; i += 1) {
|
3364 | if (expectations[i].allowsCall(thisValue, args)) {
|
3365 | available = available || expectations[i];
|
3366 | } else {
|
3367 | exhausted += 1;
|
3368 | }
|
3369 | push.call(messages, " " + expectations[i].toString());
|
3370 | }
|
3371 |
|
3372 | if (exhausted === 0) {
|
3373 | return available.apply(thisValue, args);
|
3374 | }
|
3375 |
|
3376 | messages.unshift("Unexpected call: " + sinon.spyCall.toString.call({
|
3377 | proxy: method,
|
3378 | args: args
|
3379 | }));
|
3380 |
|
3381 | sinon.expectation.fail(messages.join("\n"));
|
3382 | }
|
3383 | });
|
3384 |
|
3385 | var times = sinon.timesInWords;
|
3386 | var slice = Array.prototype.slice;
|
3387 |
|
3388 | function callCountInWords(callCount) {
|
3389 | if (callCount == 0) {
|
3390 | return "never called";
|
3391 | } else {
|
3392 | return "called " + times(callCount);
|
3393 | }
|
3394 | }
|
3395 |
|
3396 | function expectedCallCountInWords(expectation) {
|
3397 | var min = expectation.minCalls;
|
3398 | var max = expectation.maxCalls;
|
3399 |
|
3400 | if (typeof min == "number" && typeof max == "number") {
|
3401 | var str = times(min);
|
3402 |
|
3403 | if (min != max) {
|
3404 | str = "at least " + str + " and at most " + times(max);
|
3405 | }
|
3406 |
|
3407 | return str;
|
3408 | }
|
3409 |
|
3410 | if (typeof min == "number") {
|
3411 | return "at least " + times(min);
|
3412 | }
|
3413 |
|
3414 | return "at most " + times(max);
|
3415 | }
|
3416 |
|
3417 | function receivedMinCalls(expectation) {
|
3418 | var hasMinLimit = typeof expectation.minCalls == "number";
|
3419 | return !hasMinLimit || expectation.callCount >= expectation.minCalls;
|
3420 | }
|
3421 |
|
3422 | function receivedMaxCalls(expectation) {
|
3423 | if (typeof expectation.maxCalls != "number") {
|
3424 | return false;
|
3425 | }
|
3426 |
|
3427 | return expectation.callCount == expectation.maxCalls;
|
3428 | }
|
3429 |
|
3430 | function verifyMatcher(possibleMatcher, arg) {
|
3431 | if (match && match.isMatcher(possibleMatcher)) {
|
3432 | return possibleMatcher.test(arg);
|
3433 | } else {
|
3434 | return true;
|
3435 | }
|
3436 | }
|
3437 |
|
3438 | sinon.expectation = {
|
3439 | minCalls: 1,
|
3440 | maxCalls: 1,
|
3441 |
|
3442 | create: function create(methodName) {
|
3443 | var expectation = sinon.extend(sinon.stub.create(), sinon.expectation);
|
3444 | delete expectation.create;
|
3445 | expectation.method = methodName;
|
3446 |
|
3447 | return expectation;
|
3448 | },
|
3449 |
|
3450 | invoke: function invoke(func, thisValue, args) {
|
3451 | this.verifyCallAllowed(thisValue, args);
|
3452 |
|
3453 | return sinon.spy.invoke.apply(this, arguments);
|
3454 | },
|
3455 |
|
3456 | atLeast: function atLeast(num) {
|
3457 | if (typeof num != "number") {
|
3458 | throw new TypeError("'" + num + "' is not number");
|
3459 | }
|
3460 |
|
3461 | if (!this.limitsSet) {
|
3462 | this.maxCalls = null;
|
3463 | this.limitsSet = true;
|
3464 | }
|
3465 |
|
3466 | this.minCalls = num;
|
3467 |
|
3468 | return this;
|
3469 | },
|
3470 |
|
3471 | atMost: function atMost(num) {
|
3472 | if (typeof num != "number") {
|
3473 | throw new TypeError("'" + num + "' is not number");
|
3474 | }
|
3475 |
|
3476 | if (!this.limitsSet) {
|
3477 | this.minCalls = null;
|
3478 | this.limitsSet = true;
|
3479 | }
|
3480 |
|
3481 | this.maxCalls = num;
|
3482 |
|
3483 | return this;
|
3484 | },
|
3485 |
|
3486 | never: function never() {
|
3487 | return this.exactly(0);
|
3488 | },
|
3489 |
|
3490 | once: function once() {
|
3491 | return this.exactly(1);
|
3492 | },
|
3493 |
|
3494 | twice: function twice() {
|
3495 | return this.exactly(2);
|
3496 | },
|
3497 |
|
3498 | thrice: function thrice() {
|
3499 | return this.exactly(3);
|
3500 | },
|
3501 |
|
3502 | exactly: function exactly(num) {
|
3503 | if (typeof num != "number") {
|
3504 | throw new TypeError("'" + num + "' is not a number");
|
3505 | }
|
3506 |
|
3507 | this.atLeast(num);
|
3508 | return this.atMost(num);
|
3509 | },
|
3510 |
|
3511 | met: function met() {
|
3512 | return !this.failed && receivedMinCalls(this);
|
3513 | },
|
3514 |
|
3515 | verifyCallAllowed: function verifyCallAllowed(thisValue, args) {
|
3516 | if (receivedMaxCalls(this)) {
|
3517 | this.failed = true;
|
3518 | sinon.expectation.fail(this.method + " already called " + times(this.maxCalls));
|
3519 | }
|
3520 |
|
3521 | if ("expectedThis" in this && this.expectedThis !== thisValue) {
|
3522 | sinon.expectation.fail(this.method + " called with " + thisValue + " as thisValue, expected " +
|
3523 | this.expectedThis);
|
3524 | }
|
3525 |
|
3526 | if (!("expectedArguments" in this)) {
|
3527 | return;
|
3528 | }
|
3529 |
|
3530 | if (!args) {
|
3531 | sinon.expectation.fail(this.method + " received no arguments, expected " +
|
3532 | sinon.format(this.expectedArguments));
|
3533 | }
|
3534 |
|
3535 | if (args.length < this.expectedArguments.length) {
|
3536 | sinon.expectation.fail(this.method + " received too few arguments (" + sinon.format(args) +
|
3537 | "), expected " + sinon.format(this.expectedArguments));
|
3538 | }
|
3539 |
|
3540 | if (this.expectsExactArgCount &&
|
3541 | args.length != this.expectedArguments.length) {
|
3542 | sinon.expectation.fail(this.method + " received too many arguments (" + sinon.format(args) +
|
3543 | "), expected " + sinon.format(this.expectedArguments));
|
3544 | }
|
3545 |
|
3546 | for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
|
3547 |
|
3548 | if (!verifyMatcher(this.expectedArguments[i], args[i])) {
|
3549 | sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) +
|
3550 | ", didn't match " + this.expectedArguments.toString());
|
3551 | }
|
3552 |
|
3553 | if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
|
3554 | sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) +
|
3555 | ", expected " + sinon.format(this.expectedArguments));
|
3556 | }
|
3557 | }
|
3558 | },
|
3559 |
|
3560 | allowsCall: function allowsCall(thisValue, args) {
|
3561 | if (this.met() && receivedMaxCalls(this)) {
|
3562 | return false;
|
3563 | }
|
3564 |
|
3565 | if ("expectedThis" in this && this.expectedThis !== thisValue) {
|
3566 | return false;
|
3567 | }
|
3568 |
|
3569 | if (!("expectedArguments" in this)) {
|
3570 | return true;
|
3571 | }
|
3572 |
|
3573 | args = args || [];
|
3574 |
|
3575 | if (args.length < this.expectedArguments.length) {
|
3576 | return false;
|
3577 | }
|
3578 |
|
3579 | if (this.expectsExactArgCount &&
|
3580 | args.length != this.expectedArguments.length) {
|
3581 | return false;
|
3582 | }
|
3583 |
|
3584 | for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
|
3585 | if (!verifyMatcher(this.expectedArguments[i], args[i])) {
|
3586 | return false;
|
3587 | }
|
3588 |
|
3589 | if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
|
3590 | return false;
|
3591 | }
|
3592 | }
|
3593 |
|
3594 | return true;
|
3595 | },
|
3596 |
|
3597 | withArgs: function withArgs() {
|
3598 | this.expectedArguments = slice.call(arguments);
|
3599 | return this;
|
3600 | },
|
3601 |
|
3602 | withExactArgs: function withExactArgs() {
|
3603 | this.withArgs.apply(this, arguments);
|
3604 | this.expectsExactArgCount = true;
|
3605 | return this;
|
3606 | },
|
3607 |
|
3608 | on: function on(thisValue) {
|
3609 | this.expectedThis = thisValue;
|
3610 | return this;
|
3611 | },
|
3612 |
|
3613 | toString: function() {
|
3614 | var args = (this.expectedArguments || []).slice();
|
3615 |
|
3616 | if (!this.expectsExactArgCount) {
|
3617 | push.call(args, "[...]");
|
3618 | }
|
3619 |
|
3620 | var callStr = sinon.spyCall.toString.call({
|
3621 | proxy: this.method || "anonymous mock expectation",
|
3622 | args: args
|
3623 | });
|
3624 |
|
3625 | var message = callStr.replace(", [...", "[, ...") + " " +
|
3626 | expectedCallCountInWords(this);
|
3627 |
|
3628 | if (this.met()) {
|
3629 | return "Expectation met: " + message;
|
3630 | }
|
3631 |
|
3632 | return "Expected " + message + " (" +
|
3633 | callCountInWords(this.callCount) + ")";
|
3634 | },
|
3635 |
|
3636 | verify: function verify() {
|
3637 | if (!this.met()) {
|
3638 | sinon.expectation.fail(this.toString());
|
3639 | } else {
|
3640 | sinon.expectation.pass(this.toString());
|
3641 | }
|
3642 |
|
3643 | return true;
|
3644 | },
|
3645 |
|
3646 | pass: function pass(message) {
|
3647 | sinon.assert.pass(message);
|
3648 | },
|
3649 |
|
3650 | fail: function fail(message) {
|
3651 | var exception = new Error(message);
|
3652 | exception.name = "ExpectationError";
|
3653 |
|
3654 | throw exception;
|
3655 | }
|
3656 | };
|
3657 |
|
3658 | sinon.mock = mock;
|
3659 | return mock;
|
3660 | }
|
3661 |
|
3662 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
3663 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
3664 |
|
3665 | function loadDependencies(require, exports, module) {
|
3666 | var sinon = require("./util/core");
|
3667 | require("./call");
|
3668 | require("./match");
|
3669 | require("./spy");
|
3670 | module.exports = makeApi(sinon);
|
3671 | }
|
3672 |
|
3673 | if (isAMD) {
|
3674 | define(loadDependencies);
|
3675 | } else if (isNode) {
|
3676 | loadDependencies(require, module.exports, module);
|
3677 | } else if (!sinon) {
|
3678 | return;
|
3679 | } else {
|
3680 | makeApi(sinon);
|
3681 | }
|
3682 | }(typeof sinon == "object" && sinon || null));
|
3683 |
|
3684 | |
3685 |
|
3686 |
|
3687 |
|
3688 |
|
3689 | |
3690 |
|
3691 |
|
3692 |
|
3693 |
|
3694 |
|
3695 |
|
3696 |
|
3697 |
|
3698 | (function(sinon) {
|
3699 | var push = [].push;
|
3700 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
3701 |
|
3702 | function getFakes(fakeCollection) {
|
3703 | if (!fakeCollection.fakes) {
|
3704 | fakeCollection.fakes = [];
|
3705 | }
|
3706 |
|
3707 | return fakeCollection.fakes;
|
3708 | }
|
3709 |
|
3710 | function each(fakeCollection, method) {
|
3711 | var fakes = getFakes(fakeCollection);
|
3712 |
|
3713 | for (var i = 0, l = fakes.length; i < l; i += 1) {
|
3714 | if (typeof fakes[i][method] == "function") {
|
3715 | fakes[i][method]();
|
3716 | }
|
3717 | }
|
3718 | }
|
3719 |
|
3720 | function compact(fakeCollection) {
|
3721 | var fakes = getFakes(fakeCollection);
|
3722 | var i = 0;
|
3723 | while (i < fakes.length) {
|
3724 | fakes.splice(i, 1);
|
3725 | }
|
3726 | }
|
3727 |
|
3728 | function makeApi(sinon) {
|
3729 | var collection = {
|
3730 | verify: function resolve() {
|
3731 | each(this, "verify");
|
3732 | },
|
3733 |
|
3734 | restore: function restore() {
|
3735 | each(this, "restore");
|
3736 | compact(this);
|
3737 | },
|
3738 |
|
3739 | reset: function restore() {
|
3740 | each(this, "reset");
|
3741 | },
|
3742 |
|
3743 | verifyAndRestore: function verifyAndRestore() {
|
3744 | var exception;
|
3745 |
|
3746 | try {
|
3747 | this.verify();
|
3748 | } catch (e) {
|
3749 | exception = e;
|
3750 | }
|
3751 |
|
3752 | this.restore();
|
3753 |
|
3754 | if (exception) {
|
3755 | throw exception;
|
3756 | }
|
3757 | },
|
3758 |
|
3759 | add: function add(fake) {
|
3760 | push.call(getFakes(this), fake);
|
3761 | return fake;
|
3762 | },
|
3763 |
|
3764 | spy: function spy() {
|
3765 | return this.add(sinon.spy.apply(sinon, arguments));
|
3766 | },
|
3767 |
|
3768 | stub: function stub(object, property, value) {
|
3769 | if (property) {
|
3770 | var original = object[property];
|
3771 |
|
3772 | if (typeof original != "function") {
|
3773 | if (!hasOwnProperty.call(object, property)) {
|
3774 | throw new TypeError("Cannot stub non-existent own property " + property);
|
3775 | }
|
3776 |
|
3777 | object[property] = value;
|
3778 |
|
3779 | return this.add({
|
3780 | restore: function() {
|
3781 | object[property] = original;
|
3782 | }
|
3783 | });
|
3784 | }
|
3785 | }
|
3786 | if (!property && !! object && typeof object == "object") {
|
3787 | var stubbedObj = sinon.stub.apply(sinon, arguments);
|
3788 |
|
3789 | for (var prop in stubbedObj) {
|
3790 | if (typeof stubbedObj[prop] === "function") {
|
3791 | this.add(stubbedObj[prop]);
|
3792 | }
|
3793 | }
|
3794 |
|
3795 | return stubbedObj;
|
3796 | }
|
3797 |
|
3798 | return this.add(sinon.stub.apply(sinon, arguments));
|
3799 | },
|
3800 |
|
3801 | mock: function mock() {
|
3802 | return this.add(sinon.mock.apply(sinon, arguments));
|
3803 | },
|
3804 |
|
3805 | inject: function inject(obj) {
|
3806 | var col = this;
|
3807 |
|
3808 | obj.spy = function() {
|
3809 | return col.spy.apply(col, arguments);
|
3810 | };
|
3811 |
|
3812 | obj.stub = function() {
|
3813 | return col.stub.apply(col, arguments);
|
3814 | };
|
3815 |
|
3816 | obj.mock = function() {
|
3817 | return col.mock.apply(col, arguments);
|
3818 | };
|
3819 |
|
3820 | return obj;
|
3821 | }
|
3822 | };
|
3823 |
|
3824 | sinon.collection = collection;
|
3825 | return collection;
|
3826 | }
|
3827 |
|
3828 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
3829 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
3830 |
|
3831 | function loadDependencies(require, exports, module) {
|
3832 | var sinon = require("./util/core");
|
3833 | require("./mock");
|
3834 | require("./spy");
|
3835 | require("./stub");
|
3836 | module.exports = makeApi(sinon);
|
3837 | }
|
3838 |
|
3839 | if (isAMD) {
|
3840 | define(loadDependencies);
|
3841 | } else if (isNode) {
|
3842 | loadDependencies(require, module.exports, module);
|
3843 | } else if (!sinon) {
|
3844 | return;
|
3845 | } else {
|
3846 | makeApi(sinon);
|
3847 | }
|
3848 | }(typeof sinon == "object" && sinon || null));
|
3849 |
|
3850 |
|
3851 |
|
3852 | |
3853 |
|
3854 |
|
3855 |
|
3856 |
|
3857 |
|
3858 |
|
3859 |
|
3860 |
|
3861 |
|
3862 |
|
3863 |
|
3864 |
|
3865 |
|
3866 |
|
3867 |
|
3868 |
|
3869 |
|
3870 | if (typeof sinon == "undefined") {
|
3871 | var sinon = {};
|
3872 | }
|
3873 |
|
3874 | (function(global) {
|
3875 | function makeApi(sinon, lol) {
|
3876 | var _lolex = typeof lolex !== "undefined" ? lolex : lol;
|
3877 |
|
3878 | sinon.useFakeTimers = function() {
|
3879 | var now, methods = Array.prototype.slice.call(arguments);
|
3880 |
|
3881 | if (typeof methods[0] === "string") {
|
3882 | now = 0;
|
3883 | } else {
|
3884 | now = methods.shift();
|
3885 | }
|
3886 |
|
3887 | var clock = _lolex.install(now || 0, methods);
|
3888 | clock.restore = clock.uninstall;
|
3889 | return clock;
|
3890 | };
|
3891 |
|
3892 | sinon.clock = {
|
3893 | create: function(now) {
|
3894 | return _lolex.createClock(now);
|
3895 | }
|
3896 | };
|
3897 |
|
3898 | sinon.timers = {
|
3899 | setTimeout: setTimeout,
|
3900 | clearTimeout: clearTimeout,
|
3901 | setImmediate: (typeof setImmediate !== "undefined" ? setImmediate : undefined),
|
3902 | clearImmediate: (typeof clearImmediate !== "undefined" ? clearImmediate : undefined),
|
3903 | setInterval: setInterval,
|
3904 | clearInterval: clearInterval,
|
3905 | Date: Date
|
3906 | };
|
3907 | }
|
3908 |
|
3909 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
3910 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
3911 |
|
3912 | function loadDependencies(require, epxorts, module) {
|
3913 | var sinon = require("./core");
|
3914 | makeApi(sinon, require("lolex"));
|
3915 | module.exports = sinon;
|
3916 | }
|
3917 |
|
3918 | if (isAMD) {
|
3919 | define(loadDependencies);
|
3920 | } else if (isNode) {
|
3921 | loadDependencies(require, module.exports, module);
|
3922 | } else {
|
3923 | makeApi(sinon);
|
3924 | }
|
3925 | }(typeof global != "undefined" && typeof global !== "function" ? global : this));
|
3926 |
|
3927 | |
3928 |
|
3929 |
|
3930 |
|
3931 |
|
3932 |
|
3933 |
|
3934 |
|
3935 |
|
3936 |
|
3937 |
|
3938 |
|
3939 |
|
3940 | if (typeof sinon == "undefined") {
|
3941 | this.sinon = {};
|
3942 | }
|
3943 |
|
3944 | (function() {
|
3945 | var push = [].push;
|
3946 |
|
3947 | function makeApi(sinon) {
|
3948 | sinon.Event = function Event(type, bubbles, cancelable, target) {
|
3949 | this.initEvent(type, bubbles, cancelable, target);
|
3950 | };
|
3951 |
|
3952 | sinon.Event.prototype = {
|
3953 | initEvent: function(type, bubbles, cancelable, target) {
|
3954 | this.type = type;
|
3955 | this.bubbles = bubbles;
|
3956 | this.cancelable = cancelable;
|
3957 | this.target = target;
|
3958 | },
|
3959 |
|
3960 | stopPropagation: function() {},
|
3961 |
|
3962 | preventDefault: function() {
|
3963 | this.defaultPrevented = true;
|
3964 | }
|
3965 | };
|
3966 |
|
3967 | sinon.ProgressEvent = function ProgressEvent(type, progressEventRaw, target) {
|
3968 | this.initEvent(type, false, false, target);
|
3969 | this.loaded = progressEventRaw.loaded || null;
|
3970 | this.total = progressEventRaw.total || null;
|
3971 | };
|
3972 |
|
3973 | sinon.ProgressEvent.prototype = new sinon.Event();
|
3974 |
|
3975 | sinon.ProgressEvent.prototype.constructor = sinon.ProgressEvent;
|
3976 |
|
3977 | sinon.CustomEvent = function CustomEvent(type, customData, target) {
|
3978 | this.initEvent(type, false, false, target);
|
3979 | this.detail = customData.detail || null;
|
3980 | };
|
3981 |
|
3982 | sinon.CustomEvent.prototype = new sinon.Event();
|
3983 |
|
3984 | sinon.CustomEvent.prototype.constructor = sinon.CustomEvent;
|
3985 |
|
3986 | sinon.EventTarget = {
|
3987 | addEventListener: function addEventListener(event, listener) {
|
3988 | this.eventListeners = this.eventListeners || {};
|
3989 | this.eventListeners[event] = this.eventListeners[event] || [];
|
3990 | push.call(this.eventListeners[event], listener);
|
3991 | },
|
3992 |
|
3993 | removeEventListener: function removeEventListener(event, listener) {
|
3994 | var listeners = this.eventListeners && this.eventListeners[event] || [];
|
3995 |
|
3996 | for (var i = 0, l = listeners.length; i < l; ++i) {
|
3997 | if (listeners[i] == listener) {
|
3998 | return listeners.splice(i, 1);
|
3999 | }
|
4000 | }
|
4001 | },
|
4002 |
|
4003 | dispatchEvent: function dispatchEvent(event) {
|
4004 | var type = event.type;
|
4005 | var listeners = this.eventListeners && this.eventListeners[type] || [];
|
4006 |
|
4007 | for (var i = 0; i < listeners.length; i++) {
|
4008 | if (typeof listeners[i] == "function") {
|
4009 | listeners[i].call(this, event);
|
4010 | } else {
|
4011 | listeners[i].handleEvent(event);
|
4012 | }
|
4013 | }
|
4014 |
|
4015 | return !!event.defaultPrevented;
|
4016 | }
|
4017 | };
|
4018 | }
|
4019 |
|
4020 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
4021 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
4022 |
|
4023 | function loadDependencies(require) {
|
4024 | var sinon = require("./core");
|
4025 | makeApi(sinon);
|
4026 | }
|
4027 |
|
4028 | if (isAMD) {
|
4029 | define(loadDependencies);
|
4030 | } else if (isNode) {
|
4031 | loadDependencies(require);
|
4032 | } else {
|
4033 | makeApi(sinon);
|
4034 | }
|
4035 | }());
|
4036 |
|
4037 | |
4038 |
|
4039 |
|
4040 | |
4041 |
|
4042 |
|
4043 |
|
4044 |
|
4045 |
|
4046 |
|
4047 |
|
4048 |
|
4049 | (function(sinon) {
|
4050 |
|
4051 |
|
4052 |
|
4053 | var realSetTimeout = setTimeout;
|
4054 |
|
4055 | function makeApi(sinon) {
|
4056 |
|
4057 | function log() {}
|
4058 |
|
4059 | function logError(label, err) {
|
4060 | var msg = label + " threw exception: ";
|
4061 |
|
4062 | sinon.log(msg + "[" + err.name + "] " + err.message);
|
4063 |
|
4064 | if (err.stack) {
|
4065 | sinon.log(err.stack);
|
4066 | }
|
4067 |
|
4068 | logError.setTimeout(function() {
|
4069 | err.message = msg + err.message;
|
4070 | throw err;
|
4071 | }, 0);
|
4072 | };
|
4073 |
|
4074 |
|
4075 | logError.setTimeout = function(func, timeout) {
|
4076 | realSetTimeout(func, timeout);
|
4077 | }
|
4078 |
|
4079 | var exports = {};
|
4080 | exports.log = sinon.log = log;
|
4081 | exports.logError = sinon.logError = logError;
|
4082 |
|
4083 | return exports;
|
4084 | }
|
4085 |
|
4086 | function loadDependencies(require, exports, module) {
|
4087 | var sinon = require("./util/core");
|
4088 | module.exports = makeApi(sinon);
|
4089 | }
|
4090 |
|
4091 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
4092 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
4093 |
|
4094 | if (isAMD) {
|
4095 | define(loadDependencies);
|
4096 | } else if (isNode) {
|
4097 | loadDependencies(require, module.exports, module);
|
4098 | } else if (!sinon) {
|
4099 | return;
|
4100 | } else {
|
4101 | makeApi(sinon);
|
4102 | }
|
4103 | }(typeof sinon == "object" && sinon || null));
|
4104 |
|
4105 | |
4106 |
|
4107 |
|
4108 |
|
4109 |
|
4110 |
|
4111 | |
4112 |
|
4113 |
|
4114 |
|
4115 |
|
4116 |
|
4117 |
|
4118 |
|
4119 |
|
4120 | (function(global) {
|
4121 |
|
4122 | var supportsProgress = typeof ProgressEvent !== "undefined";
|
4123 | var supportsCustomEvent = typeof CustomEvent !== "undefined";
|
4124 | var sinonXhr = {
|
4125 | XMLHttpRequest: global.XMLHttpRequest
|
4126 | };
|
4127 | sinonXhr.GlobalXMLHttpRequest = global.XMLHttpRequest;
|
4128 | sinonXhr.GlobalActiveXObject = global.ActiveXObject;
|
4129 | sinonXhr.supportsActiveX = typeof sinonXhr.GlobalActiveXObject != "undefined";
|
4130 | sinonXhr.supportsXHR = typeof sinonXhr.GlobalXMLHttpRequest != "undefined";
|
4131 | sinonXhr.workingXHR = sinonXhr.supportsXHR ? sinonXhr.GlobalXMLHttpRequest : sinonXhr.supportsActiveX ? function() {
|
4132 | return new sinonXhr.GlobalActiveXObject("MSXML2.XMLHTTP.3.0")
|
4133 | } : false;
|
4134 | sinonXhr.supportsCORS = sinonXhr.supportsXHR && "withCredentials" in (new sinonXhr.GlobalXMLHttpRequest());
|
4135 |
|
4136 |
|
4137 | var unsafeHeaders = {
|
4138 | "Accept-Charset": true,
|
4139 | "Accept-Encoding": true,
|
4140 | Connection: true,
|
4141 | "Content-Length": true,
|
4142 | Cookie: true,
|
4143 | Cookie2: true,
|
4144 | "Content-Transfer-Encoding": true,
|
4145 | Date: true,
|
4146 | Expect: true,
|
4147 | Host: true,
|
4148 | "Keep-Alive": true,
|
4149 | Referer: true,
|
4150 | TE: true,
|
4151 | Trailer: true,
|
4152 | "Transfer-Encoding": true,
|
4153 | Upgrade: true,
|
4154 | "User-Agent": true,
|
4155 | Via: true
|
4156 | };
|
4157 |
|
4158 |
|
4159 | function FakeXMLHttpRequest() {
|
4160 | this.readyState = FakeXMLHttpRequest.UNSENT;
|
4161 | this.requestHeaders = {};
|
4162 | this.requestBody = null;
|
4163 | this.status = 0;
|
4164 | this.statusText = "";
|
4165 | this.upload = new UploadProgress();
|
4166 | if (sinonXhr.supportsCORS) {
|
4167 | this.withCredentials = false;
|
4168 | }
|
4169 |
|
4170 | var xhr = this;
|
4171 | var events = ["loadstart", "load", "abort", "loadend"];
|
4172 |
|
4173 | function addEventListener(eventName) {
|
4174 | xhr.addEventListener(eventName, function(event) {
|
4175 | var listener = xhr["on" + eventName];
|
4176 |
|
4177 | if (listener && typeof listener == "function") {
|
4178 | listener.call(this, event);
|
4179 | }
|
4180 | });
|
4181 | }
|
4182 |
|
4183 | for (var i = events.length - 1; i >= 0; i--) {
|
4184 | addEventListener(events[i]);
|
4185 | }
|
4186 |
|
4187 | if (typeof FakeXMLHttpRequest.onCreate == "function") {
|
4188 | FakeXMLHttpRequest.onCreate(this);
|
4189 | }
|
4190 | }
|
4191 |
|
4192 |
|
4193 |
|
4194 |
|
4195 |
|
4196 |
|
4197 | function UploadProgress() {
|
4198 | this.eventListeners = {
|
4199 | progress: [],
|
4200 | load: [],
|
4201 | abort: [],
|
4202 | error: []
|
4203 | }
|
4204 | }
|
4205 |
|
4206 | UploadProgress.prototype.addEventListener = function addEventListener(event, listener) {
|
4207 | this.eventListeners[event].push(listener);
|
4208 | };
|
4209 |
|
4210 | UploadProgress.prototype.removeEventListener = function removeEventListener(event, listener) {
|
4211 | var listeners = this.eventListeners[event] || [];
|
4212 |
|
4213 | for (var i = 0, l = listeners.length; i < l; ++i) {
|
4214 | if (listeners[i] == listener) {
|
4215 | return listeners.splice(i, 1);
|
4216 | }
|
4217 | }
|
4218 | };
|
4219 |
|
4220 | UploadProgress.prototype.dispatchEvent = function dispatchEvent(event) {
|
4221 | var listeners = this.eventListeners[event.type] || [];
|
4222 |
|
4223 | for (var i = 0, listener;
|
4224 | (listener = listeners[i]) != null; i++) {
|
4225 | listener(event);
|
4226 | }
|
4227 | };
|
4228 |
|
4229 | function verifyState(xhr) {
|
4230 | if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
|
4231 | throw new Error("INVALID_STATE_ERR");
|
4232 | }
|
4233 |
|
4234 | if (xhr.sendFlag) {
|
4235 | throw new Error("INVALID_STATE_ERR");
|
4236 | }
|
4237 | }
|
4238 |
|
4239 | function getHeader(headers, header) {
|
4240 | header = header.toLowerCase();
|
4241 |
|
4242 | for (var h in headers) {
|
4243 | if (h.toLowerCase() == header) {
|
4244 | return h;
|
4245 | }
|
4246 | }
|
4247 |
|
4248 | return null;
|
4249 | }
|
4250 |
|
4251 |
|
4252 |
|
4253 |
|
4254 | function each(collection, callback) {
|
4255 | if (!collection) {
|
4256 | return;
|
4257 | }
|
4258 |
|
4259 | for (var i = 0, l = collection.length; i < l; i += 1) {
|
4260 | callback(collection[i]);
|
4261 | }
|
4262 | }
|
4263 |
|
4264 | function some(collection, callback) {
|
4265 | for (var index = 0; index < collection.length; index++) {
|
4266 | if (callback(collection[index]) === true) {
|
4267 | return true;
|
4268 | }
|
4269 | }
|
4270 | return false;
|
4271 | }
|
4272 |
|
4273 | var apply = function(obj, method, args) {
|
4274 | switch (args.length) {
|
4275 | case 0:
|
4276 | return obj[method]();
|
4277 | case 1:
|
4278 | return obj[method](args[0]);
|
4279 | case 2:
|
4280 | return obj[method](args[0], args[1]);
|
4281 | case 3:
|
4282 | return obj[method](args[0], args[1], args[2]);
|
4283 | case 4:
|
4284 | return obj[method](args[0], args[1], args[2], args[3]);
|
4285 | case 5:
|
4286 | return obj[method](args[0], args[1], args[2], args[3], args[4]);
|
4287 | }
|
4288 | };
|
4289 |
|
4290 | FakeXMLHttpRequest.filters = [];
|
4291 | FakeXMLHttpRequest.addFilter = function addFilter(fn) {
|
4292 | this.filters.push(fn)
|
4293 | };
|
4294 | var IE6Re = /MSIE 6/;
|
4295 | FakeXMLHttpRequest.defake = function defake(fakeXhr, xhrArgs) {
|
4296 | var xhr = new sinonXhr.workingXHR();
|
4297 | each([
|
4298 | "open",
|
4299 | "setRequestHeader",
|
4300 | "send",
|
4301 | "abort",
|
4302 | "getResponseHeader",
|
4303 | "getAllResponseHeaders",
|
4304 | "addEventListener",
|
4305 | "overrideMimeType",
|
4306 | "removeEventListener"
|
4307 | ], function(method) {
|
4308 | fakeXhr[method] = function() {
|
4309 | return apply(xhr, method, arguments);
|
4310 | };
|
4311 | });
|
4312 |
|
4313 | var copyAttrs = function(args) {
|
4314 | each(args, function(attr) {
|
4315 | try {
|
4316 | fakeXhr[attr] = xhr[attr]
|
4317 | } catch (e) {
|
4318 | if (!IE6Re.test(navigator.userAgent)) {
|
4319 | throw e;
|
4320 | }
|
4321 | }
|
4322 | });
|
4323 | };
|
4324 |
|
4325 | var stateChange = function stateChange() {
|
4326 | fakeXhr.readyState = xhr.readyState;
|
4327 | if (xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) {
|
4328 | copyAttrs(["status", "statusText"]);
|
4329 | }
|
4330 | if (xhr.readyState >= FakeXMLHttpRequest.LOADING) {
|
4331 | copyAttrs(["responseText", "response"]);
|
4332 | }
|
4333 | if (xhr.readyState === FakeXMLHttpRequest.DONE) {
|
4334 | copyAttrs(["responseXML"]);
|
4335 | }
|
4336 | if (fakeXhr.onreadystatechange) {
|
4337 | fakeXhr.onreadystatechange.call(fakeXhr, {
|
4338 | target: fakeXhr
|
4339 | });
|
4340 | }
|
4341 | };
|
4342 |
|
4343 | if (xhr.addEventListener) {
|
4344 | for (var event in fakeXhr.eventListeners) {
|
4345 | if (fakeXhr.eventListeners.hasOwnProperty(event)) {
|
4346 | each(fakeXhr.eventListeners[event], function(handler) {
|
4347 | xhr.addEventListener(event, handler);
|
4348 | });
|
4349 | }
|
4350 | }
|
4351 | xhr.addEventListener("readystatechange", stateChange);
|
4352 | } else {
|
4353 | xhr.onreadystatechange = stateChange;
|
4354 | }
|
4355 | apply(xhr, "open", xhrArgs);
|
4356 | };
|
4357 | FakeXMLHttpRequest.useFilters = false;
|
4358 |
|
4359 | function verifyRequestOpened(xhr) {
|
4360 | if (xhr.readyState != FakeXMLHttpRequest.OPENED) {
|
4361 | throw new Error("INVALID_STATE_ERR - " + xhr.readyState);
|
4362 | }
|
4363 | }
|
4364 |
|
4365 | function verifyRequestSent(xhr) {
|
4366 | if (xhr.readyState == FakeXMLHttpRequest.DONE) {
|
4367 | throw new Error("Request done");
|
4368 | }
|
4369 | }
|
4370 |
|
4371 | function verifyHeadersReceived(xhr) {
|
4372 | if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
|
4373 | throw new Error("No headers received");
|
4374 | }
|
4375 | }
|
4376 |
|
4377 | function verifyResponseBodyType(body) {
|
4378 | if (typeof body != "string") {
|
4379 | var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
|
4380 | body + ", which is not a string.");
|
4381 | error.name = "InvalidBodyException";
|
4382 | throw error;
|
4383 | }
|
4384 | }
|
4385 |
|
4386 | FakeXMLHttpRequest.parseXML = function parseXML(text) {
|
4387 | var xmlDoc;
|
4388 |
|
4389 | if (typeof DOMParser != "undefined") {
|
4390 | var parser = new DOMParser();
|
4391 | xmlDoc = parser.parseFromString(text, "text/xml");
|
4392 | } else {
|
4393 | xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
|
4394 | xmlDoc.async = "false";
|
4395 | xmlDoc.loadXML(text);
|
4396 | }
|
4397 |
|
4398 | return xmlDoc;
|
4399 | };
|
4400 |
|
4401 | FakeXMLHttpRequest.statusCodes = {
|
4402 | 100: "Continue",
|
4403 | 101: "Switching Protocols",
|
4404 | 200: "OK",
|
4405 | 201: "Created",
|
4406 | 202: "Accepted",
|
4407 | 203: "Non-Authoritative Information",
|
4408 | 204: "No Content",
|
4409 | 205: "Reset Content",
|
4410 | 206: "Partial Content",
|
4411 | 300: "Multiple Choice",
|
4412 | 301: "Moved Permanently",
|
4413 | 302: "Found",
|
4414 | 303: "See Other",
|
4415 | 304: "Not Modified",
|
4416 | 305: "Use Proxy",
|
4417 | 307: "Temporary Redirect",
|
4418 | 400: "Bad Request",
|
4419 | 401: "Unauthorized",
|
4420 | 402: "Payment Required",
|
4421 | 403: "Forbidden",
|
4422 | 404: "Not Found",
|
4423 | 405: "Method Not Allowed",
|
4424 | 406: "Not Acceptable",
|
4425 | 407: "Proxy Authentication Required",
|
4426 | 408: "Request Timeout",
|
4427 | 409: "Conflict",
|
4428 | 410: "Gone",
|
4429 | 411: "Length Required",
|
4430 | 412: "Precondition Failed",
|
4431 | 413: "Request Entity Too Large",
|
4432 | 414: "Request-URI Too Long",
|
4433 | 415: "Unsupported Media Type",
|
4434 | 416: "Requested Range Not Satisfiable",
|
4435 | 417: "Expectation Failed",
|
4436 | 422: "Unprocessable Entity",
|
4437 | 500: "Internal Server Error",
|
4438 | 501: "Not Implemented",
|
4439 | 502: "Bad Gateway",
|
4440 | 503: "Service Unavailable",
|
4441 | 504: "Gateway Timeout",
|
4442 | 505: "HTTP Version Not Supported"
|
4443 | };
|
4444 |
|
4445 | function makeApi(sinon) {
|
4446 | sinon.xhr = sinonXhr;
|
4447 |
|
4448 | sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
|
4449 | async: true,
|
4450 |
|
4451 | open: function open(method, url, async, username, password) {
|
4452 | this.method = method;
|
4453 | this.url = url;
|
4454 | this.async = typeof async == "boolean" ? async : true;
|
4455 | this.username = username;
|
4456 | this.password = password;
|
4457 | this.responseText = null;
|
4458 | this.responseXML = null;
|
4459 | this.requestHeaders = {};
|
4460 | this.sendFlag = false;
|
4461 |
|
4462 | if (FakeXMLHttpRequest.useFilters === true) {
|
4463 | var xhrArgs = arguments;
|
4464 | var defake = some(FakeXMLHttpRequest.filters, function(filter) {
|
4465 | return filter.apply(this, xhrArgs)
|
4466 | });
|
4467 | if (defake) {
|
4468 | return FakeXMLHttpRequest.defake(this, arguments);
|
4469 | }
|
4470 | }
|
4471 | this.readyStateChange(FakeXMLHttpRequest.OPENED);
|
4472 | },
|
4473 |
|
4474 | readyStateChange: function readyStateChange(state) {
|
4475 | this.readyState = state;
|
4476 |
|
4477 | if (typeof this.onreadystatechange == "function") {
|
4478 | try {
|
4479 | this.onreadystatechange();
|
4480 | } catch (e) {
|
4481 | sinon.logError("Fake XHR onreadystatechange handler", e);
|
4482 | }
|
4483 | }
|
4484 |
|
4485 | this.dispatchEvent(new sinon.Event("readystatechange"));
|
4486 |
|
4487 | switch (this.readyState) {
|
4488 | case FakeXMLHttpRequest.DONE:
|
4489 | this.dispatchEvent(new sinon.Event("load", false, false, this));
|
4490 | this.dispatchEvent(new sinon.Event("loadend", false, false, this));
|
4491 | this.upload.dispatchEvent(new sinon.Event("load", false, false, this));
|
4492 | if (supportsProgress) {
|
4493 | this.upload.dispatchEvent(new sinon.ProgressEvent("progress", {
|
4494 | loaded: 100,
|
4495 | total: 100
|
4496 | }));
|
4497 | }
|
4498 | break;
|
4499 | }
|
4500 | },
|
4501 |
|
4502 | setRequestHeader: function setRequestHeader(header, value) {
|
4503 | verifyState(this);
|
4504 |
|
4505 | if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
|
4506 | throw new Error("Refused to set unsafe header \"" + header + "\"");
|
4507 | }
|
4508 |
|
4509 | if (this.requestHeaders[header]) {
|
4510 | this.requestHeaders[header] += "," + value;
|
4511 | } else {
|
4512 | this.requestHeaders[header] = value;
|
4513 | }
|
4514 | },
|
4515 |
|
4516 |
|
4517 | setResponseHeaders: function setResponseHeaders(headers) {
|
4518 | verifyRequestOpened(this);
|
4519 | this.responseHeaders = {};
|
4520 |
|
4521 | for (var header in headers) {
|
4522 | if (headers.hasOwnProperty(header)) {
|
4523 | this.responseHeaders[header] = headers[header];
|
4524 | }
|
4525 | }
|
4526 |
|
4527 | if (this.async) {
|
4528 | this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
|
4529 | } else {
|
4530 | this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
|
4531 | }
|
4532 | },
|
4533 |
|
4534 |
|
4535 | send: function send(data) {
|
4536 | verifyState(this);
|
4537 |
|
4538 | if (!/^(get|head)$/i.test(this.method)) {
|
4539 | var contentType = getHeader(this.requestHeaders, "Content-Type");
|
4540 | if (this.requestHeaders[contentType]) {
|
4541 | var value = this.requestHeaders[contentType].split(";");
|
4542 | this.requestHeaders[contentType] = value[0] + ";charset=utf-8";
|
4543 | } else {
|
4544 | this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
|
4545 | }
|
4546 |
|
4547 | this.requestBody = data;
|
4548 | }
|
4549 |
|
4550 | this.errorFlag = false;
|
4551 | this.sendFlag = this.async;
|
4552 | this.readyStateChange(FakeXMLHttpRequest.OPENED);
|
4553 |
|
4554 | if (typeof this.onSend == "function") {
|
4555 | this.onSend(this);
|
4556 | }
|
4557 |
|
4558 | this.dispatchEvent(new sinon.Event("loadstart", false, false, this));
|
4559 | },
|
4560 |
|
4561 | abort: function abort() {
|
4562 | this.aborted = true;
|
4563 | this.responseText = null;
|
4564 | this.errorFlag = true;
|
4565 | this.requestHeaders = {};
|
4566 |
|
4567 | if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) {
|
4568 | this.readyStateChange(FakeXMLHttpRequest.DONE);
|
4569 | this.sendFlag = false;
|
4570 | }
|
4571 |
|
4572 | this.readyState = FakeXMLHttpRequest.UNSENT;
|
4573 |
|
4574 | this.dispatchEvent(new sinon.Event("abort", false, false, this));
|
4575 |
|
4576 | this.upload.dispatchEvent(new sinon.Event("abort", false, false, this));
|
4577 |
|
4578 | if (typeof this.onerror === "function") {
|
4579 | this.onerror();
|
4580 | }
|
4581 | },
|
4582 |
|
4583 | getResponseHeader: function getResponseHeader(header) {
|
4584 | if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
|
4585 | return null;
|
4586 | }
|
4587 |
|
4588 | if (/^Set-Cookie2?$/i.test(header)) {
|
4589 | return null;
|
4590 | }
|
4591 |
|
4592 | header = getHeader(this.responseHeaders, header);
|
4593 |
|
4594 | return this.responseHeaders[header] || null;
|
4595 | },
|
4596 |
|
4597 | getAllResponseHeaders: function getAllResponseHeaders() {
|
4598 | if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
|
4599 | return "";
|
4600 | }
|
4601 |
|
4602 | var headers = "";
|
4603 |
|
4604 | for (var header in this.responseHeaders) {
|
4605 | if (this.responseHeaders.hasOwnProperty(header) && !/^Set-Cookie2?$/i.test(header)) {
|
4606 | headers += header + ": " + this.responseHeaders[header] + "\r\n";
|
4607 | }
|
4608 | }
|
4609 |
|
4610 | return headers;
|
4611 | },
|
4612 |
|
4613 | setResponseBody: function setResponseBody(body) {
|
4614 | verifyRequestSent(this);
|
4615 | verifyHeadersReceived(this);
|
4616 | verifyResponseBodyType(body);
|
4617 |
|
4618 | var chunkSize = this.chunkSize || 10;
|
4619 | var index = 0;
|
4620 | this.responseText = "";
|
4621 |
|
4622 | do {
|
4623 | if (this.async) {
|
4624 | this.readyStateChange(FakeXMLHttpRequest.LOADING);
|
4625 | }
|
4626 |
|
4627 | this.responseText += body.substring(index, index + chunkSize);
|
4628 | index += chunkSize;
|
4629 | } while (index < body.length);
|
4630 |
|
4631 | var type = this.getResponseHeader("Content-Type");
|
4632 |
|
4633 | if (this.responseText &&
|
4634 | (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
|
4635 | try {
|
4636 | this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText);
|
4637 | } catch (e) {
|
4638 |
|
4639 | }
|
4640 | }
|
4641 |
|
4642 | this.readyStateChange(FakeXMLHttpRequest.DONE);
|
4643 | },
|
4644 |
|
4645 | respond: function respond(status, headers, body) {
|
4646 | this.status = typeof status == "number" ? status : 200;
|
4647 | this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
|
4648 | this.setResponseHeaders(headers || {});
|
4649 | this.setResponseBody(body || "");
|
4650 | },
|
4651 |
|
4652 | uploadProgress: function uploadProgress(progressEventRaw) {
|
4653 | if (supportsProgress) {
|
4654 | this.upload.dispatchEvent(new sinon.ProgressEvent("progress", progressEventRaw));
|
4655 | }
|
4656 | },
|
4657 |
|
4658 | uploadError: function uploadError(error) {
|
4659 | if (supportsCustomEvent) {
|
4660 | this.upload.dispatchEvent(new sinon.CustomEvent("error", {
|
4661 | detail: error
|
4662 | }));
|
4663 | }
|
4664 | }
|
4665 | });
|
4666 |
|
4667 | sinon.extend(FakeXMLHttpRequest, {
|
4668 | UNSENT: 0,
|
4669 | OPENED: 1,
|
4670 | HEADERS_RECEIVED: 2,
|
4671 | LOADING: 3,
|
4672 | DONE: 4
|
4673 | });
|
4674 |
|
4675 | sinon.useFakeXMLHttpRequest = function() {
|
4676 | FakeXMLHttpRequest.restore = function restore(keepOnCreate) {
|
4677 | if (sinonXhr.supportsXHR) {
|
4678 | global.XMLHttpRequest = sinonXhr.GlobalXMLHttpRequest;
|
4679 | }
|
4680 |
|
4681 | if (sinonXhr.supportsActiveX) {
|
4682 | global.ActiveXObject = sinonXhr.GlobalActiveXObject;
|
4683 | }
|
4684 |
|
4685 | delete FakeXMLHttpRequest.restore;
|
4686 |
|
4687 | if (keepOnCreate !== true) {
|
4688 | delete FakeXMLHttpRequest.onCreate;
|
4689 | }
|
4690 | };
|
4691 | if (sinonXhr.supportsXHR) {
|
4692 | global.XMLHttpRequest = FakeXMLHttpRequest;
|
4693 | }
|
4694 |
|
4695 | if (sinonXhr.supportsActiveX) {
|
4696 | global.ActiveXObject = function ActiveXObject(objId) {
|
4697 | if (objId == "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) {
|
4698 |
|
4699 | return new FakeXMLHttpRequest();
|
4700 | }
|
4701 |
|
4702 | return new sinonXhr.GlobalActiveXObject(objId);
|
4703 | };
|
4704 | }
|
4705 |
|
4706 | return FakeXMLHttpRequest;
|
4707 | };
|
4708 |
|
4709 | sinon.FakeXMLHttpRequest = FakeXMLHttpRequest;
|
4710 | }
|
4711 |
|
4712 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
4713 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
4714 |
|
4715 | function loadDependencies(require, exports, module) {
|
4716 | var sinon = require("./core");
|
4717 | require("./event");
|
4718 | makeApi(sinon);
|
4719 | module.exports = sinon;
|
4720 | }
|
4721 |
|
4722 | if (isAMD) {
|
4723 | define(loadDependencies);
|
4724 | } else if (isNode) {
|
4725 | loadDependencies(require, module.exports, module);
|
4726 | } else if (typeof sinon === "undefined") {
|
4727 | return;
|
4728 | } else {
|
4729 | makeApi(sinon);
|
4730 | }
|
4731 |
|
4732 | })(typeof self !== "undefined" ? self : this);
|
4733 |
|
4734 | |
4735 |
|
4736 |
|
4737 |
|
4738 |
|
4739 | |
4740 |
|
4741 |
|
4742 |
|
4743 |
|
4744 |
|
4745 |
|
4746 |
|
4747 |
|
4748 |
|
4749 |
|
4750 |
|
4751 | if (typeof sinon == "undefined") {
|
4752 | var sinon = {};
|
4753 | }
|
4754 |
|
4755 | (function() {
|
4756 | var push = [].push;
|
4757 |
|
4758 | function F() {}
|
4759 |
|
4760 | function create(proto) {
|
4761 | F.prototype = proto;
|
4762 | return new F();
|
4763 | }
|
4764 |
|
4765 | function responseArray(handler) {
|
4766 | var response = handler;
|
4767 |
|
4768 | if (Object.prototype.toString.call(handler) != "[object Array]") {
|
4769 | response = [200, {},
|
4770 | handler
|
4771 | ];
|
4772 | }
|
4773 |
|
4774 | if (typeof response[2] != "string") {
|
4775 | throw new TypeError("Fake server response body should be string, but was " +
|
4776 | typeof response[2]);
|
4777 | }
|
4778 |
|
4779 | return response;
|
4780 | }
|
4781 |
|
4782 | var wloc = typeof window !== "undefined" ? window.location : {};
|
4783 | var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
|
4784 |
|
4785 | function matchOne(response, reqMethod, reqUrl) {
|
4786 | var rmeth = response.method;
|
4787 | var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase();
|
4788 | var url = response.url;
|
4789 | var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl));
|
4790 |
|
4791 | return matchMethod && matchUrl;
|
4792 | }
|
4793 |
|
4794 | function match(response, request) {
|
4795 | var requestUrl = request.url;
|
4796 |
|
4797 | if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
|
4798 | requestUrl = requestUrl.replace(rCurrLoc, "");
|
4799 | }
|
4800 |
|
4801 | if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
|
4802 | if (typeof response.response == "function") {
|
4803 | var ru = response.url;
|
4804 | var args = [request].concat(ru && typeof ru.exec == "function" ? ru.exec(requestUrl).slice(1) : []);
|
4805 | return response.response.apply(response, args);
|
4806 | }
|
4807 |
|
4808 | return true;
|
4809 | }
|
4810 |
|
4811 | return false;
|
4812 | }
|
4813 |
|
4814 | function makeApi(sinon) {
|
4815 | sinon.fakeServer = {
|
4816 | create: function() {
|
4817 | var server = create(this);
|
4818 | this.xhr = sinon.useFakeXMLHttpRequest();
|
4819 | server.requests = [];
|
4820 |
|
4821 | this.xhr.onCreate = function(xhrObj) {
|
4822 | server.addRequest(xhrObj);
|
4823 | };
|
4824 |
|
4825 | return server;
|
4826 | },
|
4827 |
|
4828 | addRequest: function addRequest(xhrObj) {
|
4829 | var server = this;
|
4830 | push.call(this.requests, xhrObj);
|
4831 |
|
4832 | xhrObj.onSend = function() {
|
4833 | server.handleRequest(this);
|
4834 |
|
4835 | if (server.autoRespond && !server.responding) {
|
4836 | setTimeout(function() {
|
4837 | server.responding = false;
|
4838 | server.respond();
|
4839 | }, server.autoRespondAfter || 10);
|
4840 |
|
4841 | server.responding = true;
|
4842 | }
|
4843 | };
|
4844 | },
|
4845 |
|
4846 | getHTTPMethod: function getHTTPMethod(request) {
|
4847 | if (this.fakeHTTPMethods && /post/i.test(request.method)) {
|
4848 | var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
|
4849 | return !!matches ? matches[1] : request.method;
|
4850 | }
|
4851 |
|
4852 | return request.method;
|
4853 | },
|
4854 |
|
4855 | handleRequest: function handleRequest(xhr) {
|
4856 | if (xhr.async) {
|
4857 | if (!this.queue) {
|
4858 | this.queue = [];
|
4859 | }
|
4860 |
|
4861 | push.call(this.queue, xhr);
|
4862 | } else {
|
4863 | this.processRequest(xhr);
|
4864 | }
|
4865 | },
|
4866 |
|
4867 | log: function log(response, request) {
|
4868 | var str;
|
4869 |
|
4870 | str = "Request:\n" + sinon.format(request) + "\n\n";
|
4871 | str += "Response:\n" + sinon.format(response) + "\n\n";
|
4872 |
|
4873 | sinon.log(str);
|
4874 | },
|
4875 |
|
4876 | respondWith: function respondWith(method, url, body) {
|
4877 | if (arguments.length == 1 && typeof method != "function") {
|
4878 | this.response = responseArray(method);
|
4879 | return;
|
4880 | }
|
4881 |
|
4882 | if (!this.responses) {
|
4883 | this.responses = [];
|
4884 | }
|
4885 |
|
4886 | if (arguments.length == 1) {
|
4887 | body = method;
|
4888 | url = method = null;
|
4889 | }
|
4890 |
|
4891 | if (arguments.length == 2) {
|
4892 | body = url;
|
4893 | url = method;
|
4894 | method = null;
|
4895 | }
|
4896 |
|
4897 | push.call(this.responses, {
|
4898 | method: method,
|
4899 | url: url,
|
4900 | response: typeof body == "function" ? body : responseArray(body)
|
4901 | });
|
4902 | },
|
4903 |
|
4904 | respond: function respond() {
|
4905 | if (arguments.length > 0) {
|
4906 | this.respondWith.apply(this, arguments);
|
4907 | }
|
4908 |
|
4909 | var queue = this.queue || [];
|
4910 | var requests = queue.splice(0, queue.length);
|
4911 | var request;
|
4912 |
|
4913 | while (request = requests.shift()) {
|
4914 | this.processRequest(request);
|
4915 | }
|
4916 | },
|
4917 |
|
4918 | processRequest: function processRequest(request) {
|
4919 | try {
|
4920 | if (request.aborted) {
|
4921 | return;
|
4922 | }
|
4923 |
|
4924 | var response = this.response || [404, {}, ""];
|
4925 |
|
4926 | if (this.responses) {
|
4927 | for (var l = this.responses.length, i = l - 1; i >= 0; i--) {
|
4928 | if (match.call(this, this.responses[i], request)) {
|
4929 | response = this.responses[i].response;
|
4930 | break;
|
4931 | }
|
4932 | }
|
4933 | }
|
4934 |
|
4935 | if (request.readyState != 4) {
|
4936 | this.log(response, request);
|
4937 |
|
4938 | request.respond(response[0], response[1], response[2]);
|
4939 | }
|
4940 | } catch (e) {
|
4941 | sinon.logError("Fake server request processing", e);
|
4942 | }
|
4943 | },
|
4944 |
|
4945 | restore: function restore() {
|
4946 | return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
|
4947 | }
|
4948 | };
|
4949 | }
|
4950 |
|
4951 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
4952 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
4953 |
|
4954 | function loadDependencies(require, exports, module) {
|
4955 | var sinon = require("./core");
|
4956 | require("./fake_xml_http_request");
|
4957 | makeApi(sinon);
|
4958 | module.exports = sinon;
|
4959 | }
|
4960 |
|
4961 | if (isAMD) {
|
4962 | define(loadDependencies);
|
4963 | } else if (isNode) {
|
4964 | loadDependencies(require, module.exports, module);
|
4965 | } else {
|
4966 | makeApi(sinon);
|
4967 | }
|
4968 | }());
|
4969 |
|
4970 | |
4971 |
|
4972 |
|
4973 |
|
4974 | |
4975 |
|
4976 |
|
4977 |
|
4978 |
|
4979 |
|
4980 |
|
4981 |
|
4982 |
|
4983 |
|
4984 |
|
4985 |
|
4986 |
|
4987 |
|
4988 |
|
4989 | (function() {
|
4990 | function makeApi(sinon) {
|
4991 | function Server() {}
|
4992 | Server.prototype = sinon.fakeServer;
|
4993 |
|
4994 | sinon.fakeServerWithClock = new Server();
|
4995 |
|
4996 | sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
|
4997 | if (xhr.async) {
|
4998 | if (typeof setTimeout.clock == "object") {
|
4999 | this.clock = setTimeout.clock;
|
5000 | } else {
|
5001 | this.clock = sinon.useFakeTimers();
|
5002 | this.resetClock = true;
|
5003 | }
|
5004 |
|
5005 | if (!this.longestTimeout) {
|
5006 | var clockSetTimeout = this.clock.setTimeout;
|
5007 | var clockSetInterval = this.clock.setInterval;
|
5008 | var server = this;
|
5009 |
|
5010 | this.clock.setTimeout = function(fn, timeout) {
|
5011 | server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
|
5012 |
|
5013 | return clockSetTimeout.apply(this, arguments);
|
5014 | };
|
5015 |
|
5016 | this.clock.setInterval = function(fn, timeout) {
|
5017 | server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
|
5018 |
|
5019 | return clockSetInterval.apply(this, arguments);
|
5020 | };
|
5021 | }
|
5022 | }
|
5023 |
|
5024 | return sinon.fakeServer.addRequest.call(this, xhr);
|
5025 | };
|
5026 |
|
5027 | sinon.fakeServerWithClock.respond = function respond() {
|
5028 | var returnVal = sinon.fakeServer.respond.apply(this, arguments);
|
5029 |
|
5030 | if (this.clock) {
|
5031 | this.clock.tick(this.longestTimeout || 0);
|
5032 | this.longestTimeout = 0;
|
5033 |
|
5034 | if (this.resetClock) {
|
5035 | this.clock.restore();
|
5036 | this.resetClock = false;
|
5037 | }
|
5038 | }
|
5039 |
|
5040 | return returnVal;
|
5041 | };
|
5042 |
|
5043 | sinon.fakeServerWithClock.restore = function restore() {
|
5044 | if (this.clock) {
|
5045 | this.clock.restore();
|
5046 | }
|
5047 |
|
5048 | return sinon.fakeServer.restore.apply(this, arguments);
|
5049 | };
|
5050 | }
|
5051 |
|
5052 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
5053 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
5054 |
|
5055 | function loadDependencies(require) {
|
5056 | var sinon = require("./core");
|
5057 | require("./fake_server");
|
5058 | require("./fake_timers");
|
5059 | makeApi(sinon);
|
5060 | }
|
5061 |
|
5062 | if (isAMD) {
|
5063 | define(loadDependencies);
|
5064 | } else if (isNode) {
|
5065 | loadDependencies(require);
|
5066 | } else {
|
5067 | makeApi(sinon);
|
5068 | }
|
5069 | }());
|
5070 |
|
5071 | |
5072 |
|
5073 |
|
5074 |
|
5075 |
|
5076 |
|
5077 |
|
5078 | |
5079 |
|
5080 |
|
5081 |
|
5082 |
|
5083 |
|
5084 |
|
5085 |
|
5086 |
|
5087 |
|
5088 | (function() {
|
5089 | function makeApi(sinon) {
|
5090 | var push = [].push;
|
5091 |
|
5092 | function exposeValue(sandbox, config, key, value) {
|
5093 | if (!value) {
|
5094 | return;
|
5095 | }
|
5096 |
|
5097 | if (config.injectInto && !(key in config.injectInto)) {
|
5098 | config.injectInto[key] = value;
|
5099 | sandbox.injectedKeys.push(key);
|
5100 | } else {
|
5101 | push.call(sandbox.args, value);
|
5102 | }
|
5103 | }
|
5104 |
|
5105 | function prepareSandboxFromConfig(config) {
|
5106 | var sandbox = sinon.create(sinon.sandbox);
|
5107 |
|
5108 | if (config.useFakeServer) {
|
5109 | if (typeof config.useFakeServer == "object") {
|
5110 | sandbox.serverPrototype = config.useFakeServer;
|
5111 | }
|
5112 |
|
5113 | sandbox.useFakeServer();
|
5114 | }
|
5115 |
|
5116 | if (config.useFakeTimers) {
|
5117 | if (typeof config.useFakeTimers == "object") {
|
5118 | sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers);
|
5119 | } else {
|
5120 | sandbox.useFakeTimers();
|
5121 | }
|
5122 | }
|
5123 |
|
5124 | return sandbox;
|
5125 | }
|
5126 |
|
5127 | sinon.sandbox = sinon.extend(sinon.create(sinon.collection), {
|
5128 | useFakeTimers: function useFakeTimers() {
|
5129 | this.clock = sinon.useFakeTimers.apply(sinon, arguments);
|
5130 |
|
5131 | return this.add(this.clock);
|
5132 | },
|
5133 |
|
5134 | serverPrototype: sinon.fakeServer,
|
5135 |
|
5136 | useFakeServer: function useFakeServer() {
|
5137 | var proto = this.serverPrototype || sinon.fakeServer;
|
5138 |
|
5139 | if (!proto || !proto.create) {
|
5140 | return null;
|
5141 | }
|
5142 |
|
5143 | this.server = proto.create();
|
5144 | return this.add(this.server);
|
5145 | },
|
5146 |
|
5147 | inject: function(obj) {
|
5148 | sinon.collection.inject.call(this, obj);
|
5149 |
|
5150 | if (this.clock) {
|
5151 | obj.clock = this.clock;
|
5152 | }
|
5153 |
|
5154 | if (this.server) {
|
5155 | obj.server = this.server;
|
5156 | obj.requests = this.server.requests;
|
5157 | }
|
5158 |
|
5159 | obj.match = sinon.match;
|
5160 |
|
5161 | return obj;
|
5162 | },
|
5163 |
|
5164 | restore: function() {
|
5165 | sinon.collection.restore.apply(this, arguments);
|
5166 | this.restoreContext();
|
5167 | },
|
5168 |
|
5169 | restoreContext: function() {
|
5170 | if (this.injectedKeys) {
|
5171 | for (var i = 0, j = this.injectedKeys.length; i < j; i++) {
|
5172 | delete this.injectInto[this.injectedKeys[i]];
|
5173 | }
|
5174 | this.injectedKeys = [];
|
5175 | }
|
5176 | },
|
5177 |
|
5178 | create: function(config) {
|
5179 | if (!config) {
|
5180 | return sinon.create(sinon.sandbox);
|
5181 | }
|
5182 |
|
5183 | var sandbox = prepareSandboxFromConfig(config);
|
5184 | sandbox.args = sandbox.args || [];
|
5185 | sandbox.injectedKeys = [];
|
5186 | sandbox.injectInto = config.injectInto;
|
5187 | var prop, value, exposed = sandbox.inject({});
|
5188 |
|
5189 | if (config.properties) {
|
5190 | for (var i = 0, l = config.properties.length; i < l; i++) {
|
5191 | prop = config.properties[i];
|
5192 | value = exposed[prop] || prop == "sandbox" && sandbox;
|
5193 | exposeValue(sandbox, config, prop, value);
|
5194 | }
|
5195 | } else {
|
5196 | exposeValue(sandbox, config, "sandbox", value);
|
5197 | }
|
5198 |
|
5199 | return sandbox;
|
5200 | },
|
5201 |
|
5202 | match: sinon.match
|
5203 | });
|
5204 |
|
5205 | sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;
|
5206 |
|
5207 | return sinon.sandbox;
|
5208 | }
|
5209 |
|
5210 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
5211 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
5212 |
|
5213 | function loadDependencies(require, exports, module) {
|
5214 | var sinon = require("./util/core");
|
5215 | require("./util/fake_server");
|
5216 | require("./util/fake_timers");
|
5217 | require("./collection");
|
5218 | module.exports = makeApi(sinon);
|
5219 | }
|
5220 |
|
5221 | if (isAMD) {
|
5222 | define(loadDependencies);
|
5223 | } else if (isNode) {
|
5224 | loadDependencies(require, module.exports, module);
|
5225 | } else if (!sinon) {
|
5226 | return;
|
5227 | } else {
|
5228 | makeApi(sinon);
|
5229 | }
|
5230 | }());
|
5231 |
|
5232 | |
5233 |
|
5234 |
|
5235 |
|
5236 |
|
5237 |
|
5238 | |
5239 |
|
5240 |
|
5241 |
|
5242 |
|
5243 |
|
5244 |
|
5245 |
|
5246 |
|
5247 | (function(sinon) {
|
5248 | function makeApi(sinon) {
|
5249 | function test(callback) {
|
5250 | var type = typeof callback;
|
5251 |
|
5252 | if (type != "function") {
|
5253 | throw new TypeError("sinon.test needs to wrap a test function, got " + type);
|
5254 | }
|
5255 |
|
5256 | function sinonSandboxedTest() {
|
5257 | var config = sinon.getConfig(sinon.config);
|
5258 | config.injectInto = config.injectIntoThis && this || config.injectInto;
|
5259 | var sandbox = sinon.sandbox.create(config);
|
5260 | var exception, result;
|
5261 | var doneIsWrapped = false;
|
5262 | var argumentsCopy = Array.prototype.slice.call(arguments);
|
5263 | if (argumentsCopy.length > 0 && typeof argumentsCopy[arguments.length - 1] == "function") {
|
5264 | var oldDone = argumentsCopy[arguments.length - 1];
|
5265 | argumentsCopy[arguments.length - 1] = function done(result) {
|
5266 | if (result) {
|
5267 | sandbox.restore();
|
5268 | throw exception;
|
5269 | } else {
|
5270 | sandbox.verifyAndRestore();
|
5271 | }
|
5272 | oldDone(result);
|
5273 | }
|
5274 | doneIsWrapped = true;
|
5275 | }
|
5276 |
|
5277 | var args = argumentsCopy.concat(sandbox.args);
|
5278 |
|
5279 | try {
|
5280 | result = callback.apply(this, args);
|
5281 | } catch (e) {
|
5282 | exception = e;
|
5283 | }
|
5284 |
|
5285 | if (!doneIsWrapped) {
|
5286 | if (typeof exception !== "undefined") {
|
5287 | sandbox.restore();
|
5288 | throw exception;
|
5289 | } else {
|
5290 | sandbox.verifyAndRestore();
|
5291 | }
|
5292 | }
|
5293 |
|
5294 | return result;
|
5295 | };
|
5296 |
|
5297 | if (callback.length) {
|
5298 | return function sinonAsyncSandboxedTest(callback) {
|
5299 | return sinonSandboxedTest.apply(this, arguments);
|
5300 | };
|
5301 | }
|
5302 |
|
5303 | return sinonSandboxedTest;
|
5304 | }
|
5305 |
|
5306 | test.config = {
|
5307 | injectIntoThis: true,
|
5308 | injectInto: null,
|
5309 | properties: ["spy", "stub", "mock", "clock", "server", "requests"],
|
5310 | useFakeTimers: true,
|
5311 | useFakeServer: true
|
5312 | };
|
5313 |
|
5314 | sinon.test = test;
|
5315 | return test;
|
5316 | }
|
5317 |
|
5318 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
5319 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
5320 |
|
5321 | function loadDependencies(require, exports, module) {
|
5322 | var sinon = require("./util/core");
|
5323 | require("./sandbox");
|
5324 | module.exports = makeApi(sinon);
|
5325 | }
|
5326 |
|
5327 | if (isAMD) {
|
5328 | define(loadDependencies);
|
5329 | } else if (isNode) {
|
5330 | loadDependencies(require, module.exports, module);
|
5331 | } else if (!sinon) {
|
5332 | return;
|
5333 | } else {
|
5334 | makeApi(sinon);
|
5335 | }
|
5336 | }(typeof sinon == "object" && sinon || null));
|
5337 |
|
5338 | |
5339 |
|
5340 |
|
5341 |
|
5342 | |
5343 |
|
5344 |
|
5345 |
|
5346 |
|
5347 |
|
5348 |
|
5349 |
|
5350 |
|
5351 | (function(sinon) {
|
5352 | function createTest(property, setUp, tearDown) {
|
5353 | return function() {
|
5354 | if (setUp) {
|
5355 | setUp.apply(this, arguments);
|
5356 | }
|
5357 |
|
5358 | var exception, result;
|
5359 |
|
5360 | try {
|
5361 | result = property.apply(this, arguments);
|
5362 | } catch (e) {
|
5363 | exception = e;
|
5364 | }
|
5365 |
|
5366 | if (tearDown) {
|
5367 | tearDown.apply(this, arguments);
|
5368 | }
|
5369 |
|
5370 | if (exception) {
|
5371 | throw exception;
|
5372 | }
|
5373 |
|
5374 | return result;
|
5375 | };
|
5376 | }
|
5377 |
|
5378 | function makeApi(sinon) {
|
5379 | function testCase(tests, prefix) {
|
5380 |
|
5381 | if (!tests || typeof tests != "object") {
|
5382 | throw new TypeError("sinon.testCase needs an object with test functions");
|
5383 | }
|
5384 |
|
5385 |
|
5386 | prefix = prefix || "test";
|
5387 | var rPrefix = new RegExp("^" + prefix);
|
5388 | var methods = {}, testName, property, method;
|
5389 | var setUp = tests.setUp;
|
5390 | var tearDown = tests.tearDown;
|
5391 |
|
5392 | for (testName in tests) {
|
5393 | if (tests.hasOwnProperty(testName)) {
|
5394 | property = tests[testName];
|
5395 |
|
5396 | if (/^(setUp|tearDown)$/.test(testName)) {
|
5397 | continue;
|
5398 | }
|
5399 |
|
5400 | if (typeof property == "function" && rPrefix.test(testName)) {
|
5401 | method = property;
|
5402 |
|
5403 | if (setUp || tearDown) {
|
5404 | method = createTest(property, setUp, tearDown);
|
5405 | }
|
5406 |
|
5407 | methods[testName] = sinon.test(method);
|
5408 | } else {
|
5409 | methods[testName] = tests[testName];
|
5410 | }
|
5411 | }
|
5412 | }
|
5413 |
|
5414 | return methods;
|
5415 | }
|
5416 |
|
5417 | sinon.testCase = testCase;
|
5418 | return testCase;
|
5419 | }
|
5420 |
|
5421 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
5422 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
5423 |
|
5424 | function loadDependencies(require, exports, module) {
|
5425 | var sinon = require("./util/core");
|
5426 | require("./test");
|
5427 | module.exports = makeApi(sinon);
|
5428 | }
|
5429 |
|
5430 | if (isAMD) {
|
5431 | define(loadDependencies);
|
5432 | } else if (isNode) {
|
5433 | loadDependencies(require, module.exports, module);
|
5434 | } else if (!sinon) {
|
5435 | return;
|
5436 | } else {
|
5437 | makeApi(sinon);
|
5438 | }
|
5439 | }(typeof sinon == "object" && sinon || null));
|
5440 |
|
5441 | |
5442 |
|
5443 |
|
5444 |
|
5445 |
|
5446 |
|
5447 | |
5448 |
|
5449 |
|
5450 |
|
5451 |
|
5452 |
|
5453 |
|
5454 |
|
5455 |
|
5456 | (function(sinon, global) {
|
5457 | var slice = Array.prototype.slice;
|
5458 |
|
5459 | function makeApi(sinon) {
|
5460 | var assert;
|
5461 |
|
5462 | function verifyIsStub() {
|
5463 | var method;
|
5464 |
|
5465 | for (var i = 0, l = arguments.length; i < l; ++i) {
|
5466 | method = arguments[i];
|
5467 |
|
5468 | if (!method) {
|
5469 | assert.fail("fake is not a spy");
|
5470 | }
|
5471 |
|
5472 | if (typeof method != "function") {
|
5473 | assert.fail(method + " is not a function");
|
5474 | }
|
5475 |
|
5476 | if (typeof method.getCall != "function") {
|
5477 | assert.fail(method + " is not stubbed");
|
5478 | }
|
5479 | }
|
5480 | }
|
5481 |
|
5482 | function failAssertion(object, msg) {
|
5483 | object = object || global;
|
5484 | var failMethod = object.fail || assert.fail;
|
5485 | failMethod.call(object, msg);
|
5486 | }
|
5487 |
|
5488 | function mirrorPropAsAssertion(name, method, message) {
|
5489 | if (arguments.length == 2) {
|
5490 | message = method;
|
5491 | method = name;
|
5492 | }
|
5493 |
|
5494 | assert[name] = function(fake) {
|
5495 | verifyIsStub(fake);
|
5496 |
|
5497 | var args = slice.call(arguments, 1);
|
5498 | var failed = false;
|
5499 |
|
5500 | if (typeof method == "function") {
|
5501 | failed = !method(fake);
|
5502 | } else {
|
5503 | failed = typeof fake[method] == "function" ? !fake[method].apply(fake, args) : !fake[method];
|
5504 | }
|
5505 |
|
5506 | if (failed) {
|
5507 | failAssertion(this, fake.printf.apply(fake, [message].concat(args)));
|
5508 | } else {
|
5509 | assert.pass(name);
|
5510 | }
|
5511 | };
|
5512 | }
|
5513 |
|
5514 | function exposedName(prefix, prop) {
|
5515 | return !prefix || /^fail/.test(prop) ? prop :
|
5516 | prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
|
5517 | }
|
5518 |
|
5519 | assert = {
|
5520 | failException: "AssertError",
|
5521 |
|
5522 | fail: function fail(message) {
|
5523 | var error = new Error(message);
|
5524 | error.name = this.failException || assert.failException;
|
5525 |
|
5526 | throw error;
|
5527 | },
|
5528 |
|
5529 | pass: function pass(assertion) {},
|
5530 |
|
5531 | callOrder: function assertCallOrder() {
|
5532 | verifyIsStub.apply(null, arguments);
|
5533 | var expected = "",
|
5534 | actual = "";
|
5535 |
|
5536 | if (!sinon.calledInOrder(arguments)) {
|
5537 | try {
|
5538 | expected = [].join.call(arguments, ", ");
|
5539 | var calls = slice.call(arguments);
|
5540 | var i = calls.length;
|
5541 | while (i) {
|
5542 | if (!calls[--i].called) {
|
5543 | calls.splice(i, 1);
|
5544 | }
|
5545 | }
|
5546 | actual = sinon.orderByFirstCall(calls).join(", ");
|
5547 | } catch (e) {
|
5548 |
|
5549 | }
|
5550 |
|
5551 | failAssertion(this, "expected " + expected + " to be " +
|
5552 | "called in order but were called as " + actual);
|
5553 | } else {
|
5554 | assert.pass("callOrder");
|
5555 | }
|
5556 | },
|
5557 |
|
5558 | callCount: function assertCallCount(method, count) {
|
5559 | verifyIsStub(method);
|
5560 |
|
5561 | if (method.callCount != count) {
|
5562 | var msg = "expected %n to be called " + sinon.timesInWords(count) +
|
5563 | " but was called %c%C";
|
5564 | failAssertion(this, method.printf(msg));
|
5565 | } else {
|
5566 | assert.pass("callCount");
|
5567 | }
|
5568 | },
|
5569 |
|
5570 | expose: function expose(target, options) {
|
5571 | if (!target) {
|
5572 | throw new TypeError("target is null or undefined");
|
5573 | }
|
5574 |
|
5575 | var o = options || {};
|
5576 | var prefix = typeof o.prefix == "undefined" && "assert" || o.prefix;
|
5577 | var includeFail = typeof o.includeFail == "undefined" || !! o.includeFail;
|
5578 |
|
5579 | for (var method in this) {
|
5580 | if (method != "expose" && (includeFail || !/^(fail)/.test(method))) {
|
5581 | target[exposedName(prefix, method)] = this[method];
|
5582 | }
|
5583 | }
|
5584 |
|
5585 | return target;
|
5586 | },
|
5587 |
|
5588 | match: function match(actual, expectation) {
|
5589 | var matcher = sinon.match(expectation);
|
5590 | if (matcher.test(actual)) {
|
5591 | assert.pass("match");
|
5592 | } else {
|
5593 | var formatted = [
|
5594 | "expected value to match",
|
5595 | " expected = " + sinon.format(expectation),
|
5596 | " actual = " + sinon.format(actual)
|
5597 | ]
|
5598 | failAssertion(this, formatted.join("\n"));
|
5599 | }
|
5600 | }
|
5601 | };
|
5602 |
|
5603 | mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
|
5604 | mirrorPropAsAssertion("notCalled", function(spy) {
|
5605 | return !spy.called;
|
5606 | },
|
5607 | "expected %n to not have been called but was called %c%C");
|
5608 | mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
|
5609 | mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
|
5610 | mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
|
5611 | mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
|
5612 | mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
|
5613 | mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
|
5614 | mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
|
5615 | mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
|
5616 | mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
|
5617 | mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
|
5618 | mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
|
5619 | mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
|
5620 | mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
|
5621 | mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
|
5622 | mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
|
5623 | mirrorPropAsAssertion("threw", "%n did not throw exception%C");
|
5624 | mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
|
5625 |
|
5626 | sinon.assert = assert;
|
5627 | return assert;
|
5628 | }
|
5629 |
|
5630 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
5631 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
5632 |
|
5633 | function loadDependencies(require, exports, module) {
|
5634 | var sinon = require("./util/core");
|
5635 | require("./match");
|
5636 | module.exports = makeApi(sinon);
|
5637 | }
|
5638 |
|
5639 | if (isAMD) {
|
5640 | define(loadDependencies);
|
5641 | } else if (isNode) {
|
5642 | loadDependencies(require, module.exports, module);
|
5643 | } else if (!sinon) {
|
5644 | return;
|
5645 | } else {
|
5646 | makeApi(sinon);
|
5647 | }
|
5648 |
|
5649 | }(typeof sinon == "object" && sinon || null, typeof window != "undefined" ? window : (typeof self != "undefined") ? self : global));
|
5650 |
|
5651 | |
5652 |
|
5653 |
|
5654 |
|
5655 |
|
5656 |
|
5657 | |
5658 |
|
5659 |
|
5660 |
|
5661 | if (typeof sinon == "undefined") {
|
5662 | this.sinon = {};
|
5663 | }
|
5664 |
|
5665 |
|
5666 | (function(global) {
|
5667 | var xdr = {
|
5668 | XDomainRequest: global.XDomainRequest
|
5669 | };
|
5670 | xdr.GlobalXDomainRequest = global.XDomainRequest;
|
5671 | xdr.supportsXDR = typeof xdr.GlobalXDomainRequest != "undefined";
|
5672 | xdr.workingXDR = xdr.supportsXDR ? xdr.GlobalXDomainRequest : false;
|
5673 |
|
5674 | function makeApi(sinon) {
|
5675 | sinon.xdr = xdr;
|
5676 |
|
5677 | function FakeXDomainRequest() {
|
5678 | this.readyState = FakeXDomainRequest.UNSENT;
|
5679 | this.requestBody = null;
|
5680 | this.requestHeaders = {};
|
5681 | this.status = 0;
|
5682 | this.timeout = null;
|
5683 |
|
5684 | if (typeof FakeXDomainRequest.onCreate == "function") {
|
5685 | FakeXDomainRequest.onCreate(this);
|
5686 | }
|
5687 | }
|
5688 |
|
5689 | function verifyState(xdr) {
|
5690 | if (xdr.readyState !== FakeXDomainRequest.OPENED) {
|
5691 | throw new Error("INVALID_STATE_ERR");
|
5692 | }
|
5693 |
|
5694 | if (xdr.sendFlag) {
|
5695 | throw new Error("INVALID_STATE_ERR");
|
5696 | }
|
5697 | }
|
5698 |
|
5699 | function verifyRequestSent(xdr) {
|
5700 | if (xdr.readyState == FakeXDomainRequest.UNSENT) {
|
5701 | throw new Error("Request not sent");
|
5702 | }
|
5703 | if (xdr.readyState == FakeXDomainRequest.DONE) {
|
5704 | throw new Error("Request done");
|
5705 | }
|
5706 | }
|
5707 |
|
5708 | function verifyResponseBodyType(body) {
|
5709 | if (typeof body != "string") {
|
5710 | var error = new Error("Attempted to respond to fake XDomainRequest with " +
|
5711 | body + ", which is not a string.");
|
5712 | error.name = "InvalidBodyException";
|
5713 | throw error;
|
5714 | }
|
5715 | }
|
5716 |
|
5717 | sinon.extend(FakeXDomainRequest.prototype, sinon.EventTarget, {
|
5718 | open: function open(method, url) {
|
5719 | this.method = method;
|
5720 | this.url = url;
|
5721 |
|
5722 | this.responseText = null;
|
5723 | this.sendFlag = false;
|
5724 |
|
5725 | this.readyStateChange(FakeXDomainRequest.OPENED);
|
5726 | },
|
5727 |
|
5728 | readyStateChange: function readyStateChange(state) {
|
5729 | this.readyState = state;
|
5730 | var eventName = "";
|
5731 | switch (this.readyState) {
|
5732 | case FakeXDomainRequest.UNSENT:
|
5733 | break;
|
5734 | case FakeXDomainRequest.OPENED:
|
5735 | break;
|
5736 | case FakeXDomainRequest.LOADING:
|
5737 | if (this.sendFlag) {
|
5738 |
|
5739 | eventName = "onprogress";
|
5740 | }
|
5741 | break;
|
5742 | case FakeXDomainRequest.DONE:
|
5743 | if (this.isTimeout) {
|
5744 | eventName = "ontimeout"
|
5745 | } else if (this.errorFlag || (this.status < 200 || this.status > 299)) {
|
5746 | eventName = "onerror";
|
5747 | } else {
|
5748 | eventName = "onload"
|
5749 | }
|
5750 | break;
|
5751 | }
|
5752 |
|
5753 |
|
5754 | if (eventName) {
|
5755 | if (typeof this[eventName] == "function") {
|
5756 | try {
|
5757 | this[eventName]();
|
5758 | } catch (e) {
|
5759 | sinon.logError("Fake XHR " + eventName + " handler", e);
|
5760 | }
|
5761 | }
|
5762 | }
|
5763 | },
|
5764 |
|
5765 | send: function send(data) {
|
5766 | verifyState(this);
|
5767 |
|
5768 | if (!/^(get|head)$/i.test(this.method)) {
|
5769 | this.requestBody = data;
|
5770 | }
|
5771 | this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
|
5772 |
|
5773 | this.errorFlag = false;
|
5774 | this.sendFlag = true;
|
5775 | this.readyStateChange(FakeXDomainRequest.OPENED);
|
5776 |
|
5777 | if (typeof this.onSend == "function") {
|
5778 | this.onSend(this);
|
5779 | }
|
5780 | },
|
5781 |
|
5782 | abort: function abort() {
|
5783 | this.aborted = true;
|
5784 | this.responseText = null;
|
5785 | this.errorFlag = true;
|
5786 |
|
5787 | if (this.readyState > sinon.FakeXDomainRequest.UNSENT && this.sendFlag) {
|
5788 | this.readyStateChange(sinon.FakeXDomainRequest.DONE);
|
5789 | this.sendFlag = false;
|
5790 | }
|
5791 | },
|
5792 |
|
5793 | setResponseBody: function setResponseBody(body) {
|
5794 | verifyRequestSent(this);
|
5795 | verifyResponseBodyType(body);
|
5796 |
|
5797 | var chunkSize = this.chunkSize || 10;
|
5798 | var index = 0;
|
5799 | this.responseText = "";
|
5800 |
|
5801 | do {
|
5802 | this.readyStateChange(FakeXDomainRequest.LOADING);
|
5803 | this.responseText += body.substring(index, index + chunkSize);
|
5804 | index += chunkSize;
|
5805 | } while (index < body.length);
|
5806 |
|
5807 | this.readyStateChange(FakeXDomainRequest.DONE);
|
5808 | },
|
5809 |
|
5810 | respond: function respond(status, contentType, body) {
|
5811 |
|
5812 |
|
5813 |
|
5814 | this.status = typeof status == "number" ? status : 200;
|
5815 | this.setResponseBody(body || "");
|
5816 | },
|
5817 |
|
5818 | simulatetimeout: function simulatetimeout() {
|
5819 | this.status = 0;
|
5820 | this.isTimeout = true;
|
5821 |
|
5822 | this.responseText = undefined;
|
5823 | this.readyStateChange(FakeXDomainRequest.DONE);
|
5824 | }
|
5825 | });
|
5826 |
|
5827 | sinon.extend(FakeXDomainRequest, {
|
5828 | UNSENT: 0,
|
5829 | OPENED: 1,
|
5830 | LOADING: 3,
|
5831 | DONE: 4
|
5832 | });
|
5833 |
|
5834 | sinon.useFakeXDomainRequest = function useFakeXDomainRequest() {
|
5835 | sinon.FakeXDomainRequest.restore = function restore(keepOnCreate) {
|
5836 | if (xdr.supportsXDR) {
|
5837 | global.XDomainRequest = xdr.GlobalXDomainRequest;
|
5838 | }
|
5839 |
|
5840 | delete sinon.FakeXDomainRequest.restore;
|
5841 |
|
5842 | if (keepOnCreate !== true) {
|
5843 | delete sinon.FakeXDomainRequest.onCreate;
|
5844 | }
|
5845 | };
|
5846 | if (xdr.supportsXDR) {
|
5847 | global.XDomainRequest = sinon.FakeXDomainRequest;
|
5848 | }
|
5849 | return sinon.FakeXDomainRequest;
|
5850 | };
|
5851 |
|
5852 | sinon.FakeXDomainRequest = FakeXDomainRequest;
|
5853 | }
|
5854 |
|
5855 | var isNode = typeof module !== "undefined" && module.exports && typeof require == "function";
|
5856 | var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
|
5857 |
|
5858 | function loadDependencies(require, exports, module) {
|
5859 | var sinon = require("./core");
|
5860 | require("./event");
|
5861 | makeApi(sinon);
|
5862 | module.exports = sinon;
|
5863 | }
|
5864 |
|
5865 | if (isAMD) {
|
5866 | define(loadDependencies);
|
5867 | } else if (isNode) {
|
5868 | loadDependencies(require, module.exports, module);
|
5869 | } else {
|
5870 | makeApi(sinon);
|
5871 | }
|
5872 | })(this);
|
5873 |
|
5874 | return sinon;
|
5875 | }));
|