-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMightyMock.cfc
415 lines (330 loc) · 11.1 KB
/
MightyMock.cfc
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<cfcomponent output="true">
<cfscript>
/*------------------------------------------------------------------------------
Public API. Any method NOT prefixed with _$ are considered public.
All other methods are readily available, but will likely produce
unexpected behavior if not used correctly.
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
Creational Methods - init() ... create()
------------------------------------------------------------------------------*/
function init(){
var proxyVars = '';
/*
Make "fast mock" and bypass scope acrobatics.
*/
if( arguments.size() eq 0 ) {
mocked.name = 'Undefined MightyMock Object';
return this;
};
if( arguments.size() eq 1){
if (isObject(arguments[1])) {
return createTypeSafeMock(arguments[1]);
} else {
mocked.name = arguments[1];
return this;
}
}
/*
Make a type safe mock.
*/
if( arguments.size() eq 2 ) {
return createTypeSafeMock(arguments[1]);
}
}
//Clears all methods in object to be mocked.
function createTypeSafeMock(mockee){
var proxy = 0;
try{
if (not IsObject(mockee)) {
proxy = createObject('component', mockee);
mocked.name = mockee;
} else {
proxy = mockee;
mocked.name = getMetaData(mockee).name;
}
proxy.snif = _$snif; //sniffer for variables scope
proxyVars = proxy.snif();
structClear(proxyVars);
structClear(proxy);
proxy.variables = proxyVars;
/* Need to write to THIS and VARIABLES scope because some weird scoping
issue when invoking a variable-scoped method from within another
method, CF sees this as undefined.
Ex: this works normally:
function foo(){
return bar();
}
But if copying both foo and bar to another component:
newCfc.foo = foo;
newCfc.bar = bar;
calling foo() fails with undefined bar exception. However, if we
do this:
newCfc.foo = foo;
newCfc.variables.foo = foo;
newCfc.bar = bar;
newCfc.variables.bar = bar;
All is well ...
*/
proxy.RETURNS = RETURNS ;
proxy._$SETSTATE = _$SETSTATE;
proxy.variables._$SETSTATE = _$SETSTATE;
proxy.DEBUGMOCK = DEBUGMOCK;
proxy.variables.DEBUGMOCK = DEBUGMOCK;
proxy._$GETPREVIOUSSTATE = _$GETPREVIOUSSTATE;
proxy.variables._$GETPREVIOUSSTATE = _$GETPREVIOUSSTATE;
proxy.MOCK = MOCK;
proxy.variables.MOCK = MOCK;
proxy.VERIFYATLEAST=VERIFYATLEAST;
proxy.variables.VERIFYATLEAST=VERIFYATLEAST;
proxy.THROWS = THROWS ;
proxy.variables.THROWS = THROWS ;
proxy._$INVOKEMOCK = _$INVOKEMOCK;
proxy.variables._$INVOKEMOCK = _$INVOKEMOCK;
proxy.VERIFYONCE = VERIFYONCE;
proxy.variables.VERIFYONCE = VERIFYONCE;
proxy.variables.CURRENTMETHOD=CURRENTMETHOD;
proxy._$THROW = _$THROW;
proxy.variables._$THROW = _$THROW;
proxy.variables.STATES = STATES;
proxy.variables.PREVIOUSSTATE = PREVIOUSSTATE;
proxy.VERIFYNEVER = VERIFYNEVER;
proxy.variables.VERIFYNEVER = VERIFYNEVER;
proxy.variables.TEMPRULE =TEMPRULE;
proxy.variables.CURRENTSTATE = variables.CURRENTSTATE;
proxy._$DEBUGREG = _$DEBUGREG ;
proxy.variables._$DEBUGREG = _$DEBUGREG;
proxy.variables.REGISTRY = REGISTRY;
proxy._$GETSTATE =_$GETSTATE;
proxy.RESET = RESET;
proxy.variables.RESET = RESET;
proxy.ONMISSINGMETHOD = ONMISSINGMETHOD;
proxy.VERIFYATMOST=VERIFYATMOST;
proxy.variables.VERIFYATMOST=VERIFYATMOST;
proxy.variables.MATCHER = MATCHER;
proxy.variables.SPY= SPY;
proxy.variables.VERIFIER = VERIFIER;
proxy.REGISTER = REGISTER;
proxy.variables.REGISTER = REGISTER;
proxy._$DEBUGINVOKE = _$DEBUGINVOKE;
proxy._$DUMP = _$DUMP;
proxy.VERIFY = VERIFY;
proxy.variables.VERIFY = VERIFY;
proxy.VERIFYTIMES = VERIFYTIMES;
proxy.variables.VERIFYTIMES = VERIFYTIMES;
proxy._$GETREGISTRY = _$GETREGISTRY;
proxy.variables._$GETREGISTRY = _$GETREGISTRY;
proxy.GETMOCKED = GETMOCKED;
proxy.variables.GETMOCKED = GETMOCKED;
proxy.MOCKED = MOCKED;
proxy.variables.MOCKED = MOCKED;
proxy._$DUMP = _$DUMP;
proxy.variables._$DUMP = _$DUMP;
return proxy;
}
catch (coldfusion.runtime.CfJspPage$NoSuchTemplateException e){
_$throw('InvalidMockException',e.getMessage(),e.getDetail());
}
}
/*--------------------------------------------------------------------
* * * * Behavioral Methods * * * *
Main entry points.
--------------------------------------------------------------------*/
function onMissingMethod(missingMethodName,missingMethodArguments){
var tempMock = chr(0);
var temp = '';
if( currentState == 'verifying'){
verifier.doVerify(tempRule[1], missingMethodName, missingMethodArguments, tempRule[2], registry );
_$setState('idle');
return this;
}
else if(!registry.exists(missingMethodName,missingMethodArguments)) {
if (!registry.isPattern(missingMethodArguments)){ //pee-yew!
try{
//To Do: record the literal and invoke pattern behavior
//Record both if they exist. This will help for lookups
tempMock = registry.findByPattern(missingMethodName,missingMethodArguments);
return _$invokeMock(tempMock['missingMethodName'],tempMock['missingMethodArguments']);
}
catch(MismatchedArgumentPatternException e){
// If we get here, it's because we're registering the method
// the first time
//_$rethrow(e);
}
}
//Now we try to register the mock.
_$setState('registering');
registry.register(missingMethodName,missingMethodArguments); //could return id
currentMethod['name'] = missingMethodName;
currentMethod['missingMethodArguments'] = missingMethodArguments;
// what logic can we implement to simply return '' if not mocked? _AND_
// implement chaining?
return this;
}
else{
_$setState('executing');
currentMethod = {};
try{
retval = _$invokeMock(missingMethodName,missingMethodArguments);
}
catch(UnmockedBehaviorException e){
retval = chr(0);
}
return retval;
}
return chr(0);
}
//--------------------------------------------------------------------------------------//
/*--------------------------------------------------------------------
--------------------------------------------------------------------*/
function returns(){
var arg = '';
_$setState('idle');
if( arguments.size() ) arg = arguments[1];
registry.updateRegistry(currentMethod['name'],currentMethod['missingMethodArguments'],'returns',arg);
return this;
}
function throws(type){
registry.updateRegistry(currentMethod['name'],currentMethod['missingMethodArguments'],'throws',type);
return this;
}
/*-------------------------------------------------------------------------------------
Method Verifications
-------------------------------------------------------------------------------------*/
function verify(){
var count = 1;
_$setState('verifying');
tempRule[1] = 'verify';
if(arguments.size()) count = arguments[1];
tempRule[2] = count;
return this;
}
//Could put all this into onMissingMethod?
function verifyTimes(count){
_$setState('verifying');
tempRule[1] = 'verifyTimes';
tempRule[2] = arguments.count;
return this;
}
function verifyAtLeast(count){
_$setState('verifying');
tempRule[1] = 'verifyAtLeast';
tempRule[2] = arguments.count;
return this;
}
function verifyAtMost(count){
_$setState('verifying');
tempRule[1] = 'verifyAtMost';
tempRule[2] = arguments.count;
return this;
}
function verifyOnce(){
_$setState('verifying');
tempRule[1] = 'verifyOnce';
tempRule[2] = 1;
return this;
}
function verifyNever(){
_$setState('verifying');
tempRule[1] = 'verifyNever';
tempRule[2] = 0;
return this;
}
/*------------------------------------------------------------------------------
Utils
------------------------------------------------------------------------------*/
function debugMock(){
var verbose = true;
if(arguments.size()) verbose = arguments[1];
return createObject('component', 'MockDebug').debug(this,verbose);
}
function reset(){
registry.reset();
_$setState('idle');
currentMethod = {};
//getMetaData(this).name = 'MightyMock';
//getMetaData(this).fullname = 'MightyMock';
return this;
}
function register(){
_$setState('registering');
return this;
}
function mock(){
_$setState('registering');
return this;
}
/*------------------------------------------------------------------------------
Private API.
------------------------------------------------------------------------------*/
//sniffer hook into another object's variables scope
function _$snif(){
return variables;
}
function _$invokeMock(target,args){
var behavior = registry.getRegisteredBehavior(target,args);
if(behavior == 'returns') return registry.getReturnsData(target,args);
if(behavior == 'throws') _$throw(registry.getReturnsData(target,args));
}
function _$debugReg(){
return registry.getRegistry();
}
function _$debugInvoke(){
return registry.invocationRecord;
}
function _$getRegistry(){
return registry;
}
function _$setState(state){
previousState = currentState;
currentState = state;
}
function _$getState(){
return currentState;
}
function _$getPreviousState(){
return previousState;
}
function _$getSpy(){
return spy;
}
function getMocked() {
return mocked; //returns
}
/*------------------------------------------------------------------------------
Private Instance Members
------------------------------------------------------------------------------*/
variables.registry = createObject('component','MockRegistry');
matcher = createObject('component','ArgumentMatcher');
verifier = createObject('component','Verifier');
mocked = {}; //meta data about the object being mocked
spy = chr(0); //reference to the real object being mocked
tempRule = []; //tech debt for verfier
states = [
'idle', // mock is waiting to be invoked
'registering', // mock is registering methods
'executing', // mock is executing a mocked method
'verifying', // mock is verifying behavior
'error' // problem
];
currentState = states[1];
previousState = '';
currentMethod = {};
</cfscript>
<cffunction name="_$dump">
<cfset var i = 0 />
<cfloop from="1" to="#arrayLen(arguments)#" index="i">
<cfdump var="#arguments[i]#">
</cfloop>
</cffunction>
<cffunction name="_$throw">
<cfargument name="type" required="false" default="mxunit.exception.AssertionFailedError">
<cfargument name="message" required="false" default="Failed Mock Behaviour">
<cfargument name="detail" required="false" default="">
<cfthrow type="#arguments.type#" message="#arguments.message#" detail="#arguments.detail#" />
</cffunction>
<cffunction name="_$rethrow">
<cfargument name="e" >
<cfthrow object="#e#" />
</cffunction>
</cfcomponent>