Kernel-Tests.st 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202
  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 basicAddSubclassOf: SystemAnnouncement named: 'TestAnnouncement1' instanceVariableNames: #() 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 instanceVariableNames 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. testClassMigrationWithClassInstanceVariables
  313. builder copyClass: ObjectMock named: 'ObjectMock2'.
  314. ObjectMock2 class instanceVariableNames: ' foo bar '.
  315. "Change the superclass of ObjectMock2"
  316. theClass := ObjectMock subclass: #ObjectMock2
  317. instanceVariableNames: ''
  318. package: 'Kernel-Tests'.
  319. self assert: ObjectMock2 class instanceVariableNames 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' instanceVariableNames: #(foo bar) package: 'Kernel-Tests'.
  335. self assert: theClass superclass equals: ObjectMock.
  336. self assert: theClass instanceVariableNames 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. testSetJavaScriptConstructor
  506. | instance |
  507. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  508. theClass javaScriptConstructor: self jsConstructor.
  509. self assert: theClass name equals: 'ObjectMock2'.
  510. self assert: theClass isClassCopyOf: ObjectMock.
  511. "testing specific to late-coupled detached root class"
  512. instance := theClass new.
  513. self assert: instance class == theClass.
  514. self assert: instance value equals: 4.
  515. self shouldnt: [ instance foo: 9 ] raise: Error.
  516. self assert: instance foo equals: 9
  517. !
  518. testTrickySetJavaScriptConstructor
  519. | instance |
  520. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  521. theClass javaScriptConstructor: self trickyJsConstructor.
  522. self assert: theClass name equals: 'ObjectMock2'.
  523. self assert: theClass isClassCopyOf: ObjectMock.
  524. "testing specific to late-coupled detached root class"
  525. instance := theClass new.
  526. self assert: instance class == theClass.
  527. self assert: instance value equals: 4.
  528. self shouldnt: [ instance foo: 9 ] raise: Error.
  529. self assert: instance foo equals: 9
  530. ! !
  531. TestCase subclass: #CollectionTest
  532. slots: {#sampleBlock}
  533. package: 'Kernel-Tests'!
  534. !CollectionTest methodsFor: 'convenience'!
  535. assertSameContents: aCollection as: anotherCollection
  536. self assert: (aCollection size = anotherCollection size).
  537. aCollection do: [ :each |
  538. self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
  539. ! !
  540. !CollectionTest methodsFor: 'fixture'!
  541. collection
  542. "Answers pre-filled collection of type tested."
  543. self subclassResponsibility
  544. !
  545. collectionClass
  546. "Answers class of collection type tested"
  547. ^ self class collectionClass
  548. !
  549. collectionOfPrintStrings
  550. "Answers self collection but with values
  551. changed to their printStrings"
  552. self subclassResponsibility
  553. !
  554. collectionSize
  555. "Answers size of self collection."
  556. self subclassResponsibility
  557. !
  558. collectionWithDuplicates
  559. "Answers pre-filled collection of type tested,
  560. with exactly six distinct elements,
  561. some of them appearing multiple times, if possible."
  562. self subclassResponsibility
  563. !
  564. collectionWithNewValue
  565. "Answers a collection which shows how
  566. self collection would look after adding
  567. self sampleNewValue"
  568. self subclassResponsibility
  569. !
  570. sampleNewValue
  571. "Answers a value that is not yet there
  572. and can be put into a tested collection"
  573. ^ 'N'
  574. !
  575. sampleNewValueAsCollection
  576. "Answers self sampleNewValue
  577. wrapped in single element collection
  578. of tested type"
  579. ^ self collectionClass with: self sampleNewValue
  580. ! !
  581. !CollectionTest methodsFor: 'initialization'!
  582. initialize
  583. super initialize.
  584. sampleBlock := []
  585. ! !
  586. !CollectionTest methodsFor: 'tests'!
  587. testAddAll
  588. self assert: (self collection addAll: self collectionClass new; yourself) equals: self collection.
  589. self assert: (self collectionClass new addAll: self collection; yourself) equals: self collection.
  590. self assert: (self collectionClass new addAll: self collectionClass new; yourself) equals: self collectionClass new.
  591. self assert: (self collection addAll: self sampleNewValueAsCollection; yourself) equals: self collectionWithNewValue.
  592. self assertSameContents: (self sampleNewValueAsCollection addAll: self collection; yourself) as: self collectionWithNewValue
  593. !
  594. testAllSatisfy
  595. | collection anyOne |
  596. collection := self collection.
  597. anyOne := collection anyOne.
  598. self assert: (collection allSatisfy: [ :each | collection includes: each ]).
  599. self deny: (collection allSatisfy: [ :each | each ~= anyOne ])
  600. !
  601. testAnyOne
  602. self should: [ self collectionClass new anyOne ] raise: Error.
  603. self assert: (self collection includes: self collection anyOne)
  604. !
  605. testAnySatisfy
  606. | anyOne |
  607. anyOne := self collection anyOne.
  608. self assert: (self collection anySatisfy: [ :each | each = anyOne ]).
  609. self deny: (self collection anySatisfy: [ :each | each = Object new ])
  610. !
  611. testAsArray
  612. self
  613. assertSameContents: self collection
  614. as: self collection asArray
  615. !
  616. testAsOrderedCollection
  617. self
  618. assertSameContents: self collection
  619. as: self collection asOrderedCollection
  620. !
  621. testAsSet
  622. | c set |
  623. c := self collectionWithDuplicates.
  624. set := c asSet.
  625. self assert: set size equals: 6.
  626. c do: [ :each |
  627. self assert: (set includes: each) ]
  628. !
  629. testCollect
  630. self assert: (self collection collect: [ :each | each ]) equals: self collection.
  631. self assert: (self collectionWithNewValue collect: [ :each | each ]) equals: self collectionWithNewValue.
  632. self assert: (self collectionClass new collect: [ :each | each printString ]) equals: self collectionClass new.
  633. self assert: ((self collection collect: [ self sampleNewValue ]) detect: [ true ]) equals: self sampleNewValue.
  634. self assert: (self collection collect: [ :each | each printString ]) equals: self collectionOfPrintStrings
  635. !
  636. testComma
  637. self assert: self collection, self collectionClass new equals: self collection.
  638. self assert: self collectionClass new, self collection equals: self collection.
  639. self assert: self collectionClass new, self collectionClass new equals: self collectionClass new.
  640. self assert: self collection, self sampleNewValueAsCollection equals: self collectionWithNewValue.
  641. self assertSameContents: self sampleNewValueAsCollection, self collection as: self collectionWithNewValue
  642. !
  643. testCopy
  644. self assert: self collectionClass new copy equals: self collectionClass new.
  645. self assert: self collection copy equals: self collection.
  646. self assert: self collectionWithNewValue copy equals: self collectionWithNewValue.
  647. self deny: self collectionClass new copy = self collection.
  648. self deny: self collection copy = self collectionClass new.
  649. self deny: self collection copy = self collectionWithNewValue
  650. !
  651. testCopyEmpty
  652. self assert: self collectionClass new copyEmpty equals: self collectionClass new.
  653. self assert: self collection copyEmpty equals: self collectionClass new.
  654. self assert: self collectionWithNewValue copyEmpty equals: self collectionClass new
  655. !
  656. testCopySeparates
  657. | original copy |
  658. original := self collection.
  659. copy := original copy.
  660. copy addAll: self sampleNewValueAsCollection.
  661. self assert: original = self collection
  662. !
  663. testDetect
  664. self
  665. shouldnt: [ self collection detect: [ true ] ]
  666. raise: Error.
  667. self
  668. should: [ self collection detect: [ false ] ]
  669. raise: Error.
  670. self assert: (self sampleNewValueAsCollection detect: [ true ]) equals: self sampleNewValue.
  671. self assert: (self collectionWithNewValue detect: [ :each | each = self sampleNewValue ]) equals: self sampleNewValue.
  672. self
  673. should: [ self collection detect: [ :each | each = self sampleNewValue ] ]
  674. raise: Error
  675. !
  676. testDetectIfNone
  677. | sentinel |
  678. sentinel := Object new.
  679. self assert: (self collection detect: [ true ] ifNone: [ sentinel ]) ~= sentinel.
  680. self assert: (self collection detect: [ false ] ifNone: [ sentinel ]) equals: sentinel.
  681. self assert: (self sampleNewValueAsCollection detect: [ true ] ifNone: [ sentinel ]) equals: self sampleNewValue.
  682. self assert: (self collectionWithNewValue detect: [ :each | each = self sampleNewValue ] ifNone: [ sentinel ]) equals: self sampleNewValue.
  683. self assert: (self collection detect: [ :each | each = self sampleNewValue ] ifNone: [ sentinel ]) equals: sentinel
  684. !
  685. testDo
  686. | newCollection |
  687. newCollection := OrderedCollection new.
  688. self collection do: [ :each |
  689. newCollection add: each ].
  690. self
  691. assertSameContents: self collection
  692. as: newCollection.
  693. newCollection := OrderedCollection new.
  694. self collectionWithDuplicates do: [ :each |
  695. newCollection add: each ].
  696. self
  697. assertSameContents: self collectionWithDuplicates
  698. as: newCollection
  699. !
  700. testEquality
  701. self assert: self collectionClass new equals: self collectionClass new.
  702. self assert: self collection equals: self collection.
  703. self assert: self collectionWithNewValue equals: self collectionWithNewValue.
  704. self deny: self collectionClass new = self collection.
  705. self deny: self collection = self collectionClass new.
  706. self deny: self collection = self collectionWithNewValue
  707. !
  708. testIfEmptyFamily
  709. self assert: (self collectionClass new ifEmpty: [ 42 ]) equals: 42.
  710. self assert: (self collection ifEmpty: [ 42 ]) equals: self collection.
  711. self assert: (self collectionClass new ifNotEmpty: [ 42 ]) equals: self collectionClass new.
  712. self assert: (self collection ifNotEmpty: [ 42 ]) equals: 42.
  713. self assert: (self collection ifNotEmpty: [ :col | col ]) equals: self collection.
  714. self assert: (self collectionClass new ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 42.
  715. self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 999.
  716. self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ :col | col ]) equals: self collection.
  717. self assert: (self collectionClass new ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 999.
  718. self assert: (self collection ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 42.
  719. self assert: (self collection ifNotEmpty: [ :col | col ] ifEmpty: [ 999 ]) equals: self collection.
  720. !
  721. testIsEmpty
  722. self assert: self collectionClass new isEmpty.
  723. self deny: self collection isEmpty
  724. !
  725. testNoneSatisfy
  726. | anyOne |
  727. anyOne := self collection anyOne.
  728. self deny: (self collection noneSatisfy: [ :each | each = anyOne ]).
  729. self assert: (self collection noneSatisfy: [ :each | each = Object new ])
  730. !
  731. testRegression1224
  732. self assert: (self collectionClass new
  733. remove: self sampleNewValue ifAbsent: [];
  734. yourself) size equals: 0
  735. !
  736. testRemoveAll
  737. self assert: (self collection removeAll; yourself) equals: self collectionClass new
  738. !
  739. testSelect
  740. self assert: (self collection select: [ false ]) equals: self collectionClass new.
  741. self assert: (self collection select: [ true ]) equals: self collection.
  742. self assert: (self collectionWithNewValue select: [ :each | each = self sampleNewValue ]) equals: self sampleNewValueAsCollection.
  743. self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ]) equals: self collection.
  744. self assert: (self collection select: [ :each | each = self sampleNewValue ]) equals: self collectionClass new.
  745. self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ]) equals: self collection
  746. !
  747. testSelectThenCollect
  748. self assert: (self collection select: [ false ] thenCollect: #isString) equals: self collectionClass new.
  749. self assert: (self collection select: [ true ] thenCollect: [:x|x]) equals: self collection.
  750. self assert: (self collection select: [ :each | each = self sampleNewValue ] thenCollect: [:x|x]) equals: self collectionClass new.
  751. self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ] thenCollect: #printString) equals: self collectionOfPrintStrings
  752. !
  753. testSingle
  754. self should: [ self collectionClass new single ] raise: Error.
  755. self should: [ self collection single ] raise: Error.
  756. self assert: self sampleNewValueAsCollection single equals: self sampleNewValue
  757. !
  758. testSize
  759. self assert: self collectionClass new size equals: 0.
  760. self assert: self sampleNewValueAsCollection size equals: 1.
  761. self assert: self collection size equals: self collectionSize
  762. ! !
  763. !CollectionTest class methodsFor: 'fixture'!
  764. collectionClass
  765. "Answers class of collection type tested,
  766. or nil if test is abstract"
  767. ^ nil
  768. ! !
  769. !CollectionTest class methodsFor: 'testing'!
  770. isAbstract
  771. ^ self collectionClass isNil
  772. ! !
  773. CollectionTest subclass: #AssociativeCollectionTest
  774. slots: {}
  775. package: 'Kernel-Tests'!
  776. !AssociativeCollectionTest methodsFor: 'fixture'!
  777. collectionKeys
  778. self subclassResponsibility
  779. !
  780. collectionValues
  781. self subclassResponsibility
  782. !
  783. nonIndexesDo: aBlock
  784. aBlock value: 5.
  785. aBlock value: [].
  786. aBlock value: Object new.
  787. aBlock value: 'z'
  788. !
  789. sampleNewIndex
  790. ^ 'new'
  791. !
  792. samplesDo: aBlock
  793. aBlock value: 'a' value: 2
  794. ! !
  795. !AssociativeCollectionTest methodsFor: 'tests'!
  796. testAddAll
  797. super testAddAll.
  798. self assert: (self collection addAll: self collection; yourself) equals: self collection.
  799. self assert: (self collection addAll: self collectionWithNewValue; yourself) equals: self collectionWithNewValue.
  800. self assert: (self collectionWithNewValue addAll: self collection; yourself) equals: self collectionWithNewValue
  801. !
  802. testAsDictionary
  803. self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
  804. !
  805. testAsHashedCollection
  806. self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
  807. !
  808. testComma
  809. super testComma.
  810. self assert: self collection, self collection equals: self collection.
  811. self assert: self collection, self collectionWithNewValue equals: self collectionWithNewValue.
  812. self assert: self collectionWithNewValue, self collection equals: self collectionWithNewValue
  813. !
  814. testFrom
  815. "Accept a collection of associations."
  816. | associations |
  817. associations := { 'a' -> 1. 'b' -> 2 }.
  818. self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  819. !
  820. testKeys
  821. self assert:self collectionClass new keys isEmpty.
  822. self assertSameContents:self collection keys as: self collectionKeys.
  823. self assertSameContents:self collectionWithNewValue keys as: self collectionKeys, { self sampleNewIndex }
  824. !
  825. testNewFromPairs
  826. "Accept an array in which all odd indexes are keys and evens are values."
  827. | flattenedAssociations |
  828. flattenedAssociations := { 'a'. 1. 'b'. 2 }.
  829. self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  830. !
  831. testPrintString
  832. self
  833. assert: (self collectionClass new
  834. at:'firstname' put: 'James';
  835. at:'lastname' put: 'Bond';
  836. printString)
  837. equals: 'a ', self collectionClass name, ' (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  838. !
  839. testRemoveKey
  840. self nonIndexesDo: [ :each |
  841. | collection |
  842. collection := self collection.
  843. self should: [ collection removeKey: each ] raise: Error.
  844. self assert: collection equals: self collection ].
  845. self samplesDo: [ :index :value |
  846. | collection |
  847. collection := self collection.
  848. self assert: (collection removeKey: index) equals: value.
  849. self deny: collection = self collection ].
  850. self
  851. assert: (self collectionWithNewValue removeKey: self sampleNewIndex; yourself)
  852. equals: self collection
  853. !
  854. testRemoveKeyIfAbsent
  855. self nonIndexesDo: [ :each |
  856. | collection |
  857. collection := self collection.
  858. self assert: (collection removeKey: each ifAbsent: [ self sampleNewValue ]) equals: self sampleNewValue.
  859. self assert: collection equals: self collection ].
  860. self samplesDo: [ :index :value |
  861. | collection |
  862. collection := self collection.
  863. self assert: (collection removeKey: index ifAbsent: [ self sampleNewValue ]) equals: value.
  864. self deny: collection = self collection ].
  865. self
  866. assert: (self collectionWithNewValue removeKey: self sampleNewIndex ifAbsent: [ self assert: false ]; yourself)
  867. equals: self collection
  868. !
  869. testValues
  870. self assert:self collectionClass new values isEmpty.
  871. self assertSameContents:self collection values as: self collectionValues.
  872. self assertSameContents:self collectionWithNewValue values as: self collectionValues, { self sampleNewValue }
  873. ! !
  874. AssociativeCollectionTest subclass: #DictionaryTest
  875. slots: {}
  876. package: 'Kernel-Tests'!
  877. !DictionaryTest methodsFor: 'fixture'!
  878. collection
  879. ^ Dictionary new
  880. at: 1 put: 1;
  881. at: 'a' put: 2;
  882. at: true put: 3;
  883. at: 1@3 put: -4;
  884. at: sampleBlock put: 9;
  885. yourself
  886. !
  887. collectionKeys
  888. ^ {1. 'a'. true. 1@3. sampleBlock}
  889. !
  890. collectionOfPrintStrings
  891. ^ Dictionary new
  892. at: 1 put: '1';
  893. at: 'a' put: '2';
  894. at: true put: '3';
  895. at: 1@3 put: '-4';
  896. at: sampleBlock put: '9';
  897. yourself
  898. !
  899. collectionSize
  900. ^ 5
  901. !
  902. collectionValues
  903. ^ {1. 2. 3. -4. 9}
  904. !
  905. collectionWithDuplicates
  906. ^ Dictionary new
  907. at: 1 put: 1;
  908. at: 'a' put: 2;
  909. at: true put: 3;
  910. at: 4 put: -4;
  911. at: sampleBlock put: 9;
  912. at: 'b' put: 1;
  913. at: 3 put: 3;
  914. at: false put: 12;
  915. yourself
  916. !
  917. collectionWithNewValue
  918. ^ Dictionary new
  919. at: 1 put: 1;
  920. at: 'a' put: 2;
  921. at: true put: 3;
  922. at: 1@3 put: -4;
  923. at: sampleBlock put: 9;
  924. at: 'new' put: 'N';
  925. yourself
  926. !
  927. sampleNewValueAsCollection
  928. ^ Dictionary new
  929. at: 'new' put: 'N';
  930. yourself
  931. !
  932. samplesDo: aBlock
  933. super samplesDo: aBlock.
  934. aBlock value: true value: 3.
  935. aBlock value: 1@3 value: -4.
  936. aBlock value: sampleBlock value: 9
  937. ! !
  938. !DictionaryTest methodsFor: 'tests'!
  939. testAccessing
  940. | d |
  941. d := Dictionary new.
  942. d at: 'hello' put: 'world'.
  943. self assert: (d at: 'hello') equals: 'world'.
  944. self assert: (d at: 'hello' ifAbsent: [ nil ]) equals: 'world'.
  945. self deny: (d at: 'foo' ifAbsent: [ nil ]) = 'world'.
  946. self assert: (d includesKey: 'hello').
  947. self deny: (d includesKey: 'foo').
  948. d at: 1 put: 2.
  949. self assert: (d at: 1) equals: 2.
  950. d at: 1@3 put: 3.
  951. self assert: (d at: 1@3) equals: 3.
  952. self assert: (d includesKey: 1@3).
  953. self deny: (d includesKey: 3@1)
  954. !
  955. testDynamicDictionaries
  956. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  957. ! !
  958. !DictionaryTest class methodsFor: 'fixture'!
  959. collectionClass
  960. ^ Dictionary
  961. ! !
  962. AssociativeCollectionTest subclass: #HashedCollectionTest
  963. slots: {}
  964. package: 'Kernel-Tests'!
  965. !HashedCollectionTest methodsFor: 'fixture'!
  966. collection
  967. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  968. !
  969. collectionKeys
  970. ^ { 'b'. 'a'. 'c'. 'd' }
  971. !
  972. collectionOfPrintStrings
  973. ^ #{ 'b' -> '1'. 'a' -> '2'. 'c' -> '3'. 'd' -> '-4' }
  974. !
  975. collectionSize
  976. ^ 4
  977. !
  978. collectionValues
  979. ^ { 1. 2. 3. -4 }
  980. !
  981. collectionWithDuplicates
  982. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10. 'h' -> 0 }
  983. !
  984. collectionWithNewValue
  985. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'new' -> 'N' }
  986. !
  987. sampleNewValueAsCollection
  988. ^ #{ 'new' -> 'N' }
  989. ! !
  990. !HashedCollectionTest methodsFor: 'tests'!
  991. testDynamicDictionaries
  992. self assert: #{'hello' -> 1} asHashedCollection equals: (HashedCollection with: 'hello' -> 1)
  993. ! !
  994. !HashedCollectionTest class methodsFor: 'fixture'!
  995. collectionClass
  996. ^ HashedCollection
  997. ! !
  998. CollectionTest subclass: #SequenceableCollectionTest
  999. slots: {}
  1000. package: 'Kernel-Tests'!
  1001. !SequenceableCollectionTest methodsFor: 'fixture'!
  1002. collectionFirst
  1003. self subclassResponsibility
  1004. !
  1005. collectionFirstTwo
  1006. self subclassResponsibility
  1007. !
  1008. collectionLast
  1009. self subclassResponsibility
  1010. !
  1011. collectionLastTwo
  1012. self subclassResponsibility
  1013. !
  1014. nonIndexesDo: aBlock
  1015. aBlock value: 0.
  1016. aBlock value: self collectionSize + 1.
  1017. aBlock value: 'z'
  1018. !
  1019. samplesDo: aBlock
  1020. aBlock value: 1 value: self collectionFirst.
  1021. aBlock value: self collectionSize value: self collectionLast
  1022. ! !
  1023. !SequenceableCollectionTest methodsFor: 'tests'!
  1024. testBeginsWith
  1025. self assert: (self collection beginsWith: self collectionClass new).
  1026. self assert: (self collection beginsWith: self collection).
  1027. self assert: (self collection beginsWith: self collectionFirstTwo).
  1028. self deny: (self collection beginsWith: self collectionLastTwo)
  1029. !
  1030. testEndsWith
  1031. self assert: (self collection endsWith: self collectionClass new).
  1032. self assert: (self collection endsWith: self collection).
  1033. self assert: (self collection endsWith: self collectionLastTwo).
  1034. self deny: (self collection endsWith: self collectionFirstTwo)
  1035. !
  1036. testFirst
  1037. self assert: self collection first equals: self collectionFirst
  1038. !
  1039. testFirstN
  1040. self
  1041. assert: (self collection first: 2)
  1042. equals: self collectionFirstTwo.
  1043. self
  1044. assert: (self collection first: 0)
  1045. equals: self collectionClass new.
  1046. self
  1047. assert: (self collection first: self collectionSize)
  1048. equals: self collection.
  1049. self should: [ self collection first: 33 ] raise: Error
  1050. !
  1051. testFourth
  1052. self assert: (self collection fourth) equals: (self collection at: 4)
  1053. !
  1054. testIndexOfStartingAt
  1055. | jsNull |
  1056. jsNull := JSON parse: 'null'.
  1057. self samplesDo: [ :index :value |
  1058. self assert: (self collection indexOf: value startingAt: 1) equals: index.
  1059. self assert: (self collection indexOf: value startingAt: index) equals: index.
  1060. self assert: (self collection indexOf: value startingAt: index+1) equals: 0 ]
  1061. !
  1062. testIndexOfStartingAtWithNull
  1063. | jsNull |
  1064. jsNull := JSON parse: 'null'.
  1065. self samplesDo: [ :index :value | | collection |
  1066. collection := self collection.
  1067. collection at: index put: jsNull.
  1068. self assert: (collection indexOf: jsNull startingAt: 1) equals: index.
  1069. self assert: (collection indexOf: jsNull startingAt: index) equals: index.
  1070. self assert: (collection indexOf: jsNull startingAt: index+1) equals: 0 ]
  1071. !
  1072. testLast
  1073. self assert: self collection last equals: self collectionLast
  1074. !
  1075. testLastN
  1076. self
  1077. assert: (self collection last: 2)
  1078. equals: self collectionLastTwo.
  1079. self
  1080. assert: (self collection last: 0)
  1081. equals: self collectionClass new.
  1082. self
  1083. assert: (self collection last: self collectionSize)
  1084. equals: self collection.
  1085. self should: [ self collection last: 33 ] raise: Error
  1086. !
  1087. testSecond
  1088. self assert: (self collection second) equals: (self collection at: 2)
  1089. !
  1090. testThird
  1091. self assert: (self collection third) equals: (self collection at: 3)
  1092. ! !
  1093. SequenceableCollectionTest subclass: #ArrayTest
  1094. slots: {}
  1095. package: 'Kernel-Tests'!
  1096. !ArrayTest methodsFor: 'fixture'!
  1097. collection
  1098. ^ #(1 2 3 -4)
  1099. !
  1100. collectionFirst
  1101. ^ 1
  1102. !
  1103. collectionFirstTwo
  1104. ^ #(1 2)
  1105. !
  1106. collectionLast
  1107. ^ -4
  1108. !
  1109. collectionLastTwo
  1110. ^ #(3 -4)
  1111. !
  1112. collectionOfPrintStrings
  1113. ^ #('1' '2' '3' '-4')
  1114. !
  1115. collectionSize
  1116. ^ 4
  1117. !
  1118. collectionWithDuplicates
  1119. ^ #('a' 'b' 'c' 1 2 1 'a' ())
  1120. !
  1121. collectionWithNewValue
  1122. ^ #(1 2 3 -4 'N')
  1123. !
  1124. sampleNewIndex
  1125. ^ 5
  1126. !
  1127. samplesDo: aBlock
  1128. super samplesDo: aBlock.
  1129. aBlock value: 3 value: 3.
  1130. ! !
  1131. !ArrayTest methodsFor: 'tests'!
  1132. testAdd
  1133. | array |
  1134. array := self collection.
  1135. array add: 6.
  1136. self assert: array last equals: 6
  1137. !
  1138. testAddFirst
  1139. self assert: (self collection addFirst: 0; yourself) first equals: 0
  1140. !
  1141. testPrintString
  1142. | array |
  1143. array := Array new.
  1144. self assert: array printString equals: 'an Array ()'.
  1145. array add: 1; add: 3.
  1146. self assert: array printString equals: 'an Array (1 3)'.
  1147. array add: 'foo'.
  1148. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  1149. array remove: 1; remove: 3.
  1150. self assert: array printString equals: 'an Array (''foo'')'.
  1151. array addLast: 3.
  1152. self assert: array printString equals: 'an Array (''foo'' 3)'.
  1153. array addLast: 3.
  1154. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  1155. !
  1156. testRemove
  1157. | array |
  1158. array := #(1 2 3 4 5).
  1159. array remove: 3.
  1160. self assert: array equals: #(1 2 4 5).
  1161. self should: [ array remove: 3 ] raise: Error
  1162. !
  1163. testRemoveFromTo
  1164. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  1165. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  1166. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  1167. !
  1168. testRemoveIndex
  1169. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  1170. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  1171. self assert: (#('hello') removeIndex: 1) equals: #()
  1172. !
  1173. testRemoveLast
  1174. | array |
  1175. array := #(1 2).
  1176. array removeLast.
  1177. self assert: array last equals: 1
  1178. !
  1179. testReversed
  1180. |array|
  1181. array := #(5 4 3 2 1).
  1182. self assert: (array reversed) equals: #(1 2 3 4 5)
  1183. !
  1184. testSort
  1185. | array |
  1186. array := #(10 1 5).
  1187. array sort.
  1188. self assert: array equals: #(1 5 10)
  1189. ! !
  1190. !ArrayTest class methodsFor: 'fixture'!
  1191. collectionClass
  1192. ^ Array
  1193. ! !
  1194. SequenceableCollectionTest subclass: #StringTest
  1195. slots: {}
  1196. package: 'Kernel-Tests'!
  1197. !StringTest methodsFor: 'fixture'!
  1198. collection
  1199. ^ 'helLo'
  1200. !
  1201. collectionFirst
  1202. ^ 'h'
  1203. !
  1204. collectionFirstTwo
  1205. ^ 'he'
  1206. !
  1207. collectionLast
  1208. ^ 'o'
  1209. !
  1210. collectionLastTwo
  1211. ^ 'Lo'
  1212. !
  1213. collectionOfPrintStrings
  1214. ^ '''h''''e''''l''''L''''o'''
  1215. !
  1216. collectionSize
  1217. ^ 5
  1218. !
  1219. collectionWithDuplicates
  1220. ^ 'abbaerten'
  1221. !
  1222. collectionWithNewValue
  1223. ^ 'helLoN'
  1224. !
  1225. sampleNewValueAsCollection
  1226. ^ 'N'
  1227. !
  1228. samplesDo: aBlock
  1229. super samplesDo: aBlock.
  1230. aBlock value: 3 value: 'l'
  1231. ! !
  1232. !StringTest methodsFor: 'tests'!
  1233. testAddAll
  1234. "String instances are read-only"
  1235. self should: [ self collection addAll: self collection ] raise: Error
  1236. !
  1237. testAddRemove
  1238. self should: [ 'hello' add: 'a' ] raise: Error.
  1239. self should: [ 'hello' remove: 'h' ] raise: Error
  1240. !
  1241. testAsArray
  1242. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  1243. !
  1244. testAsLowerCase
  1245. self assert: 'JACKIE' asLowercase equals: 'jackie'.
  1246. !
  1247. testAsNumber
  1248. self assert: '3' asNumber equals: 3.
  1249. self assert: '-3' asNumber equals: -3.
  1250. self assert: '-1.5' asNumber equals: -1.5.
  1251. !
  1252. testAsUpperCase
  1253. self assert: 'jackie' asUppercase equals: 'JACKIE'.
  1254. !
  1255. testAsciiValue
  1256. | characterA characterU |
  1257. characterA := 'A'.
  1258. characterU := 'U'.
  1259. self assert: (characterA asciiValue) equals:65.
  1260. self assert: (characterU asciiValue) equals:85
  1261. !
  1262. testAtIfAbsentPut
  1263. "String instances are read-only"
  1264. self should: [ 'hello' at: 6 ifAbsentPut: [ 'a' ] ] raise: Error
  1265. !
  1266. testAtPut
  1267. "String instances are read-only"
  1268. self should: [ 'hello' at: 1 put: 'a' ] raise: Error
  1269. !
  1270. testCapitalized
  1271. self assert: 'test' capitalized equals: 'Test'.
  1272. self assert: 'Test' capitalized equals: 'Test'.
  1273. self assert: '' capitalized equals: ''.
  1274. self assert: 'Test' isCapitalized equals: true.
  1275. self assert: 'test' isCapitalized equals: false.
  1276. !
  1277. testCharCodeAt
  1278. self assert: ('jackie' charCodeAt:1) equals: 106.
  1279. self assert: ('jackie' charCodeAt:2) equals: 97.
  1280. self assert: ('jackie' charCodeAt:3) equals: 99.
  1281. self assert: ('jackie' charCodeAt:4) equals: 107.
  1282. self assert: ('jackie' charCodeAt:5) equals: 105.
  1283. self assert: ('jackie' charCodeAt:6) equals: 101
  1284. !
  1285. testCopyFromTo
  1286. self assert: ('jackie' copyFrom: 1 to: 3) equals: 'jac'.
  1287. self assert: ('jackie' copyFrom: 4 to: 6) equals: 'kie'.
  1288. !
  1289. testCopySeparates
  1290. "String instances are immutable"
  1291. self assert: self collection copy == self collection
  1292. !
  1293. testCopyWithoutAll
  1294. self
  1295. assert: ('*hello* *world*' copyWithoutAll: '*')
  1296. equals: 'hello world'
  1297. !
  1298. testEquality
  1299. self assert: 'hello' equals: 'hello'.
  1300. self deny: 'hello' = 'world'.
  1301. "Test for issue 459"
  1302. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  1303. self assert: 'hello' equals: 'hello' yourself.
  1304. self assert: 'hello' yourself equals: 'hello'.
  1305. "test JS falsy value"
  1306. self deny: '' = 0
  1307. !
  1308. testIdentity
  1309. self assert: 'hello' == 'hello'.
  1310. self deny: 'hello' == 'world'.
  1311. self assert: 'hello' == 'hello' yourself.
  1312. self assert: 'hello' yourself == 'hello'.
  1313. "test JS falsy value"
  1314. self deny: '' == 0
  1315. !
  1316. testIncludesSubString
  1317. self assert: ('amber' includesSubString: 'ber').
  1318. self deny: ('amber' includesSubString: 'zork').
  1319. !
  1320. testIndexOfStartingAtWithNull
  1321. "String cannot hold JS null"
  1322. !
  1323. testIndexOfWithNull
  1324. "String cannot hold JS null"
  1325. !
  1326. testIsVowel
  1327. |vowel consonant|
  1328. vowel := 'u'.
  1329. consonant := 'z'.
  1330. self assert: vowel isVowel equals: true.
  1331. self assert: consonant isVowel equals: false
  1332. !
  1333. testJoin
  1334. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  1335. !
  1336. testRegression1224
  1337. "String instances are read-only"
  1338. self should: [ (self collectionClass new
  1339. remove: self sampleNewValue ifAbsent: [];
  1340. yourself) size ] raise: Error
  1341. !
  1342. testRemoveAll
  1343. self should: [ self collection removeAll ] raise: Error
  1344. !
  1345. testReversed
  1346. self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
  1347. !
  1348. testStreamContents
  1349. self
  1350. assert: (String streamContents: [ :aStream |
  1351. aStream
  1352. nextPutAll: 'hello'; space;
  1353. nextPutAll: 'world' ])
  1354. equals: 'hello world'
  1355. !
  1356. testSubStrings
  1357. self assert: ('jackiechan' subStrings: 'ie') equals: #( 'jack' 'chan' ).
  1358. !
  1359. testTrim
  1360. self assert: ' jackie' trimLeft equals: 'jackie'.
  1361. self assert: 'jackie ' trimRight equals: 'jackie'.
  1362. !
  1363. testValue
  1364. self assert: (#asString value: 1) equals: '1'.
  1365. "Which (since String and BlockClosure are now polymorphic) enables the nice idiom..."
  1366. self assert: (#(1 2 3) collect: #asString) equals: #('1' '2' '3')
  1367. ! !
  1368. !StringTest class methodsFor: 'fixture'!
  1369. collectionClass
  1370. ^ String
  1371. ! !
  1372. CollectionTest subclass: #SetTest
  1373. slots: {}
  1374. package: 'Kernel-Tests'!
  1375. !SetTest methodsFor: 'fixture'!
  1376. collection
  1377. ^ Set new
  1378. add: Smalltalk;
  1379. add: nil;
  1380. add: 3@3;
  1381. add: false;
  1382. add: sampleBlock;
  1383. yourself
  1384. !
  1385. collectionOfPrintStrings
  1386. ^ Set new
  1387. add: 'a SmalltalkImage';
  1388. add: 'nil';
  1389. add: '3@3';
  1390. add: 'false';
  1391. add: 'a BlockClosure';
  1392. yourself
  1393. !
  1394. collectionSize
  1395. ^ 5
  1396. !
  1397. collectionWithDuplicates
  1398. "Set has no duplicates"
  1399. ^ self collection add: 0; yourself
  1400. !
  1401. collectionWithNewValue
  1402. ^ Set new
  1403. add: Smalltalk;
  1404. add: nil;
  1405. add: 3@3;
  1406. add: 'N';
  1407. add: false;
  1408. add: sampleBlock;
  1409. yourself
  1410. ! !
  1411. !SetTest methodsFor: 'tests'!
  1412. testAddAll
  1413. super testAddAll.
  1414. self assert: (self collection addAll: self collection; yourself) equals: self collection.
  1415. self assert: (self collection addAll: self collectionWithNewValue; yourself) equals: self collectionWithNewValue.
  1416. self assert: (self collectionWithNewValue addAll: self collection; yourself) equals: self collectionWithNewValue
  1417. !
  1418. testAddRemove
  1419. | set |
  1420. set := Set new.
  1421. self assert: set isEmpty.
  1422. set add: 3.
  1423. self assert: (set includes: 3).
  1424. set add: 5.
  1425. self assert: (set includes: 5).
  1426. set remove: 3.
  1427. self deny: (set includes: 3)
  1428. !
  1429. testAt
  1430. self should: [ Set new at: 1 put: 2 ] raise: Error
  1431. !
  1432. testCollect
  1433. super testCollect.
  1434. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1435. !
  1436. testComma
  1437. super testComma.
  1438. self assert: self collection, self collection equals: self collection.
  1439. self assert: self collection, self collectionWithNewValue equals: self collectionWithNewValue.
  1440. self assert: self collectionWithNewValue, self collection equals: self collectionWithNewValue
  1441. !
  1442. testComparing
  1443. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1444. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1445. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1446. self deny: #(1 2) asSet = #(0 2) asSet
  1447. !
  1448. testPrintString
  1449. | set |
  1450. set := Set new.
  1451. self assert: set printString equals: 'a Set ()'.
  1452. set add: 1; add: 3.
  1453. self assert: set printString equals: 'a Set (1 3)'.
  1454. set add: 'foo'.
  1455. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1456. set remove: 1; remove: 3.
  1457. self assert: set printString equals: 'a Set (''foo'')'.
  1458. set add: 3.
  1459. self assert: set printString equals: 'a Set (3 ''foo'')'.
  1460. set add: 3.
  1461. self assert: set printString equals: 'a Set (3 ''foo'')'
  1462. !
  1463. testRegression1225
  1464. self assert: (#(1 2 3) asSet add: 3) equals: 3
  1465. !
  1466. testRegression1226
  1467. self assert: (#(1 2 3) asSet remove: 3) equals: 3
  1468. !
  1469. testRegression1227
  1470. self assert: (#(1 2 3) asSet remove: 4 ifAbsent: [5]) equals: 5
  1471. !
  1472. testRegression1228
  1473. self should: [#(1 2 3) asSet remove: 4] raise: Error
  1474. !
  1475. testRegression1245
  1476. self assert: ({Object. String} asSet remove: String) equals: String
  1477. !
  1478. testUnboxedObjects
  1479. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1480. !
  1481. testUnicity
  1482. | set |
  1483. set := Set new.
  1484. set add: 21.
  1485. set add: 'hello'.
  1486. set add: 21.
  1487. self assert: set size equals: 2.
  1488. set add: 'hello'.
  1489. self assert: set size equals: 2.
  1490. self assert: set asArray equals: #(21 'hello')
  1491. ! !
  1492. !SetTest class methodsFor: 'fixture'!
  1493. collectionClass
  1494. ^ Set
  1495. ! !
  1496. TestCase subclass: #ConsoleTranscriptTest
  1497. slots: {}
  1498. package: 'Kernel-Tests'!
  1499. !ConsoleTranscriptTest methodsFor: 'tests'!
  1500. testShow
  1501. | originalTranscript |
  1502. originalTranscript := Transcript current.
  1503. Transcript register: ConsoleTranscript new.
  1504. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  1505. self shouldnt: [ Transcript show: console ] raise: Error.
  1506. Transcript register: originalTranscript.
  1507. ! !
  1508. TestCase subclass: #DateTest
  1509. slots: {}
  1510. package: 'Kernel-Tests'!
  1511. !DateTest methodsFor: 'tests'!
  1512. testEquality
  1513. | now |
  1514. now := Date new.
  1515. self assert: now = now.
  1516. self deny: now = (Date fromMilliseconds: 0).
  1517. self assert: (Date fromMilliseconds: 12345678) = (Date fromMilliseconds: 12345678).
  1518. self assert: now = (Date fromMilliseconds: now asMilliseconds).
  1519. self assert: (Date fromMilliseconds: now asMilliseconds) = now
  1520. !
  1521. testIdentity
  1522. | now |
  1523. now := Date new.
  1524. self assert: now == now.
  1525. self deny: now == (Date fromMilliseconds: 0).
  1526. self deny: (Date fromMilliseconds: 12345678) == (Date fromMilliseconds: 12345678).
  1527. self deny: now == (Date fromMilliseconds: now asMilliseconds).
  1528. self deny: (Date fromMilliseconds: now asMilliseconds) == now
  1529. !
  1530. testPlusAndMinus
  1531. | a b now zeroDuration |
  1532. a := Date fromString: '1974-07-12 14:30'.
  1533. now := Date now.
  1534. b := Date fromString: '2616-03-06'.
  1535. zeroDuration := 0.
  1536. self assert: {a-a. now-now. b-b} asSet equals: (Set with: zeroDuration).
  1537. self assert: now + (b - now) equals: b.
  1538. self assert: a + (b - a) equals: b.
  1539. self assert: now + (a - now) equals: a.
  1540. self assert: a + ((now - a) + (b - now)) equals: b
  1541. ! !
  1542. TestCase subclass: #JSObjectProxyTest
  1543. slots: {}
  1544. package: 'Kernel-Tests'!
  1545. !JSObjectProxyTest methodsFor: 'accessing'!
  1546. jsObject
  1547. <inlineJS:
  1548. 'return {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: "", "e": null, "f": void 0}'>
  1549. !
  1550. jsUndefined
  1551. <inlineJS: 'return'>
  1552. ! !
  1553. !JSObjectProxyTest methodsFor: 'tests'!
  1554. testAtIfAbsent
  1555. | testObject |
  1556. testObject := self jsObject.
  1557. self assert: (testObject at: 'abc' ifAbsent: [ 'Property does not exist' ]) equals: 'Property does not exist'.
  1558. self assert: (testObject at: 'e' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  1559. self assert: (testObject at: 'a' ifAbsent: [ 'Property does not exist' ]) equals: 1.
  1560. self assert: (testObject at: 'f' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  1561. !
  1562. testAtIfPresent
  1563. | testObject |
  1564. testObject := self jsObject.
  1565. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  1566. self assert: (testObject at: 'e' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  1567. self assert: (testObject at: 'a' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello 1'.
  1568. self assert: (testObject at: 'f' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  1569. !
  1570. testAtIfPresentIfAbsent
  1571. | testObject |
  1572. testObject := self jsObject.
  1573. self assert: (testObject at: 'abc' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'not present'.
  1574. self assert: (testObject at: 'e' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  1575. self assert: (testObject at: 'a' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello 1'.
  1576. self assert: (testObject at: 'f' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  1577. !
  1578. testAtPut
  1579. | testObject |
  1580. testObject := self jsObject.
  1581. self assert: (testObject at: 'abc') ~= 'xyz'.
  1582. self assert: (testObject at: 'abc' put: 'xyz') equals: 'xyz'.
  1583. self assert: (testObject at: 'abc') equals: 'xyz'
  1584. !
  1585. testComparison
  1586. self assert: ({ console. 2 } indexOf: console) equals: 1.
  1587. self assert: console = console.
  1588. self deny: console = Object new.
  1589. self deny: console = self jsObject
  1590. !
  1591. testDNU
  1592. self should: [ self jsObject foo ] raise: MessageNotUnderstood
  1593. !
  1594. testDNUWithAllowJavaScriptCalls
  1595. | jsObject |
  1596. jsObject := #().
  1597. jsObject basicAt: 'allowJavaScriptCalls' put: true.
  1598. self should: [ jsObject foo ] raise: MessageNotUnderstood
  1599. !
  1600. testMessageSend
  1601. self assert: self jsObject a equals: 1.
  1602. self assert: self jsObject b equals: 2.
  1603. self assert: (self jsObject c: 3) equals: 3
  1604. !
  1605. testMethodWithArguments
  1606. self assert: (self jsObject c: 1) equals: 1
  1607. !
  1608. testNull
  1609. self assert: JSObjectProxy null isNil.
  1610. self
  1611. assert: (JSON stringify: #{#foo -> JSObjectProxy null})
  1612. equals: '{"foo":null}'
  1613. !
  1614. testPrinting
  1615. self assert: self jsObject printString equals: '[object Object]'
  1616. !
  1617. testPropertyThatReturnsEmptyString
  1618. | object |
  1619. object := self jsObject.
  1620. self assert: object d equals: ''.
  1621. object d: 'hello'.
  1622. self assert: object d equals: 'hello'
  1623. !
  1624. testPropertyThatReturnsUndefined
  1625. | object |
  1626. object := self jsObject.
  1627. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  1628. self assert: object e isNil
  1629. !
  1630. testSetPropertyWithFalsyValue
  1631. | jsObject |
  1632. jsObject := self jsObject.
  1633. self assert: (jsObject a) equals: 1.
  1634. jsObject a: JSObjectProxy null.
  1635. self assert: (jsObject a) equals: nil.
  1636. jsObject a: 0.
  1637. self assert: (jsObject a) equals: 0.
  1638. jsObject a: self jsUndefined.
  1639. self assert: (jsObject a) equals: nil.
  1640. jsObject a: ''.
  1641. self assert: (jsObject a) equals: ''.
  1642. jsObject a: false.
  1643. self assert: (jsObject a) equals: false
  1644. !
  1645. testUndefined
  1646. self assert: JSObjectProxy undefined isNil.
  1647. self
  1648. assert: (JSON stringify: #{#foo -> JSObjectProxy undefined})
  1649. equals: '{}'
  1650. !
  1651. testValue
  1652. | testObject |
  1653. testObject := self jsObject.
  1654. testObject at: 'value' put: 'aValue'.
  1655. self assert: testObject value equals: 'aValue'
  1656. !
  1657. testYourself
  1658. | object |
  1659. object := self jsObject
  1660. d: 'test';
  1661. yourself.
  1662. self assert: object d equals: 'test'
  1663. ! !
  1664. TestCase subclass: #JavaScriptExceptionTest
  1665. slots: {}
  1666. package: 'Kernel-Tests'!
  1667. !JavaScriptExceptionTest methodsFor: 'helpers'!
  1668. throwException
  1669. <inlineJS: 'throw "test"'>
  1670. ! !
  1671. !JavaScriptExceptionTest methodsFor: 'tests'!
  1672. testCatchingException
  1673. [ self throwException ]
  1674. on: Error
  1675. do: [ :error |
  1676. self assert: error exception = 'test' ]
  1677. !
  1678. testRaisingException
  1679. self should: [ self throwException ] raise: JavaScriptException
  1680. ! !
  1681. TestCase subclass: #MessageSendTest
  1682. slots: {}
  1683. package: 'Kernel-Tests'!
  1684. !MessageSendTest methodsFor: 'tests'!
  1685. testValue
  1686. | messageSend |
  1687. messageSend := MessageSend new
  1688. receiver: Object new;
  1689. selector: #asString;
  1690. yourself.
  1691. self assert: messageSend value equals: 'an Object'
  1692. !
  1693. testValueWithArguments
  1694. | messageSend |
  1695. messageSend := MessageSend new
  1696. receiver: 2;
  1697. selector: '+';
  1698. yourself.
  1699. self assert: (messageSend value: 3) equals: 5.
  1700. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  1701. ! !
  1702. TestCase subclass: #MethodInheritanceTest
  1703. slots: {#receiverTop. #receiverMiddle. #receiverBottom. #method. #performBlock}
  1704. package: 'Kernel-Tests'!
  1705. !MethodInheritanceTest methodsFor: 'accessing'!
  1706. codeGeneratorClass
  1707. ^ CodeGenerator
  1708. !
  1709. targetClassBottom
  1710. ^ JavaScriptException
  1711. !
  1712. targetClassMiddle
  1713. ^ Error
  1714. !
  1715. targetClassTop
  1716. ^ Object
  1717. ! !
  1718. !MethodInheritanceTest methodsFor: 'factory'!
  1719. compiler
  1720. ^ Compiler new
  1721. codeGeneratorClass: self codeGeneratorClass;
  1722. yourself
  1723. ! !
  1724. !MethodInheritanceTest methodsFor: 'initialization'!
  1725. setUp
  1726. receiverTop := self targetClassTop new.
  1727. receiverMiddle := self targetClassMiddle new.
  1728. receiverBottom := self targetClassBottom new.
  1729. method := nil.
  1730. performBlock := [ self error: 'performBlock not initialized' ]
  1731. !
  1732. tearDown
  1733. [ self deinstallTop ] on: Error do: [ ].
  1734. [ self deinstallMiddle ] on: Error do: [ ].
  1735. [ self deinstallBottom ] on: Error do: [ ]
  1736. ! !
  1737. !MethodInheritanceTest methodsFor: 'testing'!
  1738. deinstallBottom
  1739. self targetClassBottom removeCompiledMethod: method
  1740. !
  1741. deinstallMiddle
  1742. self targetClassMiddle removeCompiledMethod: method
  1743. !
  1744. deinstallTop
  1745. self targetClassTop removeCompiledMethod: method
  1746. !
  1747. installBottom: aString
  1748. method := self compiler install: aString forClass: self targetClassBottom protocol: 'tests'
  1749. !
  1750. installMiddle: aString
  1751. method := self compiler install: aString forClass: self targetClassMiddle protocol: 'tests'
  1752. !
  1753. installTop: aString
  1754. method := self compiler install: aString forClass: self targetClassTop protocol: 'tests'
  1755. !
  1756. shouldMNU
  1757. self shouldMNUTop.
  1758. self shouldMNUMiddle.
  1759. self shouldMNUBottom
  1760. !
  1761. shouldMNUBottom
  1762. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  1763. !
  1764. shouldMNUMiddle
  1765. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  1766. !
  1767. shouldMNUTop
  1768. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  1769. !
  1770. shouldReturn: anObject
  1771. | result |
  1772. result := performBlock value: receiverTop.
  1773. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1774. result := performBlock value: receiverMiddle.
  1775. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  1776. result := performBlock value: receiverBottom.
  1777. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  1778. !
  1779. shouldReturn: anObject and: anObject2 and: anObject3
  1780. | result |
  1781. result := performBlock value: receiverTop.
  1782. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1783. result := performBlock value: receiverMiddle.
  1784. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  1785. result := performBlock value: receiverBottom.
  1786. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  1787. ! !
  1788. !MethodInheritanceTest methodsFor: 'tests'!
  1789. testMNU11
  1790. performBlock := [ :x | x foo ].
  1791. self shouldMNU.
  1792. self installTop: 'foo ^ false'.
  1793. self installTop: 'foo ^ true'.
  1794. self deinstallTop.
  1795. self shouldMNU
  1796. !
  1797. testMNU22
  1798. performBlock := [ :x | x foo ].
  1799. self shouldMNU.
  1800. self installMiddle: 'foo ^ false'.
  1801. self installMiddle: 'foo ^ true'.
  1802. self deinstallMiddle.
  1803. self shouldMNU
  1804. !
  1805. testReturns1
  1806. performBlock := [ :x | x foo ].
  1807. self installTop: 'foo ^ false'.
  1808. self shouldReturn: false.
  1809. self installTop: 'foo ^ true'.
  1810. self shouldReturn: true
  1811. ! !
  1812. TestCase subclass: #NumberTest
  1813. slots: {}
  1814. package: 'Kernel-Tests'!
  1815. !NumberTest methodsFor: 'tests'!
  1816. testAbs
  1817. self assert: 4 abs equals: 4.
  1818. self assert: -4 abs equals: 4
  1819. !
  1820. testArithmetic
  1821. "We rely on JS here, so we won't test complex behavior, just check if
  1822. message sends are corrects"
  1823. self assert: 1.5 + 1 equals: 2.5.
  1824. self assert: 2 - 1 equals: 1.
  1825. self assert: -2 - 1 equals: -3.
  1826. self assert: 12 / 2 equals: 6.
  1827. self assert: 3 * 4 equals: 12.
  1828. self assert: 7 // 2 equals: 3.
  1829. self assert: 7 \\ 2 equals: 1.
  1830. "Simple parenthesis and execution order"
  1831. self assert: 1 + 2 * 3 equals: 9.
  1832. self assert: 1 + (2 * 3) equals: 7
  1833. !
  1834. testAsNumber
  1835. self assert: 3 asNumber equals: 3.
  1836. !
  1837. testBetweenAnd
  1838. self assert: (4 between: 3 and: 5).
  1839. self assert: (1 between: 5 and: 6) not.
  1840. self assert: (90 between: 67 and: 87) not.
  1841. self assert: (1 between: 1 and: 1).
  1842. !
  1843. testBitAnd
  1844. self assert: (15 bitAnd: 2) equals: 2.
  1845. self assert: (15 bitAnd: 15) equals: 15.
  1846. self assert: (-1 bitAnd: 1021) equals: 1021
  1847. !
  1848. testBitNot
  1849. self assert: 2 bitNot equals: -3.
  1850. self assert: -1 bitNot equals: 0.
  1851. self assert: -1022 bitNot equals: 1021
  1852. !
  1853. testBitOr
  1854. self assert: (2 bitOr: 4) equals: 6.
  1855. self assert: (7 bitOr: 2) equals: 7.
  1856. self assert: (-1 bitOr: 1021) equals: -1
  1857. !
  1858. testBitXor
  1859. self assert: (2 bitXor: 4) equals: 6.
  1860. self assert: (7 bitXor: 2) equals: 5.
  1861. self assert: (-1 bitXor: 1021) equals: -1022.
  1862. self assert: (91 bitXor: 91) equals: 0
  1863. !
  1864. testCeiling
  1865. self assert: 1.2 ceiling equals: 2.
  1866. self assert: -1.2 ceiling equals: -1.
  1867. self assert: 1.0 ceiling equals: 1.
  1868. !
  1869. testComparison
  1870. self assert: 3 > 2.
  1871. self assert: 2 < 3.
  1872. self deny: 3 < 2.
  1873. self deny: 2 > 3.
  1874. self assert: 3 >= 3.
  1875. self assert: 3.1 >= 3.
  1876. self assert: 3 <= 3.
  1877. self assert: 3 <= 3.1
  1878. !
  1879. testCopying
  1880. self assert: 1 copy == 1.
  1881. self assert: 1 deepCopy == 1
  1882. !
  1883. testDegreesToRadians
  1884. self assert: (180 degreesToRadians - Number pi) abs <= 0.01.
  1885. !
  1886. testEquality
  1887. self assert: (1 = 1).
  1888. self assert: (0 = 0).
  1889. self deny: (1 = 0).
  1890. self assert: (1 yourself = 1).
  1891. self assert: (1 = 1 yourself).
  1892. self assert: (1 yourself = 1 yourself).
  1893. self deny: 0 = false.
  1894. self deny: false = 0.
  1895. self deny: '' = 0.
  1896. self deny: 0 = ''
  1897. !
  1898. testFloor
  1899. self assert: 1.2 floor equals: 1.
  1900. self assert: -1.2 floor equals: -2.
  1901. self assert: 1.0 floor equals: 1.
  1902. !
  1903. testHexNumbers
  1904. self assert: 16r9 equals: 9.
  1905. self assert: 16rA truncated equals: 10.
  1906. self assert: 16rB truncated equals: 11.
  1907. self assert: 16rC truncated equals: 12.
  1908. self assert: 16rD truncated equals: 13.
  1909. self assert: 16rE truncated equals: 14.
  1910. self assert: 16rF truncated equals: 15
  1911. !
  1912. testIdentity
  1913. self assert: 1 == 1.
  1914. self assert: 0 == 0.
  1915. self deny: 1 == 0.
  1916. self assert: 1 yourself == 1.
  1917. self assert: 1 == 1 yourself.
  1918. self assert: 1 yourself == 1 yourself.
  1919. self deny: 1 == 2
  1920. !
  1921. testInvalidHexNumbers
  1922. self should: [ 16rG ] raise: MessageNotUnderstood.
  1923. self should: [ 16rg ] raise: MessageNotUnderstood.
  1924. self should: [ 16rH ] raise: MessageNotUnderstood.
  1925. self should: [ 16rh ] raise: MessageNotUnderstood.
  1926. self should: [ 16rI ] raise: MessageNotUnderstood.
  1927. self should: [ 16ri ] raise: MessageNotUnderstood.
  1928. self should: [ 16rJ ] raise: MessageNotUnderstood.
  1929. self should: [ 16rj ] raise: MessageNotUnderstood.
  1930. self should: [ 16rK ] raise: MessageNotUnderstood.
  1931. self should: [ 16rk ] raise: MessageNotUnderstood.
  1932. self should: [ 16rL ] raise: MessageNotUnderstood.
  1933. self should: [ 16rl ] raise: MessageNotUnderstood.
  1934. self should: [ 16rM ] raise: MessageNotUnderstood.
  1935. self should: [ 16rm ] raise: MessageNotUnderstood.
  1936. self should: [ 16rN ] raise: MessageNotUnderstood.
  1937. self should: [ 16rn ] raise: MessageNotUnderstood.
  1938. self should: [ 16rO ] raise: MessageNotUnderstood.
  1939. self should: [ 16ro ] raise: MessageNotUnderstood.
  1940. self should: [ 16rP ] raise: MessageNotUnderstood.
  1941. self should: [ 16rp ] raise: MessageNotUnderstood.
  1942. self should: [ 16rQ ] raise: MessageNotUnderstood.
  1943. self should: [ 16rq ] raise: MessageNotUnderstood.
  1944. self should: [ 16rR ] raise: MessageNotUnderstood.
  1945. self should: [ 16rr ] raise: MessageNotUnderstood.
  1946. self should: [ 16rS ] raise: MessageNotUnderstood.
  1947. self should: [ 16rs ] raise: MessageNotUnderstood.
  1948. self should: [ 16rT ] raise: MessageNotUnderstood.
  1949. self should: [ 16rt ] raise: MessageNotUnderstood.
  1950. self should: [ 16rU ] raise: MessageNotUnderstood.
  1951. self should: [ 16ru ] raise: MessageNotUnderstood.
  1952. self should: [ 16rV ] raise: MessageNotUnderstood.
  1953. self should: [ 16rv ] raise: MessageNotUnderstood.
  1954. self should: [ 16rW ] raise: MessageNotUnderstood.
  1955. self should: [ 16rw ] raise: MessageNotUnderstood.
  1956. self should: [ 16rX ] raise: MessageNotUnderstood.
  1957. self should: [ 16rx ] raise: MessageNotUnderstood.
  1958. self should: [ 16rY ] raise: MessageNotUnderstood.
  1959. self should: [ 16ry ] raise: MessageNotUnderstood.
  1960. self should: [ 16rZ ] raise: MessageNotUnderstood.
  1961. self should: [ 16rz ] raise: MessageNotUnderstood.
  1962. self should: [ 16rABcdEfZ ] raise: MessageNotUnderstood.
  1963. !
  1964. testLog
  1965. self assert: 10000 log equals: 4.
  1966. self assert: (512 log: 2) equals: 9.
  1967. self assert: Number e ln equals: 1.
  1968. !
  1969. testMinMax
  1970. self assert: (2 max: 5) equals: 5.
  1971. self assert: (2 min: 5) equals: 2.
  1972. self assert: (2 min: 5 max: 3) equals: 3.
  1973. self assert: (7 min: 5 max: 3) equals: 5.
  1974. self assert: (4 min: 5 max: 3) equals: 4.
  1975. !
  1976. testNegated
  1977. self assert: 3 negated equals: -3.
  1978. self assert: -3 negated equals: 3
  1979. !
  1980. testPrintShowingDecimalPlaces
  1981. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1982. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1983. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1984. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1985. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1986. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1987. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1988. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1989. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1990. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1991. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1992. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  1993. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  1994. !
  1995. testPrintStringBase
  1996. self assert: (15 printStringBase: 2) equals: '1111'.
  1997. self assert: (15 printStringBase: 16) equals: 'f'.
  1998. self assert: (256 printStringBase: 16) equals: '100'.
  1999. self assert: (256 printStringBase: 2) equals: '100000000'
  2000. !
  2001. testRadiansToDegrees
  2002. self assert: (Number pi radiansToDegrees - 180) abs <= 0.01.
  2003. !
  2004. testRaisedTo
  2005. self assert: (2 raisedTo: 4) equals: 16.
  2006. self assert: (2 raisedTo: 0) equals: 1.
  2007. self assert: (2 raisedTo: -3) equals: 0.125.
  2008. self assert: (4 raisedTo: 0.5) equals: 2.
  2009. self assert: 2 ** 4 equals: 16.
  2010. !
  2011. testRounded
  2012. self assert: 3 rounded equals: 3.
  2013. self assert: 3.212 rounded equals: 3.
  2014. self assert: 3.51 rounded equals: 4
  2015. !
  2016. testSign
  2017. self assert: 5 sign equals: 1.
  2018. self assert: 0 sign equals: 0.
  2019. self assert: -1.4 sign equals: -1.
  2020. !
  2021. testSqrt
  2022. self assert: 4 sqrt equals: 2.
  2023. self assert: 16 sqrt equals: 4
  2024. !
  2025. testSquared
  2026. self assert: 4 squared equals: 16
  2027. !
  2028. testTimesRepeat
  2029. | i |
  2030. i := 0.
  2031. 0 timesRepeat: [ i := i + 1 ].
  2032. self assert: i equals: 0.
  2033. 5 timesRepeat: [ i := i + 1 ].
  2034. self assert: i equals: 5
  2035. !
  2036. testTo
  2037. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  2038. !
  2039. testToBy
  2040. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  2041. self should: [ 1 to: 4 by: 0 ] raise: Error
  2042. !
  2043. testTrigonometry
  2044. self assert: 0 cos equals: 1.
  2045. self assert: 0 sin equals: 0.
  2046. self assert: 0 tan equals: 0.
  2047. self assert: 1 arcCos equals: 0.
  2048. self assert: 0 arcSin equals: 0.
  2049. self assert: 0 arcTan equals: 0.
  2050. self assert: (0 arcTan: 1) equals: 0.
  2051. self assert: (1 arcTan: 0) equals: (Number pi / 2)
  2052. !
  2053. testTruncated
  2054. self assert: 3 truncated equals: 3.
  2055. self assert: 3.212 truncated equals: 3.
  2056. self assert: 3.51 truncated equals: 3
  2057. ! !
  2058. Object subclass: #ObjectMock
  2059. slots: {#foo. #bar}
  2060. package: 'Kernel-Tests'!
  2061. !ObjectMock commentStamp!
  2062. ObjectMock is there only to perform tests on classes.!
  2063. !ObjectMock methodsFor: 'not yet classified'!
  2064. foo
  2065. ^ foo
  2066. !
  2067. foo: anObject
  2068. foo := anObject
  2069. ! !
  2070. TestCase subclass: #ObjectTest
  2071. slots: {}
  2072. package: 'Kernel-Tests'!
  2073. !ObjectTest methodsFor: 'tests'!
  2074. notDefined
  2075. <inlineJS: 'return void 0;'>
  2076. !
  2077. testBasicAccess
  2078. | o |
  2079. o := Object new.
  2080. o basicAt: 'a' put: 1.
  2081. self assert: (o basicAt: 'a') equals: 1.
  2082. self assert: (o basicAt: 'b') equals: nil
  2083. !
  2084. testBasicPerform
  2085. | o |
  2086. o := Object new.
  2087. o basicAt: 'func' put: [ 'hello' ].
  2088. o basicAt: 'func2' put: [ :a | a + 1 ].
  2089. self assert: (o basicPerform: 'func') equals: 'hello'.
  2090. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  2091. !
  2092. testDNU
  2093. self should: [ Object new foo ] raise: MessageNotUnderstood
  2094. !
  2095. testEquality
  2096. | o |
  2097. o := Object new.
  2098. self deny: o = Object new.
  2099. self assert: (o = o).
  2100. self assert: (o yourself = o).
  2101. self assert: (o = o yourself)
  2102. !
  2103. testHalt
  2104. self should: [ Object new halt ] raise: Error
  2105. !
  2106. testIdentity
  2107. | o |
  2108. o := Object new.
  2109. self deny: o == Object new.
  2110. self assert: o == o.
  2111. self assert: o yourself == o.
  2112. self assert: o == o yourself
  2113. !
  2114. testIfNil
  2115. self deny: Object new isNil.
  2116. self deny: (Object new ifNil: [ true ]) = true.
  2117. self assert: (Object new ifNotNil: [ true ]) equals: true.
  2118. self assert: (Object new ifNil: [ false ] ifNotNil: [ true ]) equals: true.
  2119. self assert: (Object new ifNotNil: [ true ] ifNil: [ false ]) equals: true
  2120. !
  2121. testInstVars
  2122. | o |
  2123. o := ObjectMock new.
  2124. self assert: (o instVarNamed: #foo) equals: nil.
  2125. o instVarNamed: #foo put: 1.
  2126. self assert: (o instVarNamed: #foo) equals: 1.
  2127. self assert: (o instVarNamed: 'foo') equals: 1
  2128. !
  2129. testNilUndefined
  2130. "nil in Smalltalk is the undefined object in JS"
  2131. self assert: self notDefined equals: nil
  2132. !
  2133. testYourself
  2134. | o |
  2135. o := ObjectMock new.
  2136. self assert: o yourself == o
  2137. ! !
  2138. TestCase subclass: #PointTest
  2139. slots: {}
  2140. package: 'Kernel-Tests'!
  2141. !PointTest methodsFor: 'tests'!
  2142. testAccessing
  2143. self assert: (Point x: 3 y: 4) x equals: 3.
  2144. self assert: (Point x: 3 y: 4) y equals: 4.
  2145. self assert: (Point new x: 3) x equals: 3.
  2146. self assert: (Point new y: 4) y equals: 4
  2147. !
  2148. testAngle
  2149. self assert: (-1@0) angle equals: Number pi
  2150. !
  2151. testArithmetic
  2152. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  2153. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  2154. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  2155. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  2156. !
  2157. testAt
  2158. self assert: 3@4 equals: (Point x: 3 y: 4)
  2159. !
  2160. testComparison
  2161. self assert: 3@4 < (4@5).
  2162. self deny: 3@4 < (4@4).
  2163. self assert: 4@5 <= (4@5).
  2164. self deny: 4@5 <= (3@5).
  2165. self assert: 5@6 > (4@5).
  2166. self deny: 5@6 > (6@6).
  2167. self assert: 4@5 >= (4@5).
  2168. self deny: 4@5 >= (5@5)
  2169. !
  2170. testDotProduct
  2171. self assert: (2@3 dotProduct: 3@7) equals: 27
  2172. !
  2173. testEgality
  2174. self assert: (3@4 = (3@4)).
  2175. self deny: 3@5 = (3@6)
  2176. !
  2177. testNew
  2178. self assert: (Point new x: 3) y equals: nil.
  2179. self deny: (Point new x: 3) x = 0.
  2180. self assert: (Point new y: 4) x equals: nil.
  2181. self deny: (Point new y: 4) y = 0
  2182. !
  2183. testNormal
  2184. self assert: (1@0) normal equals: 0@1
  2185. !
  2186. testNormalized
  2187. self assert: (0@2) normalized equals: 0@1.
  2188. self assert: (0@0) normalized equals: 0@0.
  2189. !
  2190. testPolarCoordinates
  2191. self assert: (1@0) r equals: 1.
  2192. self assert: (0@0) r equals: 0.
  2193. !
  2194. testRectangleCreation
  2195. self assert: (1@1 corner: 2@2) equals: (Rectangle origin: 1@1 corner: 2@2).
  2196. self assert: (1@1 rectangle: 2@2) equals: (Rectangle point: 1@1 point: 2@2).
  2197. self assert: (1@1 extent: 2@2) equals: (Rectangle origin: 1@1 extent: 2@2)
  2198. !
  2199. testTranslateBy
  2200. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  2201. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  2202. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  2203. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  2204. ! !
  2205. TestCase subclass: #QueueTest
  2206. slots: {}
  2207. package: 'Kernel-Tests'!
  2208. !QueueTest methodsFor: 'tests'!
  2209. testNextIfAbsent
  2210. | queue |
  2211. queue := Queue new.
  2212. queue nextPut: 'index1'.
  2213. self assert: (queue nextIfAbsent: 'empty') = 'index1'.
  2214. self deny: (queue nextIfAbsent: 'empty') = 'index1'
  2215. !
  2216. testQueueNext
  2217. | queue |
  2218. queue := Queue new.
  2219. queue
  2220. nextPut: 'index1';
  2221. nextPut: 'index2'.
  2222. self assert: queue next = 'index1'.
  2223. self deny: queue next = 'index'.
  2224. self should: [ queue next ] raise: Error
  2225. ! !
  2226. TestCase subclass: #RandomTest
  2227. slots: {}
  2228. package: 'Kernel-Tests'!
  2229. !RandomTest methodsFor: 'tests'!
  2230. testAtRandomNumber
  2231. |val|
  2232. 100 timesRepeat: [
  2233. val := 10 atRandom.
  2234. self assert: (val > 0).
  2235. self assert: (val <11)
  2236. ]
  2237. !
  2238. testAtRandomSequenceableCollection
  2239. |val|
  2240. 100 timesRepeat: [
  2241. val := 'abc' atRandom.
  2242. self assert: ((val = 'a') | (val = 'b') | (val = 'c' )).
  2243. ].
  2244. !
  2245. textNext
  2246. 10000 timesRepeat: [
  2247. | current next |
  2248. next := Random new next.
  2249. self assert: (next >= 0).
  2250. self assert: (next < 1).
  2251. self deny: current = next.
  2252. next = current ]
  2253. ! !
  2254. TestCase subclass: #RectangleTest
  2255. slots: {}
  2256. package: 'Kernel-Tests'!
  2257. !RectangleTest methodsFor: 'tests'!
  2258. testContainsPoint
  2259. | rect |
  2260. rect := Rectangle origin: 0@0 corner: 4@4.
  2261. self assert: (rect containsPoint: 1@2).
  2262. self assert: (rect containsPoint: 5@4) not.
  2263. !
  2264. testContainsRect
  2265. self assert: ((Rectangle origin: 0@0 corner: 6@6) containsRect: (Rectangle origin: 1@1 corner: 5@5)).
  2266. self assert: ((Rectangle origin: 0@0 corner: 6@6) containsRect: (Rectangle origin: 1@(-1) corner: 5@5)) not.
  2267. !
  2268. testOriginExtent
  2269. | rectangle |
  2270. rectangle := Rectangle origin: 3@4 extent: 7@8.
  2271. self assert: rectangle origin equals: 3@4.
  2272. self assert: rectangle corner equals: 10@12.
  2273. ! !
  2274. TestCase subclass: #StreamTest
  2275. slots: {}
  2276. package: 'Kernel-Tests'!
  2277. !StreamTest methodsFor: 'accessing'!
  2278. collectionClass
  2279. ^ self class collectionClass
  2280. !
  2281. newCollection
  2282. ^ self collectionClass new
  2283. !
  2284. newStream
  2285. ^ self collectionClass new stream
  2286. ! !
  2287. !StreamTest methodsFor: 'tests'!
  2288. testAtStartAtEnd
  2289. | stream |
  2290. stream := self newStream.
  2291. self assert: stream atStart.
  2292. self assert: stream atEnd.
  2293. stream nextPutAll: self newCollection.
  2294. self assert: stream atEnd.
  2295. self deny: stream atStart.
  2296. stream position: 1.
  2297. self deny: stream atEnd.
  2298. self deny: stream atStart
  2299. !
  2300. testContents
  2301. | stream |
  2302. stream := self newStream.
  2303. stream nextPutAll: self newCollection.
  2304. self assert: stream contents equals: self newCollection
  2305. !
  2306. testIsEmpty
  2307. | stream |
  2308. stream := self newStream.
  2309. self assert: stream isEmpty.
  2310. stream nextPutAll: self newCollection.
  2311. self deny: stream isEmpty
  2312. !
  2313. testPosition
  2314. | collection stream |
  2315. collection := self newCollection.
  2316. stream := self newStream.
  2317. stream nextPutAll: collection.
  2318. self assert: stream position equals: collection size.
  2319. stream position: 0.
  2320. self assert: stream position equals: 0.
  2321. stream next.
  2322. self assert: stream position equals: 1.
  2323. stream next.
  2324. self assert: stream position equals: 2
  2325. !
  2326. testReading
  2327. | stream collection |
  2328. collection := self newCollection.
  2329. stream := self newStream.
  2330. stream
  2331. nextPutAll: collection;
  2332. position: 0.
  2333. collection do: [ :each |
  2334. self assert: stream next equals: each ].
  2335. self assert: stream next isNil
  2336. !
  2337. testStreamContents
  2338. !
  2339. testWrite
  2340. | stream collection |
  2341. collection := self newCollection.
  2342. stream := self newStream.
  2343. collection do: [ :each | stream << each ].
  2344. self assert: stream contents equals: collection
  2345. !
  2346. testWriting
  2347. | stream collection |
  2348. collection := self newCollection.
  2349. stream := self newStream.
  2350. collection do: [ :each | stream nextPut: each ].
  2351. self assert: stream contents equals: collection.
  2352. stream := self newStream.
  2353. stream nextPutAll: collection.
  2354. self assert: stream contents equals: collection
  2355. ! !
  2356. !StreamTest class methodsFor: 'accessing'!
  2357. collectionClass
  2358. ^ nil
  2359. ! !
  2360. !StreamTest class methodsFor: 'testing'!
  2361. isAbstract
  2362. ^ self collectionClass isNil
  2363. ! !
  2364. StreamTest subclass: #ArrayStreamTest
  2365. slots: {}
  2366. package: 'Kernel-Tests'!
  2367. !ArrayStreamTest methodsFor: 'accessing'!
  2368. newCollection
  2369. ^ { true. 1. 3@4. 'foo' }
  2370. ! !
  2371. !ArrayStreamTest class methodsFor: 'accessing'!
  2372. collectionClass
  2373. ^ Array
  2374. ! !
  2375. StreamTest subclass: #StringStreamTest
  2376. slots: {}
  2377. package: 'Kernel-Tests'!
  2378. !StringStreamTest methodsFor: 'accessing'!
  2379. newCollection
  2380. ^ 'hello world'
  2381. ! !
  2382. !StringStreamTest class methodsFor: 'accessing'!
  2383. collectionClass
  2384. ^ String
  2385. ! !
  2386. Trait named: #TClassBuildingTest
  2387. package: 'Kernel-Tests'!
  2388. !TClassBuildingTest methodsFor: 'accessing'!
  2389. theClass
  2390. self subclassResponsibility
  2391. ! !
  2392. !TClassBuildingTest methodsFor: 'private'!
  2393. is: anObject javaScriptInstanceOf: aJavaScriptClass
  2394. <inlineJS: 'return anObject instanceof aJavaScriptClass'>
  2395. ! !
  2396. !TClassBuildingTest methodsFor: 'running'!
  2397. assert: aClass isClassCopyOf: anotherClass
  2398. self assert: aClass superclass == anotherClass superclass.
  2399. self assert: aClass instanceVariableNames == anotherClass instanceVariableNames.
  2400. self assert: aClass package == anotherClass package.
  2401. self assert: (aClass package classes includes: aClass).
  2402. self assert: aClass methodDictionary keys equals: anotherClass methodDictionary keys
  2403. !
  2404. assert: anObject isJavaScriptInstanceOf: aJavaScriptClass
  2405. self assert: (self is: anObject javaScriptInstanceOf: aJavaScriptClass)
  2406. !
  2407. tearDown
  2408. self theClass ifNotNil: [ :theClass |
  2409. (Array streamContents: [ :s | theClass allSubclassesDo: [ :each | s nextPut: each ] ])
  2410. reverseDo: [ :each | Smalltalk removeClass: each ].
  2411. Smalltalk removeClass: theClass ]
  2412. ! !
  2413. Trait named: #TKeyValueCollectionTest
  2414. package: 'Kernel-Tests'!
  2415. !TKeyValueCollectionTest methodsFor: 'fixture'!
  2416. nonIndexesDo: aBlock
  2417. "Executes block a few times,
  2418. each time passing value that is known
  2419. not to be an index, as the first parameter"
  2420. self subclassResponsibility
  2421. !
  2422. sampleNewIndex
  2423. "Answers a value that can be used as index in at:put: or at:ifAbsentPut:"
  2424. self subclassResponsibility
  2425. !
  2426. samplesDo: aBlock
  2427. "Executes block a few times,
  2428. each time passing known index and value stored
  2429. under that index as the parameters"
  2430. self subclassResponsibility
  2431. ! !
  2432. !TKeyValueCollectionTest methodsFor: 'tests'!
  2433. testAt
  2434. self nonIndexesDo: [ :each |
  2435. self should: [ self collection at: each ] raise: Error ].
  2436. self samplesDo: [ :index :value |
  2437. self assert: (self collection at: index) equals: value ]
  2438. !
  2439. testAtIfAbsent
  2440. self nonIndexesDo: [ :each |
  2441. self assert: (self collection at: each ifAbsent: [ self sampleNewValue ]) equals: self sampleNewValue ].
  2442. self samplesDo: [ :index :value |
  2443. self assert: (self collection at: index ifAbsent: [ self sampleNewValue ]) equals: value ].
  2444. !
  2445. testAtIfAbsentPut
  2446. | newCollection |
  2447. newCollection := self collection.
  2448. self samplesDo: [ :index :value |
  2449. self assert: (newCollection at: index ifAbsentPut: [ self sampleNewValue ]) equals: value ].
  2450. self assert: newCollection equals: self collection.
  2451. self assert: (newCollection at: self sampleNewIndex ifAbsentPut: [ self sampleNewValue ]) equals: self sampleNewValue.
  2452. self assert: newCollection equals: self collectionWithNewValue
  2453. !
  2454. testAtIfPresent
  2455. | visited sentinel |
  2456. sentinel := Object new.
  2457. self nonIndexesDo: [ :each |
  2458. visited := nil.
  2459. self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ]) equals: nil.
  2460. self assert: visited isNil ].
  2461. self samplesDo: [ :index :value |
  2462. visited := nil.
  2463. self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ]) equals: sentinel.
  2464. self assert: visited equals: (self collection at: index) ]
  2465. !
  2466. testAtIfPresentIfAbsent
  2467. | visited sentinel |
  2468. sentinel := Object new.
  2469. self nonIndexesDo: [ :each |
  2470. visited := nil.
  2471. self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ] ifAbsent: [ self sampleNewValue ] ) equals: self sampleNewValue.
  2472. self assert: visited isNil ].
  2473. self samplesDo: [ :index :value |
  2474. visited := nil.
  2475. self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ] ifAbsent: [ self sampleNewValue ]) equals: sentinel.
  2476. self assert: visited equals: (self collection at: index) ]
  2477. !
  2478. testAtPut
  2479. | newCollection |
  2480. newCollection := self collection.
  2481. self samplesDo: [ :index :value |
  2482. newCollection at: index put: value ].
  2483. self assert: newCollection equals: self collection.
  2484. newCollection at: self sampleNewIndex put: self sampleNewValue.
  2485. self assert: newCollection equals: self collectionWithNewValue
  2486. !
  2487. testIndexOf
  2488. self should: [ self collection indexOf: self sampleNewValue ] raise: Error.
  2489. self samplesDo: [ :index :value |
  2490. self assert: (self collection indexOf: value) equals: index ]
  2491. !
  2492. testIndexOfWithNull
  2493. | jsNull |
  2494. jsNull := JSON parse: 'null'.
  2495. self samplesDo: [ :index :value |
  2496. self assert: (self collection at: index put: jsNull; indexOf: jsNull) equals: index ]
  2497. !
  2498. testWithIndexDo
  2499. | collection |
  2500. collection := self collection.
  2501. self collection withIndexDo: [ :each :index |
  2502. self assert: (collection at: index) equals: each ]
  2503. ! !
  2504. TestCase subclass: #UndefinedTest
  2505. slots: {}
  2506. package: 'Kernel-Tests'!
  2507. !UndefinedTest methodsFor: 'tests'!
  2508. testCopying
  2509. self assert: nil copy equals: nil
  2510. !
  2511. testDeepCopy
  2512. self assert: nil deepCopy = nil
  2513. !
  2514. testIfNil
  2515. self assert: (nil ifNil: [ true ]) equals: true.
  2516. self deny: (nil ifNotNil: [ true ]) = true.
  2517. self assert: (nil ifNil: [ true ] ifNotNil: [ false ]) equals: true.
  2518. self deny: (nil ifNotNil: [ true ] ifNil: [ false ]) = true
  2519. !
  2520. testIsNil
  2521. self assert: nil isNil.
  2522. self deny: nil notNil.
  2523. ! !
  2524. ClassBuilderTest setTraitComposition: {TClassBuildingTest @ {#tearDownTheClass -> #tearDown}} asTraitComposition!
  2525. ClassTest setTraitComposition: {TClassBuildingTest} asTraitComposition!
  2526. AssociativeCollectionTest setTraitComposition: {TKeyValueCollectionTest} asTraitComposition!
  2527. SequenceableCollectionTest setTraitComposition: {TKeyValueCollectionTest} asTraitComposition!
  2528. ! !