Kernel-Tests.st 77 KB

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