Kernel-Tests.st 63 KB

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