Kernel-Tests.st 74 KB

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