SUnit.st 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. Smalltalk current createPackage: 'SUnit' properties: #{}!
  2. Object subclass: #ResultAnnouncement
  3. instanceVariableNames: 'result'
  4. package: 'SUnit'!
  5. !ResultAnnouncement methodsFor: 'accessing'!
  6. result
  7. ^result
  8. !
  9. result: aTestResult
  10. result := aTestResult
  11. ! !
  12. Object subclass: #TestCase
  13. instanceVariableNames: 'testSelector'
  14. package: 'SUnit'!
  15. !TestCase methodsFor: 'accessing'!
  16. selector
  17. ^testSelector
  18. !
  19. setTestSelector: aSelector
  20. testSelector := aSelector
  21. ! !
  22. !TestCase methodsFor: 'private'!
  23. signalFailure: aString
  24. TestFailure new
  25. messageText: aString;
  26. signal
  27. ! !
  28. !TestCase methodsFor: 'running'!
  29. performTestFor: aResult
  30. [[self perform: self selector]
  31. on: TestFailure do: [:ex | aResult addFailure: self]]
  32. on: Error do: [:ex | aResult addError: self]
  33. !
  34. runCaseFor: aTestResult
  35. self setUp.
  36. aTestResult increaseRuns.
  37. self performTestFor: aTestResult.
  38. self tearDown
  39. !
  40. setUp
  41. !
  42. tearDown
  43. ! !
  44. !TestCase methodsFor: 'testing'!
  45. assert: aBoolean
  46. self assert: aBoolean description: 'Assertion failed'
  47. !
  48. assert: aBoolean description: aString
  49. aBoolean ifFalse: [self signalFailure: aString]
  50. !
  51. assert: expected equals: actual
  52. ^ self assert: (expected = actual) description: 'Expected: ', expected asString, ' but was: ', actual asString
  53. !
  54. deny: aBoolean
  55. self assert: aBoolean not
  56. !
  57. should: aBlock
  58. self assert: aBlock value
  59. !
  60. should: aBlock raise: anExceptionClass
  61. self assert: ([aBlock value. false]
  62. on: anExceptionClass
  63. do: [:ex | true])
  64. !
  65. shouldnt: aBlock raise: anExceptionClass
  66. self assert: ([aBlock value. true]
  67. on: anExceptionClass
  68. do: [:ex | false])
  69. ! !
  70. !TestCase class methodsFor: 'accessing'!
  71. allTestSelectors
  72. | selectors |
  73. selectors := self testSelectors.
  74. self shouldInheritSelectors ifTrue: [
  75. selectors addAll: self superclass allTestSelectors].
  76. ^selectors
  77. !
  78. buildSuite
  79. ^self allTestSelectors collect: [:each | self selector: each]
  80. !
  81. lookupHierarchyRoot
  82. ^TestCase
  83. !
  84. selector: aSelector
  85. ^self new
  86. setTestSelector: aSelector;
  87. yourself
  88. !
  89. testSelectors
  90. ^self methodDictionary keys select: [:each | each match: '^test']
  91. ! !
  92. !TestCase class methodsFor: 'testing'!
  93. isAbstract
  94. ^ self name = 'TestCase'
  95. !
  96. shouldInheritSelectors
  97. ^self ~= self lookupHierarchyRoot
  98. ! !
  99. Error subclass: #TestFailure
  100. instanceVariableNames: ''
  101. package: 'SUnit'!
  102. Object subclass: #TestResult
  103. instanceVariableNames: 'timestamp runs errors failures total'
  104. package: 'SUnit'!
  105. !TestResult methodsFor: 'accessing'!
  106. addError: anError
  107. self errors add: anError
  108. !
  109. addFailure: aFailure
  110. self failures add: aFailure
  111. !
  112. errors
  113. ^errors
  114. !
  115. failures
  116. ^failures
  117. !
  118. increaseRuns
  119. runs := runs + 1
  120. !
  121. runs
  122. ^runs
  123. !
  124. status
  125. ^self errors isEmpty
  126. ifTrue: [
  127. self failures isEmpty
  128. ifTrue: ['success']
  129. ifFalse: ['failure']]
  130. ifFalse: ['error']
  131. !
  132. timestamp
  133. ^timestamp
  134. !
  135. total
  136. ^total
  137. !
  138. total: aNumber
  139. total := aNumber
  140. ! !
  141. !TestResult methodsFor: 'initialization'!
  142. initialize
  143. super initialize.
  144. timestamp := Date now.
  145. runs := 0.
  146. errors := Array new.
  147. failures := Array new.
  148. total := 0
  149. ! !
  150. Object subclass: #TestSuiteRunner
  151. instanceVariableNames: 'suite result announcer'
  152. package: 'SUnit'!
  153. !TestSuiteRunner methodsFor: 'accessing'!
  154. announcer
  155. ^announcer
  156. !
  157. result
  158. ^result
  159. !
  160. suite: aCollection
  161. suite := aCollection
  162. ! !
  163. !TestSuiteRunner methodsFor: 'actions'!
  164. run
  165. | worker index |
  166. result total: suite size.
  167. announcer announce: (ResultAnnouncement new result: result).
  168. index := 1.
  169. worker := [ index <= suite size ifTrue: [
  170. (suite at: index) runCaseFor: result.
  171. index := index + 1.
  172. announcer announce: (ResultAnnouncement new result: result).
  173. worker valueWithTimeout: 0
  174. ]].
  175. (suite size min: 25) timesRepeat: [ worker valueWithTimeout: 0 ]
  176. ! !
  177. !TestSuiteRunner methodsFor: 'initialization'!
  178. initialize
  179. super initialize.
  180. announcer := Announcer new.
  181. result := TestResult new
  182. ! !
  183. !TestSuiteRunner class methodsFor: 'instance creation'!
  184. new
  185. self shouldNotImplement
  186. !
  187. on: aCollection
  188. ^super new suite: aCollection
  189. ! !