-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
spy.js
192 lines (160 loc) · 5.89 KB
/
spy.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
var arrayProto = require("@sinonjs/commons").prototypes.array;
var createProxy = require("./proxy");
var extend = require("./util/core/extend");
var functionName = require("@sinonjs/commons").functionName;
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
var deepEqual = require("@sinonjs/samsam").deepEqual;
var isEsModule = require("./util/core/is-es-module");
var proxyCallUtil = require("./proxy-call-util");
var walkObject = require("./util/core/walk-object");
var wrapMethod = require("./util/core/wrap-method");
var sinonFormat = require("./util/core/format");
var valueToString = require("@sinonjs/commons").valueToString;
/* cache references to library methods so that they also can be stubbed without problems */
var forEach = arrayProto.forEach;
var pop = arrayProto.pop;
var push = arrayProto.push;
var slice = arrayProto.slice;
var filter = Array.prototype.filter;
var uuid = 0;
function matches(fake, args, strict) {
var margs = fake.matchingArguments;
if (margs.length <= args.length && deepEqual(slice(args, 0, margs.length), margs)) {
return !strict || margs.length === args.length;
}
return false;
}
// Public API
var spyApi = {
formatters: require("./spy-formatters"),
withArgs: function() {
var args = slice(arguments);
var matching = pop(this.matchingFakes(args, true));
if (matching) {
return matching;
}
var original = this;
var fake = this.instantiateFake();
fake.matchingArguments = args;
fake.parent = this;
push(this.fakes, fake);
fake.withArgs = function() {
return original.withArgs.apply(original, arguments);
};
forEach(original.args, function(arg, i) {
if (!matches(fake, arg)) {
return;
}
proxyCallUtil.incrementCallCount(fake);
push(fake.thisValues, original.thisValues[i]);
push(fake.args, arg);
push(fake.returnValues, original.returnValues[i]);
push(fake.exceptions, original.exceptions[i]);
push(fake.callIds, original.callIds[i]);
});
proxyCallUtil.createCallProperties(fake);
return fake;
},
// Override proxy default implementation
matchingFakes: function(args, strict) {
return filter.call(this.fakes, function(fake) {
return matches(fake, args, strict);
});
},
printf: function(format) {
var spyInstance = this;
var args = slice(arguments, 1);
var formatter;
return (format || "").replace(/%(.)/g, function(match, specifyer) {
formatter = spyApi.formatters[specifyer];
if (typeof formatter === "function") {
return String(formatter(spyInstance, args));
} else if (!isNaN(parseInt(specifyer, 10))) {
return sinonFormat(args[specifyer - 1]);
}
return "%" + specifyer;
});
}
};
/* eslint-disable local-rules/no-prototype-methods */
var delegateToCalls = proxyCallUtil.delegateToCalls;
delegateToCalls(spyApi, "callArg", false, "callArgWith", true, function() {
throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
});
spyApi.callArgWith = spyApi.callArg;
delegateToCalls(spyApi, "callArgOn", false, "callArgOnWith", true, function() {
throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
});
spyApi.callArgOnWith = spyApi.callArgOn;
delegateToCalls(spyApi, "throwArg", false, "throwArg", false, function() {
throw new Error(this.toString() + " cannot throw arg since it was not yet invoked.");
});
delegateToCalls(spyApi, "yield", false, "yield", true, function() {
throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
});
// "invokeCallback" is an alias for "yield" since "yield" is invalid in strict mode.
spyApi.invokeCallback = spyApi.yield;
delegateToCalls(spyApi, "yieldOn", false, "yieldOn", true, function() {
throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
});
delegateToCalls(spyApi, "yieldTo", false, "yieldTo", true, function(property) {
throw new Error(
this.toString() + " cannot yield to '" + valueToString(property) + "' since it was not yet invoked."
);
});
delegateToCalls(spyApi, "yieldToOn", false, "yieldToOn", true, function(property) {
throw new Error(
this.toString() + " cannot yield to '" + valueToString(property) + "' since it was not yet invoked."
);
});
/* eslint-enable local-rules/no-prototype-methods */
function createSpy(func) {
var name;
var funk = func;
if (typeof funk !== "function") {
funk = function() {
return;
};
} else {
name = functionName(funk);
}
var proxy = createProxy(funk, funk);
// Inherit spy API:
extend.nonEnum(proxy, spyApi);
extend.nonEnum(proxy, {
displayName: name || "spy",
fakes: [],
instantiateFake: createSpy,
id: "spy#" + uuid++
});
return proxy;
}
function spy(object, property, types) {
var descriptor, methodDesc;
if (isEsModule(object)) {
throw new TypeError("ES Modules cannot be spied");
}
if (!property && typeof object === "function") {
return createSpy(object);
}
if (!property && typeof object === "object") {
return walkObject(spy, object);
}
if (!object && !property) {
return createSpy(function() {
return;
});
}
if (!types) {
return wrapMethod(object, property, createSpy(object[property]));
}
descriptor = {};
methodDesc = getPropertyDescriptor(object, property);
forEach(types, function(type) {
descriptor[type] = createSpy(methodDesc[type]);
});
return wrapMethod(object, property, descriptor);
}
extend(spy, spyApi);
module.exports = spy;