Kernel-Tests.st 52 KB

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