Kernel-Tests.st 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358
  1. Smalltalk createPackage: 'Kernel-Tests'!
  2. TestCase subclass: #AnnouncementSubscriptionTest
  3. slots: {}
  4. package: 'Kernel-Tests'!
  5. !AnnouncementSubscriptionTest methodsFor: 'tests'!
  6. testAddExtensionMethod
  7. | method dirty |
  8. dirty := self class package isDirty.
  9. self class package beClean.
  10. method := self class compile: 'doNothing' protocol: '**not-a-package'.
  11. self deny: self class package isDirty.
  12. self class removeCompiledMethod: method.
  13. dirty ifTrue: [ self class package beDirty ]
  14. !
  15. testHandlesAnnouncement
  16. | subscription announcementClass1 announcementClass2 classBuilder |
  17. classBuilder := ClassBuilder new.
  18. announcementClass1 := classBuilder addSubclassOf: SystemAnnouncement named: 'TestAnnouncement1' slots: #() package: 'Kernel-Tests'.
  19. subscription := AnnouncementSubscription new announcementClass: SystemAnnouncement.
  20. "Test whether the same class triggers the announcement"
  21. self assert: (subscription handlesAnnouncement: SystemAnnouncement new) equals: true.
  22. "Test whether a subclass triggers the announcement"
  23. self assert: (subscription handlesAnnouncement: announcementClass1 new) equals: true.
  24. "Test whether an unrelated class does not trigger the announcement"
  25. self assert: (subscription handlesAnnouncement: Object new) equals: false.
  26. classBuilder basicRemoveClass: announcementClass1.
  27. ! !
  28. TestCase subclass: #AnnouncerTest
  29. slots: {}
  30. package: 'Kernel-Tests'!
  31. !AnnouncerTest methodsFor: 'tests'!
  32. testOnDo
  33. | counter announcer |
  34. counter := 0.
  35. announcer := Announcer new.
  36. announcer on: SystemAnnouncement do: [ counter := counter + 1 ].
  37. announcer announce: (SystemAnnouncement new).
  38. self assert: counter equals: 1.
  39. announcer announce: (SystemAnnouncement new).
  40. self assert: counter equals: 2.
  41. !
  42. testOnDoFor
  43. | counter announcer |
  44. counter := 0.
  45. announcer := Announcer new.
  46. announcer on: SystemAnnouncement do: [ counter := counter + 1 ] for: self.
  47. announcer announce: (SystemAnnouncement new).
  48. self assert: counter equals: 1.
  49. announcer announce: (SystemAnnouncement new).
  50. self assert: counter equals: 2.
  51. announcer unsubscribe: self.
  52. announcer announce: (SystemAnnouncement new).
  53. self assert: counter equals: 2.
  54. !
  55. testOnDoOnce
  56. | counter announcer |
  57. counter := 0.
  58. announcer := Announcer new.
  59. announcer on: SystemAnnouncement doOnce: [ counter := counter + 1 ].
  60. announcer announce: (SystemAnnouncement new).
  61. self assert: counter equals: 1.
  62. announcer announce: (SystemAnnouncement new).
  63. self assert: counter equals: 1.
  64. ! !
  65. TestCase subclass: #BlockClosureTest
  66. slots: {}
  67. package: 'Kernel-Tests'!
  68. !BlockClosureTest methodsFor: 'fixture'!
  69. localReturnOnDoCatch
  70. [ ^ 2 ] on: Error do: [].
  71. ^ 3
  72. !
  73. localReturnOnDoMiss
  74. [ ^ 2 ] on: Class do: [].
  75. ^ 3
  76. ! !
  77. !BlockClosureTest methodsFor: 'tests'!
  78. testCanClearInterval
  79. self shouldnt: [ ([ Error new signal ] valueWithInterval: 0) clearInterval ] raise: Error
  80. !
  81. testCanClearTimeout
  82. self shouldnt: [ ([ Error new signal ] valueWithTimeout: 0) clearTimeout ] raise: Error
  83. !
  84. testCompiledSource
  85. self assert: ([ 1+1 ] compiledSource includesSubString: 'function')
  86. !
  87. testCurrySelf
  88. | curriedMethod array |
  89. curriedMethod := [ :selfarg :x | selfarg at: x ] currySelf asCompiledMethod: 'foo:'.
  90. curriedMethod protocol: '**test helper'.
  91. array := #(3 1 4).
  92. Array addCompiledMethod: curriedMethod.
  93. [ self assert: (array foo: 2) equals: 1 ]
  94. ensure: [ Array removeCompiledMethod: curriedMethod ]
  95. !
  96. testEnsure
  97. self assert: ([ 3 ] ensure: [ 4 ]) equals: 3
  98. !
  99. testEnsureRaises
  100. self should: [ [Error new signal ] ensure: [ true ]] raise: Error
  101. !
  102. testExceptionSemantics
  103. "See https://lolg.it/amber/amber/issues/314"
  104. self timeout: 100.
  105. (self async: [
  106. [
  107. self assert: true.
  108. Error signal.
  109. "The following should *not* be run"
  110. self deny: true.
  111. self finished.
  112. ] on: Error do: [ :ex | self finished ]
  113. ]) valueWithTimeout: 0
  114. !
  115. testLocalReturnOnDoCatch
  116. self assert: self localReturnOnDoCatch equals: 2
  117. !
  118. testLocalReturnOnDoMiss
  119. self assert: self localReturnOnDoMiss equals: 2
  120. !
  121. testNewWithValues
  122. <inlineJS: '
  123. function TestConstructor(arg1, arg2, arg3) {}
  124. TestConstructor.prototype.name = "theTestPrototype";
  125. var wrappedConstructor = $recv(TestConstructor);
  126. var result = wrappedConstructor._newWithValues_([1, 2, 3]);
  127. $self._assert_(result instanceof TestConstructor);
  128. $self._assert_equals_(result.name, "theTestPrototype");
  129. /* newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made. */
  130. $self._should_raise_(function () {wrappedConstructor._newWithValues_("single argument");}, $globals.Error);
  131. '>
  132. !
  133. testNumArgs
  134. self assert: [] numArgs equals: 0.
  135. self assert: [ :a :b | ] numArgs equals: 2
  136. !
  137. testOnDo
  138. self assert: ([ Error new signal ] on: Error do: [ :ex | true ])
  139. !
  140. testValue
  141. self assert: ([ 1+1 ] value) equals: 2.
  142. self assert: ([ :x | x +1 ] value: 2) equals: 3.
  143. self assert: ([ :x :y | x*y ] value: 2 value: 4) equals: 8.
  144. "Arguments are optional in Amber. This isn't ANSI compliant."
  145. self assert: ([ :a :b :c | 1 ] value) equals: 1
  146. !
  147. testValueWithPossibleArguments
  148. self assert: ([ 1 ] valueWithPossibleArguments: #(3 4)) equals: 1.
  149. self assert: ([ :a | a + 4 ] valueWithPossibleArguments: #(3 4)) equals: 7.
  150. self assert: ([ :a :b | a + b ] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  151. !
  152. testWhileFalse
  153. | i |
  154. i := 0.
  155. [ i > 5 ] whileFalse: [ i := i + 1 ].
  156. self assert: i equals: 6.
  157. i := 0.
  158. [ i := i + 1. i > 5 ] whileFalse.
  159. self assert: i equals: 6
  160. !
  161. testWhileTrue
  162. | i |
  163. i := 0.
  164. [ i < 5 ] whileTrue: [ i := i + 1 ].
  165. self assert: i equals: 5.
  166. i := 0.
  167. [ i := i + 1. i < 5 ] whileTrue.
  168. self assert: i equals: 5
  169. ! !
  170. TestCase subclass: #BooleanTest
  171. slots: {}
  172. package: 'Kernel-Tests'!
  173. !BooleanTest methodsFor: 'tests'!
  174. testEquality
  175. "We're on top of JS...just be sure to check the basics!!"
  176. self deny: 0 = false.
  177. self deny: false = 0.
  178. self deny: '' = false.
  179. self deny: false = ''.
  180. self assert: (true = true).
  181. self deny: false = true.
  182. self deny: true = false.
  183. self assert: (false = false).
  184. "JS may do some type coercing after sending a message"
  185. self assert: (true yourself = true).
  186. self assert: (true yourself = true yourself)
  187. !
  188. testIdentity
  189. "We're on top of JS...just be sure to check the basics!!"
  190. self deny: 0 == false.
  191. self deny: false == 0.
  192. self deny: '' == false.
  193. self deny: false == ''.
  194. self assert: true == true.
  195. self deny: false == true.
  196. self deny: true == false.
  197. self assert: false == false.
  198. "JS may do some type coercing after sending a message"
  199. self assert: true yourself == true.
  200. self assert: true yourself == true yourself
  201. !
  202. testIfTrueIfFalse
  203. self assert: (true ifTrue: [ 'alternative block' ]) equals: 'alternative block'.
  204. self assert: (true ifFalse: [ 'alternative block' ]) equals: nil.
  205. self assert: (false ifTrue: [ 'alternative block' ]) equals: nil.
  206. self assert: (false ifFalse: [ 'alternative block' ]) equals: 'alternative block'.
  207. self assert: (false ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block2'.
  208. self assert: (false ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block'.
  209. self assert: (true ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block'.
  210. self assert: (true ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block2'.
  211. !
  212. testIfTrueIfFalseWithBoxing
  213. self assert: (true yourself ifTrue: [ 'alternative block' ]) equals: 'alternative block'.
  214. self assert: (true yourself ifFalse: [ 'alternative block' ]) equals: nil.
  215. self assert: (false yourself ifTrue: [ 'alternative block' ]) equals: nil.
  216. self assert: (false yourself ifFalse: [ 'alternative block' ]) equals: 'alternative block'.
  217. self assert: (false yourself ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block2'.
  218. self assert: (false yourself ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block'.
  219. self assert: (true yourself ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block'.
  220. self assert: (true yourself ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block2'.
  221. !
  222. testLogic
  223. "Trivial logic table"
  224. self assert: (true & true);
  225. deny: (true & false);
  226. deny: (false & true);
  227. deny: (false & false).
  228. self assert: (true | true);
  229. assert: (true | false);
  230. assert: (false | true);
  231. deny: (false | false).
  232. "Checking that expressions work fine too"
  233. self assert: (true & (1 > 0));
  234. deny: ((1 > 0) & false);
  235. deny: ((1 > 0) & (1 > 2)).
  236. self assert: (false | (1 > 0));
  237. assert: ((1 > 0) | false);
  238. assert: ((1 > 0) | (1 > 2))
  239. !
  240. testLogicKeywords
  241. "Trivial logic table"
  242. self
  243. assert: (true and: [ true ]);
  244. deny: (true and: [ false ]);
  245. deny: (false and: [ true ]);
  246. deny: (false and: [ false ]).
  247. self
  248. assert: (true or: [ true ]);
  249. assert: (true or: [ false ]);
  250. assert: (false or: [ true ]);
  251. deny: (false or: [ false ]).
  252. "Checking that expressions work fine too"
  253. self
  254. assert: (true and: [ 1 > 0 ]);
  255. deny: ((1 > 0) and: [ false ]);
  256. deny: ((1 > 0) and: [ 1 > 2 ]).
  257. self
  258. assert: (false or: [ 1 > 0 ]);
  259. assert: ((1 > 0) or: [ false ]);
  260. assert: ((1 > 0) or: [ 1 > 2 ])
  261. !
  262. testNonBooleanError
  263. self should: [ '' ifTrue: [] ifFalse: [] ] raise: NonBooleanReceiver
  264. ! !
  265. TestCase subclass: #ClassBuilderTest
  266. slots: {#builder. #theClass}
  267. package: 'Kernel-Tests'!
  268. !ClassBuilderTest methodsFor: 'accessing'!
  269. theClass
  270. ^ theClass
  271. ! !
  272. !ClassBuilderTest methodsFor: 'running'!
  273. setUp
  274. builder := ClassBuilder new
  275. !
  276. tearDown
  277. self tearDownTheClass.
  278. theClass ifNotNil: [
  279. self deny: (theClass package classes includes: theClass).
  280. self assert: (Smalltalk globals at: theClass name) equals: nil ]
  281. ! !
  282. !ClassBuilderTest methodsFor: 'tests'!
  283. testAddTrait
  284. theClass := builder addTraitNamed: 'ObjectMock2' package: 'Kernel-Tests'.
  285. self assert: theClass name equals: 'ObjectMock2'.
  286. self assert: (theClass package classes occurrencesOf: theClass) equals: 1.
  287. self assert: theClass package equals: ObjectMock package
  288. !
  289. testClassCopy
  290. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  291. self assert: theClass name equals: 'ObjectMock2'.
  292. self assert: theClass isClassCopyOf: ObjectMock
  293. !
  294. testClassMigration
  295. | instance oldClass |
  296. oldClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  297. instance := (Smalltalk globals at: 'ObjectMock2') new.
  298. "Change the superclass of ObjectMock2"
  299. theClass := ObjectMock subclass: #ObjectMock2
  300. instanceVariableNames: ''
  301. package: 'Kernel-Tests'.
  302. self deny: oldClass == ObjectMock2.
  303. self assert: ObjectMock2 superclass == ObjectMock.
  304. self assert: ObjectMock2 slots isEmpty.
  305. self assert: ObjectMock2 selectors equals: oldClass selectors.
  306. self assert: ObjectMock2 comment equals: oldClass comment.
  307. self assert: ObjectMock2 package name equals: 'Kernel-Tests'.
  308. self assert: (ObjectMock2 package classes includes: ObjectMock2).
  309. self deny: instance class == ObjectMock2.
  310. self assert: (Smalltalk globals at: instance class name) isNil
  311. !
  312. testClassMigrationWithClassSlots
  313. builder copyClass: ObjectMock named: 'ObjectMock2'.
  314. ObjectMock2 class slots: #(foo bar).
  315. "Change the superclass of ObjectMock2"
  316. theClass := ObjectMock subclass: #ObjectMock2
  317. instanceVariableNames: ''
  318. package: 'Kernel-Tests'.
  319. self assert: ObjectMock2 class slots equals: #('foo' 'bar')
  320. !
  321. testClassMigrationWithSubclasses
  322. builder copyClass: ObjectMock named: 'ObjectMock2'.
  323. ObjectMock2 subclass: 'ObjectMock3' instanceVariableNames: '' package: 'Kernel-Tests'.
  324. ObjectMock3 subclass: 'ObjectMock4' instanceVariableNames: '' package: 'Kernel-Tests'.
  325. "Change the superclass of ObjectMock2"
  326. theClass := ObjectMock subclass: #ObjectMock2
  327. instanceVariableNames: ''
  328. package: 'Kernel-Tests'.
  329. self assert: ObjectMock subclasses equals: {ObjectMock2}.
  330. self assert: ObjectMock2 subclasses equals: {ObjectMock3}.
  331. self assert: ObjectMock3 subclasses equals: {ObjectMock4}
  332. !
  333. testSubclass
  334. theClass := builder addSubclassOf: ObjectMock named: 'ObjectMock2' slots: #(foo bar) package: 'Kernel-Tests'.
  335. self assert: theClass superclass equals: ObjectMock.
  336. self assert: theClass slots equals: #(foo bar).
  337. self assert: theClass name equals: 'ObjectMock2'.
  338. self assert: (theClass package classes occurrencesOf: theClass) equals: 1.
  339. self assert: theClass package equals: ObjectMock package.
  340. self assert: theClass methodDictionary keys size equals: 0
  341. ! !
  342. TestCase subclass: #ClassTest
  343. slots: {#builder. #theClass}
  344. package: 'Kernel-Tests'!
  345. !ClassTest methodsFor: 'accessing'!
  346. theClass
  347. ^ theClass
  348. ! !
  349. !ClassTest methodsFor: 'running'!
  350. augmentMethodInstantiationOf: aMethod withAttachments: aHashedCollection
  351. | plain |
  352. plain := aMethod instantiateFn.
  353. aMethod instantiateFn: [ :arg |
  354. (plain value: arg)
  355. basicAt: 'a$atx' put: aHashedCollection;
  356. yourself ]
  357. !
  358. jsConstructor
  359. <inlineJS: '
  360. function Foo(){}
  361. Foo.prototype.valueOf = function () {return 4;};
  362. return Foo;
  363. '>
  364. !
  365. jsConstructorWithAction
  366. <inlineJS: '
  367. function Foo(aFoo){this.foo = aFoo;}
  368. return Foo;
  369. '>
  370. !
  371. setUp
  372. builder := ClassBuilder new
  373. !
  374. trickyJsConstructor
  375. <inlineJS: '
  376. function Foo(){}
  377. Foo.prototype.valueOf = function () {return 4;};
  378. Foo.prototype._foo = function () {return "bar";};
  379. return Foo;
  380. '>
  381. ! !
  382. !ClassTest methodsFor: 'tests'!
  383. testAllSubclasses
  384. | subclasses index |
  385. subclasses := Object subclasses.
  386. index := 1.
  387. [ index > subclasses size ]
  388. whileFalse: [ subclasses addAll: (subclasses at: index) subclasses.
  389. index := index + 1 ].
  390. self assert: Object allSubclasses equals: subclasses
  391. !
  392. testAlternateConstructorViaSelector
  393. | instance block |
  394. block := ObjectMock alternateConstructorViaSelector: #foo:.
  395. instance := block newValue: 4.
  396. self assert: instance class == ObjectMock.
  397. self assert: instance foo equals: 4.
  398. self shouldnt: [ instance foo: 9 ] raise: Error.
  399. self assert: instance foo equals: 9
  400. !
  401. testApplySuperConstructor
  402. | instance constructor |
  403. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  404. constructor := self jsConstructorWithAction.
  405. theClass beJavaScriptSubclassOf: constructor.
  406. Compiler new
  407. install: 'bar: anObject
  408. ObjectMock2
  409. applySuperConstructorOn: self
  410. withArguments: {anObject}'
  411. forClass: theClass
  412. protocol: 'tests'.
  413. "testing specific to late-coupled detached root class"
  414. instance := (theClass alternateConstructorViaSelector: #bar:) newValue: 7.
  415. self assert: instance class == theClass.
  416. self assert: instance isJavaScriptInstanceOf: constructor.
  417. self assert: instance foo equals: 7
  418. !
  419. testBeJavaScriptSubclassOf
  420. | instance constructor |
  421. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  422. constructor := self jsConstructor.
  423. theClass beJavaScriptSubclassOf: constructor.
  424. self assert: theClass name equals: 'ObjectMock2'.
  425. self assert: theClass isClassCopyOf: ObjectMock.
  426. "testing specific to late-coupled detached root class"
  427. instance := theClass new.
  428. self assert: instance class == theClass.
  429. self assert: instance isJavaScriptInstanceOf: constructor.
  430. self assert: instance value equals: 4.
  431. self shouldnt: [ instance foo: 9 ] raise: Error.
  432. self assert: instance foo equals: 9
  433. !
  434. testMetaclassSubclasses
  435. | subclasses |
  436. subclasses := (Object class instanceClass subclasses
  437. select: [ :each | each isMetaclass not ])
  438. collect: [ :each | each theMetaClass ].
  439. self assert: Object class subclasses equals: subclasses
  440. !
  441. testMethodAttachmentsAreAdded
  442. | instance theMethod anObject |
  443. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  444. anObject := #{#foo -> 'oof'}.
  445. theMethod := Compiler new
  446. compile: 'bar' forClass: ObjectMock2 protocol: '**test'.
  447. self
  448. augmentMethodInstantiationOf: theMethod
  449. withAttachments: #{#a -> 42. #b -> anObject}.
  450. ObjectMock2 addCompiledMethod: theMethod.
  451. self assert: (ObjectMock2 new basicAt: #a) equals: 42.
  452. self assert: (ObjectMock2 new basicAt: #b) equals: anObject
  453. !
  454. testMethodAttachmentsAreRemoved
  455. | instance theMethod anObject |
  456. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  457. anObject := #{#foo -> 'oof'}.
  458. theMethod := Compiler new
  459. compile: 'bar' forClass: ObjectMock2 protocol: '**test'.
  460. self
  461. augmentMethodInstantiationOf: theMethod
  462. withAttachments: #{#a -> 42. #b -> anObject}.
  463. ObjectMock2 addCompiledMethod: theMethod.
  464. theMethod := Compiler new
  465. compile: 'bar' forClass: ObjectMock2 protocol: '**test'.
  466. ObjectMock2 addCompiledMethod: theMethod.
  467. self assert: (ObjectMock2 new basicAt: #a) equals: nil.
  468. self assert: (ObjectMock2 new basicAt: #b) equals: nil
  469. !
  470. testMethodAttachmentsAreRemoved2
  471. | instance theMethod anObject |
  472. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  473. anObject := #{#foo -> 'oof'}.
  474. theMethod := Compiler new
  475. compile: 'bar' forClass: ObjectMock2 protocol: '**test'.
  476. self
  477. augmentMethodInstantiationOf: theMethod
  478. withAttachments: #{#a -> 42. #b -> anObject}.
  479. ObjectMock2 addCompiledMethod: theMethod.
  480. ObjectMock2 new bar.
  481. ObjectMock2 removeCompiledMethod: theMethod.
  482. self assert: (ObjectMock2 new basicAt: #a) equals: nil.
  483. self assert: (ObjectMock2 new basicAt: #b) equals: nil
  484. !
  485. testMethodAttachmentsAreReplaced
  486. | instance theMethod anObject |
  487. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  488. anObject := #{#foo -> 'oof'}.
  489. theMethod := Compiler new
  490. compile: 'bar' forClass: ObjectMock2 protocol: '**test'.
  491. self
  492. augmentMethodInstantiationOf: theMethod
  493. withAttachments: #{#a -> 42. #b -> anObject}.
  494. ObjectMock2 addCompiledMethod: theMethod.
  495. theMethod := Compiler new
  496. compile: 'bar' forClass: ObjectMock2 protocol: '**test'.
  497. self
  498. augmentMethodInstantiationOf: theMethod
  499. withAttachments: #{#a -> 6. #c -> [^9]}.
  500. ObjectMock2 addCompiledMethod: theMethod.
  501. self assert: (ObjectMock2 new basicAt: #a) equals: 6.
  502. self assert: (ObjectMock2 new basicAt: #b) equals: nil.
  503. self assert: (ObjectMock2 new basicPerform: #c) equals: 9
  504. !
  505. testRespondsTo
  506. self assert: (Object new respondsTo: #class).
  507. self deny: (Object new respondsTo: #foo).
  508. self assert: (Object respondsTo: #new)
  509. !
  510. testSetJavaScriptConstructor
  511. | instance |
  512. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  513. theClass javaScriptConstructor: self jsConstructor.
  514. self assert: theClass name equals: 'ObjectMock2'.
  515. self assert: theClass isClassCopyOf: ObjectMock.
  516. "testing specific to late-coupled detached root class"
  517. instance := theClass new.
  518. self assert: instance class == theClass.
  519. self assert: instance value equals: 4.
  520. self shouldnt: [ instance foo: 9 ] raise: Error.
  521. self assert: instance foo equals: 9
  522. !
  523. testTrickySetJavaScriptConstructor
  524. | instance |
  525. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  526. theClass javaScriptConstructor: self trickyJsConstructor.
  527. self assert: theClass name equals: 'ObjectMock2'.
  528. self assert: theClass isClassCopyOf: ObjectMock.
  529. "testing specific to late-coupled detached root class"
  530. instance := theClass new.
  531. self assert: instance class == theClass.
  532. self assert: instance value equals: 4.
  533. self shouldnt: [ instance foo: 9 ] raise: Error.
  534. self assert: instance foo equals: 9
  535. ! !
  536. TestCase subclass: #CollectionTest
  537. slots: {#sampleBlock}
  538. package: 'Kernel-Tests'!
  539. !CollectionTest methodsFor: 'convenience'!
  540. assertSameContents: aCollection as: anotherCollection
  541. self assert: (aCollection size = anotherCollection size).
  542. aCollection do: [ :each |
  543. self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
  544. ! !
  545. !CollectionTest methodsFor: 'fixture'!
  546. collection
  547. "Answers pre-filled collection of type tested."
  548. self subclassResponsibility
  549. !
  550. collectionClass
  551. "Answers class of collection type tested"
  552. ^ self class collectionClass
  553. !
  554. collectionOfPrintStrings
  555. "Answers self collection but with values
  556. changed to their printStrings"
  557. self subclassResponsibility
  558. !
  559. collectionSize
  560. "Answers size of self collection."
  561. self subclassResponsibility
  562. !
  563. collectionWithDuplicates
  564. "Answers pre-filled collection of type tested,
  565. with exactly six distinct elements,
  566. some of them appearing multiple times, if possible."
  567. self subclassResponsibility
  568. !
  569. collectionWithNewValue
  570. "Answers a collection which shows how
  571. self collection would look after adding
  572. self sampleNewValue"
  573. self subclassResponsibility
  574. !
  575. sampleNewValue
  576. "Answers a value that is not yet there
  577. and can be put into a tested collection"
  578. ^ 'N'
  579. !
  580. sampleNewValueAsCollection
  581. "Answers self sampleNewValue
  582. wrapped in single element collection
  583. of tested type"
  584. ^ self collectionClass with: self sampleNewValue
  585. ! !
  586. !CollectionTest methodsFor: 'initialization'!
  587. initialize
  588. super initialize.
  589. sampleBlock := []
  590. ! !
  591. !CollectionTest methodsFor: 'tests'!
  592. testAddAll
  593. self assert: (self collection addAll: self collectionClass new; yourself) equals: self collection.
  594. self assert: (self collectionClass new addAll: self collection; yourself) equals: self collection.
  595. self assert: (self collectionClass new addAll: self collectionClass new; yourself) equals: self collectionClass new.
  596. self assert: (self collection addAll: self sampleNewValueAsCollection; yourself) equals: self collectionWithNewValue.
  597. self assertSameContents: (self sampleNewValueAsCollection addAll: self collection; yourself) as: self collectionWithNewValue
  598. !
  599. testAllSatisfy
  600. | collection anyOne |
  601. collection := self collection.
  602. anyOne := collection anyOne.
  603. self assert: (collection allSatisfy: [ :each | collection includes: each ]).
  604. self deny: (collection allSatisfy: [ :each | each ~= anyOne ])
  605. !
  606. testAnyOne
  607. self should: [ self collectionClass new anyOne ] raise: Error.
  608. self assert: (self collection includes: self collection anyOne)
  609. !
  610. testAnySatisfy
  611. | anyOne |
  612. anyOne := self collection anyOne.
  613. self assert: (self collection anySatisfy: [ :each | each = anyOne ]).
  614. self deny: (self collection anySatisfy: [ :each | each = Object new ])
  615. !
  616. testAsArray
  617. self
  618. assertSameContents: self collection
  619. as: self collection asArray
  620. !
  621. testAsOrderedCollection
  622. self
  623. assertSameContents: self collection
  624. as: self collection asOrderedCollection
  625. !
  626. testAsSet
  627. | c set |
  628. c := self collectionWithDuplicates.
  629. set := c asSet.
  630. self assert: set size equals: 6.
  631. c do: [ :each |
  632. self assert: (set includes: each) ]
  633. !
  634. testCollect
  635. self assert: (self collection collect: [ :each | each ]) equals: self collection.
  636. self assert: (self collectionWithNewValue collect: [ :each | each ]) equals: self collectionWithNewValue.
  637. self assert: (self collectionClass new collect: [ :each | each printString ]) equals: self collectionClass new.
  638. self assert: ((self collection collect: [ self sampleNewValue ]) detect: [ true ]) equals: self sampleNewValue.
  639. self assert: (self collection collect: [ :each | each printString ]) equals: self collectionOfPrintStrings
  640. !
  641. testComma
  642. self assert: self collection, self collectionClass new equals: self collection.
  643. self assert: self collectionClass new, self collection equals: self collection.
  644. self assert: self collectionClass new, self collectionClass new equals: self collectionClass new.
  645. self assert: self collection, self sampleNewValueAsCollection equals: self collectionWithNewValue
  646. !
  647. testCopy
  648. self assert: self collectionClass new copy equals: self collectionClass new.
  649. self assert: self collection copy equals: self collection.
  650. self assert: self collectionWithNewValue copy equals: self collectionWithNewValue.
  651. self deny: self collectionClass new copy = self collection.
  652. self deny: self collection copy = self collectionClass new.
  653. self deny: self collection copy = self collectionWithNewValue
  654. !
  655. testCopyEmpty
  656. self assert: self collectionClass new copyEmpty equals: self collectionClass new.
  657. self assert: self collection copyEmpty equals: self collectionClass new.
  658. self assert: self collectionWithNewValue copyEmpty equals: self collectionClass new
  659. !
  660. testCopySeparates
  661. | original copy |
  662. original := self collection.
  663. copy := original copy.
  664. copy addAll: self sampleNewValueAsCollection.
  665. self assert: original = self collection
  666. !
  667. testDetect
  668. self
  669. shouldnt: [ self collection detect: [ true ] ]
  670. raise: Error.
  671. self
  672. should: [ self collection detect: [ false ] ]
  673. raise: Error.
  674. self assert: (self sampleNewValueAsCollection detect: [ true ]) equals: self sampleNewValue.
  675. self assert: (self collectionWithNewValue detect: [ :each | each = self sampleNewValue ]) equals: self sampleNewValue.
  676. self
  677. should: [ self collection detect: [ :each | each = self sampleNewValue ] ]
  678. raise: Error
  679. !
  680. testDetectIfNone
  681. | sentinel |
  682. sentinel := Object new.
  683. self assert: (self collection detect: [ true ] ifNone: [ sentinel ]) ~= sentinel.
  684. self assert: (self collection detect: [ false ] ifNone: [ sentinel ]) equals: sentinel.
  685. self assert: (self sampleNewValueAsCollection detect: [ true ] ifNone: [ sentinel ]) equals: self sampleNewValue.
  686. self assert: (self collectionWithNewValue detect: [ :each | each = self sampleNewValue ] ifNone: [ sentinel ]) equals: self sampleNewValue.
  687. self assert: (self collection detect: [ :each | each = self sampleNewValue ] ifNone: [ sentinel ]) equals: sentinel
  688. !
  689. testDo
  690. | newCollection |
  691. newCollection := OrderedCollection new.
  692. self collection do: [ :each |
  693. newCollection add: each ].
  694. self
  695. assertSameContents: self collection
  696. as: newCollection.
  697. newCollection := OrderedCollection new.
  698. self collectionWithDuplicates do: [ :each |
  699. newCollection add: each ].
  700. self
  701. assertSameContents: self collectionWithDuplicates
  702. as: newCollection
  703. !
  704. testEquality
  705. self assert: self collectionClass new equals: self collectionClass new.
  706. self assert: self collection equals: self collection.
  707. self assert: self collectionWithNewValue equals: self collectionWithNewValue.
  708. self deny: self collectionClass new = self collection.
  709. self deny: self collection = self collectionClass new.
  710. self deny: self collection = self collectionWithNewValue
  711. !
  712. testIfEmptyFamily
  713. self assert: (self collectionClass new ifEmpty: [ 42 ]) equals: 42.
  714. self assert: (self collection ifEmpty: [ 42 ]) equals: self collection.
  715. self assert: (self collectionClass new ifNotEmpty: [ 42 ]) equals: self collectionClass new.
  716. self assert: (self collection ifNotEmpty: [ 42 ]) equals: 42.
  717. self assert: (self collection ifNotEmpty: [ :col | col ]) equals: self collection.
  718. self assert: (self collectionClass new ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 42.
  719. self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 999.
  720. self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ :col | col ]) equals: self collection.
  721. self assert: (self collectionClass new ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 999.
  722. self assert: (self collection ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 42.
  723. self assert: (self collection ifNotEmpty: [ :col | col ] ifEmpty: [ 999 ]) equals: self collection.
  724. !
  725. testIsEmpty
  726. self assert: self collectionClass new isEmpty.
  727. self deny: self collection isEmpty
  728. !
  729. testNoneSatisfy
  730. | anyOne |
  731. anyOne := self collection anyOne.
  732. self deny: (self collection noneSatisfy: [ :each | each = anyOne ]).
  733. self assert: (self collection noneSatisfy: [ :each | each = Object new ])
  734. !
  735. testRegression1224
  736. self assert: (self collectionClass new
  737. remove: self sampleNewValue ifAbsent: [];
  738. yourself) size equals: 0
  739. !
  740. testRemoveAll
  741. self assert: (self collection removeAll; yourself) equals: self collectionClass new
  742. !
  743. testSelect
  744. self assert: (self collection select: [ false ]) equals: self collectionClass new.
  745. self assert: (self collection select: [ true ]) equals: self collection.
  746. self assert: (self collectionWithNewValue select: [ :each | each = self sampleNewValue ]) equals: self sampleNewValueAsCollection.
  747. self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ]) equals: self collection.
  748. self assert: (self collection select: [ :each | each = self sampleNewValue ]) equals: self collectionClass new.
  749. self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ]) equals: self collection
  750. !
  751. testSelectThenCollect
  752. self assert: (self collection select: [ false ] thenCollect: #isString) equals: self collectionClass new.
  753. self assert: (self collection select: [ true ] thenCollect: [:x|x]) equals: self collection.
  754. self assert: (self collection select: [ :each | each = self sampleNewValue ] thenCollect: [:x|x]) equals: self collectionClass new.
  755. self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ] thenCollect: #printString) equals: self collectionOfPrintStrings
  756. !
  757. testSingle
  758. self should: [ self collectionClass new single ] raise: Error.
  759. self should: [ self collection single ] raise: Error.
  760. self assert: self sampleNewValueAsCollection single equals: self sampleNewValue
  761. !
  762. testSize
  763. self assert: self collectionClass new size equals: 0.
  764. self assert: self sampleNewValueAsCollection size equals: 1.
  765. self assert: self collection size equals: self collectionSize
  766. ! !
  767. !CollectionTest class methodsFor: 'fixture'!
  768. collectionClass
  769. "Answers class of collection type tested,
  770. or nil if test is abstract"
  771. ^ nil
  772. ! !
  773. !CollectionTest class methodsFor: 'testing'!
  774. isAbstract
  775. ^ self collectionClass isNil
  776. ! !
  777. CollectionTest subclass: #AssociativeCollectionTest
  778. slots: {}
  779. package: 'Kernel-Tests'!
  780. !AssociativeCollectionTest methodsFor: 'fixture'!
  781. collectionKeys
  782. self subclassResponsibility
  783. !
  784. collectionValues
  785. self subclassResponsibility
  786. !
  787. nonIndexesDo: aBlock
  788. aBlock value: 5.
  789. aBlock value: [].
  790. aBlock value: Object new.
  791. aBlock value: 'z'
  792. !
  793. sampleNewIndex
  794. ^ 'new'
  795. !
  796. samplesDo: aBlock
  797. aBlock value: 'a' value: 2
  798. ! !
  799. !AssociativeCollectionTest methodsFor: 'tests'!
  800. testAddAll
  801. super testAddAll.
  802. self assert: (self collection addAll: self collection; yourself) equals: self collection.
  803. self assert: (self collection addAll: self collectionWithNewValue; yourself) equals: self collectionWithNewValue.
  804. self assert: (self collectionWithNewValue addAll: self collection; yourself) equals: self collectionWithNewValue
  805. !
  806. testAsDictionary
  807. self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
  808. !
  809. testAsHashedCollection
  810. self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
  811. !
  812. testFrom
  813. "Accept a collection of associations."
  814. | associations |
  815. associations := { 'a' -> 1. 'b' -> 2 }.
  816. self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  817. !
  818. testKeys
  819. self assert:self collectionClass new keys isEmpty.
  820. self assertSameContents:self collection keys as: self collectionKeys.
  821. self assertSameContents:self collectionWithNewValue keys as: self collectionKeys, { self sampleNewIndex }
  822. !
  823. testNewFromPairs
  824. "Accept an array in which all odd indexes are keys and evens are values."
  825. | flattenedAssociations |
  826. flattenedAssociations := { 'a'. 1. 'b'. 2 }.
  827. self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  828. !
  829. testPrintString
  830. self
  831. assert: (self collectionClass new
  832. at:'firstname' put: 'James';
  833. at:'lastname' put: 'Bond';
  834. printString)
  835. equals: 'a ', self collectionClass name, ' (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  836. !
  837. testRemoveKey
  838. self nonIndexesDo: [ :each |
  839. | collection |
  840. collection := self collection.
  841. self should: [ collection removeKey: each ] raise: Error.
  842. self assert: collection equals: self collection ].
  843. self samplesDo: [ :index :value |
  844. | collection |
  845. collection := self collection.
  846. self assert: (collection removeKey: index) equals: value.
  847. self deny: collection = self collection ].
  848. self
  849. assert: (self collectionWithNewValue removeKey: self sampleNewIndex; yourself)
  850. equals: self collection
  851. !
  852. testRemoveKeyIfAbsent
  853. self nonIndexesDo: [ :each |
  854. | collection |
  855. collection := self collection.
  856. self assert: (collection removeKey: each ifAbsent: [ self sampleNewValue ]) equals: self sampleNewValue.
  857. self assert: collection equals: self collection ].
  858. self samplesDo: [ :index :value |
  859. | collection |
  860. collection := self collection.
  861. self assert: (collection removeKey: index ifAbsent: [ self sampleNewValue ]) equals: value.
  862. self deny: collection = self collection ].
  863. self
  864. assert: (self collectionWithNewValue removeKey: self sampleNewIndex ifAbsent: [ self assert: false ]; yourself)
  865. equals: self collection
  866. !
  867. testUnorderedComma
  868. self assert: self collection, self collection equals: self collection.
  869. self assert: self sampleNewValueAsCollection, self collection equals: self collectionWithNewValue.
  870. self assert: self collection, self collectionWithNewValue equals: self collectionWithNewValue.
  871. self assert: self collectionWithNewValue, self collection equals: self collectionWithNewValue
  872. !
  873. testValues
  874. self assert:self collectionClass new values isEmpty.
  875. self assertSameContents:self collection values as: self collectionValues.
  876. self assertSameContents:self collectionWithNewValue values as: self collectionValues, { self sampleNewValue }
  877. ! !
  878. AssociativeCollectionTest subclass: #DictionaryTest
  879. slots: {}
  880. package: 'Kernel-Tests'!
  881. !DictionaryTest methodsFor: 'fixture'!
  882. collection
  883. ^ Dictionary new
  884. at: 1 put: 1;
  885. at: 'a' put: 2;
  886. at: true put: 3;
  887. at: 1@3 put: -4;
  888. at: sampleBlock put: 9;
  889. yourself
  890. !
  891. collectionKeys
  892. ^ {1. 'a'. true. 1@3. sampleBlock}
  893. !
  894. collectionOfPrintStrings
  895. ^ Dictionary new
  896. at: 1 put: '1';
  897. at: 'a' put: '2';
  898. at: true put: '3';
  899. at: 1@3 put: '-4';
  900. at: sampleBlock put: '9';
  901. yourself
  902. !
  903. collectionSize
  904. ^ 5
  905. !
  906. collectionValues
  907. ^ {1. 2. 3. -4. 9}
  908. !
  909. collectionWithDuplicates
  910. ^ Dictionary new
  911. at: 1 put: 1;
  912. at: 'a' put: 2;
  913. at: true put: 3;
  914. at: 4 put: -4;
  915. at: sampleBlock put: 9;
  916. at: 'b' put: 1;
  917. at: 3 put: 3;
  918. at: false put: 12;
  919. yourself
  920. !
  921. collectionWithNewValue
  922. ^ Dictionary new
  923. at: 1 put: 1;
  924. at: 'a' put: 2;
  925. at: true put: 3;
  926. at: 1@3 put: -4;
  927. at: sampleBlock put: 9;
  928. at: 'new' put: 'N';
  929. yourself
  930. !
  931. sampleNewValueAsCollection
  932. ^ Dictionary new
  933. at: 'new' put: 'N';
  934. yourself
  935. !
  936. samplesDo: aBlock
  937. super samplesDo: aBlock.
  938. aBlock value: true value: 3.
  939. aBlock value: 1@3 value: -4.
  940. aBlock value: sampleBlock value: 9
  941. ! !
  942. !DictionaryTest methodsFor: 'tests'!
  943. testAccessing
  944. | d |
  945. d := Dictionary new.
  946. d at: 'hello' put: 'world'.
  947. self assert: (d at: 'hello') equals: 'world'.
  948. self assert: (d at: 'hello' ifAbsent: [ nil ]) equals: 'world'.
  949. self deny: (d at: 'foo' ifAbsent: [ nil ]) = 'world'.
  950. self assert: (d includesKey: 'hello').
  951. self deny: (d includesKey: 'foo').
  952. d at: 1 put: 2.
  953. self assert: (d at: 1) equals: 2.
  954. d at: 1@3 put: 3.
  955. self assert: (d at: 1@3) equals: 3.
  956. self assert: (d includesKey: 1@3).
  957. self deny: (d includesKey: 3@1)
  958. !
  959. testDynamicDictionaries
  960. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  961. ! !
  962. !DictionaryTest class methodsFor: 'fixture'!
  963. collectionClass
  964. ^ Dictionary
  965. ! !
  966. AssociativeCollectionTest subclass: #HashedCollectionTest
  967. slots: {}
  968. package: 'Kernel-Tests'!
  969. !HashedCollectionTest methodsFor: 'fixture'!
  970. collection
  971. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  972. !
  973. collectionKeys
  974. ^ { 'b'. 'a'. 'c'. 'd' }
  975. !
  976. collectionOfPrintStrings
  977. ^ #{ 'b' -> '1'. 'a' -> '2'. 'c' -> '3'. 'd' -> '-4' }
  978. !
  979. collectionSize
  980. ^ 4
  981. !
  982. collectionValues
  983. ^ { 1. 2. 3. -4 }
  984. !
  985. collectionWithDuplicates
  986. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10. 'h' -> 0 }
  987. !
  988. collectionWithNewValue
  989. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'new' -> 'N' }
  990. !
  991. sampleNewValueAsCollection
  992. ^ #{ 'new' -> 'N' }
  993. ! !
  994. !HashedCollectionTest methodsFor: 'tests'!
  995. testDynamicDictionaries
  996. self assert: #{'hello' -> 1} asHashedCollection equals: (HashedCollection with: 'hello' -> 1)
  997. ! !
  998. !HashedCollectionTest class methodsFor: 'fixture'!
  999. collectionClass
  1000. ^ HashedCollection
  1001. ! !
  1002. CollectionTest subclass: #SequenceableCollectionTest
  1003. slots: {}
  1004. package: 'Kernel-Tests'!
  1005. !SequenceableCollectionTest methodsFor: 'fixture'!
  1006. collectionFirst
  1007. self subclassResponsibility
  1008. !
  1009. collectionFirstTwo
  1010. self subclassResponsibility
  1011. !
  1012. collectionLast
  1013. self subclassResponsibility
  1014. !
  1015. collectionLastTwo
  1016. self subclassResponsibility
  1017. !
  1018. nonIndexesDo: aBlock
  1019. aBlock value: 0.
  1020. aBlock value: self collectionSize + 1.
  1021. aBlock value: 'z'
  1022. !
  1023. samplesDo: aBlock
  1024. aBlock value: 1 value: self collectionFirst.
  1025. aBlock value: self collectionSize value: self collectionLast
  1026. ! !
  1027. !SequenceableCollectionTest methodsFor: 'tests'!
  1028. testBeginsWith
  1029. self assert: (self collection beginsWith: self collectionClass new).
  1030. self assert: (self collection beginsWith: self collection).
  1031. self assert: (self collection beginsWith: self collectionFirstTwo).
  1032. self deny: (self collection beginsWith: self collectionLastTwo)
  1033. !
  1034. testEndsWith
  1035. self assert: (self collection endsWith: self collectionClass new).
  1036. self assert: (self collection endsWith: self collection).
  1037. self assert: (self collection endsWith: self collectionLastTwo).
  1038. self deny: (self collection endsWith: self collectionFirstTwo)
  1039. !
  1040. testFirst
  1041. self assert: self collection first equals: self collectionFirst
  1042. !
  1043. testFirstN
  1044. self
  1045. assert: (self collection first: 2)
  1046. equals: self collectionFirstTwo.
  1047. self
  1048. assert: (self collection first: 0)
  1049. equals: self collectionClass new.
  1050. self
  1051. assert: (self collection first: self collectionSize)
  1052. equals: self collection.
  1053. self should: [ self collection first: 33 ] raise: Error
  1054. !
  1055. testFourth
  1056. self assert: (self collection fourth) equals: (self collection at: 4)
  1057. !
  1058. testIndexOfStartingAt
  1059. | jsNull |
  1060. jsNull := JSON parse: 'null'.
  1061. self samplesDo: [ :index :value |
  1062. self assert: (self collection indexOf: value startingAt: 1) equals: index.
  1063. self assert: (self collection indexOf: value startingAt: index) equals: index.
  1064. self assert: (self collection indexOf: value startingAt: index+1) equals: 0 ]
  1065. !
  1066. testIndexOfStartingAtWithNull
  1067. | jsNull |
  1068. jsNull := JSON parse: 'null'.
  1069. self samplesDo: [ :index :value | | collection |
  1070. collection := self collection.
  1071. collection at: index put: jsNull.
  1072. self assert: (collection indexOf: jsNull startingAt: 1) equals: index.
  1073. self assert: (collection indexOf: jsNull startingAt: index) equals: index.
  1074. self assert: (collection indexOf: jsNull startingAt: index+1) equals: 0 ]
  1075. !
  1076. testLast
  1077. self assert: self collection last equals: self collectionLast
  1078. !
  1079. testLastN
  1080. self
  1081. assert: (self collection last: 2)
  1082. equals: self collectionLastTwo.
  1083. self
  1084. assert: (self collection last: 0)
  1085. equals: self collectionClass new.
  1086. self
  1087. assert: (self collection last: self collectionSize)
  1088. equals: self collection.
  1089. self should: [ self collection last: 33 ] raise: Error
  1090. !
  1091. testOrderedComma
  1092. self assertSameContents: self sampleNewValueAsCollection, self collection as: self collectionWithNewValue
  1093. !
  1094. testSecond
  1095. self assert: (self collection second) equals: (self collection at: 2)
  1096. !
  1097. testThird
  1098. self assert: (self collection third) equals: (self collection at: 3)
  1099. ! !
  1100. SequenceableCollectionTest subclass: #ArrayTest
  1101. slots: {}
  1102. package: 'Kernel-Tests'!
  1103. !ArrayTest methodsFor: 'fixture'!
  1104. collection
  1105. ^ #(1 2 3 -4)
  1106. !
  1107. collectionFirst
  1108. ^ 1
  1109. !
  1110. collectionFirstTwo
  1111. ^ #(1 2)
  1112. !
  1113. collectionLast
  1114. ^ -4
  1115. !
  1116. collectionLastTwo
  1117. ^ #(3 -4)
  1118. !
  1119. collectionOfPrintStrings
  1120. ^ #('1' '2' '3' '-4')
  1121. !
  1122. collectionSize
  1123. ^ 4
  1124. !
  1125. collectionWithDuplicates
  1126. ^ #('a' 'b' 'c' 1 2 1 'a' ())
  1127. !
  1128. collectionWithNewValue
  1129. ^ #(1 2 3 -4 'N')
  1130. !
  1131. sampleNewIndex
  1132. ^ 5
  1133. !
  1134. samplesDo: aBlock
  1135. super samplesDo: aBlock.
  1136. aBlock value: 3 value: 3.
  1137. ! !
  1138. !ArrayTest methodsFor: 'tests'!
  1139. testAdd
  1140. | array |
  1141. array := self collection.
  1142. array add: 6.
  1143. self assert: array last equals: 6
  1144. !
  1145. testAddFirst
  1146. self assert: (self collection addFirst: 0; yourself) first equals: 0
  1147. !
  1148. testPrintString
  1149. | array |
  1150. array := Array new.
  1151. self assert: array printString equals: 'an Array ()'.
  1152. array add: 1; add: 3.
  1153. self assert: array printString equals: 'an Array (1 3)'.
  1154. array add: 'foo'.
  1155. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  1156. array remove: 1; remove: 3.
  1157. self assert: array printString equals: 'an Array (''foo'')'.
  1158. array addLast: 3.
  1159. self assert: array printString equals: 'an Array (''foo'' 3)'.
  1160. array addLast: 3.
  1161. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  1162. !
  1163. testRemove
  1164. | array |
  1165. array := #(1 2 3 4 5).
  1166. array remove: 3.
  1167. self assert: array equals: #(1 2 4 5).
  1168. self should: [ array remove: 3 ] raise: Error
  1169. !
  1170. testRemoveFromTo
  1171. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  1172. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  1173. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  1174. !
  1175. testRemoveIndex
  1176. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  1177. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  1178. self assert: (#('hello') removeIndex: 1) equals: #()
  1179. !
  1180. testRemoveLast
  1181. | array |
  1182. array := #(1 2).
  1183. array removeLast.
  1184. self assert: array last equals: 1
  1185. !
  1186. testReversed
  1187. |array|
  1188. array := #(5 4 3 2 1).
  1189. self assert: (array reversed) equals: #(1 2 3 4 5)
  1190. !
  1191. testSort
  1192. | array |
  1193. array := #(10 1 5).
  1194. array sort.
  1195. self assert: array equals: #(1 5 10)
  1196. ! !
  1197. !ArrayTest class methodsFor: 'fixture'!
  1198. collectionClass
  1199. ^ Array
  1200. ! !
  1201. SequenceableCollectionTest subclass: #StringTest
  1202. slots: {}
  1203. package: 'Kernel-Tests'!
  1204. !StringTest methodsFor: 'fixture'!
  1205. collection
  1206. ^ 'helLo'
  1207. !
  1208. collectionFirst
  1209. ^ 'h'
  1210. !
  1211. collectionFirstTwo
  1212. ^ 'he'
  1213. !
  1214. collectionLast
  1215. ^ 'o'
  1216. !
  1217. collectionLastTwo
  1218. ^ 'Lo'
  1219. !
  1220. collectionOfPrintStrings
  1221. ^ '''h''''e''''l''''L''''o'''
  1222. !
  1223. collectionSize
  1224. ^ 5
  1225. !
  1226. collectionWithDuplicates
  1227. ^ 'abbaerten'
  1228. !
  1229. collectionWithNewValue
  1230. ^ 'helLoN'
  1231. !
  1232. sampleNewValueAsCollection
  1233. ^ 'N'
  1234. !
  1235. samplesDo: aBlock
  1236. super samplesDo: aBlock.
  1237. aBlock value: 3 value: 'l'
  1238. ! !
  1239. !StringTest methodsFor: 'tests'!
  1240. testAddAll
  1241. "String instances are read-only"
  1242. self should: [ self collection addAll: self collection ] raise: Error
  1243. !
  1244. testAddRemove
  1245. self should: [ 'hello' add: 'a' ] raise: Error.
  1246. self should: [ 'hello' remove: 'h' ] raise: Error
  1247. !
  1248. testAsArray
  1249. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  1250. !
  1251. testAsLowerCase
  1252. self assert: 'JACKIE' asLowercase equals: 'jackie'.
  1253. !
  1254. testAsNumber
  1255. self assert: '3' asNumber equals: 3.
  1256. self assert: '-3' asNumber equals: -3.
  1257. self assert: '-1.5' asNumber equals: -1.5.
  1258. !
  1259. testAsUpperCase
  1260. self assert: 'jackie' asUppercase equals: 'JACKIE'.
  1261. !
  1262. testAsciiValue
  1263. | characterA characterU |
  1264. characterA := 'A'.
  1265. characterU := 'U'.
  1266. self assert: (characterA asciiValue) equals:65.
  1267. self assert: (characterU asciiValue) equals:85
  1268. !
  1269. testAtIfAbsentPut
  1270. "String instances are read-only"
  1271. self should: [ 'hello' at: 6 ifAbsentPut: [ 'a' ] ] raise: Error
  1272. !
  1273. testAtPut
  1274. "String instances are read-only"
  1275. self should: [ 'hello' at: 1 put: 'a' ] raise: Error
  1276. !
  1277. testCapitalized
  1278. self assert: 'test' capitalized equals: 'Test'.
  1279. self assert: 'Test' capitalized equals: 'Test'.
  1280. self assert: '' capitalized equals: ''.
  1281. self assert: 'Test' isCapitalized equals: true.
  1282. self assert: 'test' isCapitalized equals: false.
  1283. !
  1284. testCharCodeAt
  1285. self assert: ('jackie' charCodeAt:1) equals: 106.
  1286. self assert: ('jackie' charCodeAt:2) equals: 97.
  1287. self assert: ('jackie' charCodeAt:3) equals: 99.
  1288. self assert: ('jackie' charCodeAt:4) equals: 107.
  1289. self assert: ('jackie' charCodeAt:5) equals: 105.
  1290. self assert: ('jackie' charCodeAt:6) equals: 101
  1291. !
  1292. testCopyFromTo
  1293. self assert: ('jackie' copyFrom: 1 to: 3) equals: 'jac'.
  1294. self assert: ('jackie' copyFrom: 4 to: 6) equals: 'kie'.
  1295. !
  1296. testCopySeparates
  1297. "String instances are immutable"
  1298. self assert: self collection copy == self collection
  1299. !
  1300. testCopyWithoutAll
  1301. self
  1302. assert: ('*hello* *world*' copyWithoutAll: '*')
  1303. equals: 'hello world'
  1304. !
  1305. testEquality
  1306. self assert: 'hello' equals: 'hello'.
  1307. self deny: 'hello' = 'world'.
  1308. "Test for issue 459"
  1309. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  1310. self assert: 'hello' equals: 'hello' yourself.
  1311. self assert: 'hello' yourself equals: 'hello'.
  1312. "test JS falsy value"
  1313. self deny: '' = 0
  1314. !
  1315. testIdentity
  1316. self assert: 'hello' == 'hello'.
  1317. self deny: 'hello' == 'world'.
  1318. self assert: 'hello' == 'hello' yourself.
  1319. self assert: 'hello' yourself == 'hello'.
  1320. "test JS falsy value"
  1321. self deny: '' == 0
  1322. !
  1323. testIncludesSubString
  1324. self assert: ('amber' includesSubString: 'ber').
  1325. self deny: ('amber' includesSubString: 'zork').
  1326. !
  1327. testIndexOfStartingAtWithNull
  1328. "String cannot hold JS null"
  1329. !
  1330. testIndexOfWithNull
  1331. "String cannot hold JS null"
  1332. !
  1333. testIsVowel
  1334. |vowel consonant|
  1335. vowel := 'u'.
  1336. consonant := 'z'.
  1337. self assert: vowel isVowel equals: true.
  1338. self assert: consonant isVowel equals: false
  1339. !
  1340. testJoin
  1341. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  1342. !
  1343. testRegression1224
  1344. "String instances are read-only"
  1345. self should: [ (self collectionClass new
  1346. remove: self sampleNewValue ifAbsent: [];
  1347. yourself) size ] raise: Error
  1348. !
  1349. testRemoveAll
  1350. self should: [ self collection removeAll ] raise: Error
  1351. !
  1352. testReversed
  1353. self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
  1354. !
  1355. testStreamContents
  1356. self
  1357. assert: (String streamContents: [ :aStream |
  1358. aStream
  1359. nextPutAll: 'hello'; space;
  1360. nextPutAll: 'world' ])
  1361. equals: 'hello world'
  1362. !
  1363. testSubStrings
  1364. self assert: ('jackiechan' subStrings: 'ie') equals: #( 'jack' 'chan' ).
  1365. !
  1366. testTrim
  1367. self assert: ' jackie' trimLeft equals: 'jackie'.
  1368. self assert: 'jackie ' trimRight equals: 'jackie'.
  1369. !
  1370. testValue
  1371. self assert: (#asString value: 1) equals: '1'.
  1372. "Which (since String and BlockClosure are now polymorphic) enables the nice idiom..."
  1373. self assert: (#(1 2 3) collect: #asString) equals: #('1' '2' '3')
  1374. ! !
  1375. !StringTest class methodsFor: 'fixture'!
  1376. collectionClass
  1377. ^ String
  1378. ! !
  1379. CollectionTest subclass: #SetTest
  1380. slots: {}
  1381. package: 'Kernel-Tests'!
  1382. !SetTest methodsFor: 'fixture'!
  1383. collection
  1384. ^ Set new
  1385. add: Smalltalk;
  1386. add: nil;
  1387. add: 3@3;
  1388. add: false;
  1389. add: sampleBlock;
  1390. yourself
  1391. !
  1392. collectionOfPrintStrings
  1393. ^ Set new
  1394. add: 'a SmalltalkImage';
  1395. add: 'nil';
  1396. add: '3@3';
  1397. add: 'false';
  1398. add: 'a BlockClosure';
  1399. yourself
  1400. !
  1401. collectionSize
  1402. ^ 5
  1403. !
  1404. collectionWithDuplicates
  1405. "Set has no duplicates"
  1406. ^ self collection add: 0; yourself
  1407. !
  1408. collectionWithNewValue
  1409. ^ Set new
  1410. add: Smalltalk;
  1411. add: nil;
  1412. add: 3@3;
  1413. add: 'N';
  1414. add: false;
  1415. add: sampleBlock;
  1416. yourself
  1417. ! !
  1418. !SetTest methodsFor: 'tests'!
  1419. testAddAll
  1420. super testAddAll.
  1421. self assert: (self collection addAll: self collection; yourself) equals: self collection.
  1422. self assert: (self collection addAll: self collectionWithNewValue; yourself) equals: self collectionWithNewValue.
  1423. self assert: (self collectionWithNewValue addAll: self collection; yourself) equals: self collectionWithNewValue
  1424. !
  1425. testAddRemove
  1426. | set |
  1427. set := Set new.
  1428. self assert: set isEmpty.
  1429. set add: 3.
  1430. self assert: (set includes: 3).
  1431. set add: 5.
  1432. self assert: (set includes: 5).
  1433. set remove: 3.
  1434. self deny: (set includes: 3)
  1435. !
  1436. testAt
  1437. self should: [ Set new at: 1 put: 2 ] raise: Error
  1438. !
  1439. testCollect
  1440. super testCollect.
  1441. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1442. !
  1443. testComparing
  1444. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1445. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1446. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1447. self deny: #(1 2) asSet = #(0 2) asSet
  1448. !
  1449. testPrintString
  1450. | set |
  1451. set := Set new.
  1452. self assert: set printString equals: 'a Set ()'.
  1453. set add: 1; add: 3.
  1454. self assert: set printString equals: 'a Set (1 3)'.
  1455. set add: 'foo'.
  1456. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1457. set remove: 1; remove: 3.
  1458. self assert: set printString equals: 'a Set (''foo'')'.
  1459. set add: 3.
  1460. self assert: set printString equals: 'a Set (3 ''foo'')'.
  1461. set add: 3.
  1462. self assert: set printString equals: 'a Set (3 ''foo'')'
  1463. !
  1464. testRegression1225
  1465. self assert: (#(1 2 3) asSet add: 3) equals: 3
  1466. !
  1467. testRegression1226
  1468. self assert: (#(1 2 3) asSet remove: 3) equals: 3
  1469. !
  1470. testRegression1227
  1471. self assert: (#(1 2 3) asSet remove: 4 ifAbsent: [5]) equals: 5
  1472. !
  1473. testRegression1228
  1474. self should: [#(1 2 3) asSet remove: 4] raise: Error
  1475. !
  1476. testRegression1245
  1477. self assert: ({Object. String} asSet remove: String) equals: String
  1478. !
  1479. testUnboxedObjects
  1480. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1481. !
  1482. testUnicity
  1483. | set |
  1484. set := Set new.
  1485. set add: 21.
  1486. set add: 'hello'.
  1487. set add: 21.
  1488. self assert: set size equals: 2.
  1489. set add: 'hello'.
  1490. self assert: set size equals: 2.
  1491. self assert: set asArray equals: #(21 'hello')
  1492. !
  1493. testUnorderedComma
  1494. self assert: self collection, self collection equals: self collection.
  1495. self assert: self sampleNewValueAsCollection, self collection equals: self collectionWithNewValue.
  1496. self assert: self collection, self collectionWithNewValue equals: self collectionWithNewValue.
  1497. self assert: self collectionWithNewValue, self collection equals: self collectionWithNewValue
  1498. ! !
  1499. !SetTest class methodsFor: 'fixture'!
  1500. collectionClass
  1501. ^ Set
  1502. ! !
  1503. TestCase subclass: #ConsoleTranscriptTest
  1504. slots: {}
  1505. package: 'Kernel-Tests'!
  1506. !ConsoleTranscriptTest methodsFor: 'tests'!
  1507. testShow
  1508. | originalTranscript |
  1509. originalTranscript := Transcript current.
  1510. Transcript register: ConsoleTranscript new.
  1511. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  1512. self shouldnt: [ Transcript show: console ] raise: Error.
  1513. Transcript register: originalTranscript.
  1514. ! !
  1515. TestCase subclass: #DateTest
  1516. slots: {}
  1517. package: 'Kernel-Tests'!
  1518. !DateTest methodsFor: 'tests'!
  1519. testEquality
  1520. | now |
  1521. now := Date new.
  1522. self assert: now = now.
  1523. self deny: now = (Date fromMilliseconds: 0).
  1524. self assert: (Date fromMilliseconds: 12345678) = (Date fromMilliseconds: 12345678).
  1525. self assert: now = (Date fromMilliseconds: now asMilliseconds).
  1526. self assert: (Date fromMilliseconds: now asMilliseconds) = now
  1527. !
  1528. testIdentity
  1529. | now |
  1530. now := Date new.
  1531. self assert: now == now.
  1532. self deny: now == (Date fromMilliseconds: 0).
  1533. self deny: (Date fromMilliseconds: 12345678) == (Date fromMilliseconds: 12345678).
  1534. self deny: now == (Date fromMilliseconds: now asMilliseconds).
  1535. self deny: (Date fromMilliseconds: now asMilliseconds) == now
  1536. !
  1537. testPlusAndMinus
  1538. | a b now zeroDuration |
  1539. a := Date fromString: '1974-07-12 14:30'.
  1540. now := Date now.
  1541. b := Date fromString: '2616-03-06'.
  1542. zeroDuration := 0.
  1543. self assert: {a-a. now-now. b-b} asSet equals: (Set with: zeroDuration).
  1544. self assert: now + (b - now) equals: b.
  1545. self assert: a + (b - a) equals: b.
  1546. self assert: now + (a - now) equals: a.
  1547. self assert: a + ((now - a) + (b - now)) equals: b
  1548. ! !
  1549. TestCase subclass: #JSObjectProxyTest
  1550. slots: {}
  1551. package: 'Kernel-Tests'!
  1552. !JSObjectProxyTest methodsFor: 'accessing'!
  1553. jsObject
  1554. <inlineJS:
  1555. 'return {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: "", "e": null, "f": void 0}'>
  1556. !
  1557. jsUndefined
  1558. <inlineJS: 'return'>
  1559. ! !
  1560. !JSObjectProxyTest methodsFor: 'tests'!
  1561. testAtIfAbsent
  1562. | testObject |
  1563. testObject := self jsObject.
  1564. self assert: (testObject at: 'abc' ifAbsent: [ 'Property does not exist' ]) equals: 'Property does not exist'.
  1565. self assert: (testObject at: 'e' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  1566. self assert: (testObject at: 'a' ifAbsent: [ 'Property does not exist' ]) equals: 1.
  1567. self assert: (testObject at: 'f' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  1568. !
  1569. testAtIfPresent
  1570. | testObject |
  1571. testObject := self jsObject.
  1572. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  1573. self assert: (testObject at: 'e' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  1574. self assert: (testObject at: 'a' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello 1'.
  1575. self assert: (testObject at: 'f' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  1576. !
  1577. testAtIfPresentIfAbsent
  1578. | testObject |
  1579. testObject := self jsObject.
  1580. self assert: (testObject at: 'abc' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'not present'.
  1581. self assert: (testObject at: 'e' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  1582. self assert: (testObject at: 'a' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello 1'.
  1583. self assert: (testObject at: 'f' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  1584. !
  1585. testAtPut
  1586. | testObject |
  1587. testObject := self jsObject.
  1588. self assert: (testObject at: 'abc') ~= 'xyz'.
  1589. self assert: (testObject at: 'abc' put: 'xyz') equals: 'xyz'.
  1590. self assert: (testObject at: 'abc') equals: 'xyz'
  1591. !
  1592. testComparison
  1593. self assert: ({ console. 2 } indexOf: console) equals: 1.
  1594. self assert: console = console.
  1595. self deny: console = Object new.
  1596. self deny: console = self jsObject
  1597. !
  1598. testDNU
  1599. self should: [ self jsObject foo ] raise: MessageNotUnderstood
  1600. !
  1601. testDNUWithAllowJavaScriptCalls
  1602. | jsObject |
  1603. jsObject := #().
  1604. jsObject basicAt: 'allowJavaScriptCalls' put: true.
  1605. self should: [ jsObject foo ] raise: MessageNotUnderstood
  1606. !
  1607. testMessageSend
  1608. self assert: self jsObject a equals: 1.
  1609. self assert: self jsObject b equals: 2.
  1610. self assert: (self jsObject c: 3) equals: 3
  1611. !
  1612. testMethodWithArguments
  1613. self assert: (self jsObject c: 1) equals: 1
  1614. !
  1615. testNull
  1616. self assert: JSObjectProxy null isNil.
  1617. self
  1618. assert: (JSON stringify: #{#foo -> JSObjectProxy null})
  1619. equals: '{"foo":null}'
  1620. !
  1621. testPrinting
  1622. self assert: self jsObject printString equals: '[object Object]'
  1623. !
  1624. testPropertyThatReturnsEmptyString
  1625. | object |
  1626. object := self jsObject.
  1627. self assert: object d equals: ''.
  1628. object d: 'hello'.
  1629. self assert: object d equals: 'hello'
  1630. !
  1631. testPropertyThatReturnsUndefined
  1632. | object |
  1633. object := self jsObject.
  1634. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  1635. self assert: object e isNil
  1636. !
  1637. testSetPropertyWithFalsyValue
  1638. | jsObject |
  1639. jsObject := self jsObject.
  1640. self assert: (jsObject a) equals: 1.
  1641. jsObject a: JSObjectProxy null.
  1642. self assert: (jsObject a) equals: nil.
  1643. jsObject a: 0.
  1644. self assert: (jsObject a) equals: 0.
  1645. jsObject a: self jsUndefined.
  1646. self assert: (jsObject a) equals: nil.
  1647. jsObject a: ''.
  1648. self assert: (jsObject a) equals: ''.
  1649. jsObject a: false.
  1650. self assert: (jsObject a) equals: false
  1651. !
  1652. testUndefined
  1653. self assert: JSObjectProxy undefined isNil.
  1654. self
  1655. assert: (JSON stringify: #{#foo -> JSObjectProxy undefined})
  1656. equals: '{}'
  1657. !
  1658. testValue
  1659. | testObject |
  1660. testObject := self jsObject.
  1661. testObject at: 'value' put: 'aValue'.
  1662. self assert: testObject value equals: 'aValue'
  1663. !
  1664. testYourself
  1665. | object |
  1666. object := self jsObject
  1667. d: 'test';
  1668. yourself.
  1669. self assert: object d equals: 'test'
  1670. ! !
  1671. TestCase subclass: #JavaScriptExceptionTest
  1672. slots: {}
  1673. package: 'Kernel-Tests'!
  1674. !JavaScriptExceptionTest methodsFor: 'helpers'!
  1675. throwException
  1676. <inlineJS: 'throw "test"'>
  1677. ! !
  1678. !JavaScriptExceptionTest methodsFor: 'tests'!
  1679. testCatchingException
  1680. [ self throwException ]
  1681. on: Error
  1682. do: [ :error |
  1683. self assert: error exception = 'test' ]
  1684. !
  1685. testRaisingException
  1686. self should: [ self throwException ] raise: JavaScriptException
  1687. ! !
  1688. TestCase subclass: #MessageSendTest
  1689. slots: {}
  1690. package: 'Kernel-Tests'!
  1691. !MessageSendTest methodsFor: 'tests'!
  1692. testValue
  1693. | messageSend |
  1694. messageSend := MessageSend new
  1695. receiver: Object new;
  1696. selector: #asString;
  1697. yourself.
  1698. self assert: messageSend value equals: 'an Object'
  1699. !
  1700. testValueWithArguments
  1701. | messageSend |
  1702. messageSend := MessageSend new
  1703. receiver: 2;
  1704. selector: '+';
  1705. yourself.
  1706. self assert: (messageSend value: 3) equals: 5.
  1707. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  1708. ! !
  1709. TestCase subclass: #MethodInheritanceTest
  1710. slots: {#receiverTop. #receiverMiddle. #receiverBottom. #method. #performBlock}
  1711. package: 'Kernel-Tests'!
  1712. !MethodInheritanceTest methodsFor: 'accessing'!
  1713. codeGeneratorClass
  1714. ^ CodeGenerator
  1715. !
  1716. targetClassBottom
  1717. ^ JavaScriptException
  1718. !
  1719. targetClassMiddle
  1720. ^ Error
  1721. !
  1722. targetClassTop
  1723. ^ Object
  1724. ! !
  1725. !MethodInheritanceTest methodsFor: 'factory'!
  1726. compiler
  1727. ^ Compiler new
  1728. codeGeneratorClass: self codeGeneratorClass;
  1729. yourself
  1730. ! !
  1731. !MethodInheritanceTest methodsFor: 'initialization'!
  1732. setUp
  1733. receiverTop := self targetClassTop new.
  1734. receiverMiddle := self targetClassMiddle new.
  1735. receiverBottom := self targetClassBottom new.
  1736. method := nil.
  1737. performBlock := [ self error: 'performBlock not initialized' ]
  1738. !
  1739. tearDown
  1740. [ self deinstallTop ] on: Error do: [ ].
  1741. [ self deinstallMiddle ] on: Error do: [ ].
  1742. [ self deinstallBottom ] on: Error do: [ ]
  1743. ! !
  1744. !MethodInheritanceTest methodsFor: 'testing'!
  1745. deinstallBottom
  1746. self targetClassBottom removeCompiledMethod: method
  1747. !
  1748. deinstallMiddle
  1749. self targetClassMiddle removeCompiledMethod: method
  1750. !
  1751. deinstallTop
  1752. self targetClassTop removeCompiledMethod: method
  1753. !
  1754. installBottom: aString
  1755. method := self compiler install: aString forClass: self targetClassBottom protocol: 'tests'
  1756. !
  1757. installMiddle: aString
  1758. method := self compiler install: aString forClass: self targetClassMiddle protocol: 'tests'
  1759. !
  1760. installTop: aString
  1761. method := self compiler install: aString forClass: self targetClassTop protocol: 'tests'
  1762. !
  1763. shouldMNU
  1764. self shouldMNUTop.
  1765. self shouldMNUMiddle.
  1766. self shouldMNUBottom
  1767. !
  1768. shouldMNUBottom
  1769. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  1770. !
  1771. shouldMNUMiddle
  1772. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  1773. !
  1774. shouldMNUTop
  1775. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  1776. !
  1777. shouldReturn: anObject
  1778. | result |
  1779. result := performBlock value: receiverTop.
  1780. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1781. result := performBlock value: receiverMiddle.
  1782. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  1783. result := performBlock value: receiverBottom.
  1784. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  1785. !
  1786. shouldReturn: anObject and: anObject2 and: anObject3
  1787. | result |
  1788. result := performBlock value: receiverTop.
  1789. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1790. result := performBlock value: receiverMiddle.
  1791. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  1792. result := performBlock value: receiverBottom.
  1793. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  1794. ! !
  1795. !MethodInheritanceTest methodsFor: 'tests'!
  1796. testMNU11
  1797. performBlock := [ :x | x foo ].
  1798. self shouldMNU.
  1799. self installTop: 'foo ^ false'.
  1800. self installTop: 'foo ^ true'.
  1801. self deinstallTop.
  1802. self shouldMNU
  1803. !
  1804. testMNU22
  1805. performBlock := [ :x | x foo ].
  1806. self shouldMNU.
  1807. self installMiddle: 'foo ^ false'.
  1808. self installMiddle: 'foo ^ true'.
  1809. self deinstallMiddle.
  1810. self shouldMNU
  1811. !
  1812. testReturns1
  1813. performBlock := [ :x | x foo ].
  1814. self installTop: 'foo ^ false'.
  1815. self shouldReturn: false.
  1816. self installTop: 'foo ^ true'.
  1817. self shouldReturn: true
  1818. ! !
  1819. TestCase subclass: #NumberTest
  1820. slots: {}
  1821. package: 'Kernel-Tests'!
  1822. !NumberTest methodsFor: 'tests'!
  1823. testAbs
  1824. self assert: 4 abs equals: 4.
  1825. self assert: -4 abs equals: 4
  1826. !
  1827. testArithmetic
  1828. "We rely on JS here, so we won't test complex behavior, just check if
  1829. message sends are corrects"
  1830. self assert: 1.5 + 1 equals: 2.5.
  1831. self assert: 2 - 1 equals: 1.
  1832. self assert: -2 - 1 equals: -3.
  1833. self assert: 12 / 2 equals: 6.
  1834. self assert: 3 * 4 equals: 12.
  1835. self assert: 7 // 2 equals: 3.
  1836. self assert: 7 \\ 2 equals: 1.
  1837. "Simple parenthesis and execution order"
  1838. self assert: 1 + 2 * 3 equals: 9.
  1839. self assert: 1 + (2 * 3) equals: 7
  1840. !
  1841. testAsNumber
  1842. self assert: 3 asNumber equals: 3.
  1843. !
  1844. testBetweenAnd
  1845. self assert: (4 between: 3 and: 5).
  1846. self assert: (1 between: 5 and: 6) not.
  1847. self assert: (90 between: 67 and: 87) not.
  1848. self assert: (1 between: 1 and: 1).
  1849. !
  1850. testBitAnd
  1851. self assert: (15 bitAnd: 2) equals: 2.
  1852. self assert: (15 bitAnd: 15) equals: 15.
  1853. self assert: (-1 bitAnd: 1021) equals: 1021
  1854. !
  1855. testBitNot
  1856. self assert: 2 bitNot equals: -3.
  1857. self assert: -1 bitNot equals: 0.
  1858. self assert: -1022 bitNot equals: 1021
  1859. !
  1860. testBitOr
  1861. self assert: (2 bitOr: 4) equals: 6.
  1862. self assert: (7 bitOr: 2) equals: 7.
  1863. self assert: (-1 bitOr: 1021) equals: -1
  1864. !
  1865. testBitXor
  1866. self assert: (2 bitXor: 4) equals: 6.
  1867. self assert: (7 bitXor: 2) equals: 5.
  1868. self assert: (-1 bitXor: 1021) equals: -1022.
  1869. self assert: (91 bitXor: 91) equals: 0
  1870. !
  1871. testCeiling
  1872. self assert: 1.2 ceiling equals: 2.
  1873. self assert: -1.2 ceiling equals: -1.
  1874. self assert: 1.0 ceiling equals: 1.
  1875. !
  1876. testComparison
  1877. self assert: 3 > 2.
  1878. self assert: 2 < 3.
  1879. self deny: 3 < 2.
  1880. self deny: 2 > 3.
  1881. self assert: 3 >= 3.
  1882. self assert: 3.1 >= 3.
  1883. self assert: 3 <= 3.
  1884. self assert: 3 <= 3.1
  1885. !
  1886. testCopying
  1887. self assert: 1 copy == 1.
  1888. self assert: 1 deepCopy == 1
  1889. !
  1890. testDegreesToRadians
  1891. self assert: (180 degreesToRadians - Number pi) abs <= 0.01.
  1892. !
  1893. testEquality
  1894. self assert: (1 = 1).
  1895. self assert: (0 = 0).
  1896. self deny: (1 = 0).
  1897. self assert: (1 yourself = 1).
  1898. self assert: (1 = 1 yourself).
  1899. self assert: (1 yourself = 1 yourself).
  1900. self deny: 0 = false.
  1901. self deny: false = 0.
  1902. self deny: '' = 0.
  1903. self deny: 0 = ''
  1904. !
  1905. testFloor
  1906. self assert: 1.2 floor equals: 1.
  1907. self assert: -1.2 floor equals: -2.
  1908. self assert: 1.0 floor equals: 1.
  1909. !
  1910. testHexNumbers
  1911. self assert: 16r9 equals: 9.
  1912. self assert: 16rA truncated equals: 10.
  1913. self assert: 16rB truncated equals: 11.
  1914. self assert: 16rC truncated equals: 12.
  1915. self assert: 16rD truncated equals: 13.
  1916. self assert: 16rE truncated equals: 14.
  1917. self assert: 16rF truncated equals: 15
  1918. !
  1919. testIdentity
  1920. self assert: 1 == 1.
  1921. self assert: 0 == 0.
  1922. self deny: 1 == 0.
  1923. self assert: 1 yourself == 1.
  1924. self assert: 1 == 1 yourself.
  1925. self assert: 1 yourself == 1 yourself.
  1926. self deny: 1 == 2
  1927. !
  1928. testInvalidHexNumbers
  1929. self should: [ 16rG ] raise: MessageNotUnderstood.
  1930. self should: [ 16rg ] raise: MessageNotUnderstood.
  1931. self should: [ 16rH ] raise: MessageNotUnderstood.
  1932. self should: [ 16rh ] raise: MessageNotUnderstood.
  1933. self should: [ 16rI ] raise: MessageNotUnderstood.
  1934. self should: [ 16ri ] raise: MessageNotUnderstood.
  1935. self should: [ 16rJ ] raise: MessageNotUnderstood.
  1936. self should: [ 16rj ] raise: MessageNotUnderstood.
  1937. self should: [ 16rK ] raise: MessageNotUnderstood.
  1938. self should: [ 16rk ] raise: MessageNotUnderstood.
  1939. self should: [ 16rL ] raise: MessageNotUnderstood.
  1940. self should: [ 16rl ] raise: MessageNotUnderstood.
  1941. self should: [ 16rM ] raise: MessageNotUnderstood.
  1942. self should: [ 16rm ] raise: MessageNotUnderstood.
  1943. self should: [ 16rN ] raise: MessageNotUnderstood.
  1944. self should: [ 16rn ] raise: MessageNotUnderstood.
  1945. self should: [ 16rO ] raise: MessageNotUnderstood.
  1946. self should: [ 16ro ] raise: MessageNotUnderstood.
  1947. self should: [ 16rP ] raise: MessageNotUnderstood.
  1948. self should: [ 16rp ] raise: MessageNotUnderstood.
  1949. self should: [ 16rQ ] raise: MessageNotUnderstood.
  1950. self should: [ 16rq ] raise: MessageNotUnderstood.
  1951. self should: [ 16rR ] raise: MessageNotUnderstood.
  1952. self should: [ 16rr ] raise: MessageNotUnderstood.
  1953. self should: [ 16rS ] raise: MessageNotUnderstood.
  1954. self should: [ 16rs ] raise: MessageNotUnderstood.
  1955. self should: [ 16rT ] raise: MessageNotUnderstood.
  1956. self should: [ 16rt ] raise: MessageNotUnderstood.
  1957. self should: [ 16rU ] raise: MessageNotUnderstood.
  1958. self should: [ 16ru ] raise: MessageNotUnderstood.
  1959. self should: [ 16rV ] raise: MessageNotUnderstood.
  1960. self should: [ 16rv ] raise: MessageNotUnderstood.
  1961. self should: [ 16rW ] raise: MessageNotUnderstood.
  1962. self should: [ 16rw ] raise: MessageNotUnderstood.
  1963. self should: [ 16rX ] raise: MessageNotUnderstood.
  1964. self should: [ 16rx ] raise: MessageNotUnderstood.
  1965. self should: [ 16rY ] raise: MessageNotUnderstood.
  1966. self should: [ 16ry ] raise: MessageNotUnderstood.
  1967. self should: [ 16rZ ] raise: MessageNotUnderstood.
  1968. self should: [ 16rz ] raise: MessageNotUnderstood.
  1969. self should: [ 16rABcdEfZ ] raise: MessageNotUnderstood.
  1970. !
  1971. testLog
  1972. self assert: 10000 log equals: 4.
  1973. self assert: (512 log: 2) equals: 9.
  1974. self assert: Number e ln equals: 1.
  1975. !
  1976. testMinMax
  1977. self assert: (2 max: 5) equals: 5.
  1978. self assert: (2 min: 5) equals: 2.
  1979. self assert: (2 min: 5 max: 3) equals: 3.
  1980. self assert: (7 min: 5 max: 3) equals: 5.
  1981. self assert: (4 min: 5 max: 3) equals: 4.
  1982. !
  1983. testNegated
  1984. self assert: 3 negated equals: -3.
  1985. self assert: -3 negated equals: 3
  1986. !
  1987. testPrintShowingDecimalPlaces
  1988. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1989. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1990. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1991. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1992. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1993. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1994. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1995. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1996. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1997. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1998. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1999. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  2000. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  2001. !
  2002. testPrintStringBase
  2003. self assert: (15 printStringBase: 2) equals: '1111'.
  2004. self assert: (15 printStringBase: 16) equals: 'f'.
  2005. self assert: (256 printStringBase: 16) equals: '100'.
  2006. self assert: (256 printStringBase: 2) equals: '100000000'
  2007. !
  2008. testRadiansToDegrees
  2009. self assert: (Number pi radiansToDegrees - 180) abs <= 0.01.
  2010. !
  2011. testRaisedTo
  2012. self assert: (2 raisedTo: 4) equals: 16.
  2013. self assert: (2 raisedTo: 0) equals: 1.
  2014. self assert: (2 raisedTo: -3) equals: 0.125.
  2015. self assert: (4 raisedTo: 0.5) equals: 2.
  2016. self assert: 2 ** 4 equals: 16.
  2017. !
  2018. testRounded
  2019. self assert: 3 rounded equals: 3.
  2020. self assert: 3.212 rounded equals: 3.
  2021. self assert: 3.51 rounded equals: 4
  2022. !
  2023. testSign
  2024. self assert: 5 sign equals: 1.
  2025. self assert: 0 sign equals: 0.
  2026. self assert: -1.4 sign equals: -1.
  2027. !
  2028. testSqrt
  2029. self assert: 4 sqrt equals: 2.
  2030. self assert: 16 sqrt equals: 4
  2031. !
  2032. testSquared
  2033. self assert: 4 squared equals: 16
  2034. !
  2035. testTimesRepeat
  2036. | i |
  2037. i := 0.
  2038. 0 timesRepeat: [ i := i + 1 ].
  2039. self assert: i equals: 0.
  2040. 5 timesRepeat: [ i := i + 1 ].
  2041. self assert: i equals: 5
  2042. !
  2043. testTo
  2044. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  2045. !
  2046. testToBy
  2047. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  2048. self should: [ 1 to: 4 by: 0 ] raise: Error
  2049. !
  2050. testTrigonometry
  2051. self assert: 0 cos equals: 1.
  2052. self assert: 0 sin equals: 0.
  2053. self assert: 0 tan equals: 0.
  2054. self assert: 1 arcCos equals: 0.
  2055. self assert: 0 arcSin equals: 0.
  2056. self assert: 0 arcTan equals: 0.
  2057. self assert: (0 arcTan: 1) equals: 0.
  2058. self assert: (1 arcTan: 0) equals: (Number pi / 2)
  2059. !
  2060. testTruncated
  2061. self assert: 3 truncated equals: 3.
  2062. self assert: 3.212 truncated equals: 3.
  2063. self assert: 3.51 truncated equals: 3
  2064. ! !
  2065. Object subclass: #ObjectMock
  2066. slots: {#foo. #bar}
  2067. package: 'Kernel-Tests'!
  2068. !ObjectMock commentStamp!
  2069. ObjectMock is there only to perform tests on classes.!
  2070. !ObjectMock methodsFor: 'not yet classified'!
  2071. foo
  2072. ^ foo
  2073. !
  2074. foo: anObject
  2075. foo := anObject
  2076. ! !
  2077. TestCase subclass: #ObjectTest
  2078. slots: {}
  2079. package: 'Kernel-Tests'!
  2080. !ObjectTest methodsFor: 'tests'!
  2081. notDefined
  2082. <inlineJS: 'return void 0;'>
  2083. !
  2084. testBasicAccess
  2085. | o |
  2086. o := Object new.
  2087. o basicAt: 'a' put: 1.
  2088. self assert: (o basicAt: 'a') equals: 1.
  2089. self assert: (o basicAt: 'b') equals: nil
  2090. !
  2091. testBasicPerform
  2092. | o |
  2093. o := Object new.
  2094. o basicAt: 'func' put: [ 'hello' ].
  2095. o basicAt: 'func2' put: [ :a | a + 1 ].
  2096. self assert: (o basicPerform: 'func') equals: 'hello'.
  2097. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  2098. !
  2099. testDNU
  2100. self should: [ Object new foo ] raise: MessageNotUnderstood
  2101. !
  2102. testEquality
  2103. | o |
  2104. o := Object new.
  2105. self deny: o = Object new.
  2106. self assert: (o = o).
  2107. self assert: (o yourself = o).
  2108. self assert: (o = o yourself)
  2109. !
  2110. testHalt
  2111. self should: [ Object new halt ] raise: Error
  2112. !
  2113. testIdentity
  2114. | o |
  2115. o := Object new.
  2116. self deny: o == Object new.
  2117. self assert: o == o.
  2118. self assert: o yourself == o.
  2119. self assert: o == o yourself
  2120. !
  2121. testIfNil
  2122. self deny: Object new isNil.
  2123. self deny: (Object new ifNil: [ true ]) = true.
  2124. self assert: (Object new ifNotNil: [ true ]) equals: true.
  2125. self assert: (Object new ifNil: [ false ] ifNotNil: [ true ]) equals: true.
  2126. self assert: (Object new ifNotNil: [ true ] ifNil: [ false ]) equals: true
  2127. !
  2128. testInstVars
  2129. | o |
  2130. o := ObjectMock new.
  2131. self assert: (o instVarNamed: #foo) equals: nil.
  2132. o instVarNamed: #foo put: 1.
  2133. self assert: (o instVarNamed: #foo) equals: 1.
  2134. self assert: (o instVarNamed: 'foo') equals: 1
  2135. !
  2136. testNilUndefined
  2137. "nil in Smalltalk is the undefined object in JS"
  2138. self assert: self notDefined equals: nil
  2139. !
  2140. testYourself
  2141. | o |
  2142. o := ObjectMock new.
  2143. self assert: o yourself == o
  2144. ! !
  2145. TestCase subclass: #PointTest
  2146. slots: {}
  2147. package: 'Kernel-Tests'!
  2148. !PointTest methodsFor: 'tests'!
  2149. testAccessing
  2150. self assert: (Point x: 3 y: 4) x equals: 3.
  2151. self assert: (Point x: 3 y: 4) y equals: 4.
  2152. self assert: (Point new x: 3) x equals: 3.
  2153. self assert: (Point new y: 4) y equals: 4
  2154. !
  2155. testAngle
  2156. self assert: (-1@0) angle equals: Number pi
  2157. !
  2158. testArithmetic
  2159. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  2160. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  2161. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  2162. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  2163. !
  2164. testAt
  2165. self assert: 3@4 equals: (Point x: 3 y: 4)
  2166. !
  2167. testComparison
  2168. self assert: 3@4 < (4@5).
  2169. self deny: 3@4 < (4@4).
  2170. self assert: 4@5 <= (4@5).
  2171. self deny: 4@5 <= (3@5).
  2172. self assert: 5@6 > (4@5).
  2173. self deny: 5@6 > (6@6).
  2174. self assert: 4@5 >= (4@5).
  2175. self deny: 4@5 >= (5@5)
  2176. !
  2177. testDotProduct
  2178. self assert: (2@3 dotProduct: 3@7) equals: 27
  2179. !
  2180. testEgality
  2181. self assert: (3@4 = (3@4)).
  2182. self deny: 3@5 = (3@6)
  2183. !
  2184. testNew
  2185. self assert: (Point new x: 3) y equals: nil.
  2186. self deny: (Point new x: 3) x = 0.
  2187. self assert: (Point new y: 4) x equals: nil.
  2188. self deny: (Point new y: 4) y = 0
  2189. !
  2190. testNormal
  2191. self assert: (1@0) normal equals: 0@1
  2192. !
  2193. testNormalized
  2194. self assert: (0@2) normalized equals: 0@1.
  2195. self assert: (0@0) normalized equals: 0@0.
  2196. !
  2197. testPolarCoordinates
  2198. self assert: (1@0) r equals: 1.
  2199. self assert: (0@0) r equals: 0.
  2200. !
  2201. testRectangleCreation
  2202. self assert: (1@1 corner: 2@2) equals: (Rectangle origin: 1@1 corner: 2@2).
  2203. self assert: (1@1 rectangle: 2@2) equals: (Rectangle point: 1@1 point: 2@2).
  2204. self assert: (1@1 extent: 2@2) equals: (Rectangle origin: 1@1 extent: 2@2)
  2205. !
  2206. testTranslateBy
  2207. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  2208. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  2209. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  2210. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  2211. ! !
  2212. TestCase subclass: #PromiseTest
  2213. slots: {}
  2214. package: 'Kernel-Tests'!
  2215. !PromiseTest methodsFor: ' tests'!
  2216. testPromiseExecutorAsyncDoWithNonLocalReturn
  2217. self timeout: 40.
  2218. ^ (Promise new: [ :m | [ m do: [ ^ 'Intentional' ] ] fork ])
  2219. then: [ self assert: false description: 'Should not have been resolved' ]
  2220. on: NonLifoReturn do: [ :nonlifo | self assert: nonlifo value equals: 'Intentional' ]
  2221. !
  2222. testPromiseExecutorAsyncNegativeDo
  2223. self timeout: 40.
  2224. ^ (Promise new: [ :m | [ m do: [ self error: 'Intentional' ] ] fork ])
  2225. then: [ self assert: false description: 'Should not have been resolved' ]
  2226. catch: [ :error | self assert: error messageText equals: 'Intentional' ]
  2227. !
  2228. testPromiseExecutorAsyncNegativeTry
  2229. self timeout: 40.
  2230. ^ (Promise new: [ :m | [ m try: [ self error: 'Intentional' ] ] fork ])
  2231. then: [ self assert: false description: 'Should not have been resolved' ]
  2232. catch: [ :error | self assert: error messageText equals: 'Intentional' ]
  2233. !
  2234. testPromiseExecutorAsyncPositiveDo
  2235. self timeout: 40.
  2236. ^ (Promise new: [ :m | [ m do: [ 3 ] ] fork ])
  2237. then: [ :result | self assert: result equals: 3 ]
  2238. !
  2239. testPromiseExecutorAsyncPositiveTry
  2240. self timeout: 200.
  2241. ^ (Promise any: {
  2242. (Promise new: [ :m | [ m try: [ 3 ] ] fork ])
  2243. then: [ :result | self assert: result equals: 3 ].
  2244. Promise new: [ :m | [ m value: #timeout ] valueWithTimeout: 20 ]
  2245. }) then: [ :result | self assert: result equals: #timeout ].
  2246. !
  2247. testPromiseExecutorAsyncTryWithNonLocalReturn
  2248. self timeout: 40.
  2249. ^ (Promise new: [ :m | [ m try: [ ^ 'Intentional' ] ] fork ])
  2250. then: [ self assert: false description: 'Should not have been resolved' ]
  2251. on: NonLifoReturn do: [ :nonlifo | self assert: nonlifo value equals: 'Intentional' ]
  2252. !
  2253. testPromiseExecutorDoWithNonLocalReturn
  2254. self timeout: 20.
  2255. ^ (Promise new: [ :m | m do: [ ^ 'Intentional' ] ])
  2256. then: [ self assert: false description: 'Should not have been resolved' ]
  2257. on: NonLifoReturn do: [ :nonlifo | self assert: nonlifo value equals: 'Intentional' ]
  2258. !
  2259. testPromiseExecutorNegativeDo
  2260. self timeout: 40.
  2261. ^ (Promise new: [ :m | [ m do: [ self error: 'Intentional' ] ] fork ])
  2262. then: [ self assert: false description: 'Should not have been resolved' ]
  2263. catch: [ :error | self assert: error messageText equals: 'Intentional' ]
  2264. !
  2265. testPromiseExecutorNegativeTry
  2266. self timeout: 20.
  2267. ^ (Promise new: [ :m | m try: [ self error: 'Intentional' ] ])
  2268. then: [ self assert: false description: 'Should not have been resolved' ]
  2269. catch: [ :error | self assert: error messageText equals: 'Intentional' ]
  2270. !
  2271. testPromiseExecutorPositiveDo
  2272. self timeout: 20.
  2273. ^ (Promise new: [ :m | m do: [ 3 ] ])
  2274. then: [ :result | self assert: result equals: 3 ]
  2275. !
  2276. testPromiseExecutorPositiveTry
  2277. self timeout: 200.
  2278. ^ (Promise any: {
  2279. (Promise new: [ :m | m try: [ 3 ] ])
  2280. then: [ :result | self assert: result equals: 3 ].
  2281. Promise new: [ :m | [ m value: #timeout ] valueWithTimeout: 20 ]
  2282. }) then: [ :result | self assert: result equals: #timeout ].
  2283. !
  2284. testPromiseExecutorTryWithNonLocalReturn
  2285. self timeout: 20.
  2286. ^ (Promise new: [ :m | m try: [ ^ 'Intentional' ] ])
  2287. then: [ self assert: false description: 'Should not have been resolved' ]
  2288. on: NonLifoReturn do: [ :nonlifo | self assert: nonlifo value equals: 'Intentional' ]
  2289. !
  2290. testPromiseNew
  2291. self timeout: 20.
  2292. ^ Promise new
  2293. then: [ :result | self assert: result equals: nil ]
  2294. !
  2295. testPromiseWithAsyncPassingRejectingExecutor
  2296. self timeout: 60.
  2297. ^ (Promise new: [ :m | [
  2298. | passPromise |
  2299. passPromise := Promise new: [ :m2 | [ m2 signal: 4 ] fork ].
  2300. m value: passPromise
  2301. ] fork ])
  2302. then: [ self assert: false description: 'Should not have been resolved' ]
  2303. catch: [ :err | self assert: err equals: 4 ]
  2304. !
  2305. testPromiseWithAsyncPassingResolvingExecutor
  2306. self timeout: 60.
  2307. ^ (Promise new: [ :m | [
  2308. | passPromise |
  2309. passPromise := Promise new: [ :m2 | [ m2 value: 3 ] fork ].
  2310. m value: passPromise
  2311. ] fork ]) then: [ :result | self assert: result equals: 3 ]
  2312. !
  2313. testPromiseWithAsyncRejectingExecutor
  2314. self timeout: 40.
  2315. ^ (Promise new: [ :m | [ m signal: 4 ] fork ])
  2316. then: [ self assert: false description: 'Should not have been resolved' ]
  2317. catch: [ :err | self assert: err equals: 4 ]
  2318. !
  2319. testPromiseWithAsyncResolvingExecutor
  2320. self timeout: 40.
  2321. ^ (Promise new: [ :m | [ m value: 3 ] fork ])
  2322. then: [ :result | self assert: result equals: 3 ]
  2323. !
  2324. testPromiseWithRejectingExecutor
  2325. self timeout: 20.
  2326. ^ (Promise new: [ :m | m signal: 4 ])
  2327. then: [ self assert: false description: 'Should not have been resolved' ]
  2328. catch: [ :err | self assert: err equals: 4 ]
  2329. !
  2330. testPromiseWithResolvingExecutor
  2331. self timeout: 20.
  2332. ^ (Promise new: [ :m | m value: 3 ])
  2333. then: [ :result | self assert: result equals: 3 ]
  2334. ! !
  2335. TestCase subclass: #QueueTest
  2336. slots: {}
  2337. package: 'Kernel-Tests'!
  2338. !QueueTest methodsFor: 'tests'!
  2339. testNextIfAbsent
  2340. | queue |
  2341. queue := Queue new.
  2342. queue nextPut: 'index1'.
  2343. self assert: (queue nextIfAbsent: 'empty') = 'index1'.
  2344. self deny: (queue nextIfAbsent: 'empty') = 'index1'
  2345. !
  2346. testQueueNext
  2347. | queue |
  2348. queue := Queue new.
  2349. queue
  2350. nextPut: 'index1';
  2351. nextPut: 'index2'.
  2352. self assert: queue next = 'index1'.
  2353. self deny: queue next = 'index'.
  2354. self should: [ queue next ] raise: Error
  2355. ! !
  2356. TestCase subclass: #RandomTest
  2357. slots: {}
  2358. package: 'Kernel-Tests'!
  2359. !RandomTest methodsFor: 'tests'!
  2360. testAtRandomNumber
  2361. |val|
  2362. 100 timesRepeat: [
  2363. val := 10 atRandom.
  2364. self assert: (val > 0).
  2365. self assert: (val <11)
  2366. ]
  2367. !
  2368. testAtRandomSequenceableCollection
  2369. |val|
  2370. 100 timesRepeat: [
  2371. val := 'abc' atRandom.
  2372. self assert: ((val = 'a') | (val = 'b') | (val = 'c' )).
  2373. ].
  2374. !
  2375. textNext
  2376. 10000 timesRepeat: [
  2377. | current next |
  2378. next := Random new next.
  2379. self assert: (next >= 0).
  2380. self assert: (next < 1).
  2381. self deny: current = next.
  2382. next = current ]
  2383. ! !
  2384. TestCase subclass: #RectangleTest
  2385. slots: {}
  2386. package: 'Kernel-Tests'!
  2387. !RectangleTest methodsFor: 'tests'!
  2388. testContainsPoint
  2389. | rect |
  2390. rect := Rectangle origin: 0@0 corner: 4@4.
  2391. self assert: (rect containsPoint: 1@2).
  2392. self assert: (rect containsPoint: 5@4) not.
  2393. !
  2394. testContainsRect
  2395. self assert: ((Rectangle origin: 0@0 corner: 6@6) containsRect: (Rectangle origin: 1@1 corner: 5@5)).
  2396. self assert: ((Rectangle origin: 0@0 corner: 6@6) containsRect: (Rectangle origin: 1@(-1) corner: 5@5)) not.
  2397. !
  2398. testOriginExtent
  2399. | rectangle |
  2400. rectangle := Rectangle origin: 3@4 extent: 7@8.
  2401. self assert: rectangle origin equals: 3@4.
  2402. self assert: rectangle corner equals: 10@12.
  2403. ! !
  2404. TestCase subclass: #StreamTest
  2405. slots: {}
  2406. package: 'Kernel-Tests'!
  2407. !StreamTest methodsFor: 'accessing'!
  2408. collectionClass
  2409. ^ self class collectionClass
  2410. !
  2411. newCollection
  2412. ^ self collectionClass new
  2413. !
  2414. newStream
  2415. ^ self collectionClass new stream
  2416. ! !
  2417. !StreamTest methodsFor: 'tests'!
  2418. testAtStartAtEnd
  2419. | stream |
  2420. stream := self newStream.
  2421. self assert: stream atStart.
  2422. self assert: stream atEnd.
  2423. stream nextPutAll: self newCollection.
  2424. self assert: stream atEnd.
  2425. self deny: stream atStart.
  2426. stream position: 1.
  2427. self deny: stream atEnd.
  2428. self deny: stream atStart
  2429. !
  2430. testContents
  2431. | stream |
  2432. stream := self newStream.
  2433. stream nextPutAll: self newCollection.
  2434. self assert: stream contents equals: self newCollection
  2435. !
  2436. testIsEmpty
  2437. | stream |
  2438. stream := self newStream.
  2439. self assert: stream isEmpty.
  2440. stream nextPutAll: self newCollection.
  2441. self deny: stream isEmpty
  2442. !
  2443. testPosition
  2444. | collection stream |
  2445. collection := self newCollection.
  2446. stream := self newStream.
  2447. stream nextPutAll: collection.
  2448. self assert: stream position equals: collection size.
  2449. stream position: 0.
  2450. self assert: stream position equals: 0.
  2451. stream next.
  2452. self assert: stream position equals: 1.
  2453. stream next.
  2454. self assert: stream position equals: 2
  2455. !
  2456. testReading
  2457. | stream collection |
  2458. collection := self newCollection.
  2459. stream := self newStream.
  2460. stream
  2461. nextPutAll: collection;
  2462. position: 0.
  2463. collection do: [ :each |
  2464. self assert: stream next equals: each ].
  2465. self assert: stream next isNil
  2466. !
  2467. testStreamContents
  2468. !
  2469. testWrite
  2470. | stream collection |
  2471. collection := self newCollection.
  2472. stream := self newStream.
  2473. collection do: [ :each | stream << each ].
  2474. self assert: stream contents equals: collection
  2475. !
  2476. testWriting
  2477. | stream collection |
  2478. collection := self newCollection.
  2479. stream := self newStream.
  2480. collection do: [ :each | stream nextPut: each ].
  2481. self assert: stream contents equals: collection.
  2482. stream := self newStream.
  2483. stream nextPutAll: collection.
  2484. self assert: stream contents equals: collection
  2485. ! !
  2486. !StreamTest class methodsFor: 'accessing'!
  2487. collectionClass
  2488. ^ nil
  2489. ! !
  2490. !StreamTest class methodsFor: 'testing'!
  2491. isAbstract
  2492. ^ self collectionClass isNil
  2493. ! !
  2494. StreamTest subclass: #ArrayStreamTest
  2495. slots: {}
  2496. package: 'Kernel-Tests'!
  2497. !ArrayStreamTest methodsFor: 'accessing'!
  2498. newCollection
  2499. ^ { true. 1. 3@4. 'foo' }
  2500. ! !
  2501. !ArrayStreamTest class methodsFor: 'accessing'!
  2502. collectionClass
  2503. ^ Array
  2504. ! !
  2505. StreamTest subclass: #StringStreamTest
  2506. slots: {}
  2507. package: 'Kernel-Tests'!
  2508. !StringStreamTest methodsFor: 'accessing'!
  2509. newCollection
  2510. ^ 'hello world'
  2511. ! !
  2512. !StringStreamTest class methodsFor: 'accessing'!
  2513. collectionClass
  2514. ^ String
  2515. ! !
  2516. Trait named: #TClassBuildingTest
  2517. package: 'Kernel-Tests'!
  2518. !TClassBuildingTest methodsFor: 'accessing'!
  2519. theClass
  2520. self subclassResponsibility
  2521. ! !
  2522. !TClassBuildingTest methodsFor: 'private'!
  2523. is: anObject javaScriptInstanceOf: aJavaScriptClass
  2524. <inlineJS: 'return anObject instanceof aJavaScriptClass'>
  2525. ! !
  2526. !TClassBuildingTest methodsFor: 'running'!
  2527. assert: aClass isClassCopyOf: anotherClass
  2528. self assert: aClass superclass == anotherClass superclass.
  2529. self deny: aClass slots == anotherClass slots.
  2530. self assert: aClass slots equals: anotherClass slots.
  2531. self deny: aClass class slots == anotherClass class slots.
  2532. self assert: aClass class slots equals: anotherClass class slots.
  2533. self assert: aClass package == anotherClass package.
  2534. self assert: (aClass package classes includes: aClass).
  2535. self assert: aClass methodDictionary keys equals: anotherClass methodDictionary keys
  2536. !
  2537. assert: anObject isJavaScriptInstanceOf: aJavaScriptClass
  2538. self assert: (self is: anObject javaScriptInstanceOf: aJavaScriptClass)
  2539. !
  2540. tearDown
  2541. self theClass ifNotNil: [ :theClass |
  2542. (Array streamContents: [ :s | theClass allSubclassesDo: [ :each | s nextPut: each ] ])
  2543. reverseDo: [ :each | Smalltalk removeClass: each ].
  2544. Smalltalk removeClass: theClass ]
  2545. ! !
  2546. Trait named: #TKeyValueCollectionTest
  2547. package: 'Kernel-Tests'!
  2548. !TKeyValueCollectionTest methodsFor: 'fixture'!
  2549. nonIndexesDo: aBlock
  2550. "Executes block a few times,
  2551. each time passing value that is known
  2552. not to be an index, as the first parameter"
  2553. self subclassResponsibility
  2554. !
  2555. sampleNewIndex
  2556. "Answers a value that can be used as index in at:put: or at:ifAbsentPut:"
  2557. self subclassResponsibility
  2558. !
  2559. samplesDo: aBlock
  2560. "Executes block a few times,
  2561. each time passing known index and value stored
  2562. under that index as the parameters"
  2563. self subclassResponsibility
  2564. ! !
  2565. !TKeyValueCollectionTest methodsFor: 'tests'!
  2566. testAt
  2567. self nonIndexesDo: [ :each |
  2568. self should: [ self collection at: each ] raise: Error ].
  2569. self samplesDo: [ :index :value |
  2570. self assert: (self collection at: index) equals: value ]
  2571. !
  2572. testAtIfAbsent
  2573. self nonIndexesDo: [ :each |
  2574. self assert: (self collection at: each ifAbsent: [ self sampleNewValue ]) equals: self sampleNewValue ].
  2575. self samplesDo: [ :index :value |
  2576. self assert: (self collection at: index ifAbsent: [ self sampleNewValue ]) equals: value ].
  2577. !
  2578. testAtIfAbsentPut
  2579. | newCollection |
  2580. newCollection := self collection.
  2581. self samplesDo: [ :index :value |
  2582. self assert: (newCollection at: index ifAbsentPut: [ self sampleNewValue ]) equals: value ].
  2583. self assert: newCollection equals: self collection.
  2584. self assert: (newCollection at: self sampleNewIndex ifAbsentPut: [ self sampleNewValue ]) equals: self sampleNewValue.
  2585. self assert: newCollection equals: self collectionWithNewValue
  2586. !
  2587. testAtIfPresent
  2588. | visited sentinel |
  2589. sentinel := Object new.
  2590. self nonIndexesDo: [ :each |
  2591. visited := nil.
  2592. self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ]) equals: nil.
  2593. self assert: visited isNil ].
  2594. self samplesDo: [ :index :value |
  2595. visited := nil.
  2596. self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ]) equals: sentinel.
  2597. self assert: visited equals: (self collection at: index) ]
  2598. !
  2599. testAtIfPresentIfAbsent
  2600. | visited sentinel |
  2601. sentinel := Object new.
  2602. self nonIndexesDo: [ :each |
  2603. visited := nil.
  2604. self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ] ifAbsent: [ self sampleNewValue ] ) equals: self sampleNewValue.
  2605. self assert: visited isNil ].
  2606. self samplesDo: [ :index :value |
  2607. visited := nil.
  2608. self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ] ifAbsent: [ self sampleNewValue ]) equals: sentinel.
  2609. self assert: visited equals: (self collection at: index) ]
  2610. !
  2611. testAtPut
  2612. | newCollection |
  2613. newCollection := self collection.
  2614. self samplesDo: [ :index :value |
  2615. newCollection at: index put: value ].
  2616. self assert: newCollection equals: self collection.
  2617. newCollection at: self sampleNewIndex put: self sampleNewValue.
  2618. self assert: newCollection equals: self collectionWithNewValue
  2619. !
  2620. testIndexOf
  2621. self should: [ self collection indexOf: self sampleNewValue ] raise: Error.
  2622. self samplesDo: [ :index :value |
  2623. self assert: (self collection indexOf: value) equals: index ]
  2624. !
  2625. testIndexOfWithNull
  2626. | jsNull |
  2627. jsNull := JSON parse: 'null'.
  2628. self samplesDo: [ :index :value |
  2629. self assert: (self collection at: index put: jsNull; indexOf: jsNull) equals: index ]
  2630. !
  2631. testWithIndexDo
  2632. | collection |
  2633. collection := self collection.
  2634. self collection withIndexDo: [ :each :index |
  2635. self assert: (collection at: index) equals: each ]
  2636. ! !
  2637. TestCase subclass: #UndefinedTest
  2638. slots: {}
  2639. package: 'Kernel-Tests'!
  2640. !UndefinedTest methodsFor: 'tests'!
  2641. testCopying
  2642. self assert: nil copy equals: nil
  2643. !
  2644. testDeepCopy
  2645. self assert: nil deepCopy = nil
  2646. !
  2647. testIfNil
  2648. self assert: (nil ifNil: [ true ]) equals: true.
  2649. self deny: (nil ifNotNil: [ true ]) = true.
  2650. self assert: (nil ifNil: [ true ] ifNotNil: [ false ]) equals: true.
  2651. self deny: (nil ifNotNil: [ true ] ifNil: [ false ]) = true
  2652. !
  2653. testIsNil
  2654. self assert: nil isNil.
  2655. self deny: nil notNil.
  2656. ! !
  2657. ClassBuilderTest setTraitComposition: {TClassBuildingTest @ {#tearDownTheClass -> #tearDown}} asTraitComposition!
  2658. ClassTest setTraitComposition: {TClassBuildingTest} asTraitComposition!
  2659. AssociativeCollectionTest setTraitComposition: {TKeyValueCollectionTest} asTraitComposition!
  2660. SequenceableCollectionTest setTraitComposition: {TKeyValueCollectionTest} asTraitComposition!
  2661. ! !