Kernel-Tests.st 74 KB

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