Kernel-Tests.st 74 KB

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