1
0

Kernel-Tests.st 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. Smalltalk current createPackage: 'Kernel-Tests'!
  2. TestCase subclass: #AnnouncementSubscriptionTest
  3. instanceVariableNames: ''
  4. package: 'Kernel-Tests'!
  5. !AnnouncementSubscriptionTest methodsFor: 'tests'!
  6. testHandlesAnnouncement
  7. | subscription announcementClass1 announcementClass2 classBuilder |
  8. classBuilder := ClassBuilder new.
  9. announcementClass1 := classBuilder basicAddSubclassOf: SystemAnnouncement named: 'TestAnnouncement1' instanceVariableNames: #() package: 'Kernel-Tests'.
  10. subscription := AnnouncementSubscription new announcementClass: SystemAnnouncement.
  11. "Test whether the same class triggers the announcement"
  12. self assert: (subscription handlesAnnouncement: SystemAnnouncement) equals: true.
  13. "Test whether a subclass triggers the announcement"
  14. self assert: (subscription handlesAnnouncement: announcementClass1) equals: true.
  15. "Test whether an unrelated class does not trigger the announcement"
  16. self assert: (subscription handlesAnnouncement: Object) equals: false.
  17. classBuilder basicRemoveClass: announcementClass1.
  18. ! !
  19. TestCase subclass: #AnnouncerTest
  20. instanceVariableNames: ''
  21. package: 'Kernel-Tests'!
  22. !AnnouncerTest methodsFor: 'not yet classified'!
  23. testOnDo
  24. | counter announcer |
  25. counter := 0.
  26. announcer := Announcer new.
  27. announcer on: SystemAnnouncement do: [ counter := counter + 1 ].
  28. announcer announce: (SystemAnnouncement new).
  29. self assert: counter equals: 1.
  30. announcer announce: (SystemAnnouncement new).
  31. self assert: counter equals: 2.
  32. !
  33. testOnDoOnce
  34. | counter announcer |
  35. counter := 0.
  36. announcer := Announcer new.
  37. announcer on: SystemAnnouncement doOnce: [ counter := counter + 1 ].
  38. announcer announce: (SystemAnnouncement new).
  39. self assert: counter equals: 1.
  40. announcer announce: (SystemAnnouncement new).
  41. self assert: counter equals: 1.
  42. ! !
  43. TestCase subclass: #BlockClosureTest
  44. instanceVariableNames: ''
  45. package: 'Kernel-Tests'!
  46. !BlockClosureTest methodsFor: 'tests'!
  47. testCanClearInterval
  48. self shouldnt: [ ([ Error new signal ] valueWithInterval: 0) clearInterval ] raise: Error
  49. !
  50. testCanClearTimeout
  51. self shouldnt: [ ([ Error new signal ] valueWithTimeout: 0) clearTimeout ] raise: Error
  52. !
  53. testCompiledSource
  54. self assert: ([ 1+1 ] compiledSource includesSubString: 'function')
  55. !
  56. testCurrySelf
  57. | curriedMethod array |
  58. curriedMethod := [ :selfarg :x | selfarg at: x ] currySelf asCompiledMethod: 'foo:'.
  59. array := #(3 1 4).
  60. ClassBuilder new installMethod: curriedMethod forClass: Array category: '**test helper'.
  61. [ self assert: (array foo: 2) equals: 1 ]
  62. ensure: [ Array removeCompiledMethod: curriedMethod ]
  63. !
  64. testEnsure
  65. self assert: ([ 3 ] ensure: [ 4 ]) equals: 3
  66. !
  67. testEnsureRaises
  68. self should: [ [Error new signal ] ensure: [ true ]] raise: Error
  69. !
  70. testExceptionSemantics
  71. "See https://github.com/NicolasPetton/amber/issues/314"
  72. self timeout: 100.
  73. (self async: [
  74. [
  75. self assert: true.
  76. Error signal.
  77. "The following should *not* be run"
  78. self deny: true.
  79. self finished.
  80. ] on: Error do: [ :ex | self finished ]
  81. ]) valueWithTimeout: 0
  82. !
  83. testNewWithValues
  84. <
  85. function theTestPrototype() {this.name = "theTestPrototype";}
  86. function theTestConstructor(arg1, arg2, arg3) {}
  87. theTestConstructor.prototype = new theTestPrototype;
  88. var theWrappedConstructor = _st(theTestConstructor);
  89. var theResult = theWrappedConstructor._newWithValues_([1, 2, 3 ]);
  90. self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
  91. "newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
  92. function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
  93. self._should_raise_(_st(constructionShouldFail), smalltalk.Error);
  94. >
  95. !
  96. testNumArgs
  97. self assert: [] numArgs equals: 0.
  98. self assert: [ :a :b | ] numArgs equals: 2
  99. !
  100. testOnDo
  101. self assert: ([ Error new signal ] on: Error do: [ :ex | true ])
  102. !
  103. testValue
  104. self assert: ([ 1+1 ] value) equals: 2.
  105. self assert: ([ :x | x +1 ] value: 2) equals: 3.
  106. self assert: ([ :x :y | x*y ] value: 2 value: 4) equals: 8.
  107. "Arguments are optional in Amber. This isn't ANSI compliant."
  108. self assert: ([ :a :b :c | 1 ] value) equals: 1
  109. !
  110. testValueWithPossibleArguments
  111. self assert: ([ 1 ] valueWithPossibleArguments: #(3 4)) equals: 1.
  112. self assert: ([ :a | a + 4 ] valueWithPossibleArguments: #(3 4)) equals: 7.
  113. self assert: ([ :a :b | a + b ] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  114. !
  115. testWhileFalse
  116. | i |
  117. i := 0.
  118. [ i > 5 ] whileFalse: [ i := i + 1 ].
  119. self assert: i equals: 6.
  120. i := 0.
  121. [ i := i + 1. i > 5 ] whileFalse.
  122. self assert: i equals: 6
  123. !
  124. testWhileTrue
  125. | i |
  126. i := 0.
  127. [ i < 5 ] whileTrue: [ i := i + 1 ].
  128. self assert: i equals: 5.
  129. i := 0.
  130. [ i := i + 1. i < 5 ] whileTrue.
  131. self assert: i equals: 5
  132. ! !
  133. TestCase subclass: #BooleanTest
  134. instanceVariableNames: ''
  135. package: 'Kernel-Tests'!
  136. !BooleanTest methodsFor: 'tests'!
  137. testEquality
  138. "We're on top of JS...just be sure to check the basics!!"
  139. self deny: 0 = false.
  140. self deny: false = 0.
  141. self deny: '' = false.
  142. self deny: false = ''.
  143. self assert: (true = true).
  144. self deny: false = true.
  145. self deny: true = false.
  146. self assert: (false = false).
  147. "JS may do some type coercing after sending a message"
  148. self assert: (true yourself = true).
  149. self assert: (true yourself = true yourself)
  150. !
  151. testIdentity
  152. "We're on top of JS...just be sure to check the basics!!"
  153. self deny: 0 == false.
  154. self deny: false == 0.
  155. self deny: '' == false.
  156. self deny: false == ''.
  157. self assert: true == true.
  158. self deny: false == true.
  159. self deny: true == false.
  160. self assert: false == false.
  161. "JS may do some type coercing after sending a message"
  162. self assert: true yourself == true.
  163. self assert: true yourself == true yourself
  164. !
  165. testIfTrueIfFalse
  166. self assert: (true ifTrue: [ 'alternative block' ]) equals: 'alternative block'.
  167. self assert: (true ifFalse: [ 'alternative block' ]) equals: nil.
  168. self assert: (false ifTrue: [ 'alternative block' ]) equals: nil.
  169. self assert: (false ifFalse: [ 'alternative block' ]) equals: 'alternative block'.
  170. self assert: (false ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block2'.
  171. self assert: (false ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block'.
  172. self assert: (true ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block'.
  173. self assert: (true ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block2'.
  174. !
  175. testIfTrueIfFalseWithBoxing
  176. self assert: (true yourself ifTrue: [ 'alternative block' ]) equals: 'alternative block'.
  177. self assert: (true yourself ifFalse: [ 'alternative block' ]) equals: nil.
  178. self assert: (false yourself ifTrue: [ 'alternative block' ]) equals: nil.
  179. self assert: (false yourself ifFalse: [ 'alternative block' ]) equals: 'alternative block'.
  180. self assert: (false yourself ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block2'.
  181. self assert: (false yourself ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block'.
  182. self assert: (true yourself ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block'.
  183. self assert: (true yourself ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block2'.
  184. !
  185. testLogic
  186. "Trivial logic table"
  187. self assert: (true & true);
  188. deny: (true & false);
  189. deny: (false & true);
  190. deny: (false & false).
  191. self assert: (true | true);
  192. assert: (true | false);
  193. assert: (false | true);
  194. deny: (false | false).
  195. "Checking that expressions work fine too"
  196. self assert: (true & (1 > 0));
  197. deny: ((1 > 0) & false);
  198. deny: ((1 > 0) & (1 > 2)).
  199. self assert: (false | (1 > 0));
  200. assert: ((1 > 0) | false);
  201. assert: ((1 > 0) | (1 > 2))
  202. !
  203. testLogicKeywords
  204. "Trivial logic table"
  205. self
  206. assert: (true and: [ true ]);
  207. deny: (true and: [ false ]);
  208. deny: (false and: [ true ]);
  209. deny: (false and: [ false ]).
  210. self
  211. assert: (true or: [ true ]);
  212. assert: (true or: [ false ]);
  213. assert: (false or: [ true ]);
  214. deny: (false or: [ false ]).
  215. "Checking that expressions work fine too"
  216. self
  217. assert: (true and: [ 1 > 0 ]);
  218. deny: ((1 > 0) and: [ false ]);
  219. deny: ((1 > 0) and: [ 1 > 2 ]).
  220. self
  221. assert: (false or: [ 1 > 0 ]);
  222. assert: ((1 > 0) or: [ false ]);
  223. assert: ((1 > 0) or: [ 1 > 2 ])
  224. !
  225. testNonBooleanError
  226. self should: [ '' ifTrue: [] ifFalse: [] ] raise: NonBooleanReceiver
  227. ! !
  228. TestCase subclass: #ClassBuilderTest
  229. instanceVariableNames: 'builder theClass'
  230. package: 'Kernel-Tests'!
  231. !ClassBuilderTest methodsFor: 'running'!
  232. setUp
  233. builder := ClassBuilder new
  234. !
  235. tearDown
  236. theClass ifNotNil: [ Smalltalk current removeClass: theClass. theClass := nil ]
  237. ! !
  238. !ClassBuilderTest methodsFor: 'tests'!
  239. testClassCopy
  240. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  241. self assert: theClass superclass == ObjectMock superclass.
  242. self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
  243. self assert: theClass name equals: 'ObjectMock2'.
  244. self assert: theClass package == ObjectMock package.
  245. self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
  246. !
  247. testClassMigration
  248. | instance oldClass |
  249. oldClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  250. instance := (Smalltalk current at: 'ObjectMock2') new.
  251. "Change the superclass of ObjectMock2"
  252. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  253. instanceVariableNames: ''
  254. package: 'Kernel-Tests'.
  255. self deny: oldClass == ObjectMock2.
  256. self assert: ObjectMock2 superclass == ObjectMock.
  257. self assert: ObjectMock2 instanceVariableNames isEmpty.
  258. self assert: ObjectMock2 selectors equals: oldClass selectors.
  259. self assert: ObjectMock2 comment equals: oldClass comment.
  260. self assert: ObjectMock2 package name equals: 'Kernel-Tests'.
  261. self deny: instance class == ObjectMock2.
  262. "Commeting this out. Tests implementation detail."
  263. "self assert: instance class name equals: 'OldObjectMock2'."
  264. self assert: (Smalltalk current at: instance class name) isNil.
  265. Smalltalk current removeClass: ObjectMock2
  266. !
  267. testClassMigrationWithClassInstanceVariables
  268. builder copyClass: ObjectMock named: 'ObjectMock2'.
  269. ObjectMock2 class instanceVariableNames: 'foo bar'.
  270. "Change the superclass of ObjectMock2"
  271. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  272. instanceVariableNames: ''
  273. package: 'Kernel-Tests'.
  274. self assert: ObjectMock2 class instanceVariableNames equals: #('foo' 'bar').
  275. Smalltalk current removeClass: ObjectMock2
  276. !
  277. testClassMigrationWithSubclasses
  278. builder copyClass: ObjectMock named: 'ObjectMock2'.
  279. ObjectMock2 subclass: 'ObjectMock3' instanceVariableNames: '' package: 'Kernel-Tests'.
  280. ObjectMock3 subclass: 'ObjectMock4' instanceVariableNames: '' package: 'Kernel-Tests'.
  281. "Change the superclass of ObjectMock2"
  282. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  283. instanceVariableNames: ''
  284. package: 'Kernel-Tests'.
  285. self assert: (ObjectMock subclasses includes: ObjectMock2).
  286. self assert: (ObjectMock2 subclasses includes: ObjectMock3).
  287. self assert: (ObjectMock3 subclasses includes: ObjectMock4).
  288. ObjectMock allSubclasses do: [ :each | Smalltalk current removeClass: each ]
  289. !
  290. testInstanceVariableNames
  291. self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
  292. ! !
  293. TestCase subclass: #CollectionTest
  294. instanceVariableNames: ''
  295. package: 'Kernel-Tests'!
  296. !CollectionTest methodsFor: 'accessing'!
  297. collection
  298. ^ self collectionClass withAll: self defaultValues
  299. !
  300. collectionClass
  301. ^ self class collectionClass
  302. !
  303. collectionWithDuplicates
  304. ^ self collectionClass withAll: #('a' 'b' 'c' 1 2 1 'a')
  305. !
  306. defaultValues
  307. ^ #(1 2 3 -4)
  308. ! !
  309. !CollectionTest methodsFor: 'convenience'!
  310. assertSameContents: aCollection as: anotherCollection
  311. self assert: (aCollection size = anotherCollection size).
  312. aCollection do: [ :each |
  313. self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
  314. ! !
  315. !CollectionTest methodsFor: 'testing'!
  316. isCollectionReadOnly
  317. ^ false
  318. ! !
  319. !CollectionTest methodsFor: 'tests'!
  320. testAsArray
  321. self
  322. assertSameContents: self collection
  323. as: self collection asArray
  324. !
  325. testAsOrderedCollection
  326. self
  327. assertSameContents: self collection
  328. as: self collection asOrderedCollection
  329. !
  330. testAsSet
  331. | c set |
  332. c := self collectionWithDuplicates.
  333. set := c asSet.
  334. self assert: set size equals: 5.
  335. c do: [ :each |
  336. self assert: (set includes: each) ]
  337. !
  338. testCollect
  339. | newCollection |
  340. newCollection := #(1 2 3 4).
  341. self
  342. assertSameContents: (self collection collect: [ :each |
  343. each abs ])
  344. as: newCollection
  345. !
  346. testDetect
  347. self assert: (self collection detect: [ :each | each < 0 ]) equals: -4.
  348. self
  349. should: [ self collection detect: [ :each | each = 6 ] ]
  350. raise: Error
  351. !
  352. testDo
  353. | newCollection |
  354. newCollection := OrderedCollection new.
  355. self collection do: [ :each |
  356. newCollection add: each ].
  357. self
  358. assertSameContents: self collection
  359. as: newCollection
  360. !
  361. testIsEmpty
  362. self assert: self collectionClass new isEmpty.
  363. self deny: self collection isEmpty
  364. !
  365. testRemoveAll
  366. self assert: (self collection removeAll; yourself) equals: self collectionClass new
  367. !
  368. testSelect
  369. | newCollection |
  370. newCollection := #(2 -4).
  371. self
  372. assertSameContents: (self collection select: [ :each |
  373. each even ])
  374. as: newCollection
  375. !
  376. testSize
  377. self assert: self collectionClass new size equals: 0.
  378. self assert: self collection size equals: 4
  379. ! !
  380. !CollectionTest class methodsFor: 'accessing'!
  381. collectionClass
  382. ^ nil
  383. ! !
  384. !CollectionTest class methodsFor: 'testing'!
  385. isAbstract
  386. ^ self collectionClass isNil
  387. ! !
  388. CollectionTest subclass: #IndexableCollectionTest
  389. instanceVariableNames: ''
  390. package: 'Kernel-Tests'!
  391. !IndexableCollectionTest methodsFor: 'tests'!
  392. testAt
  393. self assert: (self collection at: 4) equals: -4.
  394. self should: [ self collection at: 5 ] raise: Error
  395. !
  396. testAtIfAbsent
  397. self assert: (self collection at: (self collection size + 1) ifAbsent: [ 'none' ]) equals: 'none'
  398. !
  399. testContains
  400. | collection |
  401. collection := self collection.
  402. self assert: (self collection contains: [ :each | each = self collection first ]).
  403. self deny: (self collection contains: [ :each | each = Object new ])
  404. !
  405. testIndexOf
  406. self assert: (self collection indexOf: 2) equals: 2.
  407. self should: [ self collection indexOf: 999 ] raise: Error.
  408. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  409. !
  410. testWithIndexDo
  411. | collection |
  412. collection := self collection.
  413. self collection withIndexDo: [ :each :index |
  414. self assert: (collection at: index) equals: each ]
  415. ! !
  416. IndexableCollectionTest subclass: #HashedCollectionTest
  417. instanceVariableNames: ''
  418. package: 'Kernel-Tests'!
  419. !HashedCollectionTest methodsFor: 'accessing'!
  420. collection
  421. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  422. !
  423. collectionWithDuplicates
  424. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
  425. ! !
  426. !HashedCollectionTest methodsFor: 'tests'!
  427. testAsDictionary
  428. self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
  429. !
  430. testAt
  431. self assert: (self collection at: 'a') equals: 2.
  432. self should: [ self collection at: 5 ] raise: Error
  433. !
  434. testContains
  435. | collection |
  436. collection := self collection.
  437. self assert: (self collection contains: [ :each | each = self collection values first ]).
  438. self deny: (self collection contains: [ :each | each = Object new ])
  439. !
  440. testFrom
  441. "Accept a collection of associations."
  442. | associations |
  443. associations := { 'a' -> 1. 'b' -> 2 }.
  444. self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  445. !
  446. testIndexOf
  447. self assert: (self collection indexOf: 2) equals: 'a'.
  448. self should: [ self collection indexOf: 999 ] raise: Error.
  449. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  450. !
  451. testNewFromPairs
  452. "Accept an array in which all odd indexes are keys and evens are values."
  453. | flattenedAssociations |
  454. flattenedAssociations := { 'a'. 1. 'b'. 2 }.
  455. self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  456. ! !
  457. !HashedCollectionTest class methodsFor: 'accessing'!
  458. collectionClass
  459. ^ HashedCollection
  460. ! !
  461. HashedCollectionTest subclass: #DictionaryTest
  462. instanceVariableNames: ''
  463. package: 'Kernel-Tests'!
  464. !DictionaryTest methodsFor: 'accessing'!
  465. collection
  466. ^ Dictionary new
  467. at: 1 put: 1;
  468. at: 'a' put: 2;
  469. at: true put: 3;
  470. at: 4 put: -4;
  471. yourself
  472. !
  473. collectionWithDuplicates
  474. ^ Dictionary new
  475. at: 1 put: 1;
  476. at: 'a' put: 2;
  477. at: true put: 3;
  478. at: 4 put: -4;
  479. at: 'b' put: 1;
  480. at: 3 put: 3;
  481. at: false put: 12;
  482. yourself
  483. ! !
  484. !DictionaryTest methodsFor: 'tests'!
  485. testAccessing
  486. | d |
  487. d := Dictionary new.
  488. d at: 'hello' put: 'world'.
  489. self assert: (d at: 'hello') equals: 'world'.
  490. self assert: (d at: 'hello' ifAbsent: [ nil ]) equals: 'world'.
  491. self deny: (d at: 'foo' ifAbsent: [ nil ]) = 'world'.
  492. self assert: (d includesKey: 'hello').
  493. self deny: (d includesKey: 'foo').
  494. d at: 1 put: 2.
  495. self assert: (d at: 1) equals: 2.
  496. d at: 1@3 put: 3.
  497. self assert: (d at: 1@3) equals: 3.
  498. self assert: (d includesKey: 1@3).
  499. self deny: (d includesKey: 3@1)
  500. !
  501. testAsHashedCollection
  502. self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
  503. !
  504. testDynamicDictionaries
  505. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  506. !
  507. testEquality
  508. | d1 d2 |
  509. self assert: (Dictionary new = Dictionary new).
  510. d1 := Dictionary new at: 1 put: 2; yourself.
  511. d2 := Dictionary new at: 1 put: 2; yourself.
  512. self assert: (d1 = d2).
  513. d2 := Dictionary new at: 1 put: 3; yourself.
  514. self deny: d1 = d2.
  515. d2 := Dictionary new at: 2 put: 2; yourself.
  516. self deny: d1 = d2.
  517. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  518. self deny: d1 = d2.
  519. !
  520. testIfAbsent
  521. | d visited |
  522. visited := false.
  523. d := Dictionary new.
  524. d at: 'hello' ifAbsent: [ visited := true ].
  525. self assert: visited.
  526. !
  527. testIfPresent
  528. | d visited absent |
  529. visited := false.
  530. d := Dictionary new.
  531. d at: 'hello' put: 'world'.
  532. d at: 'hello' ifPresent: [ :value | visited := value ].
  533. self assert: visited equals: 'world'.
  534. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  535. self assert: absent isNil.
  536. !
  537. testIfPresentIfAbsent
  538. | d visited |
  539. visited := false.
  540. d := Dictionary new.
  541. d at: 'hello' put: 'world'.
  542. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  543. self assert: visited equals: 'world'.
  544. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  545. self assert: visited.
  546. !
  547. testKeys
  548. | d |
  549. d := Dictionary new.
  550. d at: 1 put: 2.
  551. d at: 2 put: 3.
  552. d at: 3 put: 4.
  553. self assert: d keys equals: #(1 2 3)
  554. !
  555. testPointKey
  556. | d |
  557. d := Dictionary new.
  558. d at: 1@1 put: 'foo'.
  559. self assert: (d at: 1@1) equals: 'foo'.
  560. d at: 1@1 put: 'bar'.
  561. self assert: (d at: 1@1) equals: 'bar'.
  562. d removeKey: 1@1.
  563. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  564. self deny: (d includesKey: 1@1)
  565. !
  566. testPrintString
  567. self
  568. assert: (Dictionary new
  569. at:'firstname' put: 'James';
  570. at:'lastname' put: 'Bond';
  571. printString)
  572. equals: 'a Dictionary (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  573. !
  574. testRemoveKey
  575. | d key |
  576. d := Dictionary new.
  577. d at: 1 put: 2.
  578. d at: 2 put: 3.
  579. d at: 3 put: 4.
  580. key := 2.
  581. self assert: d keys equals: #(1 2 3).
  582. d removeKey: key.
  583. self assert: d keys equals: #(1 3).
  584. self assert: d values equals: #(2 4).
  585. self deny: (d includesKey: 2)
  586. !
  587. testRemoveKeyIfAbsent
  588. | d key |
  589. d := Dictionary new.
  590. d at: 1 put: 2.
  591. d at: 2 put: 3.
  592. d at: 3 put: 4.
  593. key := 2.
  594. self assert: (d removeKey: key) equals: 3.
  595. key := 3.
  596. self assert: (d removeKey: key ifAbsent: [ 42 ]) equals: 4.
  597. key := 'why'.
  598. self assert: (d removeKey: key ifAbsent: [ 42 ] ) equals: 42.
  599. !
  600. testSize
  601. | d |
  602. d := Dictionary new.
  603. self assert: d size equals: 0.
  604. d at: 1 put: 2.
  605. self assert: d size equals: 1.
  606. d at: 2 put: 3.
  607. self assert: d size equals: 2.
  608. !
  609. testValues
  610. | d |
  611. d := Dictionary new.
  612. d at: 1 put: 2.
  613. d at: 2 put: 3.
  614. d at: 3 put: 4.
  615. self assert: d values equals: #(2 3 4)
  616. ! !
  617. !DictionaryTest class methodsFor: 'accessing'!
  618. collectionClass
  619. ^ Dictionary
  620. ! !
  621. IndexableCollectionTest subclass: #SequenceableCollectionTest
  622. instanceVariableNames: ''
  623. package: 'Kernel-Tests'!
  624. !SequenceableCollectionTest methodsFor: 'tests'!
  625. testFirst
  626. self assert: (self collection first) equals: (self collection at: 1)
  627. !
  628. testFourth
  629. self assert: (self collection fourth) equals: (self collection at: 4)
  630. !
  631. testLast
  632. self assert: (self collection last) equals: (self collection at: self collection size)
  633. !
  634. testSecond
  635. self assert: (self collection second) equals: (self collection at: 2)
  636. !
  637. testThird
  638. self assert: (self collection third) equals: (self collection at: 3)
  639. ! !
  640. SequenceableCollectionTest subclass: #ArrayTest
  641. instanceVariableNames: ''
  642. package: 'Kernel-Tests'!
  643. !ArrayTest methodsFor: 'tests'!
  644. testAdd
  645. | array |
  646. array := self collection.
  647. array add: 6.
  648. self assert: array last equals: 6
  649. !
  650. testAddFirst
  651. self assert: (self collection addFirst: 0; yourself) first equals: 0
  652. !
  653. testAtIfAbsent
  654. | array |
  655. array := #('hello' 'world').
  656. self assert: (array at: 1) equals: 'hello'.
  657. self assert: (array at: 2) equals: 'world'.
  658. self assert: (array at: 2 ifAbsent: [ 'not found' ]) equals: 'world'.
  659. self assert: (array at: 0 ifAbsent: [ 'not found' ]) equals: 'not found'.
  660. self assert: (array at: -10 ifAbsent: [ 'not found' ]) equals: 'not found'.
  661. self assert: (array at: 3 ifAbsent: [ 'not found' ]) equals: 'not found'.
  662. !
  663. testFirstN
  664. self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
  665. !
  666. testIfEmpty
  667. self assert: ( '' ifEmpty: [ 'zork' ] ) equals: 'zork'
  668. !
  669. testPrintString
  670. | array |
  671. array := Array new.
  672. self assert: array printString equals: 'an Array ()'.
  673. array add: 1; add: 3.
  674. self assert: array printString equals: 'an Array (1 3)'.
  675. array add: 'foo'.
  676. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  677. array remove: 1; remove: 3.
  678. self assert: array printString equals: 'an Array (''foo'')'.
  679. array addLast: 3.
  680. self assert: array printString equals: 'an Array (''foo'' 3)'.
  681. array addLast: 3.
  682. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  683. !
  684. testRemove
  685. | array |
  686. array := #(1 2 3 4 5).
  687. array remove: 3.
  688. self assert: array equals: #(1 2 4 5).
  689. self should: [ array remove: 3 ] raise: Error
  690. !
  691. testRemoveFromTo
  692. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  693. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  694. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  695. !
  696. testRemoveIndex
  697. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  698. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  699. self assert: (#('hello') removeIndex: 1) equals: #()
  700. !
  701. testRemoveLast
  702. | array |
  703. array := #(1 2).
  704. array removeLast.
  705. self assert: array last equals: 1
  706. !
  707. testReversed
  708. |array|
  709. array := #(5 4 3 2 1).
  710. self assert: (array reversed) equals: #(1 2 3 4 5)
  711. !
  712. testSort
  713. | array |
  714. array := #(3 1 4 5 2).
  715. array sort.
  716. self assert: array equals: #(1 2 3 4 5)
  717. ! !
  718. !ArrayTest class methodsFor: 'accessing'!
  719. collectionClass
  720. ^ Array
  721. ! !
  722. SequenceableCollectionTest subclass: #StringTest
  723. instanceVariableNames: ''
  724. package: 'Kernel-Tests'!
  725. !StringTest methodsFor: 'accessing'!
  726. collection
  727. ^ 'hello'
  728. !
  729. collectionWithDuplicates
  730. ^ 'abbaerte'
  731. ! !
  732. !StringTest methodsFor: 'tests'!
  733. testAddRemove
  734. self should: [ 'hello' add: 'a' ] raise: Error.
  735. self should: [ 'hello' remove: 'h' ] raise: Error
  736. !
  737. testAsArray
  738. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  739. !
  740. testAsLowerCase
  741. self assert: 'JACKIE' asLowercase equals: 'jackie'.
  742. !
  743. testAsNumber
  744. self assert: '3' asNumber equals: 3.
  745. self assert: '-3' asNumber equals: -3.
  746. self assert: '-1.5' asNumber equals: -1.5.
  747. !
  748. testAsUpperCase
  749. self assert: 'jackie' asUppercase equals: 'JACKIE'.
  750. !
  751. testAsciiValue
  752. | characterA characterU |
  753. characterA := 'A'.
  754. characterU := 'U'.
  755. self assert: (characterA asciiValue) equals:65.
  756. self assert: (characterU asciiValue) equals:85
  757. !
  758. testAt
  759. self assert: ('hello' at: 1) equals: 'h'.
  760. self assert: ('hello' at: 5) equals: 'o'.
  761. self assert: ('hello' at: 6 ifAbsent: [ nil ]) equals: nil
  762. !
  763. testAtPut
  764. "String instances are read-only"
  765. self should: [ 'hello' at: 1 put: 'a' ] raise: Error
  766. !
  767. testCapitalized
  768. self assert: 'test' capitalized equals: 'Test'.
  769. self assert: 'Test' capitalized equals: 'Test'.
  770. self assert: '' capitalized equals: ''.
  771. self assert: 'Test' isCapitalized equals: true.
  772. self assert: 'test' isCapitalized equals: false.
  773. !
  774. testCharCodeAt
  775. self assert: ('jackie' charCodeAt:1) equals: 106.
  776. self assert: ('jackie' charCodeAt:2) equals: 97.
  777. self assert: ('jackie' charCodeAt:3) equals: 99.
  778. self assert: ('jackie' charCodeAt:4) equals: 107.
  779. self assert: ('jackie' charCodeAt:5) equals: 105.
  780. self assert: ('jackie' charCodeAt:6) equals: 101
  781. !
  782. testCollect
  783. | newCollection |
  784. newCollection := 'hheelllloo'.
  785. self
  786. assertSameContents: (self collection collect: [ :each |
  787. each, each ])
  788. as: newCollection
  789. !
  790. testCopyFromTo
  791. self assert: ('jackie' copyFrom: 1 to: 3) equals: 'jac'.
  792. self assert: ('jackie' copyFrom: 4 to: 6) equals: 'kie'.
  793. !
  794. testCopyWithoutAll
  795. self
  796. assert: ('*hello* *world*' copyWithoutAll: '*')
  797. equals: 'hello world'
  798. !
  799. testDetect
  800. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  801. self
  802. should: [ self collection detect: [ :each | each = 6 ] ]
  803. raise: Error
  804. !
  805. testEquality
  806. self assert: 'hello' equals: 'hello'.
  807. self deny: 'hello' = 'world'.
  808. "Test for issue 459"
  809. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  810. self assert: 'hello' equals: 'hello' yourself.
  811. self assert: 'hello' yourself equals: 'hello'.
  812. "test JS falsy value"
  813. self deny: '' = 0
  814. !
  815. testIdentity
  816. self assert: 'hello' == 'hello'.
  817. self deny: 'hello' == 'world'.
  818. self assert: 'hello' == 'hello' yourself.
  819. self assert: 'hello' yourself == 'hello'.
  820. "test JS falsy value"
  821. self deny: '' == 0
  822. !
  823. testIdentityHash
  824. self assert: 'foo' identityHash equals: 'foo' identityHash.
  825. self deny: ('foo' identityHash = 'bar' identityHash)
  826. !
  827. testIncludesSubString
  828. self assert: ('amber' includesSubString: 'ber').
  829. self deny: ('amber' includesSubString: 'zork').
  830. !
  831. testIndexOf
  832. self assert: (self collection indexOf: 'e') equals: 2.
  833. self should: [ self collection indexOf: 999 ] raise: Error.
  834. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  835. !
  836. testIsVowel
  837. |vowel consonant|
  838. vowel := 'u'.
  839. consonant := 'z'.
  840. self assert: vowel isVowel equals: true.
  841. self assert: consonant isVowel equals: false
  842. !
  843. testJoin
  844. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  845. !
  846. testRemoveAll
  847. self should: [ self collection removeAll ] raise: Error
  848. !
  849. testReversed
  850. self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
  851. !
  852. testSelect
  853. | newCollection |
  854. newCollection := 'o'.
  855. self
  856. assertSameContents: (self collection select: [ :each |
  857. each = 'o' ])
  858. as: newCollection
  859. !
  860. testSize
  861. self assert: 'smalltalk' size equals: 9.
  862. self assert: '' size equals: 0
  863. !
  864. testStreamContents
  865. self
  866. assert: (String streamContents: [ :aStream |
  867. aStream
  868. nextPutAll: 'hello'; space;
  869. nextPutAll: 'world' ])
  870. equals: 'hello world'
  871. !
  872. testSubStrings
  873. self assert: ('jackiechan' subStrings: 'ie') equals: #( 'jack' 'chan' ).
  874. !
  875. testTrim
  876. self assert: ' jackie' trimLeft equals: 'jackie'.
  877. self assert: 'jackie ' trimRight equals: 'jackie'.
  878. ! !
  879. !StringTest class methodsFor: 'accessing'!
  880. collectionClass
  881. ^ String
  882. ! !
  883. TestCase subclass: #ConsoleTranscriptTest
  884. instanceVariableNames: ''
  885. package: 'Kernel-Tests'!
  886. !ConsoleTranscriptTest methodsFor: 'tests'!
  887. testShow
  888. | originalTranscript |
  889. originalTranscript := Transcript current.
  890. Transcript register: ConsoleTranscript new.
  891. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  892. self shouldnt: [ Transcript show: console ] raise: Error.
  893. Transcript register: originalTranscript.
  894. ! !
  895. TestCase subclass: #JSObjectProxyTest
  896. instanceVariableNames: ''
  897. package: 'Kernel-Tests'!
  898. !JSObjectProxyTest methodsFor: 'accessing'!
  899. jsObject
  900. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': void 0}>
  901. ! !
  902. !JSObjectProxyTest methodsFor: 'tests'!
  903. testAtIfAbsent
  904. | testObject |
  905. testObject := self jsObject.
  906. self assert: (testObject at: 'abc' ifAbsent: [ 'Property does not exist' ]) equals: 'Property does not exist'.
  907. self assert: (testObject at: 'e' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  908. self assert: (testObject at: 'a' ifAbsent: [ 'Property does not exist' ]) equals: 1.
  909. self assert: (testObject at: 'f' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  910. !
  911. testAtIfPresent
  912. | testObject |
  913. testObject := self jsObject.
  914. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  915. self assert: (testObject at: 'e' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  916. self assert: (testObject at: 'a' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello 1'.
  917. self assert: (testObject at: 'f' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  918. !
  919. testAtIfPresentIfAbsent
  920. | testObject |
  921. testObject := self jsObject.
  922. self assert: (testObject at: 'abc' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'not present'.
  923. self assert: (testObject at: 'e' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  924. self assert: (testObject at: 'a' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello 1'.
  925. self assert: (testObject at: 'f' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  926. !
  927. testDNU
  928. self should: [ self jsObject foo ] raise: MessageNotUnderstood
  929. !
  930. testMessageSend
  931. self assert: self jsObject a equals: 1.
  932. self assert: self jsObject b equals: 2.
  933. self assert: (self jsObject c: 3) equals: 3
  934. !
  935. testMethodWithArguments
  936. self assert: (self jsObject c: 1) equals: 1
  937. !
  938. testPrinting
  939. self assert: self jsObject printString equals: '[object Object]'
  940. !
  941. testPropertyThatReturnsEmptyString
  942. | object |
  943. object := self jsObject.
  944. self assert: object d equals: ''.
  945. object d: 'hello'.
  946. self assert: object d equals: 'hello'
  947. !
  948. testPropertyThatReturnsUndefined
  949. | object |
  950. object := self jsObject.
  951. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  952. self assert: object e isNil
  953. !
  954. testValue
  955. | testObject |
  956. testObject := self jsObject.
  957. self assert: testObject value printString equals: '[object Object]'.
  958. testObject at: 'value' put: 'aValue'.
  959. self assert: testObject value equals: 'aValue'
  960. !
  961. testYourself
  962. | object |
  963. object := self jsObject
  964. d: 'test';
  965. yourself.
  966. self assert: object d equals: 'test'
  967. ! !
  968. TestCase subclass: #JavaScriptExceptionTest
  969. instanceVariableNames: ''
  970. package: 'Kernel-Tests'!
  971. !JavaScriptExceptionTest methodsFor: 'helpers'!
  972. throwException
  973. <throw 'test'>
  974. ! !
  975. !JavaScriptExceptionTest methodsFor: 'tests'!
  976. testCatchingException
  977. [ self throwException ]
  978. on: Error
  979. do: [ :error |
  980. self assert: error exception = 'test' ]
  981. !
  982. testRaisingException
  983. self should: [ self throwException ] raise: JavaScriptException
  984. ! !
  985. TestCase subclass: #MessageSendTest
  986. instanceVariableNames: ''
  987. package: 'Kernel-Tests'!
  988. !MessageSendTest methodsFor: 'tests'!
  989. testValue
  990. | messageSend |
  991. messageSend := MessageSend new
  992. receiver: Object new;
  993. selector: #asString;
  994. yourself.
  995. self assert: messageSend value equals: 'an Object'
  996. !
  997. testValueWithArguments
  998. | messageSend |
  999. messageSend := MessageSend new
  1000. receiver: 2;
  1001. selector: '+';
  1002. yourself.
  1003. self assert: (messageSend value: 3) equals: 5.
  1004. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  1005. ! !
  1006. TestCase subclass: #MethodInheritanceTest
  1007. instanceVariableNames: 'receiverTop receiverMiddle receiverBottom method performBlock'
  1008. package: 'Kernel-Tests'!
  1009. !MethodInheritanceTest methodsFor: 'accessing'!
  1010. codeGeneratorClass
  1011. ^ CodeGenerator
  1012. !
  1013. targetClassBottom
  1014. ^ JavaScriptException
  1015. !
  1016. targetClassMiddle
  1017. ^ Error
  1018. !
  1019. targetClassTop
  1020. ^ Object
  1021. ! !
  1022. !MethodInheritanceTest methodsFor: 'factory'!
  1023. compiler
  1024. ^ Compiler new
  1025. codeGeneratorClass: self codeGeneratorClass;
  1026. yourself
  1027. ! !
  1028. !MethodInheritanceTest methodsFor: 'initialization'!
  1029. setUp
  1030. receiverTop := self targetClassTop new.
  1031. receiverMiddle := self targetClassMiddle new.
  1032. receiverBottom := self targetClassBottom new.
  1033. method := nil.
  1034. performBlock := [ self error: 'performBlock not initialized' ]
  1035. !
  1036. tearDown
  1037. [ self deinstallTop ] on: Error do: [ ].
  1038. [ self deinstallMiddle ] on: Error do: [ ].
  1039. [ self deinstallBottom ] on: Error do: [ ]
  1040. ! !
  1041. !MethodInheritanceTest methodsFor: 'testing'!
  1042. deinstallBottom
  1043. self targetClassBottom removeCompiledMethod: method
  1044. !
  1045. deinstallMiddle
  1046. self targetClassMiddle removeCompiledMethod: method
  1047. !
  1048. deinstallTop
  1049. self targetClassTop removeCompiledMethod: method
  1050. !
  1051. installBottom: aString
  1052. method := self compiler install: aString forClass: self targetClassBottom category: 'tests'
  1053. !
  1054. installMiddle: aString
  1055. method := self compiler install: aString forClass: self targetClassMiddle category: 'tests'
  1056. !
  1057. installTop: aString
  1058. method := self compiler install: aString forClass: self targetClassTop category: 'tests'
  1059. !
  1060. shouldMNU
  1061. self shouldMNUTop.
  1062. self shouldMNUMiddle.
  1063. self shouldMNUBottom
  1064. !
  1065. shouldMNUBottom
  1066. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  1067. !
  1068. shouldMNUMiddle
  1069. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  1070. !
  1071. shouldMNUTop
  1072. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  1073. !
  1074. shouldReturn: anObject
  1075. | result |
  1076. result := performBlock value: receiverTop.
  1077. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1078. result := performBlock value: receiverMiddle.
  1079. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  1080. result := performBlock value: receiverBottom.
  1081. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  1082. !
  1083. shouldReturn: anObject and: anObject2 and: anObject3
  1084. | result |
  1085. result := performBlock value: receiverTop.
  1086. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1087. result := performBlock value: receiverMiddle.
  1088. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  1089. result := performBlock value: receiverBottom.
  1090. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  1091. ! !
  1092. !MethodInheritanceTest methodsFor: 'tests'!
  1093. testMNU11
  1094. performBlock := [ :x | x foo ].
  1095. self shouldMNU.
  1096. self installTop: 'foo ^ false'.
  1097. self installTop: 'foo ^ true'.
  1098. self deinstallTop.
  1099. self shouldMNU
  1100. !
  1101. testMNU22
  1102. performBlock := [ :x | x foo ].
  1103. self shouldMNU.
  1104. self installMiddle: 'foo ^ false'.
  1105. self installMiddle: 'foo ^ true'.
  1106. self deinstallMiddle.
  1107. self shouldMNU
  1108. !
  1109. testReturns1
  1110. performBlock := [ :x | x foo ].
  1111. self installTop: 'foo ^ false'.
  1112. self shouldReturn: false.
  1113. self installTop: 'foo ^ true'.
  1114. self shouldReturn: true
  1115. ! !
  1116. TestCase subclass: #NumberTest
  1117. instanceVariableNames: ''
  1118. package: 'Kernel-Tests'!
  1119. !NumberTest methodsFor: 'tests'!
  1120. testAbs
  1121. self assert: 4 abs equals: 4.
  1122. self assert: -4 abs equals: 4
  1123. !
  1124. testArithmetic
  1125. "We rely on JS here, so we won't test complex behavior, just check if
  1126. message sends are corrects"
  1127. self assert: 1.5 + 1 equals: 2.5.
  1128. self assert: 2 - 1 equals: 1.
  1129. self assert: -2 - 1 equals: -3.
  1130. self assert: 12 / 2 equals: 6.
  1131. self assert: 3 * 4 equals: 12.
  1132. self assert: 7 // 2 equals: 3.
  1133. self assert: 7 \\ 2 equals: 1.
  1134. "Simple parenthesis and execution order"
  1135. self assert: 1 + 2 * 3 equals: 9.
  1136. self assert: 1 + (2 * 3) equals: 7
  1137. !
  1138. testAsNumber
  1139. self assert: 3 asNumber equals: 3.
  1140. !
  1141. testCeiling
  1142. self assert: 1.2 ceiling equals: 2.
  1143. self assert: -1.2 ceiling equals: -1.
  1144. self assert: 1.0 ceiling equals: 1.
  1145. !
  1146. testComparison
  1147. self assert: 3 > 2.
  1148. self assert: 2 < 3.
  1149. self deny: 3 < 2.
  1150. self deny: 2 > 3.
  1151. self assert: 3 >= 3.
  1152. self assert: 3.1 >= 3.
  1153. self assert: 3 <= 3.
  1154. self assert: 3 <= 3.1
  1155. !
  1156. testCopying
  1157. self assert: 1 copy == 1.
  1158. self assert: 1 deepCopy == 1
  1159. !
  1160. testEquality
  1161. self assert: (1 = 1).
  1162. self assert: (0 = 0).
  1163. self deny: (1 = 0).
  1164. self assert: (1 yourself = 1).
  1165. self assert: (1 = 1 yourself).
  1166. self assert: (1 yourself = 1 yourself).
  1167. self deny: 0 = false.
  1168. self deny: false = 0.
  1169. self deny: '' = 0.
  1170. self deny: 0 = ''
  1171. !
  1172. testFloor
  1173. self assert: 1.2 floor equals: 1.
  1174. self assert: -1.2 floor equals: -2.
  1175. self assert: 1.0 floor equals: 1.
  1176. !
  1177. testHexNumbers
  1178. self assert: 16r9 equals: 9.
  1179. self assert: 16rA truncated equals: 10.
  1180. self assert: 16rB truncated equals: 11.
  1181. self assert: 16rC truncated equals: 12.
  1182. self assert: 16rD truncated equals: 13.
  1183. self assert: 16rE truncated equals: 14.
  1184. self assert: 16rF truncated equals: 15
  1185. !
  1186. testIdentity
  1187. self assert: 1 == 1.
  1188. self assert: 0 == 0.
  1189. self deny: 1 == 0.
  1190. self assert: 1 yourself == 1.
  1191. self assert: 1 == 1 yourself.
  1192. self assert: 1 yourself == 1 yourself.
  1193. self deny: 1 == 2
  1194. !
  1195. testInvalidHexNumbers
  1196. self should: [ 16rG ] raise: MessageNotUnderstood.
  1197. self should: [ 16rg ] raise: MessageNotUnderstood.
  1198. self should: [ 16rH ] raise: MessageNotUnderstood.
  1199. self should: [ 16rh ] raise: MessageNotUnderstood.
  1200. self should: [ 16rI ] raise: MessageNotUnderstood.
  1201. self should: [ 16ri ] raise: MessageNotUnderstood.
  1202. self should: [ 16rJ ] raise: MessageNotUnderstood.
  1203. self should: [ 16rj ] raise: MessageNotUnderstood.
  1204. self should: [ 16rK ] raise: MessageNotUnderstood.
  1205. self should: [ 16rk ] raise: MessageNotUnderstood.
  1206. self should: [ 16rL ] raise: MessageNotUnderstood.
  1207. self should: [ 16rl ] raise: MessageNotUnderstood.
  1208. self should: [ 16rM ] raise: MessageNotUnderstood.
  1209. self should: [ 16rm ] raise: MessageNotUnderstood.
  1210. self should: [ 16rN ] raise: MessageNotUnderstood.
  1211. self should: [ 16rn ] raise: MessageNotUnderstood.
  1212. self should: [ 16rO ] raise: MessageNotUnderstood.
  1213. self should: [ 16ro ] raise: MessageNotUnderstood.
  1214. self should: [ 16rP ] raise: MessageNotUnderstood.
  1215. self should: [ 16rp ] raise: MessageNotUnderstood.
  1216. self should: [ 16rQ ] raise: MessageNotUnderstood.
  1217. self should: [ 16rq ] raise: MessageNotUnderstood.
  1218. self should: [ 16rR ] raise: MessageNotUnderstood.
  1219. self should: [ 16rr ] raise: MessageNotUnderstood.
  1220. self should: [ 16rS ] raise: MessageNotUnderstood.
  1221. self should: [ 16rs ] raise: MessageNotUnderstood.
  1222. self should: [ 16rT ] raise: MessageNotUnderstood.
  1223. self should: [ 16rt ] raise: MessageNotUnderstood.
  1224. self should: [ 16rU ] raise: MessageNotUnderstood.
  1225. self should: [ 16ru ] raise: MessageNotUnderstood.
  1226. self should: [ 16rV ] raise: MessageNotUnderstood.
  1227. self should: [ 16rv ] raise: MessageNotUnderstood.
  1228. self should: [ 16rW ] raise: MessageNotUnderstood.
  1229. self should: [ 16rw ] raise: MessageNotUnderstood.
  1230. self should: [ 16rX ] raise: MessageNotUnderstood.
  1231. self should: [ 16rx ] raise: MessageNotUnderstood.
  1232. self should: [ 16rY ] raise: MessageNotUnderstood.
  1233. self should: [ 16ry ] raise: MessageNotUnderstood.
  1234. self should: [ 16rZ ] raise: MessageNotUnderstood.
  1235. self should: [ 16rz ] raise: MessageNotUnderstood.
  1236. self should: [ 16rABcdEfZ ] raise: MessageNotUnderstood.
  1237. !
  1238. testLog
  1239. self assert: 10000 log equals: 4.
  1240. self assert: (512 log: 2) equals: 9.
  1241. self assert: Number e ln equals: 1.
  1242. !
  1243. testMinMax
  1244. self assert: (2 max: 5) equals: 5.
  1245. self assert: (2 min: 5) equals: 2
  1246. !
  1247. testNegated
  1248. self assert: 3 negated equals: -3.
  1249. self assert: -3 negated equals: 3
  1250. !
  1251. testPrintShowingDecimalPlaces
  1252. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1253. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1254. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1255. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1256. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1257. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1258. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1259. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1260. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1261. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1262. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1263. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  1264. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  1265. !
  1266. testRaisedTo
  1267. self assert: (2 raisedTo: 4) equals: 16.
  1268. self assert: (2 raisedTo: 0) equals: 1.
  1269. self assert: (2 raisedTo: -3) equals: 0.125.
  1270. self assert: (4 raisedTo: 0.5) equals: 2.
  1271. self assert: 2 ** 4 equals: 16.
  1272. !
  1273. testRounded
  1274. self assert: 3 rounded equals: 3.
  1275. self assert: 3.212 rounded equals: 3.
  1276. self assert: 3.51 rounded equals: 4
  1277. !
  1278. testSign
  1279. self assert: 5 sign equals: 1.
  1280. self assert: 0 sign equals: 0.
  1281. self assert: -1.4 sign equals: -1.
  1282. !
  1283. testSqrt
  1284. self assert: 4 sqrt equals: 2.
  1285. self assert: 16 sqrt equals: 4
  1286. !
  1287. testSquared
  1288. self assert: 4 squared equals: 16
  1289. !
  1290. testTimesRepeat
  1291. | i |
  1292. i := 0.
  1293. 0 timesRepeat: [ i := i + 1 ].
  1294. self assert: i equals: 0.
  1295. 5 timesRepeat: [ i := i + 1 ].
  1296. self assert: i equals: 5
  1297. !
  1298. testTo
  1299. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  1300. !
  1301. testToBy
  1302. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  1303. self should: [ 1 to: 4 by: 0 ] raise: Error
  1304. !
  1305. testTrigonometry
  1306. self assert: 0 cos equals: 1.
  1307. self assert: 0 sin equals: 0.
  1308. self assert: 0 tan equals: 0.
  1309. self assert: 1 arcCos equals: 0.
  1310. self assert: 0 arcSin equals: 0.
  1311. self assert: 0 arcTan equals: 0.
  1312. !
  1313. testTruncated
  1314. self assert: 3 truncated equals: 3.
  1315. self assert: 3.212 truncated equals: 3.
  1316. self assert: 3.51 truncated equals: 3
  1317. ! !
  1318. Object subclass: #ObjectMock
  1319. instanceVariableNames: 'foo bar'
  1320. package: 'Kernel-Tests'!
  1321. !ObjectMock commentStamp!
  1322. ObjectMock is there only to perform tests on classes.!
  1323. !ObjectMock methodsFor: 'not yet classified'!
  1324. foo
  1325. ^ foo
  1326. !
  1327. foo: anObject
  1328. foo := anObject
  1329. ! !
  1330. TestCase subclass: #ObjectTest
  1331. instanceVariableNames: ''
  1332. package: 'Kernel-Tests'!
  1333. !ObjectTest methodsFor: 'tests'!
  1334. notDefined
  1335. <return void 0;>
  1336. !
  1337. testBasicAccess
  1338. | o |
  1339. o := Object new.
  1340. o basicAt: 'a' put: 1.
  1341. self assert: (o basicAt: 'a') equals: 1.
  1342. self assert: (o basicAt: 'b') equals: nil
  1343. !
  1344. testBasicPerform
  1345. | o |
  1346. o := Object new.
  1347. o basicAt: 'func' put: [ 'hello' ].
  1348. o basicAt: 'func2' put: [ :a | a + 1 ].
  1349. self assert: (o basicPerform: 'func') equals: 'hello'.
  1350. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1351. !
  1352. testDNU
  1353. self should: [ Object new foo ] raise: MessageNotUnderstood
  1354. !
  1355. testEquality
  1356. | o |
  1357. o := Object new.
  1358. self deny: o = Object new.
  1359. self assert: (o = o).
  1360. self assert: (o yourself = o).
  1361. self assert: (o = o yourself)
  1362. !
  1363. testHalt
  1364. self should: [ Object new halt ] raise: Error
  1365. !
  1366. testIdentity
  1367. | o |
  1368. o := Object new.
  1369. self deny: o == Object new.
  1370. self assert: o == o.
  1371. self assert: o yourself == o.
  1372. self assert: o == o yourself
  1373. !
  1374. testIfNil
  1375. self deny: Object new isNil.
  1376. self deny: (Object new ifNil: [ true ]) = true.
  1377. self assert: (Object new ifNotNil: [ true ]) equals: true.
  1378. self assert: (Object new ifNil: [ false ] ifNotNil: [ true ]) equals: true.
  1379. self assert: (Object new ifNotNil: [ true ] ifNil: [ false ]) equals: true
  1380. !
  1381. testInstVars
  1382. | o |
  1383. o := ObjectMock new.
  1384. self assert: (o instVarAt: #foo) equals: nil.
  1385. o instVarAt: #foo put: 1.
  1386. self assert: (o instVarAt: #foo) equals: 1.
  1387. self assert: (o instVarAt: 'foo') equals: 1
  1388. !
  1389. testNilUndefined
  1390. "nil in Smalltalk is the undefined object in JS"
  1391. self assert: self notDefined equals: nil
  1392. !
  1393. testYourself
  1394. | o |
  1395. o := ObjectMock new.
  1396. self assert: o yourself == o
  1397. !
  1398. testidentityHash
  1399. | o1 o2 |
  1400. o1 := Object new.
  1401. o2 := Object new.
  1402. self assert: o1 identityHash == o1 identityHash.
  1403. self deny: o1 identityHash == o2 identityHash
  1404. ! !
  1405. TestCase subclass: #PointTest
  1406. instanceVariableNames: ''
  1407. package: 'Kernel-Tests'!
  1408. !PointTest methodsFor: 'tests'!
  1409. testAccessing
  1410. self assert: (Point x: 3 y: 4) x equals: 3.
  1411. self assert: (Point x: 3 y: 4) y equals: 4.
  1412. self assert: (Point new x: 3) x equals: 3.
  1413. self assert: (Point new y: 4) y equals: 4
  1414. !
  1415. testArithmetic
  1416. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1417. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1418. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1419. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1420. !
  1421. testAt
  1422. self assert: 3@4 equals: (Point x: 3 y: 4)
  1423. !
  1424. testEgality
  1425. self assert: (3@4 = (3@4)).
  1426. self deny: 3@5 = (3@6)
  1427. !
  1428. testNew
  1429. self assert: (Point new x: 3) y equals: nil.
  1430. self deny: (Point new x: 3) x = 0.
  1431. self assert: (Point new y: 4) x equals: nil.
  1432. self deny: (Point new y: 4) y = 0
  1433. !
  1434. testTranslateBy
  1435. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1436. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1437. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1438. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1439. ! !
  1440. TestCase subclass: #QueueTest
  1441. instanceVariableNames: ''
  1442. package: 'Kernel-Tests'!
  1443. !QueueTest methodsFor: 'tests'!
  1444. testNextIfAbsent
  1445. | queue |
  1446. queue := Queue new.
  1447. queue nextPut: 'index1'.
  1448. self assert: (queue nextIfAbsent: 'empty') = 'index1'.
  1449. self deny: (queue nextIfAbsent: 'empty') = 'index1'
  1450. !
  1451. testQueueNext
  1452. | queue |
  1453. queue := Queue new.
  1454. queue
  1455. nextPut: 'index1';
  1456. nextPut: 'index2'.
  1457. self assert: queue next = 'index1'.
  1458. self deny: queue next = 'index'.
  1459. self should: [ queue next ] raise: Error
  1460. ! !
  1461. TestCase subclass: #RandomTest
  1462. instanceVariableNames: ''
  1463. package: 'Kernel-Tests'!
  1464. !RandomTest methodsFor: 'tests'!
  1465. testAtRandomNumber
  1466. |val|
  1467. 100 timesRepeat: [
  1468. val := 10 atRandom.
  1469. self assert: (val > 0).
  1470. self assert: (val <11)
  1471. ]
  1472. !
  1473. testAtRandomSequenceableCollection
  1474. |val|
  1475. 100 timesRepeat: [
  1476. val := 'abc' atRandom.
  1477. self assert: ((val = 'a') | (val = 'b') | (val = 'c' )).
  1478. ].
  1479. !
  1480. textNext
  1481. 10000 timesRepeat: [
  1482. | current next |
  1483. next := Random new next.
  1484. self assert: (next >= 0).
  1485. self assert: (next < 1).
  1486. self deny: current = next.
  1487. next = current ]
  1488. ! !
  1489. TestCase subclass: #SetTest
  1490. instanceVariableNames: ''
  1491. package: 'Kernel-Tests'!
  1492. !SetTest methodsFor: 'tests'!
  1493. testAddRemove
  1494. | set |
  1495. set := Set new.
  1496. self assert: set isEmpty.
  1497. set add: 3.
  1498. self assert: (set includes: 3).
  1499. set add: 5.
  1500. self assert: (set includes: 5).
  1501. set remove: 3.
  1502. self deny: (set includes: 3)
  1503. !
  1504. testAt
  1505. self should: [ Set new at: 1 put: 2 ] raise: Error
  1506. !
  1507. testCollect
  1508. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1509. !
  1510. testComparing
  1511. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1512. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1513. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1514. self deny: #(1 2) asSet = #(0 2) asSet
  1515. !
  1516. testPrintString
  1517. | set |
  1518. set := Set new.
  1519. self assert: set printString equals: 'a Set ()'.
  1520. set add: 1; add: 3.
  1521. self assert: set printString equals: 'a Set (1 3)'.
  1522. set add: 'foo'.
  1523. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1524. set remove: 1; remove: 3.
  1525. self assert: set printString equals: 'a Set (''foo'')'.
  1526. set add: 3.
  1527. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1528. set add: 3.
  1529. self assert: set printString equals: 'a Set (''foo'' 3)'
  1530. !
  1531. testSize
  1532. self assert: Set new size equals: 0.
  1533. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1534. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1535. !
  1536. testUnboxedObjects
  1537. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1538. !
  1539. testUnicity
  1540. | set |
  1541. set := Set new.
  1542. set add: 21.
  1543. set add: 'hello'.
  1544. set add: 21.
  1545. self assert: set size equals: 2.
  1546. set add: 'hello'.
  1547. self assert: set size equals: 2.
  1548. self assert: set asArray equals: #(21 'hello')
  1549. ! !
  1550. TestCase subclass: #StreamTest
  1551. instanceVariableNames: ''
  1552. package: 'Kernel-Tests'!
  1553. !StreamTest methodsFor: 'accessing'!
  1554. collectionClass
  1555. ^ self class collectionClass
  1556. !
  1557. newCollection
  1558. ^ self collectionClass new
  1559. !
  1560. newStream
  1561. ^ self collectionClass new stream
  1562. ! !
  1563. !StreamTest methodsFor: 'tests'!
  1564. testAtStartAtEnd
  1565. | stream |
  1566. stream := self newStream.
  1567. self assert: stream atStart.
  1568. self assert: stream atEnd.
  1569. stream nextPutAll: self newCollection.
  1570. self assert: stream atEnd.
  1571. self deny: stream atStart.
  1572. stream position: 1.
  1573. self deny: stream atEnd.
  1574. self deny: stream atStart
  1575. !
  1576. testContents
  1577. | stream |
  1578. stream := self newStream.
  1579. stream nextPutAll: self newCollection.
  1580. self assert: stream contents equals: self newCollection
  1581. !
  1582. testIsEmpty
  1583. | stream |
  1584. stream := self newStream.
  1585. self assert: stream isEmpty.
  1586. stream nextPutAll: self newCollection.
  1587. self deny: stream isEmpty
  1588. !
  1589. testPosition
  1590. | collection stream |
  1591. collection := self newCollection.
  1592. stream := self newStream.
  1593. stream nextPutAll: collection.
  1594. self assert: stream position equals: collection size.
  1595. stream position: 0.
  1596. self assert: stream position equals: 0.
  1597. stream next.
  1598. self assert: stream position equals: 1.
  1599. stream next.
  1600. self assert: stream position equals: 2
  1601. !
  1602. testReading
  1603. | stream collection |
  1604. collection := self newCollection.
  1605. stream := self newStream.
  1606. stream
  1607. nextPutAll: collection;
  1608. position: 0.
  1609. collection do: [ :each |
  1610. self assert: stream next equals: each ].
  1611. self assert: stream next isNil
  1612. !
  1613. testStreamContents
  1614. !
  1615. testWrite
  1616. | stream collection |
  1617. collection := self newCollection.
  1618. stream := self newStream.
  1619. collection do: [ :each | stream << each ].
  1620. self assert: stream contents equals: collection
  1621. !
  1622. testWriting
  1623. | stream collection |
  1624. collection := self newCollection.
  1625. stream := self newStream.
  1626. collection do: [ :each | stream nextPut: each ].
  1627. self assert: stream contents equals: collection.
  1628. stream := self newStream.
  1629. stream nextPutAll: collection.
  1630. self assert: stream contents equals: collection
  1631. ! !
  1632. !StreamTest class methodsFor: 'accessing'!
  1633. collectionClass
  1634. ^ nil
  1635. ! !
  1636. !StreamTest class methodsFor: 'testing'!
  1637. isAbstract
  1638. ^ self collectionClass isNil
  1639. ! !
  1640. StreamTest subclass: #ArrayStreamTest
  1641. instanceVariableNames: ''
  1642. package: 'Kernel-Tests'!
  1643. !ArrayStreamTest methodsFor: 'accessing'!
  1644. newCollection
  1645. ^ { true. 1. 3@4. 'foo' }
  1646. ! !
  1647. !ArrayStreamTest class methodsFor: 'accessing'!
  1648. collectionClass
  1649. ^ Array
  1650. ! !
  1651. StreamTest subclass: #StringStreamTest
  1652. instanceVariableNames: ''
  1653. package: 'Kernel-Tests'!
  1654. !StringStreamTest methodsFor: 'accessing'!
  1655. newCollection
  1656. ^ 'hello world'
  1657. ! !
  1658. !StringStreamTest class methodsFor: 'accessing'!
  1659. collectionClass
  1660. ^ String
  1661. ! !
  1662. TestCase subclass: #UndefinedTest
  1663. instanceVariableNames: ''
  1664. package: 'Kernel-Tests'!
  1665. !UndefinedTest methodsFor: 'tests'!
  1666. testCopying
  1667. self assert: nil copy equals: nil
  1668. !
  1669. testDeepCopy
  1670. self assert: nil deepCopy = nil
  1671. !
  1672. testIfNil
  1673. self assert: (nil ifNil: [ true ]) equals: true.
  1674. self deny: (nil ifNotNil: [ true ]) = true.
  1675. self assert: (nil ifNil: [ true ] ifNotNil: [ false ]) equals: true.
  1676. self deny: (nil ifNotNil: [ true ] ifNil: [ false ]) = true
  1677. !
  1678. testIsNil
  1679. self assert: nil isNil.
  1680. self deny: nil notNil.
  1681. ! !