1
0

SUnit.st 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. Smalltalk current createPackage: 'SUnit' properties: #{}!
  2. Object subclass: #TestCase
  3. instanceVariableNames: 'testSelector'
  4. category: 'SUnit'!
  5. !TestCase methodsFor: 'accessing'!
  6. setTestSelector: aSelector
  7. testSelector := aSelector
  8. !
  9. selector
  10. ^testSelector
  11. ! !
  12. !TestCase methodsFor: 'private'!
  13. signalFailure: aString
  14. TestFailure new
  15. messageText: aString;
  16. signal
  17. ! !
  18. !TestCase methodsFor: 'running'!
  19. setUp
  20. !
  21. tearDown
  22. !
  23. runCaseFor: aTestResult
  24. self setUp.
  25. aTestResult increaseRuns.
  26. self performTestFor: aTestResult.
  27. self tearDown
  28. !
  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. !TestCase methodsFor: 'testing'!
  35. assert: aBoolean
  36. self assert: aBoolean description: 'Assertion failed'
  37. !
  38. deny: aBoolean
  39. self assert: aBoolean not
  40. !
  41. assert: expected equals: actual
  42. ^ self assert: (expected = actual) description: 'Expected: ', expected asString, ' but was: ', actual asString
  43. !
  44. assert: aBoolean description: aString
  45. aBoolean ifFalse: [self signalFailure: aString]
  46. ! !
  47. !TestCase class methodsFor: 'accessing'!
  48. testSelectors
  49. ^self methodDictionary keys select: [:each | each match: '^test']
  50. !
  51. selector: aSelector
  52. ^self new
  53. setTestSelector: aSelector;
  54. yourself
  55. !
  56. lookupHierarchyRoot
  57. ^TestCase
  58. !
  59. allTestSelectors
  60. | selectors |
  61. selectors := self testSelectors.
  62. self shouldInheritSelectors ifTrue: [
  63. selectors addAll: self superclass allTestSelectors].
  64. ^selectors
  65. !
  66. buildSuite
  67. ^self allTestSelectors collect: [:each | self selector: each]
  68. ! !
  69. !TestCase class methodsFor: 'testing'!
  70. shouldInheritSelectors
  71. ^self ~= self lookupHierarchyRoot
  72. ! !
  73. Error subclass: #TestFailure
  74. instanceVariableNames: ''
  75. category: 'SUnit'!
  76. Object subclass: #TestResult
  77. instanceVariableNames: 'timestamp runs errors failures total'
  78. category: 'SUnit'!
  79. !TestResult methodsFor: 'accessing'!
  80. timestamp
  81. ^timestamp
  82. !
  83. errors
  84. ^errors
  85. !
  86. failures
  87. ^failures
  88. !
  89. total
  90. ^total
  91. !
  92. total: aNumber
  93. total := aNumber
  94. !
  95. addError: anError
  96. self errors add: anError
  97. !
  98. addFailure: aFailure
  99. self failures add: aFailure
  100. !
  101. runs
  102. ^runs
  103. !
  104. increaseRuns
  105. runs := runs + 1
  106. !
  107. status
  108. ^self errors isEmpty
  109. ifTrue: [
  110. self failures isEmpty
  111. ifTrue: ['success']
  112. ifFalse: ['failure']]
  113. ifFalse: ['error']
  114. ! !
  115. !TestResult methodsFor: 'initialization'!
  116. initialize
  117. super initialize.
  118. timestamp := Date now.
  119. runs := 0.
  120. errors := Array new.
  121. failures := Array new.
  122. total := 0
  123. ! !