Kernel-Tests.st 51 KB

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