2
0

SUnit.st 2.4 KB

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