Kernel-Tests.st 69 KB

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