Kernel-Tests.st 62 KB

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