Kernel-Tests.st 75 KB

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