Kernel-Tests.st 68 KB

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