1
0

Kernel-Tests.st 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173
  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. testSelect
  366. | newCollection |
  367. newCollection := #(2 -4).
  368. self
  369. assertSameContents: (self collection select: [ :each |
  370. each even ])
  371. as: newCollection
  372. !
  373. testSize
  374. self assert: self collectionClass new size equals: 0.
  375. self assert: self collection size equals: 4
  376. ! !
  377. !CollectionTest class methodsFor: 'accessing'!
  378. collectionClass
  379. ^ nil
  380. ! !
  381. !CollectionTest class methodsFor: 'testing'!
  382. isAbstract
  383. ^ self collectionClass isNil
  384. ! !
  385. CollectionTest subclass: #IndexableCollectionTest
  386. instanceVariableNames: ''
  387. package: 'Kernel-Tests'!
  388. !IndexableCollectionTest methodsFor: 'tests'!
  389. testAt
  390. self assert: (self collection at: 4) equals: -4.
  391. self should: [ self collection at: 5 ] raise: Error
  392. !
  393. testAtIfAbsent
  394. self assert: (self collection at: (self collection size + 1) ifAbsent: [ 'none' ]) equals: 'none'
  395. !
  396. testContains
  397. | collection |
  398. collection := self collection.
  399. self assert: (self collection contains: [ :each | each = self collection first ]).
  400. self deny: (self collection contains: [ :each | each = Object new ])
  401. !
  402. testIndexOf
  403. self assert: (self collection indexOf: 2) equals: 2.
  404. self should: [ self collection indexOf: 999 ] raise: Error.
  405. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  406. !
  407. testWithIndexDo
  408. | collection |
  409. collection := self collection.
  410. self collection withIndexDo: [ :each :index |
  411. self assert: (collection at: index) equals: each ]
  412. ! !
  413. IndexableCollectionTest subclass: #HashedCollectionTest
  414. instanceVariableNames: ''
  415. package: 'Kernel-Tests'!
  416. !HashedCollectionTest methodsFor: 'accessing'!
  417. collection
  418. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  419. !
  420. collectionWithDuplicates
  421. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
  422. ! !
  423. !HashedCollectionTest methodsFor: 'tests'!
  424. testAsDictionary
  425. self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
  426. !
  427. testAt
  428. self assert: (self collection at: 'a') equals: 2.
  429. self should: [ self collection at: 5 ] raise: Error
  430. !
  431. testContains
  432. | collection |
  433. collection := self collection.
  434. self assert: (self collection contains: [ :each | each = self collection values first ]).
  435. self deny: (self collection contains: [ :each | each = Object new ])
  436. !
  437. testFrom
  438. "Accept a collection of associations."
  439. | associations |
  440. associations := { 'a' -> 1. 'b' -> 2 }.
  441. self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  442. !
  443. testIndexOf
  444. self assert: (self collection indexOf: 2) equals: 'a'.
  445. self should: [ self collection indexOf: 999 ] raise: Error.
  446. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  447. !
  448. testNewFromPairs
  449. "Accept an array in which all odd indexes are keys and evens are values."
  450. | flattenedAssociations |
  451. flattenedAssociations := { 'a'. 1. 'b'. 2 }.
  452. self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  453. ! !
  454. !HashedCollectionTest class methodsFor: 'accessing'!
  455. collectionClass
  456. ^ HashedCollection
  457. ! !
  458. HashedCollectionTest subclass: #DictionaryTest
  459. instanceVariableNames: ''
  460. package: 'Kernel-Tests'!
  461. !DictionaryTest methodsFor: 'accessing'!
  462. collection
  463. ^ Dictionary new
  464. at: 1 put: 1;
  465. at: 'a' put: 2;
  466. at: true put: 3;
  467. at: 4 put: -4;
  468. yourself
  469. !
  470. collectionWithDuplicates
  471. ^ Dictionary new
  472. at: 1 put: 1;
  473. at: 'a' put: 2;
  474. at: true put: 3;
  475. at: 4 put: -4;
  476. at: 'b' put: 1;
  477. at: 3 put: 3;
  478. at: false put: 12;
  479. yourself
  480. ! !
  481. !DictionaryTest methodsFor: 'tests'!
  482. testAccessing
  483. | d |
  484. d := Dictionary new.
  485. d at: 'hello' put: 'world'.
  486. self assert: (d at: 'hello') equals: 'world'.
  487. self assert: (d at: 'hello' ifAbsent: [ nil ]) equals: 'world'.
  488. self deny: (d at: 'foo' ifAbsent: [ nil ]) = 'world'.
  489. self assert: (d includesKey: 'hello').
  490. self deny: (d includesKey: 'foo').
  491. d at: 1 put: 2.
  492. self assert: (d at: 1) equals: 2.
  493. d at: 1@3 put: 3.
  494. self assert: (d at: 1@3) equals: 3.
  495. self assert: (d includesKey: 1@3).
  496. self deny: (d includesKey: 3@1)
  497. !
  498. testAsHashedCollection
  499. self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
  500. !
  501. testDynamicDictionaries
  502. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  503. !
  504. testEquality
  505. | d1 d2 |
  506. self assert: (Dictionary new = Dictionary new).
  507. d1 := Dictionary new at: 1 put: 2; yourself.
  508. d2 := Dictionary new at: 1 put: 2; yourself.
  509. self assert: (d1 = d2).
  510. d2 := Dictionary new at: 1 put: 3; yourself.
  511. self deny: d1 = d2.
  512. d2 := Dictionary new at: 2 put: 2; yourself.
  513. self deny: d1 = d2.
  514. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  515. self deny: d1 = d2.
  516. !
  517. testIfAbsent
  518. | d visited |
  519. visited := false.
  520. d := Dictionary new.
  521. d at: 'hello' ifAbsent: [ visited := true ].
  522. self assert: visited.
  523. !
  524. testIfPresent
  525. | d visited absent |
  526. visited := false.
  527. d := Dictionary new.
  528. d at: 'hello' put: 'world'.
  529. d at: 'hello' ifPresent: [ :value | visited := value ].
  530. self assert: visited equals: 'world'.
  531. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  532. self assert: absent isNil.
  533. !
  534. testIfPresentIfAbsent
  535. | d visited |
  536. visited := false.
  537. d := Dictionary new.
  538. d at: 'hello' put: 'world'.
  539. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  540. self assert: visited equals: 'world'.
  541. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  542. self assert: visited.
  543. !
  544. testKeys
  545. | d |
  546. d := Dictionary new.
  547. d at: 1 put: 2.
  548. d at: 2 put: 3.
  549. d at: 3 put: 4.
  550. self assert: d keys equals: #(1 2 3)
  551. !
  552. testPointKey
  553. | d |
  554. d := Dictionary new.
  555. d at: 1@1 put: 'foo'.
  556. self assert: (d at: 1@1) equals: 'foo'.
  557. d at: 1@1 put: 'bar'.
  558. self assert: (d at: 1@1) equals: 'bar'.
  559. d removeKey: 1@1.
  560. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  561. self deny: (d includesKey: 1@1)
  562. !
  563. testPrintString
  564. self
  565. assert: (Dictionary new
  566. at:'firstname' put: 'James';
  567. at:'lastname' put: 'Bond';
  568. printString)
  569. equals: 'a Dictionary (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  570. !
  571. testRemoveKey
  572. | d key |
  573. d := Dictionary new.
  574. d at: 1 put: 2.
  575. d at: 2 put: 3.
  576. d at: 3 put: 4.
  577. key := 2.
  578. self assert: d keys equals: #(1 2 3).
  579. d removeKey: key.
  580. self assert: d keys equals: #(1 3).
  581. self assert: d values equals: #(2 4).
  582. self deny: (d includesKey: 2)
  583. !
  584. testRemoveKeyIfAbsent
  585. | d key |
  586. d := Dictionary new.
  587. d at: 1 put: 2.
  588. d at: 2 put: 3.
  589. d at: 3 put: 4.
  590. key := 2.
  591. self assert: (d removeKey: key) equals: 3.
  592. key := 3.
  593. self assert: (d removeKey: key ifAbsent: [ 42 ]) equals: 4.
  594. key := 'why'.
  595. self assert: (d removeKey: key ifAbsent: [ 42 ] ) equals: 42.
  596. !
  597. testSize
  598. | d |
  599. d := Dictionary new.
  600. self assert: d size equals: 0.
  601. d at: 1 put: 2.
  602. self assert: d size equals: 1.
  603. d at: 2 put: 3.
  604. self assert: d size equals: 2.
  605. !
  606. testValues
  607. | d |
  608. d := Dictionary new.
  609. d at: 1 put: 2.
  610. d at: 2 put: 3.
  611. d at: 3 put: 4.
  612. self assert: d values equals: #(2 3 4)
  613. ! !
  614. !DictionaryTest class methodsFor: 'accessing'!
  615. collectionClass
  616. ^ Dictionary
  617. ! !
  618. IndexableCollectionTest subclass: #SequenceableCollectionTest
  619. instanceVariableNames: ''
  620. package: 'Kernel-Tests'!
  621. !SequenceableCollectionTest methodsFor: 'tests'!
  622. testFirst
  623. self assert: (self collection first) equals: (self collection at: 1)
  624. !
  625. testFourth
  626. self assert: (self collection fourth) equals: (self collection at: 4)
  627. !
  628. testLast
  629. self assert: (self collection last) equals: (self collection at: self collection size)
  630. !
  631. testSecond
  632. self assert: (self collection second) equals: (self collection at: 2)
  633. !
  634. testThird
  635. self assert: (self collection third) equals: (self collection at: 3)
  636. ! !
  637. SequenceableCollectionTest subclass: #ArrayTest
  638. instanceVariableNames: ''
  639. package: 'Kernel-Tests'!
  640. !ArrayTest methodsFor: 'tests'!
  641. testAdd
  642. | array |
  643. array := self collection.
  644. array add: 6.
  645. self assert: array last equals: 6
  646. !
  647. testAddFirst
  648. self assert: (self collection addFirst: 0; yourself) first equals: 0
  649. !
  650. testAtIfAbsent
  651. | array |
  652. array := #('hello' 'world').
  653. self assert: (array at: 1) equals: 'hello'.
  654. self assert: (array at: 2) equals: 'world'.
  655. self assert: (array at: 2 ifAbsent: [ 'not found' ]) equals: 'world'.
  656. self assert: (array at: 0 ifAbsent: [ 'not found' ]) equals: 'not found'.
  657. self assert: (array at: -10 ifAbsent: [ 'not found' ]) equals: 'not found'.
  658. self assert: (array at: 3 ifAbsent: [ 'not found' ]) equals: 'not found'.
  659. !
  660. testFirstN
  661. self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
  662. !
  663. testIfEmpty
  664. self assert: ( '' ifEmpty: [ 'zork' ] ) equals: 'zork'
  665. !
  666. testPrintString
  667. | array |
  668. array := Array new.
  669. self assert: array printString equals: 'an Array ()'.
  670. array add: 1; add: 3.
  671. self assert: array printString equals: 'an Array (1 3)'.
  672. array add: 'foo'.
  673. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  674. array remove: 1; remove: 3.
  675. self assert: array printString equals: 'an Array (''foo'')'.
  676. array addLast: 3.
  677. self assert: array printString equals: 'an Array (''foo'' 3)'.
  678. array addLast: 3.
  679. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  680. !
  681. testRemove
  682. | array |
  683. array := #(1 2 3 4 5).
  684. array remove: 3.
  685. self assert: array equals: #(1 2 4 5).
  686. self should: [ array remove: 3 ] raise: Error
  687. !
  688. testRemoveFromTo
  689. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  690. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  691. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  692. !
  693. testRemoveIndex
  694. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  695. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  696. self assert: (#('hello') removeIndex: 1) equals: #()
  697. !
  698. testRemoveLast
  699. | array |
  700. array := #(1 2).
  701. array removeLast.
  702. self assert: array last equals: 1
  703. !
  704. testReversed
  705. |array|
  706. array := #(5 4 3 2 1).
  707. self assert: (array reversed) equals: #(1 2 3 4 5)
  708. !
  709. testSort
  710. | array |
  711. array := #(3 1 4 5 2).
  712. array sort.
  713. self assert: array equals: #(1 2 3 4 5)
  714. ! !
  715. !ArrayTest class methodsFor: 'accessing'!
  716. collectionClass
  717. ^ Array
  718. ! !
  719. SequenceableCollectionTest subclass: #StringTest
  720. instanceVariableNames: ''
  721. package: 'Kernel-Tests'!
  722. !StringTest methodsFor: 'accessing'!
  723. collection
  724. ^ 'hello'
  725. !
  726. collectionWithDuplicates
  727. ^ 'abbaerte'
  728. ! !
  729. !StringTest methodsFor: 'tests'!
  730. testAddRemove
  731. self should: [ 'hello' add: 'a' ] raise: Error.
  732. self should: [ 'hello' remove: 'h' ] raise: Error
  733. !
  734. testAsArray
  735. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  736. !
  737. testAsLowerCase
  738. self assert: 'JACKIE' asLowercase equals: 'jackie'.
  739. !
  740. testAsNumber
  741. self assert: '3' asNumber equals: 3.
  742. self assert: '-3' asNumber equals: -3.
  743. self assert: '-1.5' asNumber equals: -1.5.
  744. !
  745. testAsUpperCase
  746. self assert: 'jackie' asUppercase equals: 'JACKIE'.
  747. !
  748. testAsciiValue
  749. | characterA characterU |
  750. characterA := 'A'.
  751. characterU := 'U'.
  752. self assert: (characterA asciiValue) equals:65.
  753. self assert: (characterU asciiValue) equals:85
  754. !
  755. testAt
  756. self assert: ('hello' at: 1) equals: 'h'.
  757. self assert: ('hello' at: 5) equals: 'o'.
  758. self assert: ('hello' at: 6 ifAbsent: [ nil ]) equals: nil
  759. !
  760. testAtPut
  761. "String instances are read-only"
  762. self should: [ 'hello' at: 1 put: 'a' ] raise: Error
  763. !
  764. testCapitalized
  765. self assert: 'test' capitalized equals: 'Test'.
  766. self assert: 'Test' capitalized equals: 'Test'.
  767. self assert: '' capitalized equals: ''.
  768. self assert: 'Test' isCapitalized equals: true.
  769. self assert: 'test' isCapitalized equals: false.
  770. !
  771. testCharCodeAt
  772. self assert: ('jackie' charCodeAt:1) equals: 106.
  773. self assert: ('jackie' charCodeAt:2) equals: 97.
  774. self assert: ('jackie' charCodeAt:3) equals: 99.
  775. self assert: ('jackie' charCodeAt:4) equals: 107.
  776. self assert: ('jackie' charCodeAt:5) equals: 105.
  777. self assert: ('jackie' charCodeAt:6) equals: 101
  778. !
  779. testCollect
  780. | newCollection |
  781. newCollection := 'hheelllloo'.
  782. self
  783. assertSameContents: (self collection collect: [ :each |
  784. each, each ])
  785. as: newCollection
  786. !
  787. testCopyFromTo
  788. self assert: ('jackie' copyFrom: 1 to: 3) equals: 'jac'.
  789. self assert: ('jackie' copyFrom: 4 to: 6) equals: 'kie'.
  790. !
  791. testCopyWithoutAll
  792. self
  793. assert: ('*hello* *world*' copyWithoutAll: '*')
  794. equals: 'hello world'
  795. !
  796. testDetect
  797. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  798. self
  799. should: [ self collection detect: [ :each | each = 6 ] ]
  800. raise: Error
  801. !
  802. testEquality
  803. self assert: 'hello' equals: 'hello'.
  804. self deny: 'hello' = 'world'.
  805. "Test for issue 459"
  806. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  807. self assert: 'hello' equals: 'hello' yourself.
  808. self assert: 'hello' yourself equals: 'hello'.
  809. "test JS falsy value"
  810. self deny: '' = 0
  811. !
  812. testIdentity
  813. self assert: 'hello' == 'hello'.
  814. self deny: 'hello' == 'world'.
  815. self assert: 'hello' == 'hello' yourself.
  816. self assert: 'hello' yourself == 'hello'.
  817. "test JS falsy value"
  818. self deny: '' == 0
  819. !
  820. testIdentityHash
  821. self assert: 'foo' identityHash equals: 'foo' identityHash.
  822. self deny: ('foo' identityHash = 'bar' identityHash)
  823. !
  824. testIncludesSubString
  825. self assert: ('amber' includesSubString: 'ber').
  826. self deny: ('amber' includesSubString: 'zork').
  827. !
  828. testIndexOf
  829. self assert: (self collection indexOf: 'e') equals: 2.
  830. self should: [ self collection indexOf: 999 ] raise: Error.
  831. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  832. !
  833. testIsVowel
  834. |vowel consonant|
  835. vowel := 'u'.
  836. consonant := 'z'.
  837. self assert: vowel isVowel equals: true.
  838. self assert: consonant isVowel equals: false
  839. !
  840. testJoin
  841. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  842. !
  843. testReversed
  844. self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
  845. !
  846. testSelect
  847. | newCollection |
  848. newCollection := 'o'.
  849. self
  850. assertSameContents: (self collection select: [ :each |
  851. each = 'o' ])
  852. as: newCollection
  853. !
  854. testSize
  855. self assert: 'smalltalk' size equals: 9.
  856. self assert: '' size equals: 0
  857. !
  858. testStreamContents
  859. self
  860. assert: (String streamContents: [ :aStream |
  861. aStream
  862. nextPutAll: 'hello'; space;
  863. nextPutAll: 'world' ])
  864. equals: 'hello world'
  865. !
  866. testSubStrings
  867. self assert: ('jackiechan' subStrings: 'ie') equals: #( 'jack' 'chan' ).
  868. !
  869. testTrim
  870. self assert: ' jackie' trimLeft equals: 'jackie'.
  871. self assert: 'jackie ' trimRight equals: 'jackie'.
  872. ! !
  873. !StringTest class methodsFor: 'accessing'!
  874. collectionClass
  875. ^ String
  876. ! !
  877. TestCase subclass: #ConsoleTranscriptTest
  878. instanceVariableNames: ''
  879. package: 'Kernel-Tests'!
  880. !ConsoleTranscriptTest methodsFor: 'tests'!
  881. testShow
  882. | originalTranscript |
  883. originalTranscript := Transcript current.
  884. Transcript register: ConsoleTranscript new.
  885. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  886. self shouldnt: [ Transcript show: console ] raise: Error.
  887. Transcript register: originalTranscript.
  888. ! !
  889. TestCase subclass: #JSObjectProxyTest
  890. instanceVariableNames: ''
  891. package: 'Kernel-Tests'!
  892. !JSObjectProxyTest methodsFor: 'accessing'!
  893. jsObject
  894. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': void 0}>
  895. ! !
  896. !JSObjectProxyTest methodsFor: 'tests'!
  897. testAtIfAbsent
  898. | testObject |
  899. testObject := self jsObject.
  900. self assert: (testObject at: 'abc' ifAbsent: [ 'Property does not exist' ]) equals: 'Property does not exist'.
  901. self assert: (testObject at: 'e' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  902. self assert: (testObject at: 'a' ifAbsent: [ 'Property does not exist' ]) equals: 1.
  903. self assert: (testObject at: 'f' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  904. !
  905. testAtIfPresent
  906. | testObject |
  907. testObject := self jsObject.
  908. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  909. self assert: (testObject at: 'e' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  910. self assert: (testObject at: 'a' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello 1'.
  911. self assert: (testObject at: 'f' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  912. !
  913. testAtIfPresentIfAbsent
  914. | testObject |
  915. testObject := self jsObject.
  916. self assert: (testObject at: 'abc' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'not present'.
  917. self assert: (testObject at: 'e' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  918. self assert: (testObject at: 'a' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello 1'.
  919. self assert: (testObject at: 'f' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  920. !
  921. testDNU
  922. self should: [ self jsObject foo ] raise: MessageNotUnderstood
  923. !
  924. testMessageSend
  925. self assert: self jsObject a equals: 1.
  926. self assert: self jsObject b equals: 2.
  927. self assert: (self jsObject c: 3) equals: 3
  928. !
  929. testMethodWithArguments
  930. self assert: (self jsObject c: 1) equals: 1
  931. !
  932. testPrinting
  933. self assert: self jsObject printString equals: '[object Object]'
  934. !
  935. testPropertyThatReturnsEmptyString
  936. | object |
  937. object := self jsObject.
  938. self assert: object d equals: ''.
  939. object d: 'hello'.
  940. self assert: object d equals: 'hello'
  941. !
  942. testPropertyThatReturnsUndefined
  943. | object |
  944. object := self jsObject.
  945. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  946. self assert: object e isNil
  947. !
  948. testValue
  949. | testObject |
  950. testObject := self jsObject.
  951. self assert: testObject value printString equals: '[object Object]'.
  952. testObject at: 'value' put: 'aValue'.
  953. self assert: testObject value equals: 'aValue'
  954. !
  955. testYourself
  956. | object |
  957. object := self jsObject
  958. d: 'test';
  959. yourself.
  960. self assert: object d equals: 'test'
  961. ! !
  962. TestCase subclass: #JavaScriptExceptionTest
  963. instanceVariableNames: ''
  964. package: 'Kernel-Tests'!
  965. !JavaScriptExceptionTest methodsFor: 'helpers'!
  966. throwException
  967. <throw 'test'>
  968. ! !
  969. !JavaScriptExceptionTest methodsFor: 'tests'!
  970. testCatchingException
  971. [ self throwException ]
  972. on: Error
  973. do: [ :error |
  974. self assert: error exception = 'test' ]
  975. !
  976. testRaisingException
  977. self should: [ self throwException ] raise: JavaScriptException
  978. ! !
  979. TestCase subclass: #MessageSendTest
  980. instanceVariableNames: ''
  981. package: 'Kernel-Tests'!
  982. !MessageSendTest methodsFor: 'tests'!
  983. testValue
  984. | messageSend |
  985. messageSend := MessageSend new
  986. receiver: Object new;
  987. selector: #asString;
  988. yourself.
  989. self assert: messageSend value equals: 'an Object'
  990. !
  991. testValueWithArguments
  992. | messageSend |
  993. messageSend := MessageSend new
  994. receiver: 2;
  995. selector: '+';
  996. yourself.
  997. self assert: (messageSend value: 3) equals: 5.
  998. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  999. ! !
  1000. TestCase subclass: #MethodInheritanceTest
  1001. instanceVariableNames: 'receiverTop receiverMiddle receiverBottom method performBlock'
  1002. package: 'Kernel-Tests'!
  1003. !MethodInheritanceTest methodsFor: 'accessing'!
  1004. codeGeneratorClass
  1005. ^ CodeGenerator
  1006. !
  1007. targetClassBottom
  1008. ^ JavaScriptException
  1009. !
  1010. targetClassMiddle
  1011. ^ Error
  1012. !
  1013. targetClassTop
  1014. ^ Object
  1015. ! !
  1016. !MethodInheritanceTest methodsFor: 'factory'!
  1017. compiler
  1018. ^ Compiler new
  1019. codeGeneratorClass: self codeGeneratorClass;
  1020. yourself
  1021. ! !
  1022. !MethodInheritanceTest methodsFor: 'initialization'!
  1023. setUp
  1024. receiverTop := self targetClassTop new.
  1025. receiverMiddle := self targetClassMiddle new.
  1026. receiverBottom := self targetClassBottom new.
  1027. method := nil.
  1028. performBlock := [ self error: 'performBlock not initialized' ]
  1029. !
  1030. tearDown
  1031. [ self deinstallTop ] on: Error do: [ ].
  1032. [ self deinstallMiddle ] on: Error do: [ ].
  1033. [ self deinstallBottom ] on: Error do: [ ]
  1034. ! !
  1035. !MethodInheritanceTest methodsFor: 'testing'!
  1036. deinstallBottom
  1037. self targetClassBottom removeCompiledMethod: method
  1038. !
  1039. deinstallMiddle
  1040. self targetClassMiddle removeCompiledMethod: method
  1041. !
  1042. deinstallTop
  1043. self targetClassTop removeCompiledMethod: method
  1044. !
  1045. installBottom: aString
  1046. method := self compiler install: aString forClass: self targetClassBottom category: 'tests'
  1047. !
  1048. installMiddle: aString
  1049. method := self compiler install: aString forClass: self targetClassMiddle category: 'tests'
  1050. !
  1051. installTop: aString
  1052. method := self compiler install: aString forClass: self targetClassTop category: 'tests'
  1053. !
  1054. shouldMNU
  1055. self shouldMNUTop.
  1056. self shouldMNUMiddle.
  1057. self shouldMNUBottom
  1058. !
  1059. shouldMNUBottom
  1060. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  1061. !
  1062. shouldMNUMiddle
  1063. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  1064. !
  1065. shouldMNUTop
  1066. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  1067. !
  1068. shouldReturn: anObject
  1069. | result |
  1070. result := performBlock value: receiverTop.
  1071. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1072. result := performBlock value: receiverMiddle.
  1073. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  1074. result := performBlock value: receiverBottom.
  1075. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  1076. !
  1077. shouldReturn: anObject and: anObject2 and: anObject3
  1078. | result |
  1079. result := performBlock value: receiverTop.
  1080. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1081. result := performBlock value: receiverMiddle.
  1082. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  1083. result := performBlock value: receiverBottom.
  1084. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  1085. ! !
  1086. !MethodInheritanceTest methodsFor: 'tests'!
  1087. testMNU11
  1088. performBlock := [ :x | x foo ].
  1089. self shouldMNU.
  1090. self installTop: 'foo ^ false'.
  1091. self installTop: 'foo ^ true'.
  1092. self deinstallTop.
  1093. self shouldMNU
  1094. !
  1095. testMNU22
  1096. performBlock := [ :x | x foo ].
  1097. self shouldMNU.
  1098. self installMiddle: 'foo ^ false'.
  1099. self installMiddle: 'foo ^ true'.
  1100. self deinstallMiddle.
  1101. self shouldMNU
  1102. !
  1103. testReturns1
  1104. performBlock := [ :x | x foo ].
  1105. self installTop: 'foo ^ false'.
  1106. self shouldReturn: false.
  1107. self installTop: 'foo ^ true'.
  1108. self shouldReturn: true
  1109. ! !
  1110. TestCase subclass: #NumberTest
  1111. instanceVariableNames: ''
  1112. package: 'Kernel-Tests'!
  1113. !NumberTest methodsFor: 'tests'!
  1114. testAbs
  1115. self assert: 4 abs equals: 4.
  1116. self assert: -4 abs equals: 4
  1117. !
  1118. testArithmetic
  1119. "We rely on JS here, so we won't test complex behavior, just check if
  1120. message sends are corrects"
  1121. self assert: 1.5 + 1 equals: 2.5.
  1122. self assert: 2 - 1 equals: 1.
  1123. self assert: -2 - 1 equals: -3.
  1124. self assert: 12 / 2 equals: 6.
  1125. self assert: 3 * 4 equals: 12.
  1126. self assert: 7 // 2 equals: 3.
  1127. self assert: 7 \\ 2 equals: 1.
  1128. "Simple parenthesis and execution order"
  1129. self assert: 1 + 2 * 3 equals: 9.
  1130. self assert: 1 + (2 * 3) equals: 7
  1131. !
  1132. testAsNumber
  1133. self assert: 3 asNumber equals: 3.
  1134. !
  1135. testCeiling
  1136. self assert: 1.2 ceiling equals: 2.
  1137. self assert: -1.2 ceiling equals: -1.
  1138. self assert: 1.0 ceiling equals: 1.
  1139. !
  1140. testComparison
  1141. self assert: 3 > 2.
  1142. self assert: 2 < 3.
  1143. self deny: 3 < 2.
  1144. self deny: 2 > 3.
  1145. self assert: 3 >= 3.
  1146. self assert: 3.1 >= 3.
  1147. self assert: 3 <= 3.
  1148. self assert: 3 <= 3.1
  1149. !
  1150. testCopying
  1151. self assert: 1 copy == 1.
  1152. self assert: 1 deepCopy == 1
  1153. !
  1154. testEquality
  1155. self assert: (1 = 1).
  1156. self assert: (0 = 0).
  1157. self deny: (1 = 0).
  1158. self assert: (1 yourself = 1).
  1159. self assert: (1 = 1 yourself).
  1160. self assert: (1 yourself = 1 yourself).
  1161. self deny: 0 = false.
  1162. self deny: false = 0.
  1163. self deny: '' = 0.
  1164. self deny: 0 = ''
  1165. !
  1166. testFloor
  1167. self assert: 1.2 floor equals: 1.
  1168. self assert: -1.2 floor equals: -2.
  1169. self assert: 1.0 floor equals: 1.
  1170. !
  1171. testHexNumbers
  1172. self assert: 16r9 equals: 9.
  1173. self assert: 16rA truncated equals: 10.
  1174. self assert: 16rB truncated equals: 11.
  1175. self assert: 16rC truncated equals: 12.
  1176. self assert: 16rD truncated equals: 13.
  1177. self assert: 16rE truncated equals: 14.
  1178. self assert: 16rF truncated equals: 15
  1179. !
  1180. testIdentity
  1181. self assert: 1 == 1.
  1182. self assert: 0 == 0.
  1183. self deny: 1 == 0.
  1184. self assert: 1 yourself == 1.
  1185. self assert: 1 == 1 yourself.
  1186. self assert: 1 yourself == 1 yourself.
  1187. self deny: 1 == 2
  1188. !
  1189. testInvalidHexNumbers
  1190. self should: [ 16rG ] raise: MessageNotUnderstood.
  1191. self should: [ 16rg ] raise: MessageNotUnderstood.
  1192. self should: [ 16rH ] raise: MessageNotUnderstood.
  1193. self should: [ 16rh ] raise: MessageNotUnderstood.
  1194. self should: [ 16rI ] raise: MessageNotUnderstood.
  1195. self should: [ 16ri ] raise: MessageNotUnderstood.
  1196. self should: [ 16rJ ] raise: MessageNotUnderstood.
  1197. self should: [ 16rj ] raise: MessageNotUnderstood.
  1198. self should: [ 16rK ] raise: MessageNotUnderstood.
  1199. self should: [ 16rk ] raise: MessageNotUnderstood.
  1200. self should: [ 16rL ] raise: MessageNotUnderstood.
  1201. self should: [ 16rl ] raise: MessageNotUnderstood.
  1202. self should: [ 16rM ] raise: MessageNotUnderstood.
  1203. self should: [ 16rm ] raise: MessageNotUnderstood.
  1204. self should: [ 16rN ] raise: MessageNotUnderstood.
  1205. self should: [ 16rn ] raise: MessageNotUnderstood.
  1206. self should: [ 16rO ] raise: MessageNotUnderstood.
  1207. self should: [ 16ro ] raise: MessageNotUnderstood.
  1208. self should: [ 16rP ] raise: MessageNotUnderstood.
  1209. self should: [ 16rp ] raise: MessageNotUnderstood.
  1210. self should: [ 16rQ ] raise: MessageNotUnderstood.
  1211. self should: [ 16rq ] raise: MessageNotUnderstood.
  1212. self should: [ 16rR ] raise: MessageNotUnderstood.
  1213. self should: [ 16rr ] raise: MessageNotUnderstood.
  1214. self should: [ 16rS ] raise: MessageNotUnderstood.
  1215. self should: [ 16rs ] raise: MessageNotUnderstood.
  1216. self should: [ 16rT ] raise: MessageNotUnderstood.
  1217. self should: [ 16rt ] raise: MessageNotUnderstood.
  1218. self should: [ 16rU ] raise: MessageNotUnderstood.
  1219. self should: [ 16ru ] raise: MessageNotUnderstood.
  1220. self should: [ 16rV ] raise: MessageNotUnderstood.
  1221. self should: [ 16rv ] raise: MessageNotUnderstood.
  1222. self should: [ 16rW ] raise: MessageNotUnderstood.
  1223. self should: [ 16rw ] raise: MessageNotUnderstood.
  1224. self should: [ 16rX ] raise: MessageNotUnderstood.
  1225. self should: [ 16rx ] raise: MessageNotUnderstood.
  1226. self should: [ 16rY ] raise: MessageNotUnderstood.
  1227. self should: [ 16ry ] raise: MessageNotUnderstood.
  1228. self should: [ 16rZ ] raise: MessageNotUnderstood.
  1229. self should: [ 16rz ] raise: MessageNotUnderstood.
  1230. self should: [ 16rABcdEfZ ] raise: MessageNotUnderstood.
  1231. !
  1232. testLog
  1233. self assert: 10000 log equals: 4.
  1234. self assert: (512 log: 2) equals: 9.
  1235. self assert: Number e ln equals: 1.
  1236. !
  1237. testMinMax
  1238. self assert: (2 max: 5) equals: 5.
  1239. self assert: (2 min: 5) equals: 2
  1240. !
  1241. testNegated
  1242. self assert: 3 negated equals: -3.
  1243. self assert: -3 negated equals: 3
  1244. !
  1245. testPrintShowingDecimalPlaces
  1246. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1247. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1248. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1249. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1250. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1251. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1252. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1253. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1254. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1255. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1256. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1257. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  1258. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  1259. !
  1260. testRaisedTo
  1261. self assert: (2 raisedTo: 4) equals: 16.
  1262. self assert: (2 raisedTo: 0) equals: 1.
  1263. self assert: (2 raisedTo: -3) equals: 0.125.
  1264. self assert: (4 raisedTo: 0.5) equals: 2.
  1265. self assert: 2 ** 4 equals: 16.
  1266. !
  1267. testRounded
  1268. self assert: 3 rounded equals: 3.
  1269. self assert: 3.212 rounded equals: 3.
  1270. self assert: 3.51 rounded equals: 4
  1271. !
  1272. testSign
  1273. self assert: 5 sign equals: 1.
  1274. self assert: 0 sign equals: 0.
  1275. self assert: -1.4 sign equals: -1.
  1276. !
  1277. testSqrt
  1278. self assert: 4 sqrt equals: 2.
  1279. self assert: 16 sqrt equals: 4
  1280. !
  1281. testSquared
  1282. self assert: 4 squared equals: 16
  1283. !
  1284. testTimesRepeat
  1285. | i |
  1286. i := 0.
  1287. 0 timesRepeat: [ i := i + 1 ].
  1288. self assert: i equals: 0.
  1289. 5 timesRepeat: [ i := i + 1 ].
  1290. self assert: i equals: 5
  1291. !
  1292. testTo
  1293. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  1294. !
  1295. testToBy
  1296. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  1297. self should: [ 1 to: 4 by: 0 ] raise: Error
  1298. !
  1299. testTrigonometry
  1300. self assert: 0 cos equals: 1.
  1301. self assert: 0 sin equals: 0.
  1302. self assert: 0 tan equals: 0.
  1303. self assert: 1 arcCos equals: 0.
  1304. self assert: 0 arcSin equals: 0.
  1305. self assert: 0 arcTan equals: 0.
  1306. !
  1307. testTruncated
  1308. self assert: 3 truncated equals: 3.
  1309. self assert: 3.212 truncated equals: 3.
  1310. self assert: 3.51 truncated equals: 3
  1311. ! !
  1312. Object subclass: #ObjectMock
  1313. instanceVariableNames: 'foo bar'
  1314. package: 'Kernel-Tests'!
  1315. !ObjectMock commentStamp!
  1316. ObjectMock is there only to perform tests on classes.!
  1317. !ObjectMock methodsFor: 'not yet classified'!
  1318. foo
  1319. ^ foo
  1320. !
  1321. foo: anObject
  1322. foo := anObject
  1323. ! !
  1324. TestCase subclass: #ObjectTest
  1325. instanceVariableNames: ''
  1326. package: 'Kernel-Tests'!
  1327. !ObjectTest methodsFor: 'tests'!
  1328. notDefined
  1329. <return void 0;>
  1330. !
  1331. testBasicAccess
  1332. | o |
  1333. o := Object new.
  1334. o basicAt: 'a' put: 1.
  1335. self assert: (o basicAt: 'a') equals: 1.
  1336. self assert: (o basicAt: 'b') equals: nil
  1337. !
  1338. testBasicPerform
  1339. | o |
  1340. o := Object new.
  1341. o basicAt: 'func' put: [ 'hello' ].
  1342. o basicAt: 'func2' put: [ :a | a + 1 ].
  1343. self assert: (o basicPerform: 'func') equals: 'hello'.
  1344. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1345. !
  1346. testDNU
  1347. self should: [ Object new foo ] raise: MessageNotUnderstood
  1348. !
  1349. testEquality
  1350. | o |
  1351. o := Object new.
  1352. self deny: o = Object new.
  1353. self assert: (o = o).
  1354. self assert: (o yourself = o).
  1355. self assert: (o = o yourself)
  1356. !
  1357. testHalt
  1358. self should: [ Object new halt ] raise: Error
  1359. !
  1360. testIdentity
  1361. | o |
  1362. o := Object new.
  1363. self deny: o == Object new.
  1364. self assert: o == o.
  1365. self assert: o yourself == o.
  1366. self assert: o == o yourself
  1367. !
  1368. testIfNil
  1369. self deny: Object new isNil.
  1370. self deny: (Object new ifNil: [ true ]) = true.
  1371. self assert: (Object new ifNotNil: [ true ]) equals: true.
  1372. self assert: (Object new ifNil: [ false ] ifNotNil: [ true ]) equals: true.
  1373. self assert: (Object new ifNotNil: [ true ] ifNil: [ false ]) equals: true
  1374. !
  1375. testInstVars
  1376. | o |
  1377. o := ObjectMock new.
  1378. self assert: (o instVarAt: #foo) equals: nil.
  1379. o instVarAt: #foo put: 1.
  1380. self assert: (o instVarAt: #foo) equals: 1.
  1381. self assert: (o instVarAt: 'foo') equals: 1
  1382. !
  1383. testNilUndefined
  1384. "nil in Smalltalk is the undefined object in JS"
  1385. self assert: self notDefined equals: nil
  1386. !
  1387. testYourself
  1388. | o |
  1389. o := ObjectMock new.
  1390. self assert: o yourself == o
  1391. !
  1392. testidentityHash
  1393. | o1 o2 |
  1394. o1 := Object new.
  1395. o2 := Object new.
  1396. self assert: o1 identityHash == o1 identityHash.
  1397. self deny: o1 identityHash == o2 identityHash
  1398. ! !
  1399. TestCase subclass: #PointTest
  1400. instanceVariableNames: ''
  1401. package: 'Kernel-Tests'!
  1402. !PointTest methodsFor: 'tests'!
  1403. testAccessing
  1404. self assert: (Point x: 3 y: 4) x equals: 3.
  1405. self assert: (Point x: 3 y: 4) y equals: 4.
  1406. self assert: (Point new x: 3) x equals: 3.
  1407. self assert: (Point new y: 4) y equals: 4
  1408. !
  1409. testArithmetic
  1410. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1411. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1412. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1413. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1414. !
  1415. testAt
  1416. self assert: 3@4 equals: (Point x: 3 y: 4)
  1417. !
  1418. testEgality
  1419. self assert: (3@4 = (3@4)).
  1420. self deny: 3@5 = (3@6)
  1421. !
  1422. testNew
  1423. self assert: (Point new x: 3) y equals: nil.
  1424. self deny: (Point new x: 3) x = 0.
  1425. self assert: (Point new y: 4) x equals: nil.
  1426. self deny: (Point new y: 4) y = 0
  1427. !
  1428. testTranslateBy
  1429. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1430. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1431. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1432. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1433. ! !
  1434. TestCase subclass: #QueueTest
  1435. instanceVariableNames: ''
  1436. package: 'Kernel-Tests'!
  1437. !QueueTest methodsFor: 'tests'!
  1438. testNextIfAbsent
  1439. | queue |
  1440. queue := Queue new.
  1441. queue nextPut: 'index1'.
  1442. self assert: (queue nextIfAbsent: 'empty') = 'index1'.
  1443. self deny: (queue nextIfAbsent: 'empty') = 'index1'
  1444. !
  1445. testQueueNext
  1446. | queue |
  1447. queue := Queue new.
  1448. queue
  1449. nextPut: 'index1';
  1450. nextPut: 'index2'.
  1451. self assert: queue next = 'index1'.
  1452. self deny: queue next = 'index'.
  1453. self should: [ queue next ] raise: Error
  1454. ! !
  1455. TestCase subclass: #RandomTest
  1456. instanceVariableNames: ''
  1457. package: 'Kernel-Tests'!
  1458. !RandomTest methodsFor: 'tests'!
  1459. testAtRandomNumber
  1460. |val|
  1461. 100 timesRepeat: [
  1462. val := 10 atRandom.
  1463. self assert: (val > 0).
  1464. self assert: (val <11)
  1465. ]
  1466. !
  1467. testAtRandomSequenceableCollection
  1468. |val|
  1469. 100 timesRepeat: [
  1470. val := 'abc' atRandom.
  1471. self assert: ((val = 'a') | (val = 'b') | (val = 'c' )).
  1472. ].
  1473. !
  1474. textNext
  1475. 10000 timesRepeat: [
  1476. | current next |
  1477. next := Random new next.
  1478. self assert: (next >= 0).
  1479. self assert: (next < 1).
  1480. self deny: current = next.
  1481. next = current ]
  1482. ! !
  1483. TestCase subclass: #SetTest
  1484. instanceVariableNames: ''
  1485. package: 'Kernel-Tests'!
  1486. !SetTest methodsFor: 'tests'!
  1487. testAddRemove
  1488. | set |
  1489. set := Set new.
  1490. self assert: set isEmpty.
  1491. set add: 3.
  1492. self assert: (set includes: 3).
  1493. set add: 5.
  1494. self assert: (set includes: 5).
  1495. set remove: 3.
  1496. self deny: (set includes: 3)
  1497. !
  1498. testAt
  1499. self should: [ Set new at: 1 put: 2 ] raise: Error
  1500. !
  1501. testCollect
  1502. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1503. !
  1504. testComparing
  1505. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1506. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1507. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1508. self deny: #(1 2) asSet = #(0 2) asSet
  1509. !
  1510. testPrintString
  1511. | set |
  1512. set := Set new.
  1513. self assert: set printString equals: 'a Set ()'.
  1514. set add: 1; add: 3.
  1515. self assert: set printString equals: 'a Set (1 3)'.
  1516. set add: 'foo'.
  1517. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1518. set remove: 1; remove: 3.
  1519. self assert: set printString equals: 'a Set (''foo'')'.
  1520. set add: 3.
  1521. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1522. set add: 3.
  1523. self assert: set printString equals: 'a Set (''foo'' 3)'
  1524. !
  1525. testSize
  1526. self assert: Set new size equals: 0.
  1527. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1528. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1529. !
  1530. testUnboxedObjects
  1531. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1532. !
  1533. testUnicity
  1534. | set |
  1535. set := Set new.
  1536. set add: 21.
  1537. set add: 'hello'.
  1538. set add: 21.
  1539. self assert: set size equals: 2.
  1540. set add: 'hello'.
  1541. self assert: set size equals: 2.
  1542. self assert: set asArray equals: #(21 'hello')
  1543. ! !
  1544. TestCase subclass: #StreamTest
  1545. instanceVariableNames: ''
  1546. package: 'Kernel-Tests'!
  1547. !StreamTest methodsFor: 'accessing'!
  1548. collectionClass
  1549. ^ self class collectionClass
  1550. !
  1551. newCollection
  1552. ^ self collectionClass new
  1553. !
  1554. newStream
  1555. ^ self collectionClass new stream
  1556. ! !
  1557. !StreamTest methodsFor: 'tests'!
  1558. testAtStartAtEnd
  1559. | stream |
  1560. stream := self newStream.
  1561. self assert: stream atStart.
  1562. self assert: stream atEnd.
  1563. stream nextPutAll: self newCollection.
  1564. self assert: stream atEnd.
  1565. self deny: stream atStart.
  1566. stream position: 1.
  1567. self deny: stream atEnd.
  1568. self deny: stream atStart
  1569. !
  1570. testContents
  1571. | stream |
  1572. stream := self newStream.
  1573. stream nextPutAll: self newCollection.
  1574. self assert: stream contents equals: self newCollection
  1575. !
  1576. testIsEmpty
  1577. | stream |
  1578. stream := self newStream.
  1579. self assert: stream isEmpty.
  1580. stream nextPutAll: self newCollection.
  1581. self deny: stream isEmpty
  1582. !
  1583. testPosition
  1584. | collection stream |
  1585. collection := self newCollection.
  1586. stream := self newStream.
  1587. stream nextPutAll: collection.
  1588. self assert: stream position equals: collection size.
  1589. stream position: 0.
  1590. self assert: stream position equals: 0.
  1591. stream next.
  1592. self assert: stream position equals: 1.
  1593. stream next.
  1594. self assert: stream position equals: 2
  1595. !
  1596. testReading
  1597. | stream collection |
  1598. collection := self newCollection.
  1599. stream := self newStream.
  1600. stream
  1601. nextPutAll: collection;
  1602. position: 0.
  1603. collection do: [ :each |
  1604. self assert: stream next equals: each ].
  1605. self assert: stream next isNil
  1606. !
  1607. testStreamContents
  1608. !
  1609. testWrite
  1610. | stream collection |
  1611. collection := self newCollection.
  1612. stream := self newStream.
  1613. collection do: [ :each | stream << each ].
  1614. self assert: stream contents equals: collection
  1615. !
  1616. testWriting
  1617. | stream collection |
  1618. collection := self newCollection.
  1619. stream := self newStream.
  1620. collection do: [ :each | stream nextPut: each ].
  1621. self assert: stream contents equals: collection.
  1622. stream := self newStream.
  1623. stream nextPutAll: collection.
  1624. self assert: stream contents equals: collection
  1625. ! !
  1626. !StreamTest class methodsFor: 'accessing'!
  1627. collectionClass
  1628. ^ nil
  1629. ! !
  1630. !StreamTest class methodsFor: 'testing'!
  1631. isAbstract
  1632. ^ self collectionClass isNil
  1633. ! !
  1634. StreamTest subclass: #ArrayStreamTest
  1635. instanceVariableNames: ''
  1636. package: 'Kernel-Tests'!
  1637. !ArrayStreamTest methodsFor: 'accessing'!
  1638. newCollection
  1639. ^ { true. 1. 3@4. 'foo' }
  1640. ! !
  1641. !ArrayStreamTest class methodsFor: 'accessing'!
  1642. collectionClass
  1643. ^ Array
  1644. ! !
  1645. StreamTest subclass: #StringStreamTest
  1646. instanceVariableNames: ''
  1647. package: 'Kernel-Tests'!
  1648. !StringStreamTest methodsFor: 'accessing'!
  1649. newCollection
  1650. ^ 'hello world'
  1651. ! !
  1652. !StringStreamTest class methodsFor: 'accessing'!
  1653. collectionClass
  1654. ^ String
  1655. ! !
  1656. TestCase subclass: #UndefinedTest
  1657. instanceVariableNames: ''
  1658. package: 'Kernel-Tests'!
  1659. !UndefinedTest methodsFor: 'tests'!
  1660. testCopying
  1661. self assert: nil copy equals: nil
  1662. !
  1663. testDeepCopy
  1664. self assert: nil deepCopy = nil
  1665. !
  1666. testIfNil
  1667. self assert: (nil ifNil: [ true ]) equals: true.
  1668. self deny: (nil ifNotNil: [ true ]) = true.
  1669. self assert: (nil ifNil: [ true ] ifNotNil: [ false ]) equals: true.
  1670. self deny: (nil ifNotNil: [ true ] ifNil: [ false ]) = true
  1671. !
  1672. testIsNil
  1673. self assert: nil isNil.
  1674. self deny: nil notNil.
  1675. ! !