Kernel-Tests.st 9.7 KB

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