SUnit-Tests.st 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. Smalltalk createPackage: 'SUnit-Tests'!
  2. TestCase subclass: #ExampleSetTest
  3. slots: {#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 equals: 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) equals: 0.
  36. self assert: (full occurrencesOf: 5) equals: 1.
  37. full add: 5.
  38. self assert: (full occurrencesOf: 5) equals: 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. slots: {#flag}
  47. package: 'SUnit-Tests'!
  48. !SUnitAsyncTest methodsFor: 'helpers'!
  49. fakeError
  50. flag := 'bad'.
  51. self timeout: 30.
  52. flag := (self async: [ flag := 'ok'. self error: 'Intentional' ]) valueWithTimeout: 20
  53. !
  54. fakeErrorFailingInTearDown
  55. flag := 'bad'.
  56. self timeout: 30.
  57. flag := (self async: [ self error: 'Intentional' ]) valueWithTimeout: 20
  58. !
  59. fakeFailure
  60. flag := 'bad'.
  61. self timeout: 30.
  62. flag := (self async: [ flag := 'ok'. self assert: false ]) valueWithTimeout: 20
  63. !
  64. fakeMultipleTimeoutFailing
  65. self timeout: 100.
  66. (self async: [
  67. self timeout: 20.
  68. (self async: [ self finished ]) valueWithTimeout: 30
  69. ]) valueWithTimeout: 20
  70. !
  71. fakeMultipleTimeoutPassing
  72. self timeout: 20.
  73. (self async: [
  74. self timeout: 40.
  75. (self async: [ self finished ]) valueWithTimeout: 20
  76. ]) valueWithTimeout: 10
  77. !
  78. fakeNonLifoReturn
  79. flag := 'bad'.
  80. self timeout: 30.
  81. flag := (self async: [ flag := 'ok'. ^ 'non-lifo' ]) valueWithTimeout: 20
  82. !
  83. fakePassFailingInTearDown
  84. flag := 'bad'.
  85. self timeout: 10.
  86. (self async: [ self finished ]) fork
  87. !
  88. fakeTimeout
  89. self timeout: 10.
  90. (self async: [ self finished ]) valueWithTimeout: 20
  91. ! !
  92. !SUnitAsyncTest methodsFor: 'private'!
  93. selectorSetOf: aCollection
  94. ^ (aCollection collect: [ :each | each selector ]) asSet
  95. ! !
  96. !SUnitAsyncTest methodsFor: 'running'!
  97. setUp
  98. flag := 'ok'
  99. !
  100. tearDown
  101. self assert: 'ok' equals: flag
  102. ! !
  103. !SUnitAsyncTest methodsFor: 'tests'!
  104. testAsyncErrorsAndFailures
  105. | suite runner result assertBlock |
  106. suite := #(fakeError fakePassFailingInTearDown fakeErrorFailingInTearDown fakeFailure testPass) collect: [ :each | self class selector: each ].
  107. runner := TestSuiteRunner on: suite.
  108. self timeout: 200.
  109. result := runner result.
  110. assertBlock := self async: [
  111. self assert: (self selectorSetOf: result errors) equals: #(fakeError) asSet.
  112. self assert: (self selectorSetOf: result failures) equals: #(fakePassFailingInTearDown fakeErrorFailingInTearDown fakeFailure) asSet.
  113. self finished
  114. ].
  115. runner announcer on: ResultAnnouncement do: [ :ann |
  116. (ann result == result and: [ result runs = result total ]) ifTrue: assertBlock ].
  117. runner run
  118. !
  119. testAsyncNeedsTimeout
  120. self should: [ self async: [ ] ] raise: Error.
  121. self timeout: 0.
  122. self shouldnt: [ self async: [ ] ] raise: Error.
  123. self finished
  124. !
  125. testFinishedNeedsTimeout
  126. self should: [ self finished ] raise: Error.
  127. self timeout: 0.
  128. self shouldnt: [ self finished ] raise: Error.
  129. !
  130. testIsAsyncReturnsCorrectValues
  131. self deny: self isAsync.
  132. self timeout: 0.
  133. self assert: self isAsync.
  134. self finished.
  135. self deny: self isAsync
  136. !
  137. testNonLifo
  138. | suite runner result assertBlock |
  139. suite := #(fakeNonLifoReturn testPass) collect: [ :each | self class selector: each ].
  140. runner := TestSuiteRunner on: suite.
  141. self timeout: 200.
  142. result := runner result.
  143. assertBlock := self async: [
  144. self assert: (self selectorSetOf: result errors) equals: #(fakeNonLifoReturn) asSet.
  145. self assert: (self selectorSetOf: result failures) equals: Set new.
  146. "TODO check that error is indeed a correct NonLifoReturn"
  147. self finished
  148. ].
  149. runner announcer on: ResultAnnouncement do: [ :ann |
  150. (ann result == result and: [ result runs = result total ]) ifTrue: assertBlock ].
  151. runner run
  152. !
  153. testPass
  154. flag := 'bad'.
  155. self timeout: 10.
  156. flag := (self async: [ self assert: true. self finished. flag := 'ok' ]) valueWithTimeout: 5
  157. !
  158. testTimeouts
  159. | suite runner result assertBlock |
  160. suite := #(fakeTimeout fakeMultipleTimeoutFailing fakeMultipleTimeoutPassing testPass) collect: [ :each | self class selector: each ].
  161. runner := TestSuiteRunner on: suite.
  162. self timeout: 200.
  163. result := runner result.
  164. assertBlock := self async: [
  165. self assert: (self selectorSetOf: result errors) equals: Set new.
  166. self assert: (self selectorSetOf: result failures) equals: #(fakeMultipleTimeoutFailing fakeTimeout) asSet.
  167. self finished
  168. ].
  169. runner announcer on: ResultAnnouncement do: [ :ann |
  170. (ann result == result and: [ result runs = result total ]) ifTrue: assertBlock ].
  171. runner run
  172. !
  173. testTwoAsyncPassesWithFinishedOnlyOneIsRun
  174. | x |
  175. flag := 'bad'.
  176. self timeout: 10.
  177. x := 0.
  178. flag := (self async: [ self finished. flag := 'ok'. x := x+1. self assert: x equals: 1 ]) valueWithTimeout: 0.
  179. flag := (self async: [ self finished. flag := 'ok'. x := x+1. self assert: x equals: 1 ]) valueWithTimeout: 0.
  180. ! !
  181. TestCase subclass: #SUnitPromiseTest
  182. slots: {#flag}
  183. package: 'SUnit-Tests'!
  184. !SUnitPromiseTest methodsFor: 'helpers'!
  185. fakeError
  186. flag := 'bad'.
  187. self timeout: 30.
  188. flag := Promise delayMilliseconds: 20.
  189. ^ flag then: [ flag := 'ok'. self error: 'Intentional' ]
  190. !
  191. fakeErrorFailingInTearDown
  192. flag := 'bad'.
  193. self timeout: 30.
  194. flag := Promise delayMilliseconds: 20.
  195. ^ flag then: [ self error: 'Intentional' ]
  196. !
  197. fakeFailure
  198. flag := 'bad'.
  199. self timeout: 30.
  200. flag := Promise delayMilliseconds: 20.
  201. ^ flag then: [ flag := 'ok'. self assert: false ]
  202. !
  203. fakeMultipleTimeoutFailing
  204. self timeout: 100.
  205. ^ (Promise delayMilliseconds: 20) then: [ self timeout: 20. Promise delayMilliseconds: 30 ]
  206. !
  207. fakeMultipleTimeoutPassing
  208. self timeout: 20.
  209. ^ (Promise delayMilliseconds: 10) then: [ self timeout: 40. Promise delayMilliseconds: 20 ]
  210. !
  211. fakeNonLifoReturn
  212. flag := 'bad'.
  213. self timeout: 30.
  214. flag := Promise delayMilliseconds: 20.
  215. ^ flag then: [ flag := 'ok'. ^ 'non-lifo' ]
  216. !
  217. fakePassFailingInTearDown
  218. flag := 'bad'.
  219. self timeout: 10.
  220. ^ Promise new
  221. !
  222. fakePromiseWithoutTimeout
  223. ^ Promise delayMilliseconds: 10
  224. !
  225. fakeTimeout
  226. self timeout: 10.
  227. ^ Promise delayMilliseconds: 20
  228. !
  229. fakeTimeoutSendOnly
  230. self timeout: 10
  231. ! !
  232. !SUnitPromiseTest methodsFor: 'private'!
  233. selectorSetOf: aCollection
  234. ^ (aCollection collect: [ :each | each selector ]) asSet
  235. ! !
  236. !SUnitPromiseTest methodsFor: 'running'!
  237. setUp
  238. flag := 'ok'
  239. !
  240. tearDown
  241. self assert: 'ok' equals: flag
  242. ! !
  243. !SUnitPromiseTest methodsFor: 'tests'!
  244. testIsAsyncReturnsCorrectValues
  245. self deny: self isAsync.
  246. self timeout: 0.
  247. self assert: self isAsync.
  248. "self finished.
  249. self deny: self isAsync"
  250. ^ Promise new
  251. !
  252. testNonLifo
  253. | suite runner result |
  254. suite := #(fakeNonLifoReturn testPass) collect: [ :each | self class selector: each ].
  255. runner := TestSuiteRunner on: suite.
  256. self timeout: 200.
  257. result := runner result.
  258. ^ Promise new: [ :model |
  259. runner announcer on: ResultAnnouncement do: [ :ann |
  260. (ann result == result and: [ result runs = result total ]) ifTrue: [ model do: [
  261. self assert: (self selectorSetOf: result errors) equals: #(fakeNonLifoReturn) asSet.
  262. self assert: (self selectorSetOf: result failures) equals: Set new.
  263. "TODO check that error is indeed a correct NonLifoReturn"
  264. ] ] ].
  265. runner run ]
  266. !
  267. testPass
  268. flag := 'bad'.
  269. self timeout: 10.
  270. flag := Promise delayMilliseconds: 5.
  271. ^ flag then: [ self assert: true. flag := 'ok' ]
  272. !
  273. testPromiseErrorsAndFailures
  274. | suite runner result |
  275. suite := #(fakeError fakePassFailingInTearDown fakeErrorFailingInTearDown fakeFailure testPass) collect: [ :each | self class selector: each ].
  276. runner := TestSuiteRunner on: suite.
  277. self timeout: 200.
  278. result := runner result.
  279. ^ Promise new: [ :model |
  280. runner announcer on: ResultAnnouncement do: [ :ann |
  281. (ann result == result and: [ result runs = result total ]) ifTrue: [ model do: [
  282. self assert: (self selectorSetOf: result errors) equals: #(fakeError) asSet.
  283. self assert: (self selectorSetOf: result failures) equals: #(fakePassFailingInTearDown fakeErrorFailingInTearDown fakeFailure) asSet ] ] ].
  284. runner run ]
  285. !
  286. testTimeouts
  287. | suite runner result |
  288. suite := #(fakeTimeout fakeMultipleTimeoutFailing fakeMultipleTimeoutPassing fakeTimeoutSendOnly fakePromiseWithoutTimeout testPass) collect: [ :each | self class selector: each ].
  289. runner := TestSuiteRunner on: suite.
  290. self timeout: 200.
  291. result := runner result.
  292. ^ Promise new: [ :model |
  293. runner announcer on: ResultAnnouncement do: [ :ann |
  294. (ann result == result and: [ result runs = result total ]) ifTrue: [ model do: [
  295. self assert: (self selectorSetOf: result errors) equals: #() asSet.
  296. self assert: (self selectorSetOf: result failures) equals: #(fakeMultipleTimeoutFailing fakeTimeout fakeTimeoutSendOnly fakePromiseWithoutTimeout) asSet ] ] ].
  297. runner run ]
  298. ! !