Kernel-Tests.st 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. TestCase subclass: #StringTest
  2. instanceVariableNames: ''
  3. category: 'Kernel-Tests'!
  4. !StringTest methodsFor: 'tests'!
  5. testJoin
  6. self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
  7. !
  8. testStreamContents
  9. self
  10. assert: 'hello world'
  11. equals: (String streamContents: [:aStream| aStream
  12. nextPutAll: 'hello'; space;
  13. nextPutAll: 'world'])
  14. !
  15. testIncludesSubString
  16. self assert: ('amber' includesSubString: 'ber').
  17. self deny: ('amber' includesSubString: 'zork').
  18. !
  19. testEquality
  20. self assert: 'hello' = 'hello'.
  21. self deny: 'hello' = 'world'.
  22. self assert: 'hello' = 'hello' yourself.
  23. self assert: 'hello' yourself = 'hello'.
  24. "test JS falsy value"
  25. self deny: '' = 0
  26. !
  27. testCopyWithoutAll
  28. self
  29. assert: 'hello world'
  30. equals: ('*hello* *world*' copyWithoutAll: '*')
  31. ! !
  32. TestCase subclass: #DictionaryTest
  33. instanceVariableNames: ''
  34. category: 'Kernel-Tests'!
  35. !DictionaryTest methodsFor: 'tests'!
  36. testPrintString
  37. self
  38. assert: 'a Dictionary(''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  39. equals: (Dictionary new
  40. at:'firstname' put: 'James';
  41. at:'lastname' put: 'Bond';
  42. printString)
  43. !
  44. testEquality
  45. | d1 d2 |
  46. self assert: Dictionary new = Dictionary new.
  47. d1 := Dictionary new at: 1 put: 2; yourself.
  48. d2 := Dictionary new at: 1 put: 2; yourself.
  49. self assert: d1 = d2.
  50. d2 := Dictionary new at: 1 put: 3; yourself.
  51. self deny: d1 = d2.
  52. d2 := Dictionary new at: 2 put: 2; yourself.
  53. self deny: d1 = d2.
  54. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  55. self deny: d1 = d2.
  56. !
  57. testDynamicDictionaries
  58. self assert: #{1 -> 'hello'. 2 -> 'world'} = (Dictionary with: 1 -> 'hello' with: 2 -> 'world')
  59. ! !
  60. TestCase subclass: #BooleanTest
  61. instanceVariableNames: ''
  62. category: 'Kernel-Tests'!
  63. !BooleanTest methodsFor: 'not yet classified'!
  64. testLogic
  65. "Trivial logic table"
  66. self assert: (true & true); deny: (true & false); deny: (false & true); deny: (false & false).
  67. self assert: (true | true); assert: (true | false); assert: (false | true); deny: (false | false).
  68. "Checking that expressions work fine too"
  69. self assert: (true & (1 > 0)); deny: ((1 > 0) & false); deny: ((1 > 0) & (1 > 2)).
  70. self assert: (false | (1 > 0)); assert: ((1 > 0) | false); assert: ((1 > 0) | (1 > 2))
  71. !
  72. testEquality
  73. "We're on top of JS...just be sure to check the basics!!"
  74. self deny: 0 = false.
  75. self deny: false = 0.
  76. self deny: '' = false.
  77. self deny: false = ''.
  78. self assert: true = true.
  79. self deny: false = true.
  80. self deny: true = false.
  81. self assert: false = false.
  82. "JS may do some type coercing after sending a message"
  83. self assert: true yourself = true.
  84. self assert: true yourself = true yourself
  85. !
  86. testLogicKeywords
  87. "Trivial logic table"
  88. self
  89. assert: (true and: [ true]);
  90. deny: (true and: [ false ]);
  91. deny: (false and: [ true ]);
  92. deny: (false and: [ false ]).
  93. self
  94. assert: (true or: [ true ]);
  95. assert: (true or: [ false ]);
  96. assert: (false or: [ true ]);
  97. deny: (false or: [ false ]).
  98. "Checking that expressions work fine too"
  99. self
  100. assert: (true and: [ 1 > 0 ]);
  101. deny: ((1 > 0) and: [ false ]);
  102. deny: ((1 > 0) and: [ 1 > 2 ]).
  103. self
  104. assert: (false or: [ 1 > 0 ]);
  105. assert: ((1 > 0) or: [ false ]);
  106. assert: ((1 > 0) or: [ 1 > 2 ])
  107. !
  108. testIfTrueIfFalse
  109. self assert: (true ifTrue: ['alternative block']) = 'alternative block'.
  110. self assert: (true ifFalse: ['alternative block']) = nil.
  111. self assert: (false ifTrue: ['alternative block']) = nil.
  112. self assert: (false ifFalse: ['alternative block']) = 'alternative block'.
  113. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block2'.
  114. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block'.
  115. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block'.
  116. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block2'.
  117. ! !
  118. TestCase subclass: #NumberTest
  119. instanceVariableNames: ''
  120. category: 'Kernel-Tests'!
  121. !NumberTest methodsFor: 'tests'!
  122. testPrintShowingDecimalPlaces
  123. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  124. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  125. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  126. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  127. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  128. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  129. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  130. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  131. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  132. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  133. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  134. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  135. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  136. !
  137. testEquality
  138. self assert: 1 = 1.
  139. self assert: 0 = 0.
  140. self deny: 1 = 0.
  141. self assert: 1 yourself = 1.
  142. self assert: 1 = 1 yourself.
  143. self assert: 1 yourself = 1 yourself.
  144. self deny: 0 = false.
  145. self deny: false = 0.
  146. self deny: '' = 0.
  147. self deny: 0 = ''
  148. !
  149. testArithmetic
  150. "We rely on JS here, so we won't test complex behavior, just check if
  151. message sends are corrects"
  152. self assert: 1.5 + 1 = 2.5.
  153. self assert: 2 - 1 = 1.
  154. self assert: -2 - 1 = -3.
  155. self assert: 12 / 2 = 6.
  156. self assert: 3 * 4 = 12.
  157. "Simple parenthesis and execution order"
  158. self assert: 1 + 2 * 3 = 9.
  159. self assert: 1 + (2 * 3) = 7
  160. !
  161. testRounded
  162. self assert: 3 rounded = 3.
  163. self assert: 3.212 rounded = 3.
  164. self assert: 3.51 rounded = 4
  165. !
  166. testNegated
  167. self assert: 3 negated = -3.
  168. self assert: -3 negated = 3
  169. !
  170. testComparison
  171. self assert: 3 > 2.
  172. self assert: 2 < 3.
  173. self deny: 3 < 2.
  174. self deny: 2 > 3.
  175. self assert: 3 >= 3.
  176. self assert: 3.1 >= 3.
  177. self assert: 3 <= 3.
  178. self assert: 3 <= 3.1
  179. !
  180. testTruncated
  181. self assert: 3 truncated = 3.
  182. self assert: 3.212 truncated = 3.
  183. self assert: 3.51 truncated = 3
  184. !
  185. testCopying
  186. self assert: 1 copy = 1.
  187. self assert: 1 deepCopy = 1
  188. ! !
  189. TestCase subclass: #JSObjectProxyTest
  190. instanceVariableNames: ''
  191. category: 'Kernel-Tests'!
  192. !JSObjectProxyTest methodsFor: 'tests'!
  193. testMethodWithArguments
  194. self deny: ('body' asJQuery hasClass: 'amber').
  195. 'body' asJQuery addClass: 'amber'.
  196. self assert: ('body' asJQuery hasClass: 'amber').
  197. 'body' asJQuery removeClass: 'amber'.
  198. self deny: ('body' asJQuery hasClass: 'amber').
  199. !
  200. testYourself
  201. |body|
  202. body := 'body' asJQuery
  203. addClass: 'amber';
  204. yourself.
  205. self assert: (body hasClass: 'amber').
  206. body removeClass: 'amber'.
  207. self deny: (body hasClass: 'amber').
  208. !
  209. testPropertyThatReturnsEmptyString
  210. <document.location.hash = ''>.
  211. self assert: '' equals: document location hash.
  212. document location hash: 'test'.
  213. self assert: '#test' equals: document location hash.
  214. ! !
  215. TestCase subclass: #PackageTest
  216. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  217. category: 'Kernel-Tests'!
  218. !PackageTest methodsFor: 'running'!
  219. setUp
  220. backUpCommitPathJs := Package defaultCommitPathJs.
  221. backUpCommitPathSt := Package defaultCommitPathSt.
  222. Package resetCommitPaths.
  223. zorkPackage := Package new name: 'Zork'.
  224. grulPackage := Package new
  225. name: 'Grul';
  226. commitPathJs: 'server/grul/js';
  227. commitPathSt: 'grul/st';
  228. yourself
  229. !
  230. tearDown
  231. Package
  232. defaultCommitPathJs: backUpCommitPathJs;
  233. defaultCommitPathSt: backUpCommitPathSt
  234. ! !
  235. !PackageTest methodsFor: 'tests'!
  236. testGrulCommitPathStShouldBeGrulSt
  237. self assert: 'grul/st' equals: grulPackage commitPathSt
  238. !
  239. testZorkCommitPathStShouldBeSt
  240. self assert: 'st' equals: zorkPackage commitPathSt
  241. !
  242. testZorkCommitPathJsShouldBeJs
  243. self assert: 'js' equals: zorkPackage commitPathJs
  244. !
  245. testGrulCommitPathJsShouldBeServerGrulJs
  246. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  247. ! !
  248. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  249. instanceVariableNames: ''
  250. category: 'Kernel-Tests'!
  251. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  252. setUp
  253. super setUp.
  254. Package
  255. defaultCommitPathJs: 'javascripts/';
  256. defaultCommitPathSt: 'smalltalk/'.
  257. ! !
  258. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  259. testGrulCommitPathJsShouldBeServerGrulJs
  260. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  261. !
  262. testGrulCommitPathStShouldBeGrulSt
  263. self assert: 'grul/st' equals: grulPackage commitPathSt
  264. !
  265. testZorkCommitPathJsShouldBeJavascript
  266. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  267. !
  268. testZorkCommitPathStShouldBeSmalltalk
  269. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  270. ! !
  271. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  272. shouldInheritSelectors
  273. ^ false
  274. ! !