Kernel-Tests.st 59 KB

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