1
0

Kernel-Tests.st 70 KB

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