2
0

Kernel-Tests.st 62 KB

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