Kernel-Tests.st 55 KB

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