Kernel-Tests.st 68 KB

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