1
0

Kernel-Tests.st 62 KB

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