Kernel-Tests.st 79 KB

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