Kernel-Tests.st 71 KB

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