Kernel-Tests.st 9.9 KB

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