Kernel-Tests.st 71 KB

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