Kernel-Tests.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. testIdentity
  166. self assert: true == true.
  167. self assert: true yourself == true.
  168. self assert: true == true yourself.
  169. self assert: true yourself == true yourself.
  170. self deny: true == false
  171. ! !
  172. TestCase subclass: #NumberTest
  173. instanceVariableNames: ''
  174. category: 'Kernel-Tests'!
  175. !NumberTest methodsFor: 'tests'!
  176. testPrintShowingDecimalPlaces
  177. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  178. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  179. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  180. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  181. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  182. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  183. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  184. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  185. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  186. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  187. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  188. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  189. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  190. !
  191. testEquality
  192. self assert: 1 = 1.
  193. self assert: 0 = 0.
  194. self deny: 1 = 0.
  195. self assert: 1 yourself = 1.
  196. self assert: 1 = 1 yourself.
  197. self assert: 1 yourself = 1 yourself.
  198. self deny: 0 = false.
  199. self deny: false = 0.
  200. self deny: '' = 0.
  201. self deny: 0 = ''
  202. !
  203. testArithmetic
  204. "We rely on JS here, so we won't test complex behavior, just check if
  205. message sends are corrects"
  206. self assert: 1.5 + 1 = 2.5.
  207. self assert: 2 - 1 = 1.
  208. self assert: -2 - 1 = -3.
  209. self assert: 12 / 2 = 6.
  210. self assert: 3 * 4 = 12.
  211. "Simple parenthesis and execution order"
  212. self assert: 1 + 2 * 3 = 9.
  213. self assert: 1 + (2 * 3) = 7
  214. !
  215. testRounded
  216. self assert: 3 rounded = 3.
  217. self assert: 3.212 rounded = 3.
  218. self assert: 3.51 rounded = 4
  219. !
  220. testNegated
  221. self assert: 3 negated = -3.
  222. self assert: -3 negated = 3
  223. !
  224. testComparison
  225. self assert: 3 > 2.
  226. self assert: 2 < 3.
  227. self deny: 3 < 2.
  228. self deny: 2 > 3.
  229. self assert: 3 >= 3.
  230. self assert: 3.1 >= 3.
  231. self assert: 3 <= 3.
  232. self assert: 3 <= 3.1
  233. !
  234. testTruncated
  235. self assert: 3 truncated = 3.
  236. self assert: 3.212 truncated = 3.
  237. self assert: 3.51 truncated = 3
  238. !
  239. testCopying
  240. self assert: 1 copy == 1.
  241. self assert: 1 deepCopy == 1
  242. !
  243. testMinMax
  244. self assert: (2 max: 5) equals: 5.
  245. self assert: (2 min: 5) equals: 2
  246. !
  247. testIdentity
  248. self assert: 1 == 1.
  249. self assert: 0 == 0.
  250. self deny: 1 == 0.
  251. self assert: 1 yourself == 1.
  252. self assert: 1 == 1 yourself.
  253. self assert: 1 yourself == 1 yourself.
  254. self deny: 1 == 2
  255. !
  256. testSqrt
  257. self assert: 4 sqrt = 2.
  258. self assert: 16 sqrt = 4
  259. !
  260. testSquared
  261. self assert: 4 squared = 16
  262. !
  263. testTimesRepeat
  264. | i |
  265. i := 0.
  266. 0 timesRepeat: [i := i + 1].
  267. self assert: i equals: 0.
  268. 5 timesRepeat: [i := i + 1].
  269. self assert: i equals: 5
  270. !
  271. testTo
  272. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  273. !
  274. testToBy
  275. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  276. self should: [1 to: 4 by: 0] raise: Error
  277. ! !
  278. TestCase subclass: #JSObjectProxyTest
  279. instanceVariableNames: ''
  280. category: 'Kernel-Tests'!
  281. !JSObjectProxyTest methodsFor: 'tests'!
  282. testMethodWithArguments
  283. self deny: ('body' asJQuery hasClass: 'amber').
  284. 'body' asJQuery addClass: 'amber'.
  285. self assert: ('body' asJQuery hasClass: 'amber').
  286. 'body' asJQuery removeClass: 'amber'.
  287. self deny: ('body' asJQuery hasClass: 'amber').
  288. !
  289. testYourself
  290. |body|
  291. body := 'body' asJQuery
  292. addClass: 'amber';
  293. yourself.
  294. self assert: (body hasClass: 'amber').
  295. body removeClass: 'amber'.
  296. self deny: (body hasClass: 'amber').
  297. !
  298. testPropertyThatReturnsEmptyString
  299. <document.location.hash = ''>.
  300. self assert: '' equals: document location hash.
  301. document location hash: 'test'.
  302. self assert: '#test' equals: document location hash.
  303. ! !
  304. TestCase subclass: #PackageTest
  305. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  306. category: 'Kernel-Tests'!
  307. !PackageTest methodsFor: 'running'!
  308. setUp
  309. backUpCommitPathJs := Package defaultCommitPathJs.
  310. backUpCommitPathSt := Package defaultCommitPathSt.
  311. Package resetCommitPaths.
  312. zorkPackage := Package new name: 'Zork'.
  313. grulPackage := Package new
  314. name: 'Grul';
  315. commitPathJs: 'server/grul/js';
  316. commitPathSt: 'grul/st';
  317. yourself
  318. !
  319. tearDown
  320. Package
  321. defaultCommitPathJs: backUpCommitPathJs;
  322. defaultCommitPathSt: backUpCommitPathSt
  323. ! !
  324. !PackageTest methodsFor: 'tests'!
  325. testGrulCommitPathStShouldBeGrulSt
  326. self assert: 'grul/st' equals: grulPackage commitPathSt
  327. !
  328. testZorkCommitPathStShouldBeSt
  329. self assert: 'st' equals: zorkPackage commitPathSt
  330. !
  331. testZorkCommitPathJsShouldBeJs
  332. self assert: 'js' equals: zorkPackage commitPathJs
  333. !
  334. testGrulCommitPathJsShouldBeServerGrulJs
  335. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  336. ! !
  337. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  338. instanceVariableNames: ''
  339. category: 'Kernel-Tests'!
  340. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  341. setUp
  342. super setUp.
  343. Package
  344. defaultCommitPathJs: 'javascripts/';
  345. defaultCommitPathSt: 'smalltalk/'.
  346. ! !
  347. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  348. testGrulCommitPathJsShouldBeServerGrulJs
  349. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  350. !
  351. testGrulCommitPathStShouldBeGrulSt
  352. self assert: 'grul/st' equals: grulPackage commitPathSt
  353. !
  354. testZorkCommitPathJsShouldBeJavascript
  355. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  356. !
  357. testZorkCommitPathStShouldBeSmalltalk
  358. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  359. ! !
  360. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  361. shouldInheritSelectors
  362. ^ false
  363. ! !
  364. TestCase subclass: #BlockClosureTest
  365. instanceVariableNames: ''
  366. category: 'Kernel-Tests'!
  367. !BlockClosureTest methodsFor: 'tests'!
  368. testValue
  369. self assert: ([1+1] value) equals: 2.
  370. self assert: ([:x | x +1] value: 2) equals: 3.
  371. self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
  372. "Arguments are optional in Amber. This isn't ANSI compliant."
  373. self assert: ([:a :b :c | 1] value) equals: 1
  374. !
  375. testOnDo
  376. self assert: ([Error new signal] on: Error do: [:ex | true])
  377. !
  378. testEnsure
  379. self assert: ([Error new] ensure: [true])
  380. !
  381. testNumArgs
  382. self assert: [] numArgs equals: 0.
  383. self assert: [:a :b | ] numArgs equals: 2
  384. !
  385. testValueWithPossibleArguments
  386. self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
  387. self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
  388. self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  389. !
  390. testWhileTrue
  391. | i |
  392. i := 0.
  393. [i < 5] whileTrue: [i := i + 1].
  394. self assert: i equals: 5.
  395. i := 0.
  396. [i := i + 1. i < 5] whileTrue.
  397. self assert: i equals: 5
  398. !
  399. testWhileFalse
  400. | i |
  401. i := 0.
  402. [i > 5] whileFalse: [i := i + 1].
  403. self assert: i equals: 6.
  404. i := 0.
  405. [i := i + 1. i > 5] whileFalse.
  406. self assert: i equals: 6
  407. !
  408. testCompiledSource
  409. self assert: [1+1] compiledSource equals: 'function (){return (1) + (1);}'
  410. ! !
  411. TestCase subclass: #ObjectTest
  412. instanceVariableNames: ''
  413. category: 'Kernel-Tests'!
  414. !ObjectTest methodsFor: 'tests'!
  415. testEquality
  416. | o |
  417. o := Object new.
  418. self deny: o = Object new.
  419. self assert: o = o.
  420. self assert: o yourself = o.
  421. self assert: o = o yourself
  422. !
  423. testIdentity
  424. | o |
  425. o := Object new.
  426. self deny: o == Object new.
  427. self assert: o == o
  428. ! !