Kernel-Tests.st 76 KB

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