Kernel-Tests.st 50 KB

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