-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderedExpectation.cfc
159 lines (136 loc) · 4.41 KB
/
OrderedExpectation.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
<cfcomponent output="true">
<cfscript>
this.mocks = [];
invocations = chr(0);
expectations = [];//(1);
index = 1;
mr = createObject('component','MockRegistry');
exceptionType = 'mxunit.exception.AssertionFailedError';
function onMissingMethod(target,args){
var id = mr.id(target,args);
var expectation = {};
expectation['id'] = id;
expectation['name'] = target;
expectation['args'] = args;
expectations[index++] = expectation;
return this;
}
function getOrderedList(){
return orderedList;
}
function verify(){
var expectations = getExpectations();
var invocations = getInvocations();
var orderedList = valueList( invocations.method );
var currentExpectation = '';
var currentExpectationTime = '';
var nextExpectation = '';
var nextExpectationTime = '';
var numberOfExpectations = expectations.size();
var i=1;
for(i=1; i <= numberOfExpectations; i++){
currentExpectation = expectations[i];
//debug(currentExpectation);
if(!exists(currentExpectation['id'])){
_$throw(exceptionType,'#currentExpectation["name"]#() not found in invocation list.',
'To Do: Print list of methods' );
}
if(i < numberOfExpectations) {
nextExpectation = expectations [i+1];
if(!exists(nextExpectation['id'])){
_$throw(exceptionType,'#nextExpectation["name"]#() not found in invocation list.',
'To Do: Print list of methods' );
}
currentExpectationTime = getInvocationTime(currentExpectation['id']);
nextExpectationTime = getInvocationTime(nextExpectation['id']);
if(currentExpectationTime > nextExpectationTime ) {
_$throw(exceptionType, 'Expectation Failure : #currentExpectation["name"]#() invoked AFTER #nextExpectation["name"]#().',
'Actual invocation sequence was "#printPrettyOrderedList(orderedList)#", but expectations were defined as #prettyPrintExpectations()#' );
}
}
}//end for
return this;
}
function init(){
for(item in arguments){
this.mocks[item] = arguments[item];
}
invocations = merge();
return this;
}
function getInvocations(){
return invocations;
}
function getExpectations(){
return expectations;
}
function merge(){
var i = 1;
var s = '';
for(i; i <= this.mocks.size(); i++){
t1 = 'q_#i#';
t2 = 'q2_#i#';
'q_#i#' = this.mocks[i]._$getRegistry().invocationRecord;
'q2_#i#' = this.mocks[i]._$getRegistry().getRegistry();
s &= 'select * from q_#i#, q2_#i#' & chr(10);
s &= 'where q_#i#.id = q2_#i#.id ' & chr(10);
if(i != this.mocks.size()) s &= ' union ' & chr(10);
}
s &= 'order by [time] asc' & chr(0);
invocations = _$query(s);
return invocations;
}
function printPrettyOrderedList(list){
var s = '';
var i = 1;
for(i; i <= listLen(list); i++){
s &= listGetAt(list,i) & '()';
if(i < listLen(list) ) s &= ',';
}
return s;
}
function prettyPrintExpectations(){
var item = '';
var s = '';
var expect = {};
var i = 1;
for(i; i <= expectations.size(); i++){
expect = expectations[i];
s &= expect['name'] & '()';
if(i < expectations.size() ) s &= ',';
}
return s;
}
</cfscript>
<cffunction name="exists" returntype="boolean">
<cfargument name="id" type="string" />
<cfset var q = ''>
<cfquery name="q" dbtype="query" maxrows="1">
select count(*) as cnt
from invocations where id = '#id#'
</cfquery>
<cfreturn q.cnt eq 1 >
</cffunction>
<cffunction name="_$query" access="private">
<cfargument name="qs" type="string" />
<cfset var q = ''>
<cfquery name="q" dbtype="query">
#qs#
</cfquery>
<cfreturn q>
</cffunction>
<cffunction name="getInvocationTime" >
<cfargument name="id" type="string">
<cfset var q = ''>
<cfquery name="q" dbtype="query" maxrows="1">
select [time] from invocations where id = '#id#'
</cfquery>
<cfreturn q['time'] />
</cffunction>
<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>