-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerifier.cfc
149 lines (114 loc) · 4.39 KB
/
Verifier.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
<cfcomponent>
<cfscript>
//change as needed
exceptionType = 'mxunit.exception.AssertionFailedError';
function doVerify(verifyMethod, target, args, expected, mockreg){
switch(verifyMethod){
case 'verify':
return verify(expected, target, args, mockreg);
break;
case 'verifyTimes':
return verifyTimes(expected, target, args, mockreg);
break;
case 'verifyAtLeast':
return verifyAtLeast(expected, target, args, mockreg);
break;
case 'verifyAtMost':
return verifyAtMost(expected, target, args, mockreg);
break;
case 'verifyNever':
return verifyNever(target, args, mockreg);
break;
case 'verifyOnce':
return verifyOnce(target, args, mockreg);
break;
default:
_$throw('InvalidVerificationException','Method #verifyMethod# was not found', 'Make sure the method exists.');
break;
}
}
function verify(expected,target,args,mockreg){
var actualCount = _$getActual(target,args, mockreg);
var details = '';
var isOk = actualCount == expected;
if(!isOk){
calls = mockreg.getInvocationRecordsById(target,args).recordCount;
details = _$buildMessage('verify(#expected#)',target, args, expected, mockreg);
_$throw(exceptionType,'Mock verification failed. ',details);
}
return isOk;
}
function _$getActual(target,args,mockreg){
var rows = mockreg.getInvocationRecordsById(target,args);
return rows.recordCount;
}
function verifyNever(target, args, mockreg){
var actualCount = _$getActual(target,args, mockreg);
var details = '';
var isOk = actualCount == 0;
if(!isOk){
calls = mockreg.getInvocationRecordsById(target,args).recordCount;
details = _$buildMessage('verifyNever()',target, args, 0, mockreg);
_$throw(exceptionType,'Mock verification failed. ',details);
}
return isOk;
}
function verifyAtMost(expected, target, args, mockreg){
var actualCount = _$getActual(target,args, mockreg);
var details = '';
var isOk = actualCount <= expected;
if(!isOk){
calls = mockreg.getInvocationRecordsById(target,args).recordCount;
details = _$buildMessage('verifyAtMost(#expected#)',target, args, expected, mockreg);
_$throw(exceptionType,'Mock verification failed. ',details);
}
return isOk;
}
function verifyAtLeast(expected, target, args, mockreg){
var actualCount = _$getActual(target,args, mockreg);
var details = '';
var isOk = actualCount >= expected;
if(!isOk){
calls = mockreg.getInvocationRecordsById(target,args).recordCount;
details = _$buildMessage('verifyAtLeast(#expected#)',target, args, expected, mockreg);
_$throw(exceptionType,'Mock verification failed. ',details);
}
return isOk;
}
function verifyOnce(target, args, mockreg){
var actualCount = _$getActual(target,args, mockreg);
var details = '';
var isOk = actualCount == 1;
if(!isOk){
calls = mockreg.getInvocationRecordsById(target,args).recordCount;
details = _$buildMessage('verifyOnce()',target, args, 1, mockreg);
_$throw(exceptionType,'Mock verification failed. ',details);
}
return isOk;
}
function verifyTimes(expected, target, args, mockreg){
var actualCount = _$getActual(target,args, mockreg);
var details = '';
var isOk = actualCount == expected;
if(!isOk){
calls = mockreg.getInvocationRecordsById(target,args).recordCount;
details = _$buildMessage('verifyTimes(#expected#)',target, args, expected, mockreg);
_$throw(exceptionType,'Mock verification failed. ',details);
}
return isOk;
}
function _$buildMessage(rule, target, args, expected, mockreg){
var calls = mockreg.getInvocationRecordsById(target,args).recordCount;
var details = '';
details &= 'Expected #target#( w/#args.size()# arguments ) to be verfied using rule "#rule#" ';
details &= ': , but #target#(...) was called #calls# time(s).';
return details;
}
</cfscript>
<cffunction name="_$throw">
<cfargument name="type" required="false" default="mxunit.exception.AssertionFailedError">
<cfargument name="message" required="false" default="failed behaviour">
<cfargument name="detail" required="false" default="Details details ...">
<cfthrow type="#arguments.type#" message="#arguments.message#" detail="#arguments.detail#" />
</cffunction>
</cfcomponent>