Kernel-Tests.st 78 KB

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