Kernel-Tests.st 63 KB

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