Kernel-Tests.st 60 KB

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