Kernel-Tests.st 78 KB

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