Kernel-Tests.st 75 KB

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