Kernel-Tests.st 76 KB

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