1
0

Kernel-Tests.st 52 KB

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