Kernel-Tests.st 74 KB

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