SUnit.st 2.3 KB

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