-
Notifications
You must be signed in to change notification settings - Fork 56
/
TestSuiteRunner.cfc
executable file
·219 lines (165 loc) · 8.26 KB
/
TestSuiteRunner.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
<cfcomponent>
<cfset variables.requestScopeDebuggingEnabled = false />
<cffunction name="setDataProviderHandler">
<cfargument name="dph" />
<cfset variables.dataProviderHandler = arguments.dph />
</cffunction>
<cffunction name="setMockingFramework">
<cfargument name="mf" />
<cfset variables.mockingFramework = arguments.mf />
</cffunction>
<cffunction name="enableRequestScopeDebugging">
<!--- TODO: Add a test for request scope debugging or get rid of it. --->
<cfset variables.requestScopeDebuggingEnabled = true />
</cffunction>
<cffunction name="run" returntype="any" access="public" output="true" hint="Primary method for running TestSuites and individual tests.">
<cfargument name="allSuites" hint="a structure corresponding to the key/componentName"/>
<cfargument name="results" hint="The TestResult collecting parameter." required="no" type="TestResult" default="#createObject("component","TestResult").TestResult()#" />
<cfargument name="testMethod" hint="A single test method to run." type="string" required="no" default="">
<cfset var testCase = "">
<cfset var methodIndex = 1>
<cfset var currentTestSuiteName = "" />
<cfset var currentSuite = "" />
<cfset var iterator = arguments.allSuites.keySet().iterator() />
<cfloop condition="#iterator.hasNext()# eq true">
<cfset currentTestSuiteName = iterator.next() />
<cfset currentSuite = allSuites.get(currentTestSuiteName) />
<cfset testCase = createTestCaseFromComponentOrComponentName(currentSuite.ComponentObject) />
<!--- set the MockingFramework if one has been set for the TestSuite --->
<cfif len(variables.MockingFramework)>
<cfset testCase.setMockingFramework(variables.MockingFramework) />
</cfif>
<!--- Invoke prior to tests. Class-level setUp --->
<cfset testCase.beforeTests() />
<cfif len(arguments.testMethod)>
<cfset runTestMethod(testCase, testMethod, results, currentTestSuiteName) />
<cfelse>
<cfloop from="1" to="#arrayLen(currentSuite.methods)#" index="methodIndex">
<cfset runTestMethod(testCase, currentSuite.methods[methodIndex], results, currentTestSuiteName) />
</cfloop>
</cfif>
<!--- Invoke after tests. Class-level tearDown --->
<cfset testCase.afterTests()>
</cfloop>
<cfset results.closeResults() /><!--- Get correct time run for suite --->
<cfreturn results />
</cffunction>
<cffunction name="createTestCaseFromComponentOrComponentName">
<cfargument name="componentObject"/>
<cfif isSimpleValue(componentObject)>
<cfreturn createObject("component", currentTestSuiteName).TestCase(componentObject) />
<cfelse>
<cfreturn componentObject.TestCase(componentObject) />
</cfif>
</cffunction>
<cffunction name="runTestMethod" access="private">
<cfargument name="testCase" />
<cfargument name="methodName" />
<cfargument name="results" />
<cfargument name="currentTestSuiteName"/>
<cfset var tickCountAtStart = getTickCount() />
<cfset var outputOfTest = "" />
<cfset testCase.setExpectedExceptionType( testCase.getAnnotation(methodName,"expectedException") ) />
<cfset testCase.setExpectedExceptionMessage('') />
<cftry>
<cfset results.startTest(methodName,currentTestSuiteName) />
<cfset testCase.clearClassVariables() />
<cfset testCase.initDebug() />
<cfif requestScopeDebuggingEnabled>
<cfset testCase.createRequestScopeDebug() />
</cfif>
<cfset testCase.setUp(methodName = methodName)/>
<cfset outputOfTest = testCase.invokeTestMethod(methodName)>
<cfset assertExpectedExceptionTypeWasThrown( testCase.getExpectedExceptionType(), testCase.getExpectedExceptionMessage() ) />
<cfset results.addSuccess('Passed') />
<!--- Add the trace message from the TestCase instance --->
<cfset results.addContent(outputOfTest) />
<cfcatch type="mxunit.exception.AssertionFailedError">
<cfset addFailureToResults(results=results, expected=testCase.getExpected(), actual=testCase.getActual(), exception=cfcatch, content=outputOfTest)>
</cfcatch>
<cfcatch type="any">
<!---<cflog text="inside cfcatch. #cfcatch.message# #cfcatch.detail# #testCase.getExpectedExceptionType()#, #testCase.getexpectedExceptionMessage()# ">--->
<cfset handleCaughtException(rootOfException(cfcatch), testCase.getExpectedExceptionType(), testCase.getExpectedExceptionMessage(), results, outputOfTest, testCase, cfcatch)>
</cfcatch>
</cftry>
<cftry>
<cfset testCase.tearDown(methodName = methodName) />
<cfcatch type="any">
<cfset results.addError(cfcatch)>
</cfcatch>
</cftry>
<!--- add the debug array to the test result item --->
<cfset results.setDebug( testCase.getDebug() ) />
<!--- make sure the debug buffer is reset for the next text method --->
<cfset testCase.clearDebug() />
<!--- reset the trace message.Bill 6.10.07 --->
<cfset testCase.traceMessage="" />
<cfset results.addProcessingTime(getTickCount()-tickCountAtStart) />
<cfset results.endTest(methodName) />
</cffunction>
<cffunction name="assertExpectedExceptionTypeWasThrown">
<cfargument name="expectedExceptionType"/>
<cfif expectedExceptionType NEQ "">
<cfthrow type="mxunit.exception.AssertionFailedError" message="Exception: #expectedExceptionType# expected but no exception was thrown" />
</cfif>
</cffunction>
<cffunction name="rootOfException" access="private">
<cfargument name="caughtException"/>
<cfif structKeyExists(caughtException,"rootcause")>
<cfreturn caughtException.rootcause />
</cfif>
<cfreturn caughtException />
</cffunction>
<cffunction name="addFailureToResults" access="private">
<cfargument name="results" required="true" hint="the results object">
<cfargument name="expected" required="true">
<cfargument name="actual" required="true">
<cfargument name="exception" required="true" hint="the cfcatch struct">
<cfargument name="content" required="true">
<cfset results.addFailure(exception) />
<cfset results.addExpected(expected)>
<cfset results.addActual(actual)>
<cfset results.addContent(content) />
<cflog file="mxunit" type="error" application="false" text="#exception.message#::#exception.detail#">
</cffunction>
<cffunction name="_$snif" access="private" hint="Door into another component's variables scope">
<cfreturn variables />
</cffunction>
<cffunction name="handleCaughtException" access="private">
<cfargument name="caughtException" type="any"/>
<cfargument name="expectedExceptionType" type="string" required="true" />
<cfargument name="expectedExceptionMessage" type="string" required="true" />
<cfargument name="results" />
<cfargument name="outputOfTest" />
<cfargument name="testCase" />
<cfif arguments.expectedExceptionMessage eq ''>
<cfset arguments.expectedExceptionMessage = 'Exception: #expectedExceptionType# expected but #arguments.caughtException.type# was thrown' />
</cfif>
<cfif exceptionMatchesType(arguments.caughtException, expectedExceptionType)>
<cfset results.addSuccess('Passed') />
<cfset results.addContent(outputOfTest) />
<cfset arguments.testCase.debug(caughtException) />
<cfelseif expectedExceptionType NEQ "">
<cfset arguments.testCase.debug(caughtException) />
<cftry>
<cfthrow type="#arguments.expectedExceptionType#" message="#arguments.expectedExceptionMessage#">
<cfcatch type="any">
<cfset addFailureToResults(results=arguments.results, expected=arguments.expectedExceptionType, actual=arguments.caughtException.type, exception=arguments.caughtException, content=arguments.outputOfTest)>
</cfcatch>
</cftry>
<cfelse>
<cfset testCase.debug(caughtException) />
<cfset results.addError(caughtException) />
<cfset results.addContent(outputOfTest) />
<cflog file="mxunit" type="error" application="false" text="#arguments.caughtException.message#::#arguments.caughtException.detail#" />
</cfif>
</cffunction>
<cffunction name="exceptionMatchesType" access="private">
<cfargument name="actualException" type="any" required="true" />
<cfargument name="expectedExceptionType" type="string" required="true" />
<cfif expectedExceptionType eq "">
<cfreturn false/>
</cfif>
<cfreturn arguments.expectedExceptionType eq 'any' or listFindNoCase(expectedExceptionType, actualException.type) or listFindNoCase(expectedExceptionType, getMetaData(actualException).getName())>
</cffunction>
</cfcomponent>