Kernel-Tests.st 63 KB

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