SUnit-Tests.st 4.7 KB

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