Kernel-Tests.st 70 KB

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