-
-
Notifications
You must be signed in to change notification settings - Fork 769
/
stub.js
161 lines (129 loc) · 4.56 KB
/
stub.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* @depend ../sinon.js
* @depend spy.js
* @depend behavior.js
*/
/*jslint eqeqeq: false, onevar: false*/
/*global module, require, sinon*/
/**
* Stub functions
*
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2010-2013 Christian Johansen
*/
"use strict";
(function (sinon) {
var commonJSModule = typeof module !== "undefined" && module.exports && typeof require == "function";
if (!sinon && commonJSModule) {
sinon = require("../sinon");
}
if (!sinon) {
return;
}
function stub(object, property, func) {
if (!!func && typeof func != "function") {
throw new TypeError("Custom stub should be function");
}
var wrapper;
if (func) {
wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func;
} else {
wrapper = stub.create();
}
if (!object && typeof property === "undefined") {
return sinon.stub.create();
}
if (typeof property === "undefined" && typeof object == "object") {
for (var prop in object) {
if (typeof object[prop] === "function") {
stub(object, prop);
}
}
return object;
}
return sinon.wrapMethod(object, property, wrapper);
}
function getDefaultBehavior(stub) {
return stub.defaultBehavior || getParentBehaviour(stub) || sinon.behavior.create(stub);
}
function getParentBehaviour(stub) {
return (stub.parent && getCurrentBehavior(stub.parent));
}
function getCurrentBehavior(stub) {
var behavior = stub.behaviors[stub.callCount - 1];
return behavior && behavior.isPresent() ? behavior : getDefaultBehavior(stub);
}
var uuid = 0;
sinon.extend(stub, (function () {
var proto = {
create: function create() {
var functionStub = function () {
return getCurrentBehavior(functionStub).invoke(this, arguments);
};
functionStub.id = "stub#" + uuid++;
var orig = functionStub;
functionStub = sinon.spy.create(functionStub);
functionStub.func = orig;
sinon.extend(functionStub, stub);
functionStub._create = sinon.stub.create;
functionStub.displayName = "stub";
functionStub.toString = sinon.functionToString;
functionStub.defaultBehavior = null;
functionStub.behaviors = [];
return functionStub;
},
resetBehavior: function () {
var i;
this.defaultBehavior = null;
this.behaviors = [];
delete this.returnValue;
delete this.returnArgAt;
this.returnThis = false;
if (this.fakes) {
for (i = 0; i < this.fakes.length; i++) {
this.fakes[i].resetBehavior();
}
}
},
onCall: function(index) {
if (!this.behaviors[index]) {
this.behaviors[index] = sinon.behavior.create(this);
}
return this.behaviors[index];
},
onFirstCall: function() {
return this.onCall(0);
},
onSecondCall: function() {
return this.onCall(1);
},
onThirdCall: function() {
return this.onCall(2);
}
};
for (var method in sinon.behavior) {
if (sinon.behavior.hasOwnProperty(method) &&
!proto.hasOwnProperty(method) &&
method != 'create' &&
method != 'withArgs' &&
method != 'invoke') {
proto[method] = (function(behaviorMethod) {
return function() {
this.defaultBehavior = this.defaultBehavior || sinon.behavior.create(this);
this.defaultBehavior[behaviorMethod].apply(this.defaultBehavior, arguments);
return this;
};
}(method));
}
}
return proto;
}()));
sinon.stub = stub;
if (typeof define === "function" && define.amd) {
define(["module"], function(module) { module.exports = stub; });
} else if (commonJSModule) {
module.exports = stub;
}
}(typeof sinon == "object" && sinon || null));