Kernel-Tests.st 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332
  1. Smalltalk current createPackage: 'Kernel-Tests'!
  2. TestCase subclass: #AnnouncementSubscriptionTest
  3. instanceVariableNames: ''
  4. package: 'Kernel-Tests'!
  5. !AnnouncementSubscriptionTest methodsFor: 'tests'!
  6. testHandlesAnnouncement
  7. | subscription announcementClass1 announcementClass2 classBuilder |
  8. classBuilder := ClassBuilder new.
  9. announcementClass1 := classBuilder basicAddSubclassOf: SystemAnnouncement named: 'TestAnnouncement1' instanceVariableNames: #() package: 'Kernel-Tests'.
  10. subscription := AnnouncementSubscription new announcementClass: SystemAnnouncement.
  11. "Test whether the same class triggers the announcement"
  12. self assert: (subscription handlesAnnouncement: SystemAnnouncement) equals: true.
  13. "Test whether a subclass triggers the announcement"
  14. self assert: (subscription handlesAnnouncement: announcementClass1) equals: true.
  15. "Test whether an unrelated class does not trigger the announcement"
  16. self assert: (subscription handlesAnnouncement: Object) equals: false.
  17. classBuilder basicRemoveClass: announcementClass1.
  18. ! !
  19. TestCase subclass: #AnnouncerTest
  20. instanceVariableNames: ''
  21. package: 'Kernel-Tests'!
  22. !AnnouncerTest methodsFor: 'not yet classified'!
  23. testOnDo
  24. | counter announcer |
  25. counter := 0.
  26. announcer := Announcer new.
  27. announcer on: SystemAnnouncement do: [ counter := counter + 1 ].
  28. announcer announce: (SystemAnnouncement new).
  29. self assert: counter equals: 1.
  30. announcer announce: (SystemAnnouncement new).
  31. self assert: counter equals: 2.
  32. !
  33. testOnDoOnce
  34. | counter announcer |
  35. counter := 0.
  36. announcer := Announcer new.
  37. announcer on: SystemAnnouncement doOnce: [ counter := counter + 1 ].
  38. announcer announce: (SystemAnnouncement new).
  39. self assert: counter equals: 1.
  40. announcer announce: (SystemAnnouncement new).
  41. self assert: counter equals: 1.
  42. ! !
  43. TestCase subclass: #BlockClosureTest
  44. instanceVariableNames: ''
  45. package: 'Kernel-Tests'!
  46. !BlockClosureTest methodsFor: 'tests'!
  47. testCanClearInterval
  48. self shouldnt: [ ([ Error new signal ] valueWithInterval: 0) clearInterval ] raise: Error
  49. !
  50. testCanClearTimeout
  51. self shouldnt: [ ([ Error new signal ] valueWithTimeout: 0) clearTimeout ] raise: Error
  52. !
  53. testCompiledSource
  54. self assert: ([ 1+1 ] compiledSource includesSubString: 'function')
  55. !
  56. testCurrySelf
  57. | curriedMethod array |
  58. curriedMethod := [ :selfarg :x | selfarg at: x ] currySelf asCompiledMethod: 'foo:'.
  59. array := #(3 1 4).
  60. ClassBuilder new installMethod: curriedMethod forClass: Array protocol: '**test helper'.
  61. [ self assert: (array foo: 2) equals: 1 ]
  62. ensure: [ Array removeCompiledMethod: curriedMethod ]
  63. !
  64. testEnsure
  65. self assert: ([ 3 ] ensure: [ 4 ]) equals: 3
  66. !
  67. testEnsureRaises
  68. self should: [ [Error new signal ] ensure: [ true ]] raise: Error
  69. !
  70. testExceptionSemantics
  71. "See https://github.com/NicolasPetton/amber/issues/314"
  72. self timeout: 100.
  73. (self async: [
  74. [
  75. self assert: true.
  76. Error signal.
  77. "The following should *not* be run"
  78. self deny: true.
  79. self finished.
  80. ] on: Error do: [ :ex | self finished ]
  81. ]) valueWithTimeout: 0
  82. !
  83. testNewWithValues
  84. <
  85. function theTestPrototype() {this.name = "theTestPrototype";}
  86. function theTestConstructor(arg1, arg2, arg3) {}
  87. theTestConstructor.prototype = new theTestPrototype;
  88. var theWrappedConstructor = _st(theTestConstructor);
  89. var theResult = theWrappedConstructor._newWithValues_([1, 2, 3 ]);
  90. self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
  91. "newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
  92. function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
  93. self._should_raise_(_st(constructionShouldFail), smalltalk.Error);
  94. >
  95. !
  96. testNumArgs
  97. self assert: [] numArgs equals: 0.
  98. self assert: [ :a :b | ] numArgs equals: 2
  99. !
  100. testOnDo
  101. self assert: ([ Error new signal ] on: Error do: [ :ex | true ])
  102. !
  103. testValue
  104. self assert: ([ 1+1 ] value) equals: 2.
  105. self assert: ([ :x | x +1 ] value: 2) equals: 3.
  106. self assert: ([ :x :y | x*y ] value: 2 value: 4) equals: 8.
  107. "Arguments are optional in Amber. This isn't ANSI compliant."
  108. self assert: ([ :a :b :c | 1 ] value) equals: 1
  109. !
  110. testValueWithPossibleArguments
  111. self assert: ([ 1 ] valueWithPossibleArguments: #(3 4)) equals: 1.
  112. self assert: ([ :a | a + 4 ] valueWithPossibleArguments: #(3 4)) equals: 7.
  113. self assert: ([ :a :b | a + b ] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  114. !
  115. testWhileFalse
  116. | i |
  117. i := 0.
  118. [ i > 5 ] whileFalse: [ i := i + 1 ].
  119. self assert: i equals: 6.
  120. i := 0.
  121. [ i := i + 1. i > 5 ] whileFalse.
  122. self assert: i equals: 6
  123. !
  124. testWhileTrue
  125. | i |
  126. i := 0.
  127. [ i < 5 ] whileTrue: [ i := i + 1 ].
  128. self assert: i equals: 5.
  129. i := 0.
  130. [ i := i + 1. i < 5 ] whileTrue.
  131. self assert: i equals: 5
  132. ! !
  133. TestCase subclass: #BooleanTest
  134. instanceVariableNames: ''
  135. package: 'Kernel-Tests'!
  136. !BooleanTest methodsFor: 'tests'!
  137. testEquality
  138. "We're on top of JS...just be sure to check the basics!!"
  139. self deny: 0 = false.
  140. self deny: false = 0.
  141. self deny: '' = false.
  142. self deny: false = ''.
  143. self assert: (true = true).
  144. self deny: false = true.
  145. self deny: true = false.
  146. self assert: (false = false).
  147. "JS may do some type coercing after sending a message"
  148. self assert: (true yourself = true).
  149. self assert: (true yourself = true yourself)
  150. !
  151. testIdentity
  152. "We're on top of JS...just be sure to check the basics!!"
  153. self deny: 0 == false.
  154. self deny: false == 0.
  155. self deny: '' == false.
  156. self deny: false == ''.
  157. self assert: true == true.
  158. self deny: false == true.
  159. self deny: true == false.
  160. self assert: false == false.
  161. "JS may do some type coercing after sending a message"
  162. self assert: true yourself == true.
  163. self assert: true yourself == true yourself
  164. !
  165. testIfTrueIfFalse
  166. self assert: (true ifTrue: [ 'alternative block' ]) equals: 'alternative block'.
  167. self assert: (true ifFalse: [ 'alternative block' ]) equals: nil.
  168. self assert: (false ifTrue: [ 'alternative block' ]) equals: nil.
  169. self assert: (false ifFalse: [ 'alternative block' ]) equals: 'alternative block'.
  170. self assert: (false ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block2'.
  171. self assert: (false ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block'.
  172. self assert: (true ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block'.
  173. self assert: (true ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block2'.
  174. !
  175. testIfTrueIfFalseWithBoxing
  176. self assert: (true yourself ifTrue: [ 'alternative block' ]) equals: 'alternative block'.
  177. self assert: (true yourself ifFalse: [ 'alternative block' ]) equals: nil.
  178. self assert: (false yourself ifTrue: [ 'alternative block' ]) equals: nil.
  179. self assert: (false yourself ifFalse: [ 'alternative block' ]) equals: 'alternative block'.
  180. self assert: (false yourself ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block2'.
  181. self assert: (false yourself ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block'.
  182. self assert: (true yourself ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block'.
  183. self assert: (true yourself ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block2'.
  184. !
  185. testLogic
  186. "Trivial logic table"
  187. self assert: (true & true);
  188. deny: (true & false);
  189. deny: (false & true);
  190. deny: (false & false).
  191. self assert: (true | true);
  192. assert: (true | false);
  193. assert: (false | true);
  194. deny: (false | false).
  195. "Checking that expressions work fine too"
  196. self assert: (true & (1 > 0));
  197. deny: ((1 > 0) & false);
  198. deny: ((1 > 0) & (1 > 2)).
  199. self assert: (false | (1 > 0));
  200. assert: ((1 > 0) | false);
  201. assert: ((1 > 0) | (1 > 2))
  202. !
  203. testLogicKeywords
  204. "Trivial logic table"
  205. self
  206. assert: (true and: [ true ]);
  207. deny: (true and: [ false ]);
  208. deny: (false and: [ true ]);
  209. deny: (false and: [ false ]).
  210. self
  211. assert: (true or: [ true ]);
  212. assert: (true or: [ false ]);
  213. assert: (false or: [ true ]);
  214. deny: (false or: [ false ]).
  215. "Checking that expressions work fine too"
  216. self
  217. assert: (true and: [ 1 > 0 ]);
  218. deny: ((1 > 0) and: [ false ]);
  219. deny: ((1 > 0) and: [ 1 > 2 ]).
  220. self
  221. assert: (false or: [ 1 > 0 ]);
  222. assert: ((1 > 0) or: [ false ]);
  223. assert: ((1 > 0) or: [ 1 > 2 ])
  224. !
  225. testNonBooleanError
  226. self should: [ '' ifTrue: [] ifFalse: [] ] raise: NonBooleanReceiver
  227. ! !
  228. TestCase subclass: #ClassBuilderTest
  229. instanceVariableNames: 'builder theClass'
  230. package: 'Kernel-Tests'!
  231. !ClassBuilderTest methodsFor: 'running'!
  232. setUp
  233. builder := ClassBuilder new
  234. !
  235. tearDown
  236. theClass ifNotNil: [ Smalltalk current removeClass: theClass. theClass := nil ]
  237. ! !
  238. !ClassBuilderTest methodsFor: 'tests'!
  239. testClassCopy
  240. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  241. self assert: theClass superclass == ObjectMock superclass.
  242. self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
  243. self assert: theClass name equals: 'ObjectMock2'.
  244. self assert: theClass package == ObjectMock package.
  245. self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
  246. !
  247. testClassMigration
  248. | instance oldClass |
  249. oldClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  250. instance := (Smalltalk current at: 'ObjectMock2') new.
  251. "Change the superclass of ObjectMock2"
  252. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  253. instanceVariableNames: ''
  254. package: 'Kernel-Tests'.
  255. self deny: oldClass == ObjectMock2.
  256. self assert: ObjectMock2 superclass == ObjectMock.
  257. self assert: ObjectMock2 instanceVariableNames isEmpty.
  258. self assert: ObjectMock2 selectors equals: oldClass selectors.
  259. self assert: ObjectMock2 comment equals: oldClass comment.
  260. self assert: ObjectMock2 package name equals: 'Kernel-Tests'.
  261. self deny: instance class == ObjectMock2.
  262. "Commeting this out. Tests implementation detail."
  263. "self assert: instance class name equals: 'OldObjectMock2'."
  264. self assert: (Smalltalk current at: instance class name) isNil.
  265. Smalltalk current removeClass: ObjectMock2
  266. !
  267. testClassMigrationWithClassInstanceVariables
  268. builder copyClass: ObjectMock named: 'ObjectMock2'.
  269. ObjectMock2 class instanceVariableNames: 'foo bar'.
  270. "Change the superclass of ObjectMock2"
  271. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  272. instanceVariableNames: ''
  273. package: 'Kernel-Tests'.
  274. self assert: ObjectMock2 class instanceVariableNames equals: #('foo' 'bar').
  275. Smalltalk current removeClass: ObjectMock2
  276. !
  277. testClassMigrationWithSubclasses
  278. builder copyClass: ObjectMock named: 'ObjectMock2'.
  279. ObjectMock2 subclass: 'ObjectMock3' instanceVariableNames: '' package: 'Kernel-Tests'.
  280. ObjectMock3 subclass: 'ObjectMock4' instanceVariableNames: '' package: 'Kernel-Tests'.
  281. "Change the superclass of ObjectMock2"
  282. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  283. instanceVariableNames: ''
  284. package: 'Kernel-Tests'.
  285. self assert: (ObjectMock subclasses includes: ObjectMock2).
  286. self assert: (ObjectMock2 subclasses includes: ObjectMock3).
  287. self assert: (ObjectMock3 subclasses includes: ObjectMock4).
  288. ObjectMock allSubclasses do: [ :each | Smalltalk current removeClass: each ]
  289. !
  290. testInstanceVariableNames
  291. self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
  292. ! !
  293. TestCase subclass: #CollectionTest
  294. instanceVariableNames: ''
  295. package: 'Kernel-Tests'!
  296. !CollectionTest methodsFor: 'convenience'!
  297. assertSameContents: aCollection as: anotherCollection
  298. self assert: (aCollection size = anotherCollection size).
  299. aCollection do: [ :each |
  300. self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
  301. ! !
  302. !CollectionTest methodsFor: 'fixture'!
  303. collection
  304. "Answers pre-filled collection of type tested."
  305. self subclassResponsibility
  306. !
  307. collectionClass
  308. "Answers class of collection type tested"
  309. ^ self class collectionClass
  310. !
  311. collectionWithDuplicates
  312. "Answers pre-filled collection of type tested,
  313. with exactly five distinct elements,
  314. some of them appearing multiple times."
  315. self subclassResponsibility
  316. !
  317. sampleNonValue
  318. "Answer value that is guaranteed
  319. not to be contained by self collection"
  320. ^ 999
  321. ! !
  322. !CollectionTest methodsFor: 'testing'!
  323. isCollectionReadOnly
  324. ^ false
  325. ! !
  326. !CollectionTest methodsFor: 'tests'!
  327. testAllSatisfy
  328. | collection anyOne |
  329. collection := self collection.
  330. anyOne := collection anyOne.
  331. self assert: (collection allSatisfy: [ :each | collection includes: each ]).
  332. self deny: (collection allSatisfy: [ :each | each ~= anyOne ])
  333. !
  334. testAnyOne
  335. self should: [ self collectionClass new anyOne ] raise: Error.
  336. self assert: (self collection includes: self collection anyOne)
  337. !
  338. testAnySatisfy
  339. | anyOne |
  340. anyOne := self collection anyOne.
  341. self assert: (self collection anySatisfy: [ :each | each = anyOne ]).
  342. self deny: (self collection anySatisfy: [ :each | each = Object new ])
  343. !
  344. testAsArray
  345. self
  346. assertSameContents: self collection
  347. as: self collection asArray
  348. !
  349. testAsOrderedCollection
  350. self
  351. assertSameContents: self collection
  352. as: self collection asOrderedCollection
  353. !
  354. testAsSet
  355. | c set |
  356. c := self collectionWithDuplicates.
  357. set := c asSet.
  358. self assert: set size equals: 5.
  359. c do: [ :each |
  360. self assert: (set includes: each) ]
  361. !
  362. testCollect
  363. | newCollection |
  364. newCollection := #(1 2 3 4).
  365. self
  366. assertSameContents: (self collection collect: [ :each |
  367. each abs ])
  368. as: newCollection
  369. !
  370. testDetect
  371. self assert: (self collection detect: [ :each | each < 0 ]) equals: -4.
  372. self
  373. should: [ self collection detect: [ :each | each = 6 ] ]
  374. raise: Error
  375. !
  376. testDo
  377. | newCollection |
  378. newCollection := OrderedCollection new.
  379. self collection do: [ :each |
  380. newCollection add: each ].
  381. self
  382. assertSameContents: self collection
  383. as: newCollection
  384. !
  385. testIfEmptyFamily
  386. self assert: (self collectionClass new ifEmpty: [ 42 ]) equals: 42.
  387. self assert: (self collection ifEmpty: [ 42 ]) equals: self collection.
  388. self assert: (self collectionClass new ifNotEmpty: [ 42 ]) equals: self collectionClass new.
  389. self assert: (self collection ifNotEmpty: [ 42 ]) equals: 42.
  390. self assert: (self collectionClass new ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 42.
  391. self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 999.
  392. self assert: (self collectionClass new ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 999.
  393. self assert: (self collection ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 42
  394. !
  395. testIsEmpty
  396. self assert: self collectionClass new isEmpty.
  397. self deny: self collection isEmpty
  398. !
  399. testNoneSatisfy
  400. | anyOne |
  401. anyOne := self collection anyOne.
  402. self deny: (self collection noneSatisfy: [ :each | each = anyOne ]).
  403. self assert: (self collection noneSatisfy: [ :each | each = Object new ])
  404. !
  405. testRemoveAll
  406. self assert: (self collection removeAll; yourself) equals: self collectionClass new
  407. !
  408. testSelect
  409. | newCollection |
  410. newCollection := #(2 -4).
  411. self
  412. assertSameContents: (self collection select: [ :each |
  413. each even ])
  414. as: newCollection
  415. !
  416. testSize
  417. self assert: self collectionClass new size equals: 0.
  418. self assert: self collection size equals: 4
  419. ! !
  420. !CollectionTest class methodsFor: 'fixture'!
  421. collectionClass
  422. "Answers class of collection type tested"
  423. self subclassResponsibility
  424. ! !
  425. !CollectionTest class methodsFor: 'testing'!
  426. isAbstract
  427. ^ self collectionClass isNil
  428. ! !
  429. CollectionTest subclass: #IndexableCollectionTest
  430. instanceVariableNames: ''
  431. package: 'Kernel-Tests'!
  432. !IndexableCollectionTest methodsFor: 'fixture'!
  433. sampleNonIndexesDo: aBlock
  434. "Executes block a few times,
  435. each time passing value that is known
  436. not to be an index, as the first parameter"
  437. self subclassResponsibility
  438. !
  439. samplesDo: aBlock
  440. "Executes block a few times,
  441. each time passing known index and value stored
  442. under that index as the parameters"
  443. self subclassResponsibility
  444. ! !
  445. !IndexableCollectionTest methodsFor: 'tests'!
  446. testAt
  447. self nonIndexesDo: [ :each |
  448. self should: [ self collection at: each ] raise: Error ].
  449. self samplesDo: [ :index :value |
  450. self assert: (self collection at: index) equals: value ]
  451. !
  452. testAtIfAbsent
  453. self nonIndexesDo: [ :each |
  454. self assert: (self collection at: each ifAbsent: [ self sampleNonValue ]) equals: self sampleNonValue ].
  455. self samplesDo: [ :index :value |
  456. self assert: (self collection at: index ifAbsent: [ self sampleNonValue ]) equals: value ].
  457. !
  458. testAtIfPresent
  459. | visited sentinel |
  460. sentinel := Object new.
  461. self nonIndexesDo: [ :each |
  462. visited := nil.
  463. self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ]) equals: nil.
  464. self assert: visited isNil ].
  465. self samplesDo: [ :index :value |
  466. visited := nil.
  467. self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ]) equals: sentinel.
  468. self assert: visited equals: (self collection at: index) ]
  469. !
  470. testAtIfPresentIfAbsent
  471. | visited sentinel |
  472. sentinel := Object new.
  473. self nonIndexesDo: [ :each |
  474. visited := nil.
  475. self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ] ifAbsent: [ self sampleNonValue ] ) equals: self sampleNonValue.
  476. self assert: visited isNil ].
  477. self samplesDo: [ :index :value |
  478. visited := nil.
  479. self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ] ifAbsent: [ self sampleNonValue ]) equals: sentinel.
  480. self assert: visited equals: (self collection at: index) ]
  481. !
  482. testIndexOf
  483. self should: [ self collection indexOf: self sampleNonValue ] raise: Error.
  484. self samplesDo: [ :index :value |
  485. self assert: (self collection indexOf: value) equals: index ]
  486. !
  487. testWithIndexDo
  488. | collection |
  489. collection := self collection.
  490. self collection withIndexDo: [ :each :index |
  491. self assert: (collection at: index) equals: each ]
  492. ! !
  493. IndexableCollectionTest subclass: #HashedCollectionTest
  494. instanceVariableNames: ''
  495. package: 'Kernel-Tests'!
  496. !HashedCollectionTest methodsFor: 'fixture'!
  497. collection
  498. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  499. !
  500. collectionWithDuplicates
  501. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
  502. !
  503. nonIndexesDo: aBlock
  504. aBlock value: 5.
  505. aBlock value: 'z'
  506. !
  507. samplesDo: aBlock
  508. aBlock value: 'a' value: 2
  509. ! !
  510. !HashedCollectionTest methodsFor: 'tests'!
  511. testAsDictionary
  512. self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
  513. !
  514. testFrom
  515. "Accept a collection of associations."
  516. | associations |
  517. associations := { 'a' -> 1. 'b' -> 2 }.
  518. self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  519. !
  520. testNewFromPairs
  521. "Accept an array in which all odd indexes are keys and evens are values."
  522. | flattenedAssociations |
  523. flattenedAssociations := { 'a'. 1. 'b'. 2 }.
  524. self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  525. ! !
  526. !HashedCollectionTest class methodsFor: 'fixture'!
  527. collectionClass
  528. ^ HashedCollection
  529. ! !
  530. HashedCollectionTest subclass: #DictionaryTest
  531. instanceVariableNames: ''
  532. package: 'Kernel-Tests'!
  533. !DictionaryTest methodsFor: 'fixture'!
  534. collection
  535. ^ Dictionary new
  536. at: 1 put: 1;
  537. at: 'a' put: 2;
  538. at: true put: 3;
  539. at: 4 put: -4;
  540. yourself
  541. !
  542. collectionWithDuplicates
  543. ^ Dictionary new
  544. at: 1 put: 1;
  545. at: 'a' put: 2;
  546. at: true put: 3;
  547. at: 4 put: -4;
  548. at: 'b' put: 1;
  549. at: 3 put: 3;
  550. at: false put: 12;
  551. yourself
  552. ! !
  553. !DictionaryTest methodsFor: 'tests'!
  554. testAccessing
  555. | d |
  556. d := Dictionary new.
  557. d at: 'hello' put: 'world'.
  558. self assert: (d at: 'hello') equals: 'world'.
  559. self assert: (d at: 'hello' ifAbsent: [ nil ]) equals: 'world'.
  560. self deny: (d at: 'foo' ifAbsent: [ nil ]) = 'world'.
  561. self assert: (d includesKey: 'hello').
  562. self deny: (d includesKey: 'foo').
  563. d at: 1 put: 2.
  564. self assert: (d at: 1) equals: 2.
  565. d at: 1@3 put: 3.
  566. self assert: (d at: 1@3) equals: 3.
  567. self assert: (d includesKey: 1@3).
  568. self deny: (d includesKey: 3@1)
  569. !
  570. testAsHashedCollection
  571. self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
  572. !
  573. testDynamicDictionaries
  574. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  575. !
  576. testEquality
  577. | d1 d2 |
  578. self assert: (Dictionary new = Dictionary new).
  579. d1 := Dictionary new at: 1 put: 2; yourself.
  580. d2 := Dictionary new at: 1 put: 2; yourself.
  581. self assert: (d1 = d2).
  582. d2 := Dictionary new at: 1 put: 3; yourself.
  583. self deny: d1 = d2.
  584. d2 := Dictionary new at: 2 put: 2; yourself.
  585. self deny: d1 = d2.
  586. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  587. self deny: d1 = d2.
  588. !
  589. testKeys
  590. | d |
  591. d := Dictionary new.
  592. d at: 1 put: 2.
  593. d at: 2 put: 3.
  594. d at: 3 put: 4.
  595. self assert: d keys equals: #(1 2 3)
  596. !
  597. testPointKey
  598. | d |
  599. d := Dictionary new.
  600. d at: 1@1 put: 'foo'.
  601. self assert: (d at: 1@1) equals: 'foo'.
  602. d at: 1@1 put: 'bar'.
  603. self assert: (d at: 1@1) equals: 'bar'.
  604. d removeKey: 1@1.
  605. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  606. self deny: (d includesKey: 1@1)
  607. !
  608. testPrintString
  609. self
  610. assert: (Dictionary new
  611. at:'firstname' put: 'James';
  612. at:'lastname' put: 'Bond';
  613. printString)
  614. equals: 'a Dictionary (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  615. !
  616. testRemoveKey
  617. | d key |
  618. d := Dictionary new.
  619. d at: 1 put: 2.
  620. d at: 2 put: 3.
  621. d at: 3 put: 4.
  622. key := 2.
  623. self assert: d keys equals: #(1 2 3).
  624. d removeKey: key.
  625. self assert: d keys equals: #(1 3).
  626. self assert: d values equals: #(2 4).
  627. self deny: (d includesKey: 2)
  628. !
  629. testRemoveKeyIfAbsent
  630. | d key |
  631. d := Dictionary new.
  632. d at: 1 put: 2.
  633. d at: 2 put: 3.
  634. d at: 3 put: 4.
  635. key := 2.
  636. self assert: (d removeKey: key) equals: 3.
  637. key := 3.
  638. self assert: (d removeKey: key ifAbsent: [ 42 ]) equals: 4.
  639. key := 'why'.
  640. self assert: (d removeKey: key ifAbsent: [ 42 ] ) equals: 42.
  641. !
  642. testSize
  643. | d |
  644. d := Dictionary new.
  645. self assert: d size equals: 0.
  646. d at: 1 put: 2.
  647. self assert: d size equals: 1.
  648. d at: 2 put: 3.
  649. self assert: d size equals: 2.
  650. !
  651. testValues
  652. | d |
  653. d := Dictionary new.
  654. d at: 1 put: 2.
  655. d at: 2 put: 3.
  656. d at: 3 put: 4.
  657. self assert: d values equals: #(2 3 4)
  658. ! !
  659. !DictionaryTest class methodsFor: 'fixture'!
  660. collectionClass
  661. ^ Dictionary
  662. ! !
  663. IndexableCollectionTest subclass: #SequenceableCollectionTest
  664. instanceVariableNames: ''
  665. package: 'Kernel-Tests'!
  666. !SequenceableCollectionTest methodsFor: 'fixture'!
  667. collectionFirst
  668. self subclassResponsibility
  669. !
  670. collectionFirstTwo
  671. self subclassResponsibility
  672. !
  673. collectionLast
  674. self subclassResponsibility
  675. !
  676. collectionLastTwo
  677. self subclassResponsibility
  678. !
  679. nonIndexesDo: aBlock
  680. aBlock value: 0.
  681. aBlock value: self collection size + 1.
  682. aBlock value: 'z'
  683. !
  684. samplesDo: aBlock
  685. aBlock value: 1 value: self collectionFirst.
  686. aBlock value: self collection size value: self collectionLast
  687. ! !
  688. !SequenceableCollectionTest methodsFor: 'tests'!
  689. testBeginsWith
  690. self assert: (self collection beginsWith: self collectionClass new).
  691. self assert: (self collection beginsWith: self collection).
  692. self assert: (self collection beginsWith: self collectionFirstTwo).
  693. self deny: (self collection beginsWith: self collectionLastTwo)
  694. !
  695. testEndsWith
  696. self assert: (self collection endsWith: self collectionClass new).
  697. self assert: (self collection endsWith: self collection).
  698. self assert: (self collection endsWith: self collectionLastTwo).
  699. self deny: (self collection endsWith: self collectionFirstTwo)
  700. !
  701. testFirst
  702. self assert: self collection first equals: self collectionFirst
  703. !
  704. testFirstN
  705. self
  706. assert: (self collection first: 2)
  707. equals: self collectionFirstTwo.
  708. self
  709. assert: (self collection first: 0)
  710. equals: self collectionClass new.
  711. self
  712. assert: (self collection first: self collection size)
  713. equals: self collection.
  714. self should: [ self collection first: 33 ] raise: Error
  715. !
  716. testFourth
  717. self assert: (self collection fourth) equals: (self collection at: 4)
  718. !
  719. testLast
  720. self assert: self collection last equals: self collectionLast
  721. !
  722. testLastN
  723. self
  724. assert: (self collection last: 2)
  725. equals: self collectionLastTwo.
  726. self
  727. assert: (self collection last: 0)
  728. equals: self collectionClass new.
  729. self
  730. assert: (self collection last: self collection size)
  731. equals: self collection.
  732. self should: [ self collection last: 33 ] raise: Error
  733. !
  734. testSecond
  735. self assert: (self collection second) equals: (self collection at: 2)
  736. !
  737. testThird
  738. self assert: (self collection third) equals: (self collection at: 3)
  739. ! !
  740. SequenceableCollectionTest subclass: #ArrayTest
  741. instanceVariableNames: ''
  742. package: 'Kernel-Tests'!
  743. !ArrayTest methodsFor: 'fixture'!
  744. collection
  745. ^ #(1 2 3 -4)
  746. !
  747. collectionFirst
  748. ^ 1
  749. !
  750. collectionFirstTwo
  751. ^ #(1 2)
  752. !
  753. collectionLast
  754. ^ -4
  755. !
  756. collectionLastTwo
  757. ^ #(3 -4)
  758. !
  759. collectionWithDuplicates
  760. ^ #('a' 'b' 'c' 1 2 1 'a')
  761. !
  762. samplesDo: aBlock
  763. super samplesDo: aBlock.
  764. aBlock value: 3 value: 3.
  765. ! !
  766. !ArrayTest methodsFor: 'tests'!
  767. testAdd
  768. | array |
  769. array := self collection.
  770. array add: 6.
  771. self assert: array last equals: 6
  772. !
  773. testAddFirst
  774. self assert: (self collection addFirst: 0; yourself) first equals: 0
  775. !
  776. testPrintString
  777. | array |
  778. array := Array new.
  779. self assert: array printString equals: 'an Array ()'.
  780. array add: 1; add: 3.
  781. self assert: array printString equals: 'an Array (1 3)'.
  782. array add: 'foo'.
  783. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  784. array remove: 1; remove: 3.
  785. self assert: array printString equals: 'an Array (''foo'')'.
  786. array addLast: 3.
  787. self assert: array printString equals: 'an Array (''foo'' 3)'.
  788. array addLast: 3.
  789. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  790. !
  791. testRemove
  792. | array |
  793. array := #(1 2 3 4 5).
  794. array remove: 3.
  795. self assert: array equals: #(1 2 4 5).
  796. self should: [ array remove: 3 ] raise: Error
  797. !
  798. testRemoveFromTo
  799. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  800. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  801. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  802. !
  803. testRemoveIndex
  804. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  805. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  806. self assert: (#('hello') removeIndex: 1) equals: #()
  807. !
  808. testRemoveLast
  809. | array |
  810. array := #(1 2).
  811. array removeLast.
  812. self assert: array last equals: 1
  813. !
  814. testReversed
  815. |array|
  816. array := #(5 4 3 2 1).
  817. self assert: (array reversed) equals: #(1 2 3 4 5)
  818. !
  819. testSort
  820. | array |
  821. array := #(3 1 4 5 2).
  822. array sort.
  823. self assert: array equals: #(1 2 3 4 5)
  824. ! !
  825. !ArrayTest class methodsFor: 'fixture'!
  826. collectionClass
  827. ^ Array
  828. ! !
  829. SequenceableCollectionTest subclass: #StringTest
  830. instanceVariableNames: ''
  831. package: 'Kernel-Tests'!
  832. !StringTest methodsFor: 'fixture'!
  833. collection
  834. ^ 'hello'
  835. !
  836. collectionFirst
  837. ^ 'h'
  838. !
  839. collectionFirstTwo
  840. ^ 'he'
  841. !
  842. collectionLast
  843. ^ 'o'
  844. !
  845. collectionLastTwo
  846. ^ 'lo'
  847. !
  848. collectionWithDuplicates
  849. ^ 'abbaerte'
  850. !
  851. samplesDo: aBlock
  852. super samplesDo: aBlock.
  853. aBlock value: 3 value: 'l'
  854. ! !
  855. !StringTest methodsFor: 'tests'!
  856. testAddRemove
  857. self should: [ 'hello' add: 'a' ] raise: Error.
  858. self should: [ 'hello' remove: 'h' ] raise: Error
  859. !
  860. testAsArray
  861. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  862. !
  863. testAsLowerCase
  864. self assert: 'JACKIE' asLowercase equals: 'jackie'.
  865. !
  866. testAsNumber
  867. self assert: '3' asNumber equals: 3.
  868. self assert: '-3' asNumber equals: -3.
  869. self assert: '-1.5' asNumber equals: -1.5.
  870. !
  871. testAsUpperCase
  872. self assert: 'jackie' asUppercase equals: 'JACKIE'.
  873. !
  874. testAsciiValue
  875. | characterA characterU |
  876. characterA := 'A'.
  877. characterU := 'U'.
  878. self assert: (characterA asciiValue) equals:65.
  879. self assert: (characterU asciiValue) equals:85
  880. !
  881. testAtPut
  882. "String instances are read-only"
  883. self should: [ 'hello' at: 1 put: 'a' ] raise: Error
  884. !
  885. testCapitalized
  886. self assert: 'test' capitalized equals: 'Test'.
  887. self assert: 'Test' capitalized equals: 'Test'.
  888. self assert: '' capitalized equals: ''.
  889. self assert: 'Test' isCapitalized equals: true.
  890. self assert: 'test' isCapitalized equals: false.
  891. !
  892. testCharCodeAt
  893. self assert: ('jackie' charCodeAt:1) equals: 106.
  894. self assert: ('jackie' charCodeAt:2) equals: 97.
  895. self assert: ('jackie' charCodeAt:3) equals: 99.
  896. self assert: ('jackie' charCodeAt:4) equals: 107.
  897. self assert: ('jackie' charCodeAt:5) equals: 105.
  898. self assert: ('jackie' charCodeAt:6) equals: 101
  899. !
  900. testCollect
  901. | newCollection |
  902. newCollection := 'hheelllloo'.
  903. self
  904. assertSameContents: (self collection collect: [ :each |
  905. each, each ])
  906. as: newCollection
  907. !
  908. testCopyFromTo
  909. self assert: ('jackie' copyFrom: 1 to: 3) equals: 'jac'.
  910. self assert: ('jackie' copyFrom: 4 to: 6) equals: 'kie'.
  911. !
  912. testCopyWithoutAll
  913. self
  914. assert: ('*hello* *world*' copyWithoutAll: '*')
  915. equals: 'hello world'
  916. !
  917. testDetect
  918. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  919. self
  920. should: [ self collection detect: [ :each | each = 6 ] ]
  921. raise: Error
  922. !
  923. testEquality
  924. self assert: 'hello' equals: 'hello'.
  925. self deny: 'hello' = 'world'.
  926. "Test for issue 459"
  927. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  928. self assert: 'hello' equals: 'hello' yourself.
  929. self assert: 'hello' yourself equals: 'hello'.
  930. "test JS falsy value"
  931. self deny: '' = 0
  932. !
  933. testIdentity
  934. self assert: 'hello' == 'hello'.
  935. self deny: 'hello' == 'world'.
  936. self assert: 'hello' == 'hello' yourself.
  937. self assert: 'hello' yourself == 'hello'.
  938. "test JS falsy value"
  939. self deny: '' == 0
  940. !
  941. testIdentityHash
  942. self assert: 'foo' identityHash equals: 'foo' identityHash.
  943. self deny: ('foo' identityHash = 'bar' identityHash)
  944. !
  945. testIncludesSubString
  946. self assert: ('amber' includesSubString: 'ber').
  947. self deny: ('amber' includesSubString: 'zork').
  948. !
  949. testIsVowel
  950. |vowel consonant|
  951. vowel := 'u'.
  952. consonant := 'z'.
  953. self assert: vowel isVowel equals: true.
  954. self assert: consonant isVowel equals: false
  955. !
  956. testJoin
  957. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  958. !
  959. testRemoveAll
  960. self should: [ self collection removeAll ] raise: Error
  961. !
  962. testReversed
  963. self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
  964. !
  965. testSelect
  966. | newCollection |
  967. newCollection := 'o'.
  968. self
  969. assertSameContents: (self collection select: [ :each |
  970. each = 'o' ])
  971. as: newCollection
  972. !
  973. testSize
  974. self assert: 'smalltalk' size equals: 9.
  975. self assert: '' size equals: 0
  976. !
  977. testStreamContents
  978. self
  979. assert: (String streamContents: [ :aStream |
  980. aStream
  981. nextPutAll: 'hello'; space;
  982. nextPutAll: 'world' ])
  983. equals: 'hello world'
  984. !
  985. testSubStrings
  986. self assert: ('jackiechan' subStrings: 'ie') equals: #( 'jack' 'chan' ).
  987. !
  988. testTrim
  989. self assert: ' jackie' trimLeft equals: 'jackie'.
  990. self assert: 'jackie ' trimRight equals: 'jackie'.
  991. ! !
  992. !StringTest class methodsFor: 'fixture'!
  993. collectionClass
  994. ^ String
  995. ! !
  996. TestCase subclass: #ConsoleTranscriptTest
  997. instanceVariableNames: ''
  998. package: 'Kernel-Tests'!
  999. !ConsoleTranscriptTest methodsFor: 'tests'!
  1000. testShow
  1001. | originalTranscript |
  1002. originalTranscript := Transcript current.
  1003. Transcript register: ConsoleTranscript new.
  1004. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  1005. self shouldnt: [ Transcript show: console ] raise: Error.
  1006. Transcript register: originalTranscript.
  1007. ! !
  1008. TestCase subclass: #JSObjectProxyTest
  1009. instanceVariableNames: ''
  1010. package: 'Kernel-Tests'!
  1011. !JSObjectProxyTest methodsFor: 'accessing'!
  1012. jsObject
  1013. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': void 0}>
  1014. ! !
  1015. !JSObjectProxyTest methodsFor: 'tests'!
  1016. testAtIfAbsent
  1017. | testObject |
  1018. testObject := self jsObject.
  1019. self assert: (testObject at: 'abc' ifAbsent: [ 'Property does not exist' ]) equals: 'Property does not exist'.
  1020. self assert: (testObject at: 'e' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  1021. self assert: (testObject at: 'a' ifAbsent: [ 'Property does not exist' ]) equals: 1.
  1022. self assert: (testObject at: 'f' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  1023. !
  1024. testAtIfPresent
  1025. | testObject |
  1026. testObject := self jsObject.
  1027. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  1028. self assert: (testObject at: 'e' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  1029. self assert: (testObject at: 'a' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello 1'.
  1030. self assert: (testObject at: 'f' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  1031. !
  1032. testAtIfPresentIfAbsent
  1033. | testObject |
  1034. testObject := self jsObject.
  1035. self assert: (testObject at: 'abc' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'not present'.
  1036. self assert: (testObject at: 'e' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  1037. self assert: (testObject at: 'a' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello 1'.
  1038. self assert: (testObject at: 'f' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  1039. !
  1040. testAtPut
  1041. | testObject |
  1042. testObject := self jsObject.
  1043. self assert: (testObject at: 'abc') ~= 'xyz'.
  1044. self assert: (testObject at: 'abc' put: 'xyz') equals: 'xyz'.
  1045. self assert: (testObject at: 'abc') equals: 'xyz'
  1046. !
  1047. testDNU
  1048. self should: [ self jsObject foo ] raise: MessageNotUnderstood
  1049. !
  1050. testMessageSend
  1051. self assert: self jsObject a equals: 1.
  1052. self assert: self jsObject b equals: 2.
  1053. self assert: (self jsObject c: 3) equals: 3
  1054. !
  1055. testMethodWithArguments
  1056. self assert: (self jsObject c: 1) equals: 1
  1057. !
  1058. testPrinting
  1059. self assert: self jsObject printString equals: '[object Object]'
  1060. !
  1061. testPropertyThatReturnsEmptyString
  1062. | object |
  1063. object := self jsObject.
  1064. self assert: object d equals: ''.
  1065. object d: 'hello'.
  1066. self assert: object d equals: 'hello'
  1067. !
  1068. testPropertyThatReturnsUndefined
  1069. | object |
  1070. object := self jsObject.
  1071. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  1072. self assert: object e isNil
  1073. !
  1074. testValue
  1075. | testObject |
  1076. testObject := self jsObject.
  1077. testObject at: 'value' put: 'aValue'.
  1078. self assert: testObject value equals: 'aValue'
  1079. !
  1080. testYourself
  1081. | object |
  1082. object := self jsObject
  1083. d: 'test';
  1084. yourself.
  1085. self assert: object d equals: 'test'
  1086. ! !
  1087. TestCase subclass: #JavaScriptExceptionTest
  1088. instanceVariableNames: ''
  1089. package: 'Kernel-Tests'!
  1090. !JavaScriptExceptionTest methodsFor: 'helpers'!
  1091. throwException
  1092. <throw 'test'>
  1093. ! !
  1094. !JavaScriptExceptionTest methodsFor: 'tests'!
  1095. testCatchingException
  1096. [ self throwException ]
  1097. on: Error
  1098. do: [ :error |
  1099. self assert: error exception = 'test' ]
  1100. !
  1101. testRaisingException
  1102. self should: [ self throwException ] raise: JavaScriptException
  1103. ! !
  1104. TestCase subclass: #MessageSendTest
  1105. instanceVariableNames: ''
  1106. package: 'Kernel-Tests'!
  1107. !MessageSendTest methodsFor: 'tests'!
  1108. testValue
  1109. | messageSend |
  1110. messageSend := MessageSend new
  1111. receiver: Object new;
  1112. selector: #asString;
  1113. yourself.
  1114. self assert: messageSend value equals: 'an Object'
  1115. !
  1116. testValueWithArguments
  1117. | messageSend |
  1118. messageSend := MessageSend new
  1119. receiver: 2;
  1120. selector: '+';
  1121. yourself.
  1122. self assert: (messageSend value: 3) equals: 5.
  1123. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  1124. ! !
  1125. TestCase subclass: #MethodInheritanceTest
  1126. instanceVariableNames: 'receiverTop receiverMiddle receiverBottom method performBlock'
  1127. package: 'Kernel-Tests'!
  1128. !MethodInheritanceTest methodsFor: 'accessing'!
  1129. codeGeneratorClass
  1130. ^ CodeGenerator
  1131. !
  1132. targetClassBottom
  1133. ^ JavaScriptException
  1134. !
  1135. targetClassMiddle
  1136. ^ Error
  1137. !
  1138. targetClassTop
  1139. ^ Object
  1140. ! !
  1141. !MethodInheritanceTest methodsFor: 'factory'!
  1142. compiler
  1143. ^ Compiler new
  1144. codeGeneratorClass: self codeGeneratorClass;
  1145. yourself
  1146. ! !
  1147. !MethodInheritanceTest methodsFor: 'initialization'!
  1148. setUp
  1149. receiverTop := self targetClassTop new.
  1150. receiverMiddle := self targetClassMiddle new.
  1151. receiverBottom := self targetClassBottom new.
  1152. method := nil.
  1153. performBlock := [ self error: 'performBlock not initialized' ]
  1154. !
  1155. tearDown
  1156. [ self deinstallTop ] on: Error do: [ ].
  1157. [ self deinstallMiddle ] on: Error do: [ ].
  1158. [ self deinstallBottom ] on: Error do: [ ]
  1159. ! !
  1160. !MethodInheritanceTest methodsFor: 'testing'!
  1161. deinstallBottom
  1162. self targetClassBottom removeCompiledMethod: method
  1163. !
  1164. deinstallMiddle
  1165. self targetClassMiddle removeCompiledMethod: method
  1166. !
  1167. deinstallTop
  1168. self targetClassTop removeCompiledMethod: method
  1169. !
  1170. installBottom: aString
  1171. method := self compiler install: aString forClass: self targetClassBottom protocol: 'tests'
  1172. !
  1173. installMiddle: aString
  1174. method := self compiler install: aString forClass: self targetClassMiddle protocol: 'tests'
  1175. !
  1176. installTop: aString
  1177. method := self compiler install: aString forClass: self targetClassTop protocol: 'tests'
  1178. !
  1179. shouldMNU
  1180. self shouldMNUTop.
  1181. self shouldMNUMiddle.
  1182. self shouldMNUBottom
  1183. !
  1184. shouldMNUBottom
  1185. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  1186. !
  1187. shouldMNUMiddle
  1188. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  1189. !
  1190. shouldMNUTop
  1191. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  1192. !
  1193. shouldReturn: anObject
  1194. | result |
  1195. result := performBlock value: receiverTop.
  1196. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1197. result := performBlock value: receiverMiddle.
  1198. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  1199. result := performBlock value: receiverBottom.
  1200. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  1201. !
  1202. shouldReturn: anObject and: anObject2 and: anObject3
  1203. | result |
  1204. result := performBlock value: receiverTop.
  1205. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1206. result := performBlock value: receiverMiddle.
  1207. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  1208. result := performBlock value: receiverBottom.
  1209. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  1210. ! !
  1211. !MethodInheritanceTest methodsFor: 'tests'!
  1212. testMNU11
  1213. performBlock := [ :x | x foo ].
  1214. self shouldMNU.
  1215. self installTop: 'foo ^ false'.
  1216. self installTop: 'foo ^ true'.
  1217. self deinstallTop.
  1218. self shouldMNU
  1219. !
  1220. testMNU22
  1221. performBlock := [ :x | x foo ].
  1222. self shouldMNU.
  1223. self installMiddle: 'foo ^ false'.
  1224. self installMiddle: 'foo ^ true'.
  1225. self deinstallMiddle.
  1226. self shouldMNU
  1227. !
  1228. testReturns1
  1229. performBlock := [ :x | x foo ].
  1230. self installTop: 'foo ^ false'.
  1231. self shouldReturn: false.
  1232. self installTop: 'foo ^ true'.
  1233. self shouldReturn: true
  1234. ! !
  1235. TestCase subclass: #NumberTest
  1236. instanceVariableNames: ''
  1237. package: 'Kernel-Tests'!
  1238. !NumberTest methodsFor: 'tests'!
  1239. testAbs
  1240. self assert: 4 abs equals: 4.
  1241. self assert: -4 abs equals: 4
  1242. !
  1243. testArithmetic
  1244. "We rely on JS here, so we won't test complex behavior, just check if
  1245. message sends are corrects"
  1246. self assert: 1.5 + 1 equals: 2.5.
  1247. self assert: 2 - 1 equals: 1.
  1248. self assert: -2 - 1 equals: -3.
  1249. self assert: 12 / 2 equals: 6.
  1250. self assert: 3 * 4 equals: 12.
  1251. self assert: 7 // 2 equals: 3.
  1252. self assert: 7 \\ 2 equals: 1.
  1253. "Simple parenthesis and execution order"
  1254. self assert: 1 + 2 * 3 equals: 9.
  1255. self assert: 1 + (2 * 3) equals: 7
  1256. !
  1257. testAsNumber
  1258. self assert: 3 asNumber equals: 3.
  1259. !
  1260. testCeiling
  1261. self assert: 1.2 ceiling equals: 2.
  1262. self assert: -1.2 ceiling equals: -1.
  1263. self assert: 1.0 ceiling equals: 1.
  1264. !
  1265. testComparison
  1266. self assert: 3 > 2.
  1267. self assert: 2 < 3.
  1268. self deny: 3 < 2.
  1269. self deny: 2 > 3.
  1270. self assert: 3 >= 3.
  1271. self assert: 3.1 >= 3.
  1272. self assert: 3 <= 3.
  1273. self assert: 3 <= 3.1
  1274. !
  1275. testCopying
  1276. self assert: 1 copy == 1.
  1277. self assert: 1 deepCopy == 1
  1278. !
  1279. testEquality
  1280. self assert: (1 = 1).
  1281. self assert: (0 = 0).
  1282. self deny: (1 = 0).
  1283. self assert: (1 yourself = 1).
  1284. self assert: (1 = 1 yourself).
  1285. self assert: (1 yourself = 1 yourself).
  1286. self deny: 0 = false.
  1287. self deny: false = 0.
  1288. self deny: '' = 0.
  1289. self deny: 0 = ''
  1290. !
  1291. testFloor
  1292. self assert: 1.2 floor equals: 1.
  1293. self assert: -1.2 floor equals: -2.
  1294. self assert: 1.0 floor equals: 1.
  1295. !
  1296. testHexNumbers
  1297. self assert: 16r9 equals: 9.
  1298. self assert: 16rA truncated equals: 10.
  1299. self assert: 16rB truncated equals: 11.
  1300. self assert: 16rC truncated equals: 12.
  1301. self assert: 16rD truncated equals: 13.
  1302. self assert: 16rE truncated equals: 14.
  1303. self assert: 16rF truncated equals: 15
  1304. !
  1305. testIdentity
  1306. self assert: 1 == 1.
  1307. self assert: 0 == 0.
  1308. self deny: 1 == 0.
  1309. self assert: 1 yourself == 1.
  1310. self assert: 1 == 1 yourself.
  1311. self assert: 1 yourself == 1 yourself.
  1312. self deny: 1 == 2
  1313. !
  1314. testInvalidHexNumbers
  1315. self should: [ 16rG ] raise: MessageNotUnderstood.
  1316. self should: [ 16rg ] raise: MessageNotUnderstood.
  1317. self should: [ 16rH ] raise: MessageNotUnderstood.
  1318. self should: [ 16rh ] raise: MessageNotUnderstood.
  1319. self should: [ 16rI ] raise: MessageNotUnderstood.
  1320. self should: [ 16ri ] raise: MessageNotUnderstood.
  1321. self should: [ 16rJ ] raise: MessageNotUnderstood.
  1322. self should: [ 16rj ] raise: MessageNotUnderstood.
  1323. self should: [ 16rK ] raise: MessageNotUnderstood.
  1324. self should: [ 16rk ] raise: MessageNotUnderstood.
  1325. self should: [ 16rL ] raise: MessageNotUnderstood.
  1326. self should: [ 16rl ] raise: MessageNotUnderstood.
  1327. self should: [ 16rM ] raise: MessageNotUnderstood.
  1328. self should: [ 16rm ] raise: MessageNotUnderstood.
  1329. self should: [ 16rN ] raise: MessageNotUnderstood.
  1330. self should: [ 16rn ] raise: MessageNotUnderstood.
  1331. self should: [ 16rO ] raise: MessageNotUnderstood.
  1332. self should: [ 16ro ] raise: MessageNotUnderstood.
  1333. self should: [ 16rP ] raise: MessageNotUnderstood.
  1334. self should: [ 16rp ] raise: MessageNotUnderstood.
  1335. self should: [ 16rQ ] raise: MessageNotUnderstood.
  1336. self should: [ 16rq ] raise: MessageNotUnderstood.
  1337. self should: [ 16rR ] raise: MessageNotUnderstood.
  1338. self should: [ 16rr ] raise: MessageNotUnderstood.
  1339. self should: [ 16rS ] raise: MessageNotUnderstood.
  1340. self should: [ 16rs ] raise: MessageNotUnderstood.
  1341. self should: [ 16rT ] raise: MessageNotUnderstood.
  1342. self should: [ 16rt ] raise: MessageNotUnderstood.
  1343. self should: [ 16rU ] raise: MessageNotUnderstood.
  1344. self should: [ 16ru ] raise: MessageNotUnderstood.
  1345. self should: [ 16rV ] raise: MessageNotUnderstood.
  1346. self should: [ 16rv ] raise: MessageNotUnderstood.
  1347. self should: [ 16rW ] raise: MessageNotUnderstood.
  1348. self should: [ 16rw ] raise: MessageNotUnderstood.
  1349. self should: [ 16rX ] raise: MessageNotUnderstood.
  1350. self should: [ 16rx ] raise: MessageNotUnderstood.
  1351. self should: [ 16rY ] raise: MessageNotUnderstood.
  1352. self should: [ 16ry ] raise: MessageNotUnderstood.
  1353. self should: [ 16rZ ] raise: MessageNotUnderstood.
  1354. self should: [ 16rz ] raise: MessageNotUnderstood.
  1355. self should: [ 16rABcdEfZ ] raise: MessageNotUnderstood.
  1356. !
  1357. testLog
  1358. self assert: 10000 log equals: 4.
  1359. self assert: (512 log: 2) equals: 9.
  1360. self assert: Number e ln equals: 1.
  1361. !
  1362. testMinMax
  1363. self assert: (2 max: 5) equals: 5.
  1364. self assert: (2 min: 5) equals: 2
  1365. !
  1366. testNegated
  1367. self assert: 3 negated equals: -3.
  1368. self assert: -3 negated equals: 3
  1369. !
  1370. testPrintShowingDecimalPlaces
  1371. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1372. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1373. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1374. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1375. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1376. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1377. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1378. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1379. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1380. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1381. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1382. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  1383. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  1384. !
  1385. testRaisedTo
  1386. self assert: (2 raisedTo: 4) equals: 16.
  1387. self assert: (2 raisedTo: 0) equals: 1.
  1388. self assert: (2 raisedTo: -3) equals: 0.125.
  1389. self assert: (4 raisedTo: 0.5) equals: 2.
  1390. self assert: 2 ** 4 equals: 16.
  1391. !
  1392. testRounded
  1393. self assert: 3 rounded equals: 3.
  1394. self assert: 3.212 rounded equals: 3.
  1395. self assert: 3.51 rounded equals: 4
  1396. !
  1397. testSign
  1398. self assert: 5 sign equals: 1.
  1399. self assert: 0 sign equals: 0.
  1400. self assert: -1.4 sign equals: -1.
  1401. !
  1402. testSqrt
  1403. self assert: 4 sqrt equals: 2.
  1404. self assert: 16 sqrt equals: 4
  1405. !
  1406. testSquared
  1407. self assert: 4 squared equals: 16
  1408. !
  1409. testTimesRepeat
  1410. | i |
  1411. i := 0.
  1412. 0 timesRepeat: [ i := i + 1 ].
  1413. self assert: i equals: 0.
  1414. 5 timesRepeat: [ i := i + 1 ].
  1415. self assert: i equals: 5
  1416. !
  1417. testTo
  1418. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  1419. !
  1420. testToBy
  1421. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  1422. self should: [ 1 to: 4 by: 0 ] raise: Error
  1423. !
  1424. testTrigonometry
  1425. self assert: 0 cos equals: 1.
  1426. self assert: 0 sin equals: 0.
  1427. self assert: 0 tan equals: 0.
  1428. self assert: 1 arcCos equals: 0.
  1429. self assert: 0 arcSin equals: 0.
  1430. self assert: 0 arcTan equals: 0.
  1431. !
  1432. testTruncated
  1433. self assert: 3 truncated equals: 3.
  1434. self assert: 3.212 truncated equals: 3.
  1435. self assert: 3.51 truncated equals: 3
  1436. ! !
  1437. Object subclass: #ObjectMock
  1438. instanceVariableNames: 'foo bar'
  1439. package: 'Kernel-Tests'!
  1440. !ObjectMock commentStamp!
  1441. ObjectMock is there only to perform tests on classes.!
  1442. !ObjectMock methodsFor: 'not yet classified'!
  1443. foo
  1444. ^ foo
  1445. !
  1446. foo: anObject
  1447. foo := anObject
  1448. ! !
  1449. TestCase subclass: #ObjectTest
  1450. instanceVariableNames: ''
  1451. package: 'Kernel-Tests'!
  1452. !ObjectTest methodsFor: 'tests'!
  1453. notDefined
  1454. <return void 0;>
  1455. !
  1456. testBasicAccess
  1457. | o |
  1458. o := Object new.
  1459. o basicAt: 'a' put: 1.
  1460. self assert: (o basicAt: 'a') equals: 1.
  1461. self assert: (o basicAt: 'b') equals: nil
  1462. !
  1463. testBasicPerform
  1464. | o |
  1465. o := Object new.
  1466. o basicAt: 'func' put: [ 'hello' ].
  1467. o basicAt: 'func2' put: [ :a | a + 1 ].
  1468. self assert: (o basicPerform: 'func') equals: 'hello'.
  1469. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1470. !
  1471. testDNU
  1472. self should: [ Object new foo ] raise: MessageNotUnderstood
  1473. !
  1474. testEquality
  1475. | o |
  1476. o := Object new.
  1477. self deny: o = Object new.
  1478. self assert: (o = o).
  1479. self assert: (o yourself = o).
  1480. self assert: (o = o yourself)
  1481. !
  1482. testHalt
  1483. self should: [ Object new halt ] raise: Error
  1484. !
  1485. testIdentity
  1486. | o |
  1487. o := Object new.
  1488. self deny: o == Object new.
  1489. self assert: o == o.
  1490. self assert: o yourself == o.
  1491. self assert: o == o yourself
  1492. !
  1493. testIfNil
  1494. self deny: Object new isNil.
  1495. self deny: (Object new ifNil: [ true ]) = true.
  1496. self assert: (Object new ifNotNil: [ true ]) equals: true.
  1497. self assert: (Object new ifNil: [ false ] ifNotNil: [ true ]) equals: true.
  1498. self assert: (Object new ifNotNil: [ true ] ifNil: [ false ]) equals: true
  1499. !
  1500. testInstVars
  1501. | o |
  1502. o := ObjectMock new.
  1503. self assert: (o instVarAt: #foo) equals: nil.
  1504. o instVarAt: #foo put: 1.
  1505. self assert: (o instVarAt: #foo) equals: 1.
  1506. self assert: (o instVarAt: 'foo') equals: 1
  1507. !
  1508. testNilUndefined
  1509. "nil in Smalltalk is the undefined object in JS"
  1510. self assert: self notDefined equals: nil
  1511. !
  1512. testYourself
  1513. | o |
  1514. o := ObjectMock new.
  1515. self assert: o yourself == o
  1516. !
  1517. testidentityHash
  1518. | o1 o2 |
  1519. o1 := Object new.
  1520. o2 := Object new.
  1521. self assert: o1 identityHash == o1 identityHash.
  1522. self deny: o1 identityHash == o2 identityHash
  1523. ! !
  1524. TestCase subclass: #PointTest
  1525. instanceVariableNames: ''
  1526. package: 'Kernel-Tests'!
  1527. !PointTest methodsFor: 'tests'!
  1528. testAccessing
  1529. self assert: (Point x: 3 y: 4) x equals: 3.
  1530. self assert: (Point x: 3 y: 4) y equals: 4.
  1531. self assert: (Point new x: 3) x equals: 3.
  1532. self assert: (Point new y: 4) y equals: 4
  1533. !
  1534. testArithmetic
  1535. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1536. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1537. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1538. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1539. !
  1540. testAt
  1541. self assert: 3@4 equals: (Point x: 3 y: 4)
  1542. !
  1543. testEgality
  1544. self assert: (3@4 = (3@4)).
  1545. self deny: 3@5 = (3@6)
  1546. !
  1547. testNew
  1548. self assert: (Point new x: 3) y equals: nil.
  1549. self deny: (Point new x: 3) x = 0.
  1550. self assert: (Point new y: 4) x equals: nil.
  1551. self deny: (Point new y: 4) y = 0
  1552. !
  1553. testTranslateBy
  1554. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1555. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1556. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1557. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1558. ! !
  1559. TestCase subclass: #QueueTest
  1560. instanceVariableNames: ''
  1561. package: 'Kernel-Tests'!
  1562. !QueueTest methodsFor: 'tests'!
  1563. testNextIfAbsent
  1564. | queue |
  1565. queue := Queue new.
  1566. queue nextPut: 'index1'.
  1567. self assert: (queue nextIfAbsent: 'empty') = 'index1'.
  1568. self deny: (queue nextIfAbsent: 'empty') = 'index1'
  1569. !
  1570. testQueueNext
  1571. | queue |
  1572. queue := Queue new.
  1573. queue
  1574. nextPut: 'index1';
  1575. nextPut: 'index2'.
  1576. self assert: queue next = 'index1'.
  1577. self deny: queue next = 'index'.
  1578. self should: [ queue next ] raise: Error
  1579. ! !
  1580. TestCase subclass: #RandomTest
  1581. instanceVariableNames: ''
  1582. package: 'Kernel-Tests'!
  1583. !RandomTest methodsFor: 'tests'!
  1584. testAtRandomNumber
  1585. |val|
  1586. 100 timesRepeat: [
  1587. val := 10 atRandom.
  1588. self assert: (val > 0).
  1589. self assert: (val <11)
  1590. ]
  1591. !
  1592. testAtRandomSequenceableCollection
  1593. |val|
  1594. 100 timesRepeat: [
  1595. val := 'abc' atRandom.
  1596. self assert: ((val = 'a') | (val = 'b') | (val = 'c' )).
  1597. ].
  1598. !
  1599. textNext
  1600. 10000 timesRepeat: [
  1601. | current next |
  1602. next := Random new next.
  1603. self assert: (next >= 0).
  1604. self assert: (next < 1).
  1605. self deny: current = next.
  1606. next = current ]
  1607. ! !
  1608. TestCase subclass: #SetTest
  1609. instanceVariableNames: ''
  1610. package: 'Kernel-Tests'!
  1611. !SetTest methodsFor: 'tests'!
  1612. testAddRemove
  1613. | set |
  1614. set := Set new.
  1615. self assert: set isEmpty.
  1616. set add: 3.
  1617. self assert: (set includes: 3).
  1618. set add: 5.
  1619. self assert: (set includes: 5).
  1620. set remove: 3.
  1621. self deny: (set includes: 3)
  1622. !
  1623. testAt
  1624. self should: [ Set new at: 1 put: 2 ] raise: Error
  1625. !
  1626. testCollect
  1627. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1628. !
  1629. testComparing
  1630. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1631. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1632. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1633. self deny: #(1 2) asSet = #(0 2) asSet
  1634. !
  1635. testPrintString
  1636. | set |
  1637. set := Set new.
  1638. self assert: set printString equals: 'a Set ()'.
  1639. set add: 1; add: 3.
  1640. self assert: set printString equals: 'a Set (1 3)'.
  1641. set add: 'foo'.
  1642. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1643. set remove: 1; remove: 3.
  1644. self assert: set printString equals: 'a Set (''foo'')'.
  1645. set add: 3.
  1646. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1647. set add: 3.
  1648. self assert: set printString equals: 'a Set (''foo'' 3)'
  1649. !
  1650. testSize
  1651. self assert: Set new size equals: 0.
  1652. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1653. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1654. !
  1655. testUnboxedObjects
  1656. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1657. !
  1658. testUnicity
  1659. | set |
  1660. set := Set new.
  1661. set add: 21.
  1662. set add: 'hello'.
  1663. set add: 21.
  1664. self assert: set size equals: 2.
  1665. set add: 'hello'.
  1666. self assert: set size equals: 2.
  1667. self assert: set asArray equals: #(21 'hello')
  1668. ! !
  1669. TestCase subclass: #StreamTest
  1670. instanceVariableNames: ''
  1671. package: 'Kernel-Tests'!
  1672. !StreamTest methodsFor: 'accessing'!
  1673. collectionClass
  1674. ^ self class collectionClass
  1675. !
  1676. newCollection
  1677. ^ self collectionClass new
  1678. !
  1679. newStream
  1680. ^ self collectionClass new stream
  1681. ! !
  1682. !StreamTest methodsFor: 'tests'!
  1683. testAtStartAtEnd
  1684. | stream |
  1685. stream := self newStream.
  1686. self assert: stream atStart.
  1687. self assert: stream atEnd.
  1688. stream nextPutAll: self newCollection.
  1689. self assert: stream atEnd.
  1690. self deny: stream atStart.
  1691. stream position: 1.
  1692. self deny: stream atEnd.
  1693. self deny: stream atStart
  1694. !
  1695. testContents
  1696. | stream |
  1697. stream := self newStream.
  1698. stream nextPutAll: self newCollection.
  1699. self assert: stream contents equals: self newCollection
  1700. !
  1701. testIsEmpty
  1702. | stream |
  1703. stream := self newStream.
  1704. self assert: stream isEmpty.
  1705. stream nextPutAll: self newCollection.
  1706. self deny: stream isEmpty
  1707. !
  1708. testPosition
  1709. | collection stream |
  1710. collection := self newCollection.
  1711. stream := self newStream.
  1712. stream nextPutAll: collection.
  1713. self assert: stream position equals: collection size.
  1714. stream position: 0.
  1715. self assert: stream position equals: 0.
  1716. stream next.
  1717. self assert: stream position equals: 1.
  1718. stream next.
  1719. self assert: stream position equals: 2
  1720. !
  1721. testReading
  1722. | stream collection |
  1723. collection := self newCollection.
  1724. stream := self newStream.
  1725. stream
  1726. nextPutAll: collection;
  1727. position: 0.
  1728. collection do: [ :each |
  1729. self assert: stream next equals: each ].
  1730. self assert: stream next isNil
  1731. !
  1732. testStreamContents
  1733. !
  1734. testWrite
  1735. | stream collection |
  1736. collection := self newCollection.
  1737. stream := self newStream.
  1738. collection do: [ :each | stream << each ].
  1739. self assert: stream contents equals: collection
  1740. !
  1741. testWriting
  1742. | stream collection |
  1743. collection := self newCollection.
  1744. stream := self newStream.
  1745. collection do: [ :each | stream nextPut: each ].
  1746. self assert: stream contents equals: collection.
  1747. stream := self newStream.
  1748. stream nextPutAll: collection.
  1749. self assert: stream contents equals: collection
  1750. ! !
  1751. !StreamTest class methodsFor: 'accessing'!
  1752. collectionClass
  1753. ^ nil
  1754. ! !
  1755. !StreamTest class methodsFor: 'testing'!
  1756. isAbstract
  1757. ^ self collectionClass isNil
  1758. ! !
  1759. StreamTest subclass: #ArrayStreamTest
  1760. instanceVariableNames: ''
  1761. package: 'Kernel-Tests'!
  1762. !ArrayStreamTest methodsFor: 'accessing'!
  1763. newCollection
  1764. ^ { true. 1. 3@4. 'foo' }
  1765. ! !
  1766. !ArrayStreamTest class methodsFor: 'accessing'!
  1767. collectionClass
  1768. ^ Array
  1769. ! !
  1770. StreamTest subclass: #StringStreamTest
  1771. instanceVariableNames: ''
  1772. package: 'Kernel-Tests'!
  1773. !StringStreamTest methodsFor: 'accessing'!
  1774. newCollection
  1775. ^ 'hello world'
  1776. ! !
  1777. !StringStreamTest class methodsFor: 'accessing'!
  1778. collectionClass
  1779. ^ String
  1780. ! !
  1781. TestCase subclass: #UndefinedTest
  1782. instanceVariableNames: ''
  1783. package: 'Kernel-Tests'!
  1784. !UndefinedTest methodsFor: 'tests'!
  1785. testCopying
  1786. self assert: nil copy equals: nil
  1787. !
  1788. testDeepCopy
  1789. self assert: nil deepCopy = nil
  1790. !
  1791. testIfNil
  1792. self assert: (nil ifNil: [ true ]) equals: true.
  1793. self deny: (nil ifNotNil: [ true ]) = true.
  1794. self assert: (nil ifNil: [ true ] ifNotNil: [ false ]) equals: true.
  1795. self deny: (nil ifNotNil: [ true ] ifNil: [ false ]) = true
  1796. !
  1797. testIsNil
  1798. self assert: nil isNil.
  1799. self deny: nil notNil.
  1800. ! !