Kernel-Tests.st 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. Smalltalk current createPackage: 'Kernel-Tests'!
  2. TestCase subclass: #BlockClosureTest
  3. instanceVariableNames: ''
  4. package: 'Kernel-Tests'!
  5. !BlockClosureTest methodsFor: 'tests'!
  6. testCanClearInterval
  7. self shouldnt: [([Error new signal] valueWithInterval: 0) clearInterval] raise: Error
  8. !
  9. testCanClearTimeout
  10. self shouldnt: [([Error new signal] valueWithTimeout: 0) clearTimeout] raise: Error
  11. !
  12. testCompiledSource
  13. self assert: ([1+1] compiledSource includesSubString: 'function')
  14. !
  15. testCurrySelf
  16. | curriedMethod array |
  17. curriedMethod := [ :selfarg :x | selfarg at: x ] currySelf asCompiledMethod: 'foo:'.
  18. array := #(3 1 4).
  19. ClassBuilder new installMethod: curriedMethod forClass: Array category: '**test helper'.
  20. [ self assert: (array foo: 2) equals: 1 ]
  21. ensure: [ Array removeCompiledMethod: curriedMethod ]
  22. !
  23. testEnsure
  24. self assert: ([3] ensure: [4]) equals: 3
  25. !
  26. testEnsureRaises
  27. self should: [[Error new signal] ensure: [true]] raise: Error
  28. !
  29. testExceptionSemantics
  30. "See https://github.com/NicolasPetton/amber/issues/314"
  31. self timeout: 100.
  32. (self async: [
  33. [
  34. self assert: true.
  35. Error signal.
  36. "The following should *not* be run"
  37. self deny: true.
  38. self finished.
  39. ] on: Error do: [ :ex | self finished ]
  40. ]) valueWithTimeout: 0
  41. !
  42. testNumArgs
  43. self assert: [] numArgs equals: 0.
  44. self assert: [:a :b | ] numArgs equals: 2
  45. !
  46. testOnDo
  47. self assert: ([Error new signal] on: Error do: [:ex | true])
  48. !
  49. testValue
  50. self assert: ([1+1] value) equals: 2.
  51. self assert: ([:x | x +1] value: 2) equals: 3.
  52. self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
  53. "Arguments are optional in Amber. This isn't ANSI compliant."
  54. self assert: ([:a :b :c | 1] value) equals: 1
  55. !
  56. testValueWithPossibleArguments
  57. self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
  58. self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
  59. self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  60. !
  61. testWhileFalse
  62. | i |
  63. i := 0.
  64. [i > 5] whileFalse: [i := i + 1].
  65. self assert: i equals: 6.
  66. i := 0.
  67. [i := i + 1. i > 5] whileFalse.
  68. self assert: i equals: 6
  69. !
  70. testWhileTrue
  71. | i |
  72. i := 0.
  73. [i < 5] whileTrue: [i := i + 1].
  74. self assert: i equals: 5.
  75. i := 0.
  76. [i := i + 1. i < 5] whileTrue.
  77. self assert: i equals: 5
  78. ! !
  79. TestCase subclass: #BooleanTest
  80. instanceVariableNames: ''
  81. package: 'Kernel-Tests'!
  82. !BooleanTest methodsFor: 'tests'!
  83. testEquality
  84. "We're on top of JS...just be sure to check the basics!!"
  85. self deny: 0 = false.
  86. self deny: false = 0.
  87. self deny: '' = false.
  88. self deny: false = ''.
  89. self assert: (true = true).
  90. self deny: false = true.
  91. self deny: true = false.
  92. self assert: (false = false).
  93. "JS may do some type coercing after sending a message"
  94. self assert: (true yourself = true).
  95. self assert: (true yourself = true yourself)
  96. !
  97. testIdentity
  98. "We're on top of JS...just be sure to check the basics!!"
  99. self deny: 0 == false.
  100. self deny: false == 0.
  101. self deny: '' == false.
  102. self deny: false == ''.
  103. self assert: true == true.
  104. self deny: false == true.
  105. self deny: true == false.
  106. self assert: false == false.
  107. "JS may do some type coercing after sending a message"
  108. self assert: true yourself == true.
  109. self assert: true yourself == true yourself
  110. !
  111. testIfTrueIfFalse
  112. self assert: (true ifTrue: ['alternative block']) equals: 'alternative block'.
  113. self assert: (true ifFalse: ['alternative block']) equals: nil.
  114. self assert: (false ifTrue: ['alternative block']) equals: nil.
  115. self assert: (false ifFalse: ['alternative block']) equals: 'alternative block'.
  116. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block2'.
  117. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block'.
  118. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block'.
  119. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block2'.
  120. !
  121. testIfTrueIfFalseWithBoxing
  122. self assert: (true yourself ifTrue: ['alternative block']) equals: 'alternative block'.
  123. self assert: (true yourself ifFalse: ['alternative block']) equals: nil.
  124. self assert: (false yourself ifTrue: ['alternative block']) equals: nil.
  125. self assert: (false yourself ifFalse: ['alternative block']) equals: 'alternative block'.
  126. self assert: (false yourself ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block2'.
  127. self assert: (false yourself ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block'.
  128. self assert: (true yourself ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block'.
  129. self assert: (true yourself ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block2'.
  130. !
  131. testLogic
  132. "Trivial logic table"
  133. self assert: (true & true);
  134. deny: (true & false);
  135. deny: (false & true);
  136. deny: (false & false).
  137. self assert: (true | true);
  138. assert: (true | false);
  139. assert: (false | true);
  140. deny: (false | false).
  141. "Checking that expressions work fine too"
  142. self assert: (true & (1 > 0));
  143. deny: ((1 > 0) & false);
  144. deny: ((1 > 0) & (1 > 2)).
  145. self assert: (false | (1 > 0));
  146. assert: ((1 > 0) | false);
  147. assert: ((1 > 0) | (1 > 2))
  148. !
  149. testLogicKeywords
  150. "Trivial logic table"
  151. self
  152. assert: (true and: [ true]);
  153. deny: (true and: [ false ]);
  154. deny: (false and: [ true ]);
  155. deny: (false and: [ false ]).
  156. self
  157. assert: (true or: [ true ]);
  158. assert: (true or: [ false ]);
  159. assert: (false or: [ true ]);
  160. deny: (false or: [ false ]).
  161. "Checking that expressions work fine too"
  162. self
  163. assert: (true and: [ 1 > 0 ]);
  164. deny: ((1 > 0) and: [ false ]);
  165. deny: ((1 > 0) and: [ 1 > 2 ]).
  166. self
  167. assert: (false or: [ 1 > 0 ]);
  168. assert: ((1 > 0) or: [ false ]);
  169. assert: ((1 > 0) or: [ 1 > 2 ])
  170. !
  171. testNonBooleanError
  172. self should: [ '' ifTrue: [] ifFalse: [] ] raise: NonBooleanReceiver
  173. ! !
  174. TestCase subclass: #ClassBuilderTest
  175. instanceVariableNames: 'builder theClass'
  176. package: 'Kernel-Tests'!
  177. !ClassBuilderTest methodsFor: 'running'!
  178. setUp
  179. builder := ClassBuilder new
  180. !
  181. tearDown
  182. theClass ifNotNil: [Smalltalk current removeClass: theClass. theClass := nil]
  183. ! !
  184. !ClassBuilderTest methodsFor: 'tests'!
  185. testClassCopy
  186. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  187. self assert: theClass superclass == ObjectMock superclass.
  188. self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
  189. self assert: theClass name equals: 'ObjectMock2'.
  190. self assert: theClass package == ObjectMock package.
  191. self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
  192. !
  193. testClassMigration
  194. | instance oldClass |
  195. oldClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  196. instance := (Smalltalk current at: 'ObjectMock2') new.
  197. "Change the superclass of ObjectMock2"
  198. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  199. instanceVariableNames: ''
  200. package: 'Kernel-Tests'.
  201. self deny: oldClass == ObjectMock2.
  202. self assert: ObjectMock2 superclass == ObjectMock.
  203. self assert: ObjectMock2 instanceVariableNames isEmpty.
  204. self assert: ObjectMock2 selectors equals: oldClass selectors.
  205. self assert: ObjectMock2 comment equals: oldClass comment.
  206. self assert: ObjectMock2 package name equals: 'Kernel-Tests'.
  207. self deny: instance class == ObjectMock2.
  208. "Commeting this out. Tests implementation detail."
  209. "self assert: instance class name equals: 'OldObjectMock2'."
  210. self assert: (Smalltalk current at: instance class name) isNil.
  211. Smalltalk current removeClass: ObjectMock2
  212. !
  213. testClassMigrationWithClassInstanceVariables
  214. builder copyClass: ObjectMock named: 'ObjectMock2'.
  215. ObjectMock2 class instanceVariableNames: 'foo bar'.
  216. "Change the superclass of ObjectMock2"
  217. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  218. instanceVariableNames: ''
  219. package: 'Kernel-Tests'.
  220. self assert: ObjectMock2 class instanceVariableNames equals: #('foo' 'bar').
  221. Smalltalk current removeClass: ObjectMock2
  222. !
  223. testClassMigrationWithSubclasses
  224. builder copyClass: ObjectMock named: 'ObjectMock2'.
  225. ObjectMock2 subclass: 'ObjectMock3' instanceVariableNames: '' package: 'Kernel-Tests'.
  226. ObjectMock3 subclass: 'ObjectMock4' instanceVariableNames: '' package: 'Kernel-Tests'.
  227. "Change the superclass of ObjectMock2"
  228. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  229. instanceVariableNames: ''
  230. package: 'Kernel-Tests'.
  231. self assert: (ObjectMock subclasses includes: ObjectMock2).
  232. self assert: (ObjectMock2 subclasses includes: ObjectMock3).
  233. self assert: (ObjectMock3 subclasses includes: ObjectMock4).
  234. ObjectMock allSubclasses do: [ :each | Smalltalk current removeClass: each ]
  235. !
  236. testInstanceVariableNames
  237. self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
  238. ! !
  239. TestCase subclass: #CollectionTest
  240. instanceVariableNames: ''
  241. package: 'Kernel-Tests'!
  242. !CollectionTest methodsFor: 'accessing'!
  243. collection
  244. ^ self collectionClass withAll: self defaultValues
  245. !
  246. collectionClass
  247. ^ self class collectionClass
  248. !
  249. collectionWithDuplicates
  250. ^ self collectionClass withAll: #('a' 'b' 'c' 1 2 1 'a')
  251. !
  252. defaultValues
  253. ^ #(1 2 3 -4)
  254. ! !
  255. !CollectionTest methodsFor: 'convenience'!
  256. assertSameContents: aCollection as: anotherCollection
  257. self assert: (aCollection size = anotherCollection size).
  258. aCollection do: [ :each |
  259. self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
  260. ! !
  261. !CollectionTest methodsFor: 'testing'!
  262. isCollectionReadOnly
  263. ^ false
  264. ! !
  265. !CollectionTest methodsFor: 'tests'!
  266. testAsArray
  267. self
  268. assertSameContents: self collection
  269. as: self collection asArray
  270. !
  271. testAsOrderedCollection
  272. self
  273. assertSameContents: self collection
  274. as: self collection asOrderedCollection
  275. !
  276. testAsSet
  277. | c set |
  278. c := self collectionWithDuplicates.
  279. set := c asSet.
  280. self assert: set size equals: 5.
  281. c do: [ :each |
  282. self assert: (set includes: each) ]
  283. !
  284. testCollect
  285. | newCollection |
  286. newCollection := #(1 2 3 4).
  287. self
  288. assertSameContents: (self collection collect: [ :each |
  289. each abs ])
  290. as: newCollection
  291. !
  292. testDetect
  293. self assert: (self collection detect: [ :each | each < 0 ]) equals: -4.
  294. self
  295. should: [ self collection detect: [ :each | each = 6 ] ]
  296. raise: Error
  297. !
  298. testDo
  299. | newCollection |
  300. newCollection := OrderedCollection new.
  301. self collection do: [ :each |
  302. newCollection add: each ].
  303. self
  304. assertSameContents: self collection
  305. as: newCollection
  306. !
  307. testIsEmpty
  308. self assert: self collectionClass new isEmpty.
  309. self deny: self collection isEmpty
  310. !
  311. testSelect
  312. | newCollection |
  313. newCollection := #(2 -4).
  314. self
  315. assertSameContents: (self collection select: [ :each |
  316. each even ])
  317. as: newCollection
  318. !
  319. testSize
  320. self assert: self collectionClass new size equals: 0.
  321. self assert: self collection size equals: 4
  322. ! !
  323. !CollectionTest class methodsFor: 'accessing'!
  324. collectionClass
  325. ^ nil
  326. ! !
  327. !CollectionTest class methodsFor: 'testing'!
  328. isAbstract
  329. ^ self collectionClass isNil
  330. ! !
  331. CollectionTest subclass: #IndexableCollectionTest
  332. instanceVariableNames: ''
  333. package: 'Kernel-Tests'!
  334. !IndexableCollectionTest methodsFor: 'tests'!
  335. testAt
  336. self assert: (self collection at: 4) equals: -4.
  337. self should: [ self collection at: 5 ] raise: Error
  338. !
  339. testAtIfAbsent
  340. self assert: (self collection at: (self collection size + 1) ifAbsent: [ 'none' ]) equals: 'none'
  341. !
  342. testIndexOf
  343. self assert: (self collection indexOf: 2) equals: 2.
  344. self should: [ self collection indexOf: 999 ] raise: Error.
  345. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  346. !
  347. testWithIndexDo
  348. | collection |
  349. collection := self collection.
  350. self collection withIndexDo: [ :each :index |
  351. self assert: (collection at: index) equals: each ]
  352. ! !
  353. IndexableCollectionTest subclass: #HashedCollectionTest
  354. instanceVariableNames: ''
  355. package: 'Kernel-Tests'!
  356. !HashedCollectionTest methodsFor: 'accessing'!
  357. collection
  358. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  359. !
  360. collectionWithDuplicates
  361. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
  362. ! !
  363. !HashedCollectionTest methodsFor: 'tests'!
  364. testAt
  365. self assert: (self collection at: 'a') equals: 2.
  366. self should: [ self collection at: 5 ] raise: Error
  367. !
  368. testIndexOf
  369. self assert: (self collection indexOf: 2) equals: 'a'.
  370. self should: [ self collection indexOf: 999 ] raise: Error.
  371. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  372. ! !
  373. !HashedCollectionTest class methodsFor: 'accessing'!
  374. collectionClass
  375. ^ HashedCollection
  376. ! !
  377. HashedCollectionTest subclass: #DictionaryTest
  378. instanceVariableNames: ''
  379. package: 'Kernel-Tests'!
  380. !DictionaryTest methodsFor: 'accessing'!
  381. collection
  382. ^ Dictionary new
  383. at: 1 put: 1;
  384. at: 'a' put: 2;
  385. at: true put: 3;
  386. at: 4 put: -4;
  387. yourself
  388. !
  389. collectionWithDuplicates
  390. ^ Dictionary new
  391. at: 1 put: 1;
  392. at: 'a' put: 2;
  393. at: true put: 3;
  394. at: 4 put: -4;
  395. at: 'b' put: 1;
  396. at: 3 put: 3;
  397. at: false put: 12;
  398. yourself
  399. ! !
  400. !DictionaryTest methodsFor: 'tests'!
  401. testAccessing
  402. | d |
  403. d := Dictionary new.
  404. d at: 'hello' put: 'world'.
  405. self assert: (d at: 'hello') equals: 'world'.
  406. self assert: (d at: 'hello' ifAbsent: [nil]) equals: 'world'.
  407. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  408. self assert: (d includesKey: 'hello').
  409. self deny: (d includesKey: 'foo').
  410. d at: 1 put: 2.
  411. self assert: (d at: 1) equals: 2.
  412. d at: 1@3 put: 3.
  413. self assert: (d at: 1@3) equals: 3.
  414. self assert: (d includesKey: 1@3).
  415. self deny: (d includesKey: 3@1)
  416. !
  417. testDynamicDictionaries
  418. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  419. !
  420. testEquality
  421. | d1 d2 |
  422. self assert: (Dictionary new = Dictionary new).
  423. d1 := Dictionary new at: 1 put: 2; yourself.
  424. d2 := Dictionary new at: 1 put: 2; yourself.
  425. self assert: (d1 = d2).
  426. d2 := Dictionary new at: 1 put: 3; yourself.
  427. self deny: d1 = d2.
  428. d2 := Dictionary new at: 2 put: 2; yourself.
  429. self deny: d1 = d2.
  430. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  431. self deny: d1 = d2.
  432. !
  433. testIfAbsent
  434. | d visited |
  435. visited := false.
  436. d := Dictionary new.
  437. d at: 'hello' ifAbsent: [ visited := true ].
  438. self assert: visited.
  439. !
  440. testIfPresent
  441. | d visited absent |
  442. visited := false.
  443. d := Dictionary new.
  444. d at: 'hello' put: 'world'.
  445. d at: 'hello' ifPresent: [ :value | visited := value ].
  446. self assert: visited equals: 'world'.
  447. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  448. self assert: absent isNil.
  449. !
  450. testIfPresentIfAbsent
  451. | d visited |
  452. visited := false.
  453. d := Dictionary new.
  454. d at: 'hello' put: 'world'.
  455. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  456. self assert: visited equals: 'world'.
  457. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  458. self assert: visited.
  459. !
  460. testKeys
  461. | d |
  462. d := Dictionary new.
  463. d at: 1 put: 2.
  464. d at: 2 put: 3.
  465. d at: 3 put: 4.
  466. self assert: d keys equals: #(1 2 3)
  467. !
  468. testPointKey
  469. | d |
  470. d := Dictionary new.
  471. d at: 1@1 put: 'foo'.
  472. self assert: (d at: 1@1) equals: 'foo'.
  473. d at: 1@1 put: 'bar'.
  474. self assert: (d at: 1@1) equals: 'bar'.
  475. d removeKey: 1@1.
  476. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  477. self deny: (d includesKey: 1@1)
  478. !
  479. testPrintString
  480. self
  481. assert: (Dictionary new
  482. at:'firstname' put: 'James';
  483. at:'lastname' put: 'Bond';
  484. printString)
  485. equals: 'a Dictionary (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  486. !
  487. testRemoveKey
  488. | d key |
  489. d := Dictionary new.
  490. d at: 1 put: 2.
  491. d at: 2 put: 3.
  492. d at: 3 put: 4.
  493. key := 2.
  494. self assert: d keys equals: #(1 2 3).
  495. d removeKey: key.
  496. self assert: d keys equals: #(1 3).
  497. self assert: d values equals: #(2 4).
  498. self deny: (d includesKey: 2)
  499. !
  500. testRemoveKeyIfAbsent
  501. | d key |
  502. d := Dictionary new.
  503. d at: 1 put: 2.
  504. d at: 2 put: 3.
  505. d at: 3 put: 4.
  506. key := 2.
  507. self assert: (d removeKey: key) equals: 3.
  508. key := 3.
  509. self assert: (d removeKey: key ifAbsent: [42]) equals: 4.
  510. key := 'why'.
  511. self assert: (d removeKey: key ifAbsent: [42] ) equals: 42.
  512. !
  513. testSize
  514. | d |
  515. d := Dictionary new.
  516. self assert: d size equals: 0.
  517. d at: 1 put: 2.
  518. self assert: d size equals: 1.
  519. d at: 2 put: 3.
  520. self assert: d size equals: 2.
  521. !
  522. testValues
  523. | d |
  524. d := Dictionary new.
  525. d at: 1 put: 2.
  526. d at: 2 put: 3.
  527. d at: 3 put: 4.
  528. self assert: d values equals: #(2 3 4)
  529. ! !
  530. !DictionaryTest class methodsFor: 'accessing'!
  531. collectionClass
  532. ^ Dictionary
  533. ! !
  534. IndexableCollectionTest subclass: #SequenceableCollectionTest
  535. instanceVariableNames: ''
  536. package: 'Kernel-Tests'!
  537. SequenceableCollectionTest subclass: #ArrayTest
  538. instanceVariableNames: ''
  539. package: 'Kernel-Tests'!
  540. !ArrayTest methodsFor: 'tests'!
  541. testAtIfAbsent
  542. | array |
  543. array := #('hello' 'world').
  544. self assert: (array at: 1) equals: 'hello'.
  545. self assert: (array at: 2) equals: 'world'.
  546. self assert: (array at: 2 ifAbsent: ['not found']) equals: 'world'.
  547. self assert: (array at: 0 ifAbsent: ['not found']) equals: 'not found'.
  548. self assert: (array at: -10 ifAbsent: ['not found']) equals: 'not found'.
  549. self assert: (array at: 3 ifAbsent: ['not found']) equals: 'not found'.
  550. !
  551. testFirstN
  552. self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
  553. !
  554. testIfEmpty
  555. self assert: ( '' ifEmpty: ['zork'] ) equals: 'zork'
  556. !
  557. testPrintString
  558. | array |
  559. array := Array new.
  560. self assert: array printString equals: 'an Array ()'.
  561. array add: 1; add: 3.
  562. self assert: array printString equals: 'an Array (1 3)'.
  563. array add: 'foo'.
  564. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  565. array remove: 1; remove: 3.
  566. self assert: array printString equals: 'an Array (''foo'')'.
  567. array addLast: 3.
  568. self assert: array printString equals: 'an Array (''foo'' 3)'.
  569. array addLast: 3.
  570. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  571. ! !
  572. !ArrayTest class methodsFor: 'accessing'!
  573. collectionClass
  574. ^ Array
  575. ! !
  576. SequenceableCollectionTest subclass: #StringTest
  577. instanceVariableNames: ''
  578. package: 'Kernel-Tests'!
  579. !StringTest methodsFor: 'accessing'!
  580. collection
  581. ^'hello'
  582. !
  583. collectionWithDuplicates
  584. ^ 'abbaerte'
  585. ! !
  586. !StringTest methodsFor: 'tests'!
  587. testAddRemove
  588. self should: ['hello' add: 'a'] raise: Error.
  589. self should: ['hello' remove: 'h'] raise: Error
  590. !
  591. testAsArray
  592. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  593. !
  594. testAt
  595. self assert: ('hello' at: 1) equals: 'h'.
  596. self assert: ('hello' at: 5) equals: 'o'.
  597. self assert: ('hello' at: 6 ifAbsent: [nil]) equals: nil
  598. !
  599. testAtPut
  600. "String instances are read-only"
  601. self should: ['hello' at: 1 put: 'a'] raise: Error
  602. !
  603. testCollect
  604. | newCollection |
  605. newCollection := 'hheelllloo'.
  606. self
  607. assertSameContents: (self collection collect: [ :each |
  608. each, each ])
  609. as: newCollection
  610. !
  611. testCopyWithoutAll
  612. self
  613. assert: ('*hello* *world*' copyWithoutAll: '*')
  614. equals: 'hello world'
  615. !
  616. testDetect
  617. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  618. self
  619. should: [ self collection detect: [ :each | each = 6 ] ]
  620. raise: Error
  621. !
  622. testEquality
  623. self assert: 'hello' equals: 'hello'.
  624. self deny: 'hello' = 'world'.
  625. self assert: 'hello' equals: 'hello' yourself.
  626. self assert: 'hello' yourself equals: 'hello'.
  627. "test JS falsy value"
  628. self deny: '' = 0
  629. !
  630. testIdentity
  631. self assert: 'hello' == 'hello'.
  632. self deny: 'hello' == 'world'.
  633. self assert: 'hello' == 'hello' yourself.
  634. self assert: 'hello' yourself == 'hello'.
  635. "test JS falsy value"
  636. self deny: '' == 0
  637. !
  638. testIncludesSubString
  639. self assert: ('amber' includesSubString: 'ber').
  640. self deny: ('amber' includesSubString: 'zork').
  641. !
  642. testIndexOf
  643. self assert: (self collection indexOf: 'e') equals: 2.
  644. self should: [ self collection indexOf: 999 ] raise: Error.
  645. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  646. !
  647. testJoin
  648. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  649. !
  650. testSelect
  651. | newCollection |
  652. newCollection := 'o'.
  653. self
  654. assertSameContents: (self collection select: [ :each |
  655. each = 'o' ])
  656. as: newCollection
  657. !
  658. testSize
  659. self assert: 'smalltalk' size equals: 9.
  660. self assert: '' size equals: 0
  661. !
  662. testStreamContents
  663. self
  664. assert: (String streamContents: [ :aStream |
  665. aStream
  666. nextPutAll: 'hello'; space;
  667. nextPutAll: 'world' ])
  668. equals: 'hello world'
  669. ! !
  670. !StringTest class methodsFor: 'accessing'!
  671. collectionClass
  672. ^ String
  673. ! !
  674. SequenceableCollectionTest subclass: #SymbolTest
  675. instanceVariableNames: ''
  676. package: 'Kernel-Tests'!
  677. !SymbolTest methodsFor: 'accessing'!
  678. collection
  679. ^ #hello
  680. !
  681. collectionWithDuplicates
  682. ^ #phhaaarorra
  683. ! !
  684. !SymbolTest methodsFor: 'tests'!
  685. testAsString
  686. self assert: #hello asString equals: 'hello'
  687. !
  688. testAsSymbol
  689. self assert: #hello == #hello asSymbol
  690. !
  691. testAt
  692. self assert: (#hello at: 1) equals: 'h'.
  693. self assert: (#hello at: 5) equals: 'o'.
  694. self assert: (#hello at: 6 ifAbsent: [nil]) equals: nil
  695. !
  696. testAtPut
  697. "Symbol instances are read-only"
  698. self should: ['hello' at: 1 put: 'a'] raise: Error
  699. !
  700. testCollect
  701. | newCollection |
  702. newCollection := #hheelllloo.
  703. self
  704. assertSameContents: (self collection collect: [ :each |
  705. each, each ])
  706. as: newCollection
  707. !
  708. testComparing
  709. self assert: #ab > #aa.
  710. self deny: #ab > #ba.
  711. self assert: #ab < #ba.
  712. self deny: #bb < #ba.
  713. self assert: #ab >= #aa.
  714. self deny: #ab >= #ba.
  715. self assert: #ab <= #ba.
  716. self deny: #bb <= #ba
  717. !
  718. testCopying
  719. self assert: #hello copy == #hello.
  720. self assert: #hello deepCopy == #hello
  721. !
  722. testDetect
  723. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  724. self
  725. should: [ self collection detect: [ :each | each = 'z' ] ]
  726. raise: Error
  727. !
  728. testEquality
  729. self assert: (#hello = #hello).
  730. self deny: #hello = #world.
  731. self assert: (#hello = #hello yourself).
  732. self assert: (#hello yourself = #hello).
  733. self deny: #hello = 'hello'.
  734. self deny: 'hello' = #hello.
  735. !
  736. testIdentity
  737. self assert: #hello == #hello.
  738. self deny: #hello == #world.
  739. self assert: #hello = #hello yourself.
  740. self assert: #hello yourself = #hello asString asSymbol
  741. !
  742. testIndexOf
  743. self assert: (self collection indexOf: 'e') equals: 2.
  744. self should: [ self collection indexOf: 999 ] raise: Error.
  745. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  746. !
  747. testIsEmpty
  748. self deny: self collection isEmpty.
  749. self assert: '' asSymbol isEmpty
  750. !
  751. testIsSymbolIsString
  752. self assert: #hello isSymbol.
  753. self deny: 'hello' isSymbol.
  754. self deny: #hello isString.
  755. self assert: 'hello' isString
  756. !
  757. testSelect
  758. | newCollection |
  759. newCollection := 'o'.
  760. self
  761. assertSameContents: (self collection select: [ :each |
  762. each = 'o' ])
  763. as: newCollection
  764. !
  765. testSize
  766. self assert: #a size equals: 1.
  767. self assert: #aaaaa size equals: 5
  768. ! !
  769. !SymbolTest class methodsFor: 'accessing'!
  770. collectionClass
  771. ^ Symbol
  772. ! !
  773. TestCase subclass: #JSObjectProxyTest
  774. instanceVariableNames: ''
  775. package: 'Kernel-Tests'!
  776. !JSObjectProxyTest methodsFor: 'accessing'!
  777. jsObject
  778. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined}>
  779. ! !
  780. !JSObjectProxyTest methodsFor: 'tests'!
  781. testAtIfAbsent
  782. | testObject |
  783. testObject := self jsObject.
  784. self assert: (testObject at: 'abc' ifAbsent: ['Property does not exist']) equals: 'Property does not exist'.
  785. self assert: (testObject at: 'e' ifAbsent: ['Property does not exist']) equals: nil.
  786. self assert: (testObject at: 'a' ifAbsent: ['Property does not exist']) equals: 1.
  787. self assert: (testObject at: 'f' ifAbsent: ['Property does not exist']) equals: nil.
  788. !
  789. testAtIfPresent
  790. | testObject |
  791. testObject := self jsObject.
  792. self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString]) equals: nil.
  793. self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString]) equals: 'hello nil'.
  794. self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString]) equals: 'hello 1'.
  795. self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString]) equals: 'hello nil'.
  796. !
  797. testAtIfPresentIfAbsent
  798. | testObject |
  799. testObject := self jsObject.
  800. self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'not present'.
  801. self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  802. self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello 1'.
  803. self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  804. !
  805. testDNU
  806. self should: [self jsObject foo] raise: MessageNotUnderstood
  807. !
  808. testMessageSend
  809. self assert: self jsObject a equals: 1.
  810. self assert: self jsObject b equals: 2.
  811. self assert: (self jsObject c: 3) equals: 3
  812. !
  813. testMethodWithArguments
  814. self assert: (self jsObject c: 1) equals: 1
  815. !
  816. testPrinting
  817. self assert: self jsObject printString equals: '[object Object]'
  818. !
  819. testPropertyThatReturnsEmptyString
  820. | object |
  821. object := self jsObject.
  822. self assert: object d equals: ''.
  823. object d: 'hello'.
  824. self assert: object d equals: 'hello'
  825. !
  826. testPropertyThatReturnsUndefined
  827. | object |
  828. object := self jsObject.
  829. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  830. self assert: object e isNil
  831. !
  832. testValue
  833. | testObject |
  834. testObject := self jsObject.
  835. self assert: testObject value printString equals: '[object Object]'.
  836. testObject at: 'value' put: 'aValue'.
  837. self assert: testObject value equals: 'aValue'
  838. !
  839. testYourself
  840. | object |
  841. object := self jsObject
  842. d: 'test';
  843. yourself.
  844. self assert: object d equals: 'test'
  845. ! !
  846. TestCase subclass: #JavaScriptExceptionTest
  847. instanceVariableNames: ''
  848. package: 'Kernel-Tests'!
  849. !JavaScriptExceptionTest methodsFor: 'helpers'!
  850. throwException
  851. <throw 'test'>
  852. ! !
  853. !JavaScriptExceptionTest methodsFor: 'tests'!
  854. testCatchingException
  855. [ self throwException ]
  856. on: Error
  857. do: [ :error |
  858. self assert: error exception = 'test' ]
  859. !
  860. testRaisingException
  861. self should: [ self throwException ] raise: JavaScriptException
  862. ! !
  863. TestCase subclass: #NumberTest
  864. instanceVariableNames: ''
  865. package: 'Kernel-Tests'!
  866. !NumberTest methodsFor: 'tests'!
  867. testAbs
  868. self assert: 4 abs equals: 4.
  869. self assert: -4 abs equals: 4
  870. !
  871. testArithmetic
  872. "We rely on JS here, so we won't test complex behavior, just check if
  873. message sends are corrects"
  874. self assert: 1.5 + 1 equals: 2.5.
  875. self assert: 2 - 1 equals: 1.
  876. self assert: -2 - 1 equals: -3.
  877. self assert: 12 / 2 equals: 6.
  878. self assert: 3 * 4 equals: 12.
  879. "Simple parenthesis and execution order"
  880. self assert: 1 + 2 * 3 equals: 9.
  881. self assert: 1 + (2 * 3) equals: 7
  882. !
  883. testComparison
  884. self assert: 3 > 2.
  885. self assert: 2 < 3.
  886. self deny: 3 < 2.
  887. self deny: 2 > 3.
  888. self assert: 3 >= 3.
  889. self assert: 3.1 >= 3.
  890. self assert: 3 <= 3.
  891. self assert: 3 <= 3.1
  892. !
  893. testCopying
  894. self assert: 1 copy == 1.
  895. self assert: 1 deepCopy == 1
  896. !
  897. testEquality
  898. self assert: (1 = 1).
  899. self assert: (0 = 0).
  900. self deny: (1 = 0).
  901. self assert: (1 yourself = 1).
  902. self assert: (1 = 1 yourself).
  903. self assert: (1 yourself = 1 yourself).
  904. self deny: 0 = false.
  905. self deny: false = 0.
  906. self deny: '' = 0.
  907. self deny: 0 = ''
  908. !
  909. testHexNumbers
  910. self assert: 16r9 equals: 9.
  911. self assert: 16rA truncated equals: 10.
  912. self assert: 16rB truncated equals: 11.
  913. self assert: 16rC truncated equals: 12.
  914. self assert: 16rD truncated equals: 13.
  915. self assert: 16rE truncated equals: 14.
  916. self assert: 16rF truncated equals: 15
  917. !
  918. testIdentity
  919. self assert: 1 == 1.
  920. self assert: 0 == 0.
  921. self deny: 1 == 0.
  922. self assert: 1 yourself == 1.
  923. self assert: 1 == 1 yourself.
  924. self assert: 1 yourself == 1 yourself.
  925. self deny: 1 == 2
  926. !
  927. testInvalidHexNumbers
  928. self should: [16rG] raise: MessageNotUnderstood.
  929. self should: [16rg] raise: MessageNotUnderstood.
  930. self should: [16rH] raise: MessageNotUnderstood.
  931. self should: [16rh] raise: MessageNotUnderstood.
  932. self should: [16rI] raise: MessageNotUnderstood.
  933. self should: [16ri] raise: MessageNotUnderstood.
  934. self should: [16rJ] raise: MessageNotUnderstood.
  935. self should: [16rj] raise: MessageNotUnderstood.
  936. self should: [16rK] raise: MessageNotUnderstood.
  937. self should: [16rk] raise: MessageNotUnderstood.
  938. self should: [16rL] raise: MessageNotUnderstood.
  939. self should: [16rl] raise: MessageNotUnderstood.
  940. self should: [16rM] raise: MessageNotUnderstood.
  941. self should: [16rm] raise: MessageNotUnderstood.
  942. self should: [16rN] raise: MessageNotUnderstood.
  943. self should: [16rn] raise: MessageNotUnderstood.
  944. self should: [16rO] raise: MessageNotUnderstood.
  945. self should: [16ro] raise: MessageNotUnderstood.
  946. self should: [16rP] raise: MessageNotUnderstood.
  947. self should: [16rp] raise: MessageNotUnderstood.
  948. self should: [16rQ] raise: MessageNotUnderstood.
  949. self should: [16rq] raise: MessageNotUnderstood.
  950. self should: [16rR] raise: MessageNotUnderstood.
  951. self should: [16rr] raise: MessageNotUnderstood.
  952. self should: [16rS] raise: MessageNotUnderstood.
  953. self should: [16rs] raise: MessageNotUnderstood.
  954. self should: [16rT] raise: MessageNotUnderstood.
  955. self should: [16rt] raise: MessageNotUnderstood.
  956. self should: [16rU] raise: MessageNotUnderstood.
  957. self should: [16ru] raise: MessageNotUnderstood.
  958. self should: [16rV] raise: MessageNotUnderstood.
  959. self should: [16rv] raise: MessageNotUnderstood.
  960. self should: [16rW] raise: MessageNotUnderstood.
  961. self should: [16rw] raise: MessageNotUnderstood.
  962. self should: [16rX] raise: MessageNotUnderstood.
  963. self should: [16rx] raise: MessageNotUnderstood.
  964. self should: [16rY] raise: MessageNotUnderstood.
  965. self should: [16ry] raise: MessageNotUnderstood.
  966. self should: [16rZ] raise: MessageNotUnderstood.
  967. self should: [16rz] raise: MessageNotUnderstood.
  968. self should: [16rABcdEfZ] raise: MessageNotUnderstood.
  969. !
  970. testMinMax
  971. self assert: (2 max: 5) equals: 5.
  972. self assert: (2 min: 5) equals: 2
  973. !
  974. testNegated
  975. self assert: 3 negated equals: -3.
  976. self assert: -3 negated equals: 3
  977. !
  978. testPrintShowingDecimalPlaces
  979. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  980. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  981. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  982. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  983. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  984. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  985. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  986. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  987. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  988. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  989. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  990. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  991. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  992. !
  993. testRounded
  994. self assert: 3 rounded equals: 3.
  995. self assert: 3.212 rounded equals: 3.
  996. self assert: 3.51 rounded equals: 4
  997. !
  998. testSqrt
  999. self assert: 4 sqrt equals: 2.
  1000. self assert: 16 sqrt equals: 4
  1001. !
  1002. testSquared
  1003. self assert: 4 squared equals: 16
  1004. !
  1005. testTimesRepeat
  1006. | i |
  1007. i := 0.
  1008. 0 timesRepeat: [i := i + 1].
  1009. self assert: i equals: 0.
  1010. 5 timesRepeat: [i := i + 1].
  1011. self assert: i equals: 5
  1012. !
  1013. testTo
  1014. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  1015. !
  1016. testToBy
  1017. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  1018. self should: [1 to: 4 by: 0] raise: Error
  1019. !
  1020. testTruncated
  1021. self assert: 3 truncated equals: 3.
  1022. self assert: 3.212 truncated equals: 3.
  1023. self assert: 3.51 truncated equals: 3
  1024. ! !
  1025. Object subclass: #ObjectMock
  1026. instanceVariableNames: 'foo bar'
  1027. package: 'Kernel-Tests'!
  1028. !ObjectMock commentStamp!
  1029. ObjectMock is there only to perform tests on classes.!
  1030. !ObjectMock methodsFor: 'not yet classified'!
  1031. foo
  1032. ^foo
  1033. !
  1034. foo: anObject
  1035. foo := anObject
  1036. ! !
  1037. TestCase subclass: #ObjectTest
  1038. instanceVariableNames: ''
  1039. package: 'Kernel-Tests'!
  1040. !ObjectTest methodsFor: 'tests'!
  1041. notDefined
  1042. <return undefined;>
  1043. !
  1044. testBasicAccess
  1045. | o |
  1046. o := Object new.
  1047. o basicAt: 'a' put: 1.
  1048. self assert: (o basicAt: 'a') equals: 1.
  1049. self assert: (o basicAt: 'b') equals: nil
  1050. !
  1051. testBasicPerform
  1052. | o |
  1053. o := Object new.
  1054. o basicAt: 'func' put: ['hello'].
  1055. o basicAt: 'func2' put: [:a | a + 1].
  1056. self assert: (o basicPerform: 'func') equals: 'hello'.
  1057. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1058. !
  1059. testDNU
  1060. self should: [Object new foo] raise: MessageNotUnderstood
  1061. !
  1062. testEquality
  1063. | o |
  1064. o := Object new.
  1065. self deny: o = Object new.
  1066. self assert: (o = o).
  1067. self assert: (o yourself = o).
  1068. self assert: (o = o yourself)
  1069. !
  1070. testHalt
  1071. self should: [Object new halt] raise: Error
  1072. !
  1073. testIdentity
  1074. | o |
  1075. o := Object new.
  1076. self deny: o == Object new.
  1077. self assert: o == o.
  1078. self assert: o yourself == o.
  1079. self assert: o == o yourself
  1080. !
  1081. testIfNil
  1082. self deny: Object new isNil.
  1083. self deny: (Object new ifNil: [true]) = true.
  1084. self assert: (Object new ifNotNil: [true]) equals: true.
  1085. self assert: (Object new ifNil: [false] ifNotNil: [true]) equals: true.
  1086. self assert: (Object new ifNotNil: [true] ifNil: [false]) equals: true
  1087. !
  1088. testInstVars
  1089. | o |
  1090. o := ObjectMock new.
  1091. self assert: (o instVarAt: #foo) equals: nil.
  1092. o instVarAt: #foo put: 1.
  1093. self assert: (o instVarAt: #foo) equals: 1.
  1094. self assert: (o instVarAt: 'foo') equals: 1
  1095. !
  1096. testNilUndefined
  1097. "nil in Smalltalk is the undefined object in JS"
  1098. self assert: self notDefined equals: nil
  1099. !
  1100. testYourself
  1101. | o |
  1102. o := ObjectMock new.
  1103. self assert: o yourself == o
  1104. !
  1105. testidentityHash
  1106. | o1 o2 |
  1107. o1 := Object new.
  1108. o2 := Object new.
  1109. self assert: o1 identityHash == o1 identityHash.
  1110. self deny: o1 identityHash == o2 identityHash
  1111. ! !
  1112. TestCase subclass: #PackageTest
  1113. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  1114. package: 'Kernel-Tests'!
  1115. !PackageTest methodsFor: 'running'!
  1116. setUp
  1117. backUpCommitPathJs := Package defaultCommitPathJs.
  1118. backUpCommitPathSt := Package defaultCommitPathSt.
  1119. Package resetCommitPaths.
  1120. zorkPackage := Package new name: 'Zork'.
  1121. grulPackage := Package new
  1122. name: 'Grul';
  1123. commitPathJs: 'server/grul/js';
  1124. commitPathSt: 'grul/st';
  1125. yourself
  1126. !
  1127. tearDown
  1128. Package
  1129. defaultCommitPathJs: backUpCommitPathJs;
  1130. defaultCommitPathSt: backUpCommitPathSt
  1131. ! !
  1132. !PackageTest methodsFor: 'tests'!
  1133. testGrulCommitPathJsShouldBeServerGrulJs
  1134. self assert: grulPackage commitPathJs equals: 'server/grul/js'
  1135. !
  1136. testGrulCommitPathStShouldBeGrulSt
  1137. self assert: grulPackage commitPathSt equals: 'grul/st'
  1138. !
  1139. testZorkCommitPathJsShouldBeJs
  1140. self assert: zorkPackage commitPathJs equals: 'js'
  1141. !
  1142. testZorkCommitPathStShouldBeSt
  1143. self assert: zorkPackage commitPathSt equals: 'st'
  1144. ! !
  1145. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  1146. instanceVariableNames: ''
  1147. package: 'Kernel-Tests'!
  1148. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  1149. setUp
  1150. super setUp.
  1151. Package
  1152. defaultCommitPathJs: 'javascripts/';
  1153. defaultCommitPathSt: 'smalltalk/'.
  1154. ! !
  1155. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  1156. testGrulCommitPathJsShouldBeServerGrulJs
  1157. self assert: grulPackage commitPathJs equals: 'server/grul/js'
  1158. !
  1159. testGrulCommitPathStShouldBeGrulSt
  1160. self assert: grulPackage commitPathSt equals: 'grul/st'
  1161. !
  1162. testZorkCommitPathJsShouldBeJavascript
  1163. self assert: zorkPackage commitPathJs equals: 'javascripts/'
  1164. !
  1165. testZorkCommitPathStShouldBeSmalltalk
  1166. self assert: zorkPackage commitPathSt equals: 'smalltalk/'
  1167. ! !
  1168. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  1169. shouldInheritSelectors
  1170. ^ false
  1171. ! !
  1172. TestCase subclass: #PointTest
  1173. instanceVariableNames: ''
  1174. package: 'Kernel-Tests'!
  1175. !PointTest methodsFor: 'tests'!
  1176. testAccessing
  1177. self assert: (Point x: 3 y: 4) x equals: 3.
  1178. self assert: (Point x: 3 y: 4) y equals: 4.
  1179. self assert: (Point new x: 3) x equals: 3.
  1180. self assert: (Point new y: 4) y equals: 4
  1181. !
  1182. testArithmetic
  1183. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1184. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1185. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1186. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1187. !
  1188. testAt
  1189. self assert: 3@4 equals: (Point x: 3 y: 4)
  1190. !
  1191. testEgality
  1192. self assert: (3@4 = (3@4)).
  1193. self deny: 3@5 = (3@6)
  1194. !
  1195. testTranslateBy
  1196. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1197. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1198. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1199. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1200. ! !
  1201. TestCase subclass: #RandomTest
  1202. instanceVariableNames: ''
  1203. package: 'Kernel-Tests'!
  1204. !RandomTest methodsFor: 'tests'!
  1205. textNext
  1206. 10000 timesRepeat: [
  1207. | current next |
  1208. next := Random new next.
  1209. self assert: (next >= 0).
  1210. self assert: (next < 1).
  1211. self deny: current = next.
  1212. next = current]
  1213. ! !
  1214. TestCase subclass: #SetTest
  1215. instanceVariableNames: ''
  1216. package: 'Kernel-Tests'!
  1217. !SetTest methodsFor: 'tests'!
  1218. testAddRemove
  1219. | set |
  1220. set := Set new.
  1221. self assert: set isEmpty.
  1222. set add: 3.
  1223. self assert: (set includes: 3).
  1224. set add: 5.
  1225. self assert: (set includes: 5).
  1226. set remove: 3.
  1227. self deny: (set includes: 3)
  1228. !
  1229. testAt
  1230. self should: [Set new at: 1 put: 2] raise: Error
  1231. !
  1232. testCollect
  1233. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1234. !
  1235. testComparing
  1236. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1237. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1238. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1239. self deny: #(1 2) asSet = #(0 2) asSet
  1240. !
  1241. testPrintString
  1242. | set |
  1243. set := Set new.
  1244. self assert: set printString equals: 'a Set ()'.
  1245. set add: 1; add: 3.
  1246. self assert: set printString equals: 'a Set (1 3)'.
  1247. set add: 'foo'.
  1248. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1249. set remove: 1; remove: 3.
  1250. self assert: set printString equals: 'a Set (''foo'')'.
  1251. set add: 3.
  1252. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1253. set add: 3.
  1254. self assert: set printString equals: 'a Set (''foo'' 3)'
  1255. !
  1256. testSize
  1257. self assert: Set new size equals: 0.
  1258. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1259. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1260. !
  1261. testUnicity
  1262. | set |
  1263. set := Set new.
  1264. set add: 21.
  1265. set add: 'hello'.
  1266. set add: 21.
  1267. self assert: set size equals: 2.
  1268. set add: 'hello'.
  1269. self assert: set size equals: 2.
  1270. self assert: set asArray equals: #(21 'hello')
  1271. ! !
  1272. TestCase subclass: #StreamTest
  1273. instanceVariableNames: ''
  1274. package: 'Kernel-Tests'!
  1275. !StreamTest methodsFor: 'accessing'!
  1276. collectionClass
  1277. ^ self class collectionClass
  1278. !
  1279. newCollection
  1280. ^ self collectionClass new
  1281. !
  1282. newStream
  1283. ^ self collectionClass new stream
  1284. ! !
  1285. !StreamTest methodsFor: 'tests'!
  1286. testAtStartAtEnd
  1287. | stream |
  1288. stream := self newStream.
  1289. self assert: stream atStart.
  1290. self assert: stream atEnd.
  1291. stream nextPutAll: self newCollection.
  1292. self assert: stream atEnd.
  1293. self deny: stream atStart.
  1294. stream position: 1.
  1295. self deny: stream atEnd.
  1296. self deny: stream atStart
  1297. !
  1298. testContents
  1299. | stream |
  1300. stream := self newStream.
  1301. stream nextPutAll: self newCollection.
  1302. self assert: stream contents equals: self newCollection
  1303. !
  1304. testIsEmpty
  1305. | stream |
  1306. stream := self newStream.
  1307. self assert: stream isEmpty.
  1308. stream nextPutAll: self newCollection.
  1309. self deny: stream isEmpty
  1310. !
  1311. testPosition
  1312. | collection stream |
  1313. collection := self newCollection.
  1314. stream := self newStream.
  1315. stream nextPutAll: collection.
  1316. self assert: stream position equals: collection size.
  1317. stream position: 0.
  1318. self assert: stream position equals: 0.
  1319. stream next.
  1320. self assert: stream position equals: 1.
  1321. stream next.
  1322. self assert: stream position equals: 2
  1323. !
  1324. testReading
  1325. | stream collection |
  1326. collection := self newCollection.
  1327. stream := self newStream.
  1328. stream
  1329. nextPutAll: collection;
  1330. position: 0.
  1331. collection do: [ :each |
  1332. self assert: stream next equals: each ].
  1333. self assert: stream next isNil
  1334. !
  1335. testStreamContents
  1336. !
  1337. testWrite
  1338. | stream collection |
  1339. collection := self newCollection.
  1340. stream := self newStream.
  1341. collection do: [ :each | stream << each ].
  1342. self assert: stream contents equals: collection
  1343. !
  1344. testWriting
  1345. | stream collection |
  1346. collection := self newCollection.
  1347. stream := self newStream.
  1348. collection do: [ :each | stream nextPut: each ].
  1349. self assert: stream contents equals: collection.
  1350. stream := self newStream.
  1351. stream nextPutAll: collection.
  1352. self assert: stream contents equals: collection
  1353. ! !
  1354. !StreamTest class methodsFor: 'accessing'!
  1355. collectionClass
  1356. ^ nil
  1357. ! !
  1358. !StreamTest class methodsFor: 'testing'!
  1359. isAbstract
  1360. ^ self collectionClass isNil
  1361. ! !
  1362. StreamTest subclass: #ArrayStreamTest
  1363. instanceVariableNames: ''
  1364. package: 'Kernel-Tests'!
  1365. !ArrayStreamTest methodsFor: 'accessing'!
  1366. newCollection
  1367. ^ { true. 1. 3@4. 'foo' }
  1368. ! !
  1369. !ArrayStreamTest class methodsFor: 'accessing'!
  1370. collectionClass
  1371. ^ Array
  1372. ! !
  1373. StreamTest subclass: #StringStreamTest
  1374. instanceVariableNames: ''
  1375. package: 'Kernel-Tests'!
  1376. !StringStreamTest methodsFor: 'accessing'!
  1377. newCollection
  1378. ^ 'hello world'
  1379. ! !
  1380. !StringStreamTest class methodsFor: 'accessing'!
  1381. collectionClass
  1382. ^ String
  1383. ! !
  1384. TestCase subclass: #UndefinedTest
  1385. instanceVariableNames: ''
  1386. package: 'Kernel-Tests'!
  1387. !UndefinedTest methodsFor: 'tests'!
  1388. testCopying
  1389. self assert: nil copy equals: nil
  1390. !
  1391. testDeepCopy
  1392. self assert: nil deepCopy = nil
  1393. !
  1394. testIfNil
  1395. self assert: (nil ifNil: [true]) equals: true.
  1396. self deny: (nil ifNotNil: [true]) = true.
  1397. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1398. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1399. !
  1400. testIsNil
  1401. self assert: nil isNil.
  1402. self deny: nil notNil.
  1403. ! !