1
0

Kernel-Tests.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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: 'tests'!
  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. testMinMax
  237. self assert: (2 max: 5) equals: 5.
  238. self assert: (2 min: 5) equals: 2
  239. !
  240. testIdentity
  241. self assert: 1 == 1.
  242. self assert: 0 == 0.
  243. self deny: 1 == 0.
  244. self assert: 1 yourself == 1.
  245. self assert: 1 == 1 yourself.
  246. self assert: 1 yourself == 1 yourself.
  247. self deny: 1 == 2
  248. !
  249. testSqrt
  250. self assert: 4 sqrt = 2.
  251. self assert: 16 sqrt = 4
  252. !
  253. testSquared
  254. self assert: 4 squared = 16
  255. !
  256. testTimesRepeat
  257. | i |
  258. i := 0.
  259. 0 timesRepeat: [i := i + 1].
  260. self assert: i equals: 0.
  261. 5 timesRepeat: [i := i + 1].
  262. self assert: i equals: 5
  263. !
  264. testTo
  265. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  266. !
  267. testToBy
  268. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  269. self should: [1 to: 4 by: 0] raise: Error
  270. ! !
  271. TestCase subclass: #JSObjectProxyTest
  272. instanceVariableNames: ''
  273. category: 'Kernel-Tests'!
  274. !JSObjectProxyTest methodsFor: 'tests'!
  275. testMethodWithArguments
  276. self deny: ('body' asJQuery hasClass: 'amber').
  277. 'body' asJQuery addClass: 'amber'.
  278. self assert: ('body' asJQuery hasClass: 'amber').
  279. 'body' asJQuery removeClass: 'amber'.
  280. self deny: ('body' asJQuery hasClass: 'amber').
  281. !
  282. testYourself
  283. |body|
  284. body := 'body' asJQuery
  285. addClass: 'amber';
  286. yourself.
  287. self assert: (body hasClass: 'amber').
  288. body removeClass: 'amber'.
  289. self deny: (body hasClass: 'amber').
  290. !
  291. testPropertyThatReturnsEmptyString
  292. <document.location.hash = ''>.
  293. self assert: '' equals: document location hash.
  294. document location hash: 'test'.
  295. self assert: '#test' equals: document location hash.
  296. ! !
  297. TestCase subclass: #PackageTest
  298. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  299. category: 'Kernel-Tests'!
  300. !PackageTest methodsFor: 'running'!
  301. setUp
  302. backUpCommitPathJs := Package defaultCommitPathJs.
  303. backUpCommitPathSt := Package defaultCommitPathSt.
  304. Package resetCommitPaths.
  305. zorkPackage := Package new name: 'Zork'.
  306. grulPackage := Package new
  307. name: 'Grul';
  308. commitPathJs: 'server/grul/js';
  309. commitPathSt: 'grul/st';
  310. yourself
  311. !
  312. tearDown
  313. Package
  314. defaultCommitPathJs: backUpCommitPathJs;
  315. defaultCommitPathSt: backUpCommitPathSt
  316. ! !
  317. !PackageTest methodsFor: 'tests'!
  318. testGrulCommitPathStShouldBeGrulSt
  319. self assert: 'grul/st' equals: grulPackage commitPathSt
  320. !
  321. testZorkCommitPathStShouldBeSt
  322. self assert: 'st' equals: zorkPackage commitPathSt
  323. !
  324. testZorkCommitPathJsShouldBeJs
  325. self assert: 'js' equals: zorkPackage commitPathJs
  326. !
  327. testGrulCommitPathJsShouldBeServerGrulJs
  328. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  329. ! !
  330. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  331. instanceVariableNames: ''
  332. category: 'Kernel-Tests'!
  333. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  334. setUp
  335. super setUp.
  336. Package
  337. defaultCommitPathJs: 'javascripts/';
  338. defaultCommitPathSt: 'smalltalk/'.
  339. ! !
  340. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  341. testGrulCommitPathJsShouldBeServerGrulJs
  342. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  343. !
  344. testGrulCommitPathStShouldBeGrulSt
  345. self assert: 'grul/st' equals: grulPackage commitPathSt
  346. !
  347. testZorkCommitPathJsShouldBeJavascript
  348. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  349. !
  350. testZorkCommitPathStShouldBeSmalltalk
  351. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  352. ! !
  353. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  354. shouldInheritSelectors
  355. ^ false
  356. ! !
  357. TestCase subclass: #BlockClosureTest
  358. instanceVariableNames: ''
  359. category: 'Kernel-Tests'!
  360. !BlockClosureTest methodsFor: 'tests'!
  361. testValue
  362. self assert: ([1+1] value) equals: 2.
  363. self assert: ([:x | x +1] value: 2) equals: 3.
  364. self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
  365. "Arguments are optional in Amber. This isn't ANSI compliant."
  366. self assert: ([:a :b :c | 1] value) equals: 1
  367. !
  368. testOnDo
  369. self assert: ([Error new signal] on: Error do: [:ex | true])
  370. !
  371. testEnsure
  372. self assert: ([Error new] ensure: [true])
  373. !
  374. testNumArgs
  375. self assert: [] numArgs equals: 0.
  376. self assert: [:a :b | ] numArgs equals: 2
  377. !
  378. testValueWithPossibleArguments
  379. self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
  380. self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
  381. self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  382. !
  383. testWhileTrue
  384. | i |
  385. i := 0.
  386. [i < 5] whileTrue: [i := i + 1].
  387. self assert: i equals: 5.
  388. i := 0.
  389. [i := i + 1. i < 5] whileTrue.
  390. self assert: i equals: 5
  391. !
  392. testWhileFalse
  393. | i |
  394. i := 0.
  395. [i > 5] whileFalse: [i := i + 1].
  396. self assert: i equals: 6.
  397. i := 0.
  398. [i := i + 1. i > 5] whileFalse.
  399. self assert: i equals: 6
  400. !
  401. testCompiledSource
  402. self assert: [1+1] compiledSource equals: 'function (){return (1) + (1);}'
  403. ! !
  404. TestCase subclass: #ObjectTest
  405. instanceVariableNames: ''
  406. category: 'Kernel-Tests'!
  407. !ObjectTest methodsFor: 'tests'!
  408. testEquality
  409. | o |
  410. o := Object new.
  411. self deny: o = Object new.
  412. self assert: o = o.
  413. self assert: o yourself = o.
  414. self assert: o = o yourself
  415. !
  416. testIdentity
  417. | o |
  418. o := Object new.
  419. self deny: o == Object new.
  420. self assert: o == o
  421. ! !