Kernel-Tests.st 84 KB

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