1
0

SUnit-Tests.st 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. Smalltalk current createPackage: 'SUnit-Tests' properties: #{}!
  2. TestCase subclass: #ExampleSetTest
  3. instanceVariableNames: 'empty full'
  4. package: 'SUnit-Tests'!
  5. !ExampleSetTest commentStamp!
  6. ExampleSetTest is taken from Pharo 1.4.
  7. THe purpose of this class is to demonstrate a simple use case of the test framework.!
  8. !ExampleSetTest methodsFor: 'running'!
  9. setUp
  10. empty := Set new.
  11. full := Set with: 5 with: #abc
  12. ! !
  13. !ExampleSetTest methodsFor: 'tests'!
  14. testAdd
  15. empty add: 5.
  16. self assert: (empty includes: 5)
  17. !
  18. testGrow
  19. empty addAll: (1 to: 100).
  20. self assert: empty size = 100
  21. !
  22. testIllegal
  23. self
  24. should: [empty at: 5]
  25. raise: Error.
  26. self
  27. should: [empty at: 5 put: #abc]
  28. raise: Error
  29. !
  30. testIncludes
  31. self assert: (full includes: 5).
  32. self assert: (full includes: #abc)
  33. !
  34. testOccurrences
  35. self assert: (empty occurrencesOf: 0) = 0.
  36. self assert: (full occurrencesOf: 5) = 1.
  37. full add: 5.
  38. self assert: (full occurrencesOf: 5) = 1
  39. !
  40. testRemove
  41. full remove: 5.
  42. self assert: (full includes: #abc).
  43. self deny: (full includes: 5)
  44. ! !
  45. TestCase subclass: #SUnitAsyncTest
  46. instanceVariableNames: 'flag'
  47. package: 'SUnit-Tests'!
  48. !SUnitAsyncTest methodsFor: 'helpers'!
  49. fakeError
  50. flag := 'bad'.
  51. self timeout: 10.
  52. flag := (self async: [ flag := 'ok'. self error: 'Intentional' ]) valueWithTimeout: 5
  53. !
  54. fakeErrorFailingInTearDown
  55. flag := 'bad'.
  56. self timeout: 10.
  57. flag := (self async: [ self error: 'Intentional' ]) valueWithTimeout: 5
  58. !
  59. fakeFailure
  60. flag := 'bad'.
  61. self timeout: 10.
  62. flag := (self async: [ flag := 'ok'. self assert: false ]) valueWithTimeout: 5
  63. !
  64. fakeMultipleTimeoutFailing
  65. self timeout: 100.
  66. (self async: [
  67. self timeout: 5.
  68. (self async: [ self finished ]) valueWithTimeout: 10
  69. ]) valueWithTimeout: 5
  70. !
  71. fakeMultipleTimeoutPassing
  72. self timeout: 10.
  73. (self async: [
  74. self timeout: 20.
  75. (self async: [ self finished ]) valueWithTimeout: 10
  76. ]) valueWithTimeout: 5
  77. !
  78. fakeTimeout
  79. self timeout: 4.
  80. (self async: [ self finished ]) valueWithTimeout: 5
  81. ! !
  82. !SUnitAsyncTest methodsFor: 'private'!
  83. sortedSelectors: aCollection
  84. ^(aCollection collect: [:each | each selector]) sorted
  85. ! !
  86. !SUnitAsyncTest methodsFor: 'running'!
  87. setUp
  88. flag := 'ok'
  89. !
  90. tearDown
  91. self assert: 'ok' equals: flag
  92. ! !
  93. !SUnitAsyncTest methodsFor: 'tests'!
  94. testAsyncErrorsAndFailures
  95. | suite runner result assertBlock |
  96. suite := #('fakeError' 'fakeErrorFailingInTearDown' 'fakeFailure' 'testPass') collect: [ :each | self class selector: each ].
  97. runner := TestSuiteRunner on: suite.
  98. self timeout: 200.
  99. result := runner result.
  100. assertBlock := self async: [
  101. self assert: #('fakeError') equals: (self sortedSelectors: result errors).
  102. self assert: #('fakeErrorFailingInTearDown' 'fakeFailure') equals: (self sortedSelectors: result failures).
  103. self finished
  104. ].
  105. runner announcer on: ResultAnnouncement do: [:ann |
  106. ann result == result ifTrue: [ result runs = result total ifTrue: assertBlock ]].
  107. runner run
  108. !
  109. testAsyncNeedsTimeout
  110. self should: [ self async: [ ] ] raise: Error.
  111. self timeout: 0.
  112. self shouldnt: [ self async: [ ] ] raise: Error.
  113. self finished
  114. !
  115. testFinishedNeedsTimeout
  116. self should: [ self finished ] raise: Error.
  117. self timeout: 0.
  118. self shouldnt: [ self finished ] raise: Error.
  119. !
  120. testIsAsyncReturnsCorrectValues
  121. self deny: self isAsync.
  122. self timeout: 0.
  123. self assert: self isAsync.
  124. self finished.
  125. self deny: self isAsync
  126. !
  127. testPass
  128. flag := 'bad'.
  129. self timeout: 10.
  130. flag := (self async: [ self assert: true. self finished. flag := 'ok' ]) valueWithTimeout: 5
  131. !
  132. testTimeouts
  133. | suite runner result assertBlock |
  134. suite := #('fakeTimeout' 'fakeMultipleTimeoutFailing' 'fakeMultipleTimeoutPassing' 'testPass') collect: [ :each | self class selector: each ].
  135. runner := TestSuiteRunner on: suite.
  136. self timeout: 200.
  137. result := runner result.
  138. assertBlock := self async: [
  139. self assert: result errors isEmpty.
  140. self assert: #('fakeMultipleTimeoutFailing' 'fakeTimeout') equals: (self sortedSelectors: result failures).
  141. self finished
  142. ].
  143. runner announcer on: ResultAnnouncement do: [:ann |
  144. ann result == result ifTrue: [ result runs = result total ifTrue: assertBlock ]].
  145. runner run
  146. !
  147. testTwoAsyncPassesWithFinishedOnlyOneIsRun
  148. | x |
  149. flag := 'bad'.
  150. self timeout: 10.
  151. x := 0.
  152. flag := (self async: [ self finished. flag := 'ok'. x := x+1. self assert: 1 equals: x ]) valueWithTimeout: 0.
  153. flag := (self async: [ self finished. flag := 'ok'. x := x+1. self assert: 1 equals: x ]) valueWithTimeout: 0.
  154. ! !