author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 13 Nov 2013 00:10:09 +0100 | |
changeset 580 | d86ef51e98ab |
parent 571 | d5a1ac6d1743 |
child 593 | 4193d093e71c |
permissions | -rw-r--r-- |
345 | 1 |
"{ Package: 'stx:goodies/sunit' }" |
2 |
||
3 |
TestResult subclass:#TestResultStX |
|
493 | 4 |
instanceVariableNames:'endTime' |
345 | 5 |
classVariableNames:'' |
6 |
poolDictionaries:'' |
|
7 |
category:'SUnit-Smalltalk/X' |
|
8 |
! |
|
9 |
||
10 |
||
500 | 11 |
!TestResultStX class methodsFor:'utilities'! |
12 |
||
13 |
sourceFilenameOfClass:aClass |
|
14 |
"that is ST/X specific" |
|
15 |
"no Smalltalk/X dialect detection needed..." |
|
16 |
||
17 |
|sourceStream testClassSourceFile| |
|
18 |
||
19 |
sourceStream := aClass sourceStream. |
|
20 |
sourceStream notNil ifTrue:[ |
|
21 |
[ |
|
22 |
sourceStream := sourceStream stream. |
|
23 |
sourceStream isFileStream ifTrue:[ |
|
24 |
testClassSourceFile := sourceStream pathName asFilename pathName. |
|
25 |
]. |
|
26 |
] ensure:[ |
|
27 |
sourceStream close. |
|
28 |
] |
|
29 |
]. |
|
30 |
^ testClassSourceFile |
|
31 |
! ! |
|
32 |
||
479 | 33 |
!TestResultStX methodsFor:'accessing'! |
34 |
||
35 |
endTime |
|
36 |
^ endTime |
|
37 |
! |
|
38 |
||
39 |
endTime:aTimestamp |
|
40 |
"sets the overall (suite) end time" |
|
41 |
||
42 |
endTime := aTimestamp. |
|
43 |
! |
|
44 |
||
482 | 45 |
executionTime |
46 |
"return the execution time (in seconds). If asked before or during a run, return nil" |
|
47 |
||
493 | 48 |
|t1 t2| |
49 |
||
50 |
(t1 := self startTime) isNil ifTrue:[^ nil]. |
|
51 |
(t2 := self endTime) isNil ifTrue:[^ nil]. |
|
52 |
^ t2 secondDeltaFrom: t1 |
|
482 | 53 |
|
54 |
" |
|
55 |
|a b| |
|
56 |
||
57 |
a := Timestamp now. |
|
58 |
Delay waitForMilliseconds:567. |
|
59 |
b := Timestamp now. |
|
60 |
b - a |
|
61 |
" |
|
62 |
! |
|
63 |
||
479 | 64 |
startTime |
493 | 65 |
^ timestamp |
479 | 66 |
! |
67 |
||
68 |
startTime:aTimestamp |
|
493 | 69 |
"sets the overall (suite) start time" |
479 | 70 |
|
493 | 71 |
timestamp := aTimestamp. |
479 | 72 |
! ! |
73 |
||
345 | 74 |
!TestResultStX methodsFor:'outcome'! |
75 |
||
500 | 76 |
printLineForContextForJavaCompatibleStackTrace:con on:aStream |
77 |
"why in java-backtrace format? |
|
78 |
Because then jenkins will be able to extract sourcefile and linenumber |
|
79 |
and generate links in the report page. |
|
80 |
I am not willing to write another plugin for this - using junit compatible format" |
|
81 |
||
82 |
|cls classSourceFile method s lineNumberOfMethod lineNumberInFile relPath| |
|
83 |
||
84 |
"/ used to be: |
|
85 |
"/ con printOn:aStream. |
|
86 |
||
87 |
"/ be careful: some tests generate methods on the fly, which are unbound!! |
|
88 |
"/ or even (javascript/other languages) may not have a method at all!!!! |
|
89 |
||
90 |
con fixAllLineNumbers. |
|
91 |
(method := con method) notNil ifTrue:[ |
|
92 |
cls := method mclass. |
|
93 |
cls notNil ifTrue:[ |
|
94 |
relPath := cls package copyReplaceAll:$: with:$/. |
|
95 |
relPath := relPath copyReplaceAll:$\ with:$/. |
|
505 | 96 |
relPath := (relPath asFilename construct:cls theNonMetaclass classFilename) name. |
508 | 97 |
classSourceFile := relPath. |
500 | 98 |
|
526 | 99 |
classSourceFile := Smalltalk getPackageFileName:relPath. |
100 |
classSourceFile isNil ifTrue:[ |
|
101 |
classSourceFile := self class sourceFilenameOfClass:cls. |
|
102 |
]. |
|
500 | 103 |
classSourceFile notNil ifTrue:[ |
580
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
104 |
method isJavaMethod ifTrue:[ |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
105 |
lineNumberOfMethod := method lineNumberForPC0: 0. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
106 |
] ifFalse:[ |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
107 |
method sourcePosition notNil ifTrue:[ |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
108 |
"/ sigh - we have the lineNumber within the method, |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
109 |
"/ and the characterPosition of the method's start. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
110 |
"/ need to calculate the absolute lineNumber in the file |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
111 |
s := LineNumberReadStream on:(classSourceFile asFilename readStream). |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
112 |
[ |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
113 |
[s atEnd not and:[s position < method sourcePosition]] whileTrue:[ |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
114 |
s nextLine |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
115 |
]. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
116 |
lineNumberOfMethod := s lineNumber. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
117 |
] ensure:[ |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
118 |
s close. |
500 | 119 |
]. |
580
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
120 |
] |
500 | 121 |
] |
122 |
] |
|
123 |
]. |
|
124 |
]. |
|
125 |
||
126 |
"/ output something (will not generate a ref to the source file) |
|
580
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
127 |
con printReceiverOn:aStream. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
128 |
aStream nextPutAll:' >> '. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
129 |
con selector printOn:aStream. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
130 |
aStream nextPutAll:' ['. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
131 |
con lineNumber printOn:aStream. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
132 |
aStream nextPut:$]. |
500 | 133 |
|
526 | 134 |
relPath notNil ifTrue:[ |
500 | 135 |
lineNumberOfMethod notNil ifTrue:[ |
136 |
lineNumberInFile := lineNumberOfMethod + con lineNumber - 1. |
|
137 |
aStream nextPutAll:(' (%1:%2)' |
|
526 | 138 |
bindWith: relPath |
500 | 139 |
with: lineNumberInFile) |
140 |
] ifFalse:[ |
|
141 |
aStream nextPutAll:(' (%1)' |
|
526 | 142 |
bindWith: relPath) |
500 | 143 |
]. |
144 |
]. |
|
145 |
aStream cr. |
|
146 |
||
147 |
"Modified: / 05-08-2012 / 12:00:00 / cg" |
|
580
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
148 |
"Modified: / 12-11-2013 / 23:09:07 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
500 | 149 |
! |
150 |
||
345 | 151 |
rememberEndTime |
479 | 152 |
"remembers the endTime of the current test (in outcome)" |
345 | 153 |
|
154 |
^outcome endTime: Timestamp now |
|
155 |
||
156 |
"Created: / 16-08-2011 / 17:36:07 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
157 |
! |
|
158 |
||
505 | 159 |
rememberException:ex |
345 | 160 |
"common for failure and error: |
161 |
called when a test fails. testCase is the failed |
|
505 | 162 |
testcase, ex is platform specific object describing |
345 | 163 |
the failure. Actually, on all platforms exeptt GemStone, |
505 | 164 |
ex is an instance of an exception that caused the failure" |
345 | 165 |
|
166 |
|backtrace| |
|
167 |
||
168 |
((Smalltalk respondsTo:#isSmalltalkX) and:[Smalltalk isSmalltalkX]) ifTrue:[ |
|
169 |
"remember the backtrace (as string, to prevent objects from being kept forwever |
|
170 |
from being garbage collected, the signal (i.e. the exception class) and the |
|
171 |
exceptions message (description). |
|
172 |
Would like to have an exceptionInfo obejct for that, but that might be hard to |
|
173 |
get returned back into the main stream sunit package..." |
|
174 |
||
175 |
backtrace := String streamContents:[:s | |
|
176 |
|con topReached| |
|
177 |
||
178 |
"could use printAllOn:s, but noone is interested in contexts above the |
|
179 |
testcase's runtest context" |
|
180 |
topReached := false. |
|
505 | 181 |
con := ex suspendedContext. |
345 | 182 |
[ |
183 |
con notNil and:[topReached not] |
|
184 |
] whileTrue:[ |
|
500 | 185 |
self printLineForContextForJavaCompatibleStackTrace:con on:s. |
345 | 186 |
topReached := (con selector == outcome selector) |
465 | 187 |
and:[con receiver == outcome |
188 |
or:[ con receiver == outcome testCase ]]. |
|
345 | 189 |
con := con sender. |
190 |
] |
|
191 |
]. |
|
465 | 192 |
|
345 | 193 |
outcome exceptionDetail:(Dictionary new |
571
d5a1ac6d1743
use #creator instead of #signal
Claus Gittinger <cg@exept.de>
parents:
526
diff
changeset
|
194 |
at:#exception put:ex creator; |
505 | 195 |
at:#description put:ex description; |
345 | 196 |
at:#backtrace put:backtrace; |
197 |
yourself). |
|
198 |
^ self. |
|
199 |
]. |
|
200 |
||
201 |
"add other dialect specifics here" |
|
202 |
||
203 |
"Created: / 06-08-2011 / 11:29:23 / cg" |
|
204 |
"Created: / 16-08-2011 / 17:32:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
205 |
! |
|
206 |
||
207 |
rememberOutput: aString |
|
208 |
outcome collectedOutput: aString |
|
209 |
||
210 |
"Created: / 16-08-2011 / 18:18:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
211 |
! |
|
212 |
||
213 |
rememberStartTime |
|
479 | 214 |
"remembers the startTime of the current test (in outcome)" |
345 | 215 |
|
216 |
^outcome startTime: Timestamp now |
|
217 |
||
218 |
"Created: / 16-08-2011 / 17:36:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
219 |
! ! |
|
220 |
||
221 |
!TestResultStX methodsFor:'running'! |
|
222 |
||
223 |
performCase: aTestCase |
|
580
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
224 |
"while performing a testcase, any output to the Transcript is also |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
225 |
collected and is attached to the test result. Nice when tests are executed |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
226 |
automatically, for example by jenkins. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
227 |
Q: I think, we MUST lock this, |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
228 |
or better: aquire the lock and making collecting explicit via |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
229 |
call to 'self collectingTranscriptOutputDo:[...]' in the testcase. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
230 |
Otherwise we'll run into trouble, when executing multiple tests in parallel. |
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
231 |
" |
345 | 232 |
|
233 |
| savedStdout savedTranscript collector| |
|
234 |
||
235 |
savedStdout := Stdout. |
|
236 |
savedTranscript := Transcript. |
|
237 |
collector := WriteStream on:(String new:100). |
|
238 |
[ |
|
239 |
Stdout := SplittingWriteStream on:collector and: Stdout. |
|
240 |
Transcript := SplittingWriteStream on:collector and: Transcript. |
|
241 |
super performCase: aTestCase. |
|
242 |
] ensure:[ |
|
243 |
Stdout := savedStdout. |
|
244 |
Transcript := savedTranscript. |
|
245 |
self rememberOutput: collector contents. |
|
246 |
]. |
|
247 |
||
248 |
"Created: / 16-08-2011 / 18:18:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
580
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
249 |
"Modified (comment): / 29-08-2013 / 11:15:36 / cg" |
345 | 250 |
! ! |
251 |
||
252 |
!TestResultStX class methodsFor:'documentation'! |
|
253 |
||
254 |
version |
|
580
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
255 |
^ '$Header: /cvs/stx/stx/goodies/sunit/TestResultStX.st,v 1.14 2013-11-12 23:10:09 vrany Exp $' |
345 | 256 |
! |
257 |
||
258 |
version_CVS |
|
580
d86ef51e98ab
Fix in TestResultStX>>#printLineForContextForJavaCompatibleStackTrace:on:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
571
diff
changeset
|
259 |
^ '$Header: /cvs/stx/stx/goodies/sunit/TestResultStX.st,v 1.14 2013-11-12 23:10:09 vrany Exp $' |
345 | 260 |
! ! |
571
d5a1ac6d1743
use #creator instead of #signal
Claus Gittinger <cg@exept.de>
parents:
526
diff
changeset
|
261 |