1
0

Kernel-Tests.st 49 KB

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