1
0

SUnit.st 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. performTest
  30. self perform: self selector
  31. !
  32. runCase
  33. [ self setUp.
  34. self performTest ] ensure: [
  35. self tearDown ]
  36. !
  37. setUp
  38. !
  39. startCase
  40. self setUp.
  41. self performTest
  42. !
  43. tearDown
  44. ! !
  45. !TestCase methodsFor: 'testing'!
  46. assert: aBoolean
  47. self assert: aBoolean description: 'Assertion failed'
  48. !
  49. assert: aBoolean description: aString
  50. aBoolean ifFalse: [self signalFailure: aString]
  51. !
  52. assert: expected equals: actual
  53. ^ self assert: (expected = actual) description: 'Expected: ', expected asString, ' but was: ', actual asString
  54. !
  55. deny: aBoolean
  56. self assert: aBoolean not
  57. !
  58. should: aBlock
  59. self assert: aBlock value
  60. !
  61. should: aBlock raise: anExceptionClass
  62. self assert: ([aBlock value. false]
  63. on: anExceptionClass
  64. do: [:ex | true])
  65. !
  66. shouldnt: aBlock raise: anExceptionClass
  67. self assert: ([aBlock value. true]
  68. on: anExceptionClass
  69. do: [:ex | false])
  70. ! !
  71. !TestCase class methodsFor: 'accessing'!
  72. allTestSelectors
  73. | selectors |
  74. selectors := self testSelectors.
  75. self shouldInheritSelectors ifTrue: [
  76. selectors addAll: self superclass allTestSelectors].
  77. ^selectors
  78. !
  79. buildSuite
  80. ^self allTestSelectors collect: [:each | self selector: each]
  81. !
  82. lookupHierarchyRoot
  83. ^TestCase
  84. !
  85. selector: aSelector
  86. ^self new
  87. setTestSelector: aSelector;
  88. yourself
  89. !
  90. testSelectors
  91. ^self methodDictionary keys select: [:each | each match: '^test']
  92. ! !
  93. !TestCase class methodsFor: 'testing'!
  94. isAbstract
  95. ^ self name = 'TestCase'
  96. !
  97. shouldInheritSelectors
  98. ^self ~= self lookupHierarchyRoot
  99. ! !
  100. Error subclass: #TestFailure
  101. instanceVariableNames: ''
  102. package: 'SUnit'!
  103. Object subclass: #TestResult
  104. instanceVariableNames: 'timestamp runs errors failures total'
  105. package: 'SUnit'!
  106. !TestResult methodsFor: 'accessing'!
  107. addError: anError
  108. self errors add: anError
  109. !
  110. addFailure: aFailure
  111. self failures add: aFailure
  112. !
  113. errors
  114. ^errors
  115. !
  116. failures
  117. ^failures
  118. !
  119. increaseRuns
  120. runs := runs + 1
  121. !
  122. runs
  123. ^runs
  124. !
  125. status
  126. ^self errors isEmpty
  127. ifTrue: [
  128. self failures isEmpty
  129. ifTrue: ['success']
  130. ifFalse: ['failure']]
  131. ifFalse: ['error']
  132. !
  133. timestamp
  134. ^timestamp
  135. !
  136. total
  137. ^total
  138. !
  139. total: aNumber
  140. total := aNumber
  141. ! !
  142. !TestResult methodsFor: 'initialization'!
  143. initialize
  144. super initialize.
  145. timestamp := Date now.
  146. runs := 0.
  147. errors := Array new.
  148. failures := Array new.
  149. total := 0
  150. ! !
  151. !TestResult methodsFor: 'running'!
  152. nextRunDo: aBlock
  153. "Runs aBlock with index of next run
  154. or does nothing if no more runs"
  155. ^self runs == self total
  156. ifFalse: [ aBlock value: self runs + 1 ]
  157. !
  158. runCase: aTestCase
  159. [[[ self increaseRuns.
  160. aTestCase startCase ] ensure: [ aTestCase tearDown ]]
  161. on: TestFailure do: [:ex | self addFailure: aTestCase]]
  162. on: Error do: [:ex | self addError: aTestCase]
  163. ! !
  164. Object subclass: #TestSuiteRunner
  165. instanceVariableNames: 'suite result announcer worker'
  166. package: 'SUnit'!
  167. !TestSuiteRunner methodsFor: 'accessing'!
  168. announcer
  169. ^announcer
  170. !
  171. result
  172. ^result
  173. !
  174. suite: aCollection
  175. suite := aCollection
  176. ! !
  177. !TestSuiteRunner methodsFor: 'actions'!
  178. resume
  179. worker fork.
  180. announcer announce: (ResultAnnouncement new result: result)
  181. !
  182. run
  183. result total: suite size.
  184. self resume
  185. ! !
  186. !TestSuiteRunner methodsFor: 'initialization'!
  187. initialize
  188. super initialize.
  189. announcer := Announcer new.
  190. result := TestResult new.
  191. worker := [ result nextRunDo: [ :index |
  192. [ result runCase: (suite at: index) ]
  193. ensure: [ self resume ]]].
  194. ! !
  195. !TestSuiteRunner class methodsFor: 'instance creation'!
  196. new
  197. self shouldNotImplement
  198. !
  199. on: aCollection
  200. ^super new suite: aCollection
  201. ! !