Kernel-Tests.st 71 KB

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