Kernel-Tests.st 66 KB

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