Kernel-Tests.st 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  1. Smalltalk current 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 current 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 current at: 'ObjectMock2') new.
  251. "Change the superclass of ObjectMock2"
  252. ObjectMock subclass: (Smalltalk current 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 current at: instance class name) isNil.
  265. Smalltalk current 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 current at: 'ObjectMock2')
  272. instanceVariableNames: ''
  273. package: 'Kernel-Tests'.
  274. self assert: ObjectMock2 class instanceVariableNames equals: #('foo' 'bar').
  275. Smalltalk current 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 current 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 current 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. collectionWithDuplicates
  312. "Answers pre-filled collection of type tested,
  313. with exactly five distinct elements,
  314. some of them appearing multiple times."
  315. self subclassResponsibility
  316. !
  317. sampleNonValue
  318. "Answer value that is guaranteed
  319. not to be contained by self collection"
  320. ^ 999
  321. ! !
  322. !CollectionTest methodsFor: 'testing'!
  323. isCollectionReadOnly
  324. ^ false
  325. ! !
  326. !CollectionTest methodsFor: 'tests'!
  327. testAllSatisfy
  328. | collection anyOne |
  329. collection := self collection.
  330. anyOne := collection anyOne.
  331. self assert: (collection allSatisfy: [ :each | collection includes: each ]).
  332. self deny: (collection allSatisfy: [ :each | each ~= anyOne ])
  333. !
  334. testAnyOne
  335. self should: [ self collectionClass new anyOne ] raise: Error.
  336. self assert: (self collection includes: self collection anyOne)
  337. !
  338. testAnySatisfy
  339. | anyOne |
  340. anyOne := self collection anyOne.
  341. self assert: (self collection anySatisfy: [ :each | each = anyOne ]).
  342. self deny: (self collection anySatisfy: [ :each | each = Object new ])
  343. !
  344. testAsArray
  345. self
  346. assertSameContents: self collection
  347. as: self collection asArray
  348. !
  349. testAsOrderedCollection
  350. self
  351. assertSameContents: self collection
  352. as: self collection asOrderedCollection
  353. !
  354. testAsSet
  355. | c set |
  356. c := self collectionWithDuplicates.
  357. set := c asSet.
  358. self assert: set size equals: 5.
  359. c do: [ :each |
  360. self assert: (set includes: each) ]
  361. !
  362. testCollect
  363. | newCollection |
  364. newCollection := #(1 2 3 4).
  365. self
  366. assertSameContents: (self collection collect: [ :each |
  367. each abs ])
  368. as: newCollection
  369. !
  370. testDetect
  371. self assert: (self collection detect: [ :each | each < 0 ]) equals: -4.
  372. self
  373. should: [ self collection detect: [ :each | each = 6 ] ]
  374. raise: Error
  375. !
  376. testDo
  377. | newCollection |
  378. newCollection := OrderedCollection new.
  379. self collection do: [ :each |
  380. newCollection add: each ].
  381. self
  382. assertSameContents: self collection
  383. as: newCollection.
  384. newCollection := OrderedCollection new.
  385. self collectionWithDuplicates do: [ :each |
  386. newCollection add: each ].
  387. self
  388. assertSameContents: self collectionWithDuplicates
  389. as: newCollection
  390. !
  391. testIfEmptyFamily
  392. self assert: (self collectionClass new ifEmpty: [ 42 ]) equals: 42.
  393. self assert: (self collection ifEmpty: [ 42 ]) equals: self collection.
  394. self assert: (self collectionClass new ifNotEmpty: [ 42 ]) equals: self collectionClass new.
  395. self assert: (self collection ifNotEmpty: [ 42 ]) equals: 42.
  396. self assert: (self collectionClass new ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 42.
  397. self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 999.
  398. self assert: (self collectionClass new ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 999.
  399. self assert: (self collection ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 42
  400. !
  401. testIsEmpty
  402. self assert: self collectionClass new isEmpty.
  403. self deny: self collection isEmpty
  404. !
  405. testNoneSatisfy
  406. | anyOne |
  407. anyOne := self collection anyOne.
  408. self deny: (self collection noneSatisfy: [ :each | each = anyOne ]).
  409. self assert: (self collection noneSatisfy: [ :each | each = Object new ])
  410. !
  411. testRemoveAll
  412. self assert: (self collection removeAll; yourself) equals: self collectionClass new
  413. !
  414. testSelect
  415. | newCollection |
  416. newCollection := #(2 -4).
  417. self
  418. assertSameContents: (self collection select: [ :each |
  419. each even ])
  420. as: newCollection
  421. !
  422. testSize
  423. self assert: self collectionClass new size equals: 0.
  424. self assert: self collection size equals: 4
  425. ! !
  426. !CollectionTest class methodsFor: 'fixture'!
  427. collectionClass
  428. "Answers class of collection type tested"
  429. self subclassResponsibility
  430. ! !
  431. !CollectionTest class methodsFor: 'testing'!
  432. isAbstract
  433. ^ self collectionClass isNil
  434. ! !
  435. CollectionTest subclass: #IndexableCollectionTest
  436. instanceVariableNames: ''
  437. package: 'Kernel-Tests'!
  438. !IndexableCollectionTest methodsFor: 'fixture'!
  439. collectionWithNewValue
  440. "Answers a collection which shows how
  441. self collection would look after adding
  442. self sampleNewValue at self sampleNewIndex"
  443. self subclassResponsibility
  444. !
  445. sampleNewIndex
  446. "Answers a value that can be used as index in at:put: or at:ifAbsentPut:"
  447. self subclassResponsibility
  448. !
  449. sampleNewValue
  450. "Answers a value that is not yet there
  451. and can be put into a tested collection"
  452. ^ 'N'
  453. !
  454. sampleNonIndexesDo: aBlock
  455. "Executes block a few times,
  456. each time passing value that is known
  457. not to be an index, as the first parameter"
  458. self subclassResponsibility
  459. !
  460. samplesDo: aBlock
  461. "Executes block a few times,
  462. each time passing known index and value stored
  463. under that index as the parameters"
  464. self subclassResponsibility
  465. ! !
  466. !IndexableCollectionTest methodsFor: 'tests'!
  467. testAt
  468. self nonIndexesDo: [ :each |
  469. self should: [ self collection at: each ] raise: Error ].
  470. self samplesDo: [ :index :value |
  471. self assert: (self collection at: index) equals: value ]
  472. !
  473. testAtIfAbsent
  474. self nonIndexesDo: [ :each |
  475. self assert: (self collection at: each ifAbsent: [ self sampleNonValue ]) equals: self sampleNonValue ].
  476. self samplesDo: [ :index :value |
  477. self assert: (self collection at: index ifAbsent: [ self sampleNonValue ]) equals: value ].
  478. !
  479. testAtIfPresent
  480. | visited sentinel |
  481. sentinel := Object new.
  482. self nonIndexesDo: [ :each |
  483. visited := nil.
  484. self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ]) equals: nil.
  485. self assert: visited isNil ].
  486. self samplesDo: [ :index :value |
  487. visited := nil.
  488. self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ]) equals: sentinel.
  489. self assert: visited equals: (self collection at: index) ]
  490. !
  491. testAtIfPresentIfAbsent
  492. | visited sentinel |
  493. sentinel := Object new.
  494. self nonIndexesDo: [ :each |
  495. visited := nil.
  496. self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ] ifAbsent: [ self sampleNonValue ] ) equals: self sampleNonValue.
  497. self assert: visited isNil ].
  498. self samplesDo: [ :index :value |
  499. visited := nil.
  500. self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ] ifAbsent: [ self sampleNonValue ]) equals: sentinel.
  501. self assert: visited equals: (self collection at: index) ]
  502. !
  503. testAtPut
  504. | newCollection |
  505. newCollection := self collection.
  506. self samplesDo: [ :index :value |
  507. newCollection at: index put: value ].
  508. self assert: newCollection equals: self collection.
  509. newCollection at: self sampleNewIndex put: self sampleNewValue.
  510. self assert: newCollection equals: self collectionWithNewValue
  511. !
  512. testIndexOf
  513. self should: [ self collection indexOf: self sampleNonValue ] raise: Error.
  514. self samplesDo: [ :index :value |
  515. self assert: (self collection indexOf: value) equals: index ]
  516. !
  517. testWithIndexDo
  518. | collection |
  519. collection := self collection.
  520. self collection withIndexDo: [ :each :index |
  521. self assert: (collection at: index) equals: each ]
  522. ! !
  523. IndexableCollectionTest subclass: #HashedCollectionTest
  524. instanceVariableNames: ''
  525. package: 'Kernel-Tests'!
  526. !HashedCollectionTest methodsFor: 'fixture'!
  527. collection
  528. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  529. !
  530. collectionWithDuplicates
  531. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
  532. !
  533. collectionWithNewValue
  534. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'new' -> 'N' }
  535. !
  536. nonIndexesDo: aBlock
  537. aBlock value: 5.
  538. aBlock value: 'z'
  539. !
  540. sampleNewIndex
  541. ^ 'new'
  542. !
  543. samplesDo: aBlock
  544. aBlock value: 'a' value: 2
  545. ! !
  546. !HashedCollectionTest methodsFor: 'tests'!
  547. testAsDictionary
  548. self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
  549. !
  550. testFrom
  551. "Accept a collection of associations."
  552. | associations |
  553. associations := { 'a' -> 1. 'b' -> 2 }.
  554. self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  555. !
  556. testNewFromPairs
  557. "Accept an array in which all odd indexes are keys and evens are values."
  558. | flattenedAssociations |
  559. flattenedAssociations := { 'a'. 1. 'b'. 2 }.
  560. self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  561. ! !
  562. !HashedCollectionTest class methodsFor: 'fixture'!
  563. collectionClass
  564. ^ HashedCollection
  565. ! !
  566. HashedCollectionTest subclass: #DictionaryTest
  567. instanceVariableNames: ''
  568. package: 'Kernel-Tests'!
  569. !DictionaryTest methodsFor: 'fixture'!
  570. collection
  571. ^ Dictionary new
  572. at: 1 put: 1;
  573. at: 'a' put: 2;
  574. at: true put: 3;
  575. at: 1@3 put: -4;
  576. yourself
  577. !
  578. collectionWithDuplicates
  579. ^ Dictionary new
  580. at: 1 put: 1;
  581. at: 'a' put: 2;
  582. at: true put: 3;
  583. at: 4 put: -4;
  584. at: 'b' put: 1;
  585. at: 3 put: 3;
  586. at: false put: 12;
  587. yourself
  588. !
  589. collectionWithNewValue
  590. ^ Dictionary new
  591. at: 1 put: 1;
  592. at: 'a' put: 2;
  593. at: true put: 3;
  594. at: 1@3 put: -4;
  595. at: 'new' put: 'N';
  596. yourself
  597. !
  598. samplesDo: aBlock
  599. super samplesDo: aBlock.
  600. aBlock value: true value: 3.
  601. aBlock value: 1@3 value: -4
  602. ! !
  603. !DictionaryTest methodsFor: 'tests'!
  604. testAccessing
  605. | d |
  606. d := Dictionary new.
  607. d at: 'hello' put: 'world'.
  608. self assert: (d at: 'hello') equals: 'world'.
  609. self assert: (d at: 'hello' ifAbsent: [ nil ]) equals: 'world'.
  610. self deny: (d at: 'foo' ifAbsent: [ nil ]) = 'world'.
  611. self assert: (d includesKey: 'hello').
  612. self deny: (d includesKey: 'foo').
  613. d at: 1 put: 2.
  614. self assert: (d at: 1) equals: 2.
  615. d at: 1@3 put: 3.
  616. self assert: (d at: 1@3) equals: 3.
  617. self assert: (d includesKey: 1@3).
  618. self deny: (d includesKey: 3@1)
  619. !
  620. testAsHashedCollection
  621. self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
  622. !
  623. testDynamicDictionaries
  624. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  625. !
  626. testEquality
  627. | d1 d2 |
  628. self assert: (Dictionary new = Dictionary new).
  629. d1 := Dictionary new at: 1 put: 2; yourself.
  630. d2 := Dictionary new at: 1 put: 2; yourself.
  631. self assert: (d1 = d2).
  632. d2 := Dictionary new at: 1 put: 3; yourself.
  633. self deny: d1 = d2.
  634. d2 := Dictionary new at: 2 put: 2; yourself.
  635. self deny: d1 = d2.
  636. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  637. self deny: d1 = d2.
  638. !
  639. testKeys
  640. | d |
  641. d := Dictionary new.
  642. d at: 1 put: 2.
  643. d at: 2 put: 3.
  644. d at: 3 put: 4.
  645. self assert: d keys equals: #(1 2 3)
  646. !
  647. testPointKey
  648. | d |
  649. d := Dictionary new.
  650. d at: 1@1 put: 'foo'.
  651. self assert: (d at: 1@1) equals: 'foo'.
  652. d at: 1@1 put: 'bar'.
  653. self assert: (d at: 1@1) equals: 'bar'.
  654. d removeKey: 1@1.
  655. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  656. self deny: (d includesKey: 1@1)
  657. !
  658. testPrintString
  659. self
  660. assert: (Dictionary new
  661. at:'firstname' put: 'James';
  662. at:'lastname' put: 'Bond';
  663. printString)
  664. equals: 'a Dictionary (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  665. !
  666. testRemoveKey
  667. | d key |
  668. d := Dictionary new.
  669. d at: 1 put: 2.
  670. d at: 2 put: 3.
  671. d at: 3 put: 4.
  672. key := 2.
  673. self assert: d keys equals: #(1 2 3).
  674. d removeKey: key.
  675. self assert: d keys equals: #(1 3).
  676. self assert: d values equals: #(2 4).
  677. self deny: (d includesKey: 2)
  678. !
  679. testRemoveKeyIfAbsent
  680. | d key |
  681. d := Dictionary new.
  682. d at: 1 put: 2.
  683. d at: 2 put: 3.
  684. d at: 3 put: 4.
  685. key := 2.
  686. self assert: (d removeKey: key) equals: 3.
  687. key := 3.
  688. self assert: (d removeKey: key ifAbsent: [ 42 ]) equals: 4.
  689. key := 'why'.
  690. self assert: (d removeKey: key ifAbsent: [ 42 ] ) equals: 42.
  691. !
  692. testSize
  693. | d |
  694. d := Dictionary new.
  695. self assert: d size equals: 0.
  696. d at: 1 put: 2.
  697. self assert: d size equals: 1.
  698. d at: 2 put: 3.
  699. self assert: d size equals: 2.
  700. !
  701. testValues
  702. | d |
  703. d := Dictionary new.
  704. d at: 1 put: 2.
  705. d at: 2 put: 3.
  706. d at: 3 put: 4.
  707. self assert: d values equals: #(2 3 4)
  708. ! !
  709. !DictionaryTest class methodsFor: 'fixture'!
  710. collectionClass
  711. ^ Dictionary
  712. ! !
  713. IndexableCollectionTest subclass: #SequenceableCollectionTest
  714. instanceVariableNames: ''
  715. package: 'Kernel-Tests'!
  716. !SequenceableCollectionTest methodsFor: 'fixture'!
  717. collectionFirst
  718. self subclassResponsibility
  719. !
  720. collectionFirstTwo
  721. self subclassResponsibility
  722. !
  723. collectionLast
  724. self subclassResponsibility
  725. !
  726. collectionLastTwo
  727. self subclassResponsibility
  728. !
  729. nonIndexesDo: aBlock
  730. aBlock value: 0.
  731. aBlock value: self collection size + 1.
  732. aBlock value: 'z'
  733. !
  734. samplesDo: aBlock
  735. aBlock value: 1 value: self collectionFirst.
  736. aBlock value: self collection size value: self collectionLast
  737. ! !
  738. !SequenceableCollectionTest methodsFor: 'tests'!
  739. testBeginsWith
  740. self assert: (self collection beginsWith: self collectionClass new).
  741. self assert: (self collection beginsWith: self collection).
  742. self assert: (self collection beginsWith: self collectionFirstTwo).
  743. self deny: (self collection beginsWith: self collectionLastTwo)
  744. !
  745. testEndsWith
  746. self assert: (self collection endsWith: self collectionClass new).
  747. self assert: (self collection endsWith: self collection).
  748. self assert: (self collection endsWith: self collectionLastTwo).
  749. self deny: (self collection endsWith: self collectionFirstTwo)
  750. !
  751. testFirst
  752. self assert: self collection first equals: self collectionFirst
  753. !
  754. testFirstN
  755. self
  756. assert: (self collection first: 2)
  757. equals: self collectionFirstTwo.
  758. self
  759. assert: (self collection first: 0)
  760. equals: self collectionClass new.
  761. self
  762. assert: (self collection first: self collection size)
  763. equals: self collection.
  764. self should: [ self collection first: 33 ] raise: Error
  765. !
  766. testFourth
  767. self assert: (self collection fourth) equals: (self collection at: 4)
  768. !
  769. testLast
  770. self assert: self collection last equals: self collectionLast
  771. !
  772. testLastN
  773. self
  774. assert: (self collection last: 2)
  775. equals: self collectionLastTwo.
  776. self
  777. assert: (self collection last: 0)
  778. equals: self collectionClass new.
  779. self
  780. assert: (self collection last: self collection size)
  781. equals: self collection.
  782. self should: [ self collection last: 33 ] raise: Error
  783. !
  784. testSecond
  785. self assert: (self collection second) equals: (self collection at: 2)
  786. !
  787. testThird
  788. self assert: (self collection third) equals: (self collection at: 3)
  789. ! !
  790. SequenceableCollectionTest subclass: #ArrayTest
  791. instanceVariableNames: ''
  792. package: 'Kernel-Tests'!
  793. !ArrayTest methodsFor: 'fixture'!
  794. collection
  795. ^ #(1 2 3 -4)
  796. !
  797. collectionFirst
  798. ^ 1
  799. !
  800. collectionFirstTwo
  801. ^ #(1 2)
  802. !
  803. collectionLast
  804. ^ -4
  805. !
  806. collectionLastTwo
  807. ^ #(3 -4)
  808. !
  809. collectionWithDuplicates
  810. ^ #('a' 'b' 'c' 1 2 1 'a')
  811. !
  812. collectionWithNewValue
  813. ^ #(1 2 3 -4 'N')
  814. !
  815. sampleNewIndex
  816. ^ 5
  817. !
  818. samplesDo: aBlock
  819. super samplesDo: aBlock.
  820. aBlock value: 3 value: 3.
  821. ! !
  822. !ArrayTest methodsFor: 'tests'!
  823. testAdd
  824. | array |
  825. array := self collection.
  826. array add: 6.
  827. self assert: array last equals: 6
  828. !
  829. testAddFirst
  830. self assert: (self collection addFirst: 0; yourself) first equals: 0
  831. !
  832. testPrintString
  833. | array |
  834. array := Array new.
  835. self assert: array printString equals: 'an Array ()'.
  836. array add: 1; add: 3.
  837. self assert: array printString equals: 'an Array (1 3)'.
  838. array add: 'foo'.
  839. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  840. array remove: 1; remove: 3.
  841. self assert: array printString equals: 'an Array (''foo'')'.
  842. array addLast: 3.
  843. self assert: array printString equals: 'an Array (''foo'' 3)'.
  844. array addLast: 3.
  845. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  846. !
  847. testRemove
  848. | array |
  849. array := #(1 2 3 4 5).
  850. array remove: 3.
  851. self assert: array equals: #(1 2 4 5).
  852. self should: [ array remove: 3 ] raise: Error
  853. !
  854. testRemoveFromTo
  855. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  856. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  857. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  858. !
  859. testRemoveIndex
  860. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  861. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  862. self assert: (#('hello') removeIndex: 1) equals: #()
  863. !
  864. testRemoveLast
  865. | array |
  866. array := #(1 2).
  867. array removeLast.
  868. self assert: array last equals: 1
  869. !
  870. testReversed
  871. |array|
  872. array := #(5 4 3 2 1).
  873. self assert: (array reversed) equals: #(1 2 3 4 5)
  874. !
  875. testSort
  876. | array |
  877. array := #(3 1 4 5 2).
  878. array sort.
  879. self assert: array equals: #(1 2 3 4 5)
  880. ! !
  881. !ArrayTest class methodsFor: 'fixture'!
  882. collectionClass
  883. ^ Array
  884. ! !
  885. SequenceableCollectionTest subclass: #StringTest
  886. instanceVariableNames: ''
  887. package: 'Kernel-Tests'!
  888. !StringTest methodsFor: 'fixture'!
  889. collection
  890. ^ 'hello'
  891. !
  892. collectionFirst
  893. ^ 'h'
  894. !
  895. collectionFirstTwo
  896. ^ 'he'
  897. !
  898. collectionLast
  899. ^ 'o'
  900. !
  901. collectionLastTwo
  902. ^ 'lo'
  903. !
  904. collectionWithDuplicates
  905. ^ 'abbaerte'
  906. !
  907. samplesDo: aBlock
  908. super samplesDo: aBlock.
  909. aBlock value: 3 value: 'l'
  910. ! !
  911. !StringTest methodsFor: 'tests'!
  912. testAddRemove
  913. self should: [ 'hello' add: 'a' ] raise: Error.
  914. self should: [ 'hello' remove: 'h' ] raise: Error
  915. !
  916. testAsArray
  917. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  918. !
  919. testAsLowerCase
  920. self assert: 'JACKIE' asLowercase equals: 'jackie'.
  921. !
  922. testAsNumber
  923. self assert: '3' asNumber equals: 3.
  924. self assert: '-3' asNumber equals: -3.
  925. self assert: '-1.5' asNumber equals: -1.5.
  926. !
  927. testAsUpperCase
  928. self assert: 'jackie' asUppercase equals: 'JACKIE'.
  929. !
  930. testAsciiValue
  931. | characterA characterU |
  932. characterA := 'A'.
  933. characterU := 'U'.
  934. self assert: (characterA asciiValue) equals:65.
  935. self assert: (characterU asciiValue) equals:85
  936. !
  937. testAtPut
  938. "String instances are read-only"
  939. self should: [ 'hello' at: 1 put: 'a' ] raise: Error
  940. !
  941. testCapitalized
  942. self assert: 'test' capitalized equals: 'Test'.
  943. self assert: 'Test' capitalized equals: 'Test'.
  944. self assert: '' capitalized equals: ''.
  945. self assert: 'Test' isCapitalized equals: true.
  946. self assert: 'test' isCapitalized equals: false.
  947. !
  948. testCharCodeAt
  949. self assert: ('jackie' charCodeAt:1) equals: 106.
  950. self assert: ('jackie' charCodeAt:2) equals: 97.
  951. self assert: ('jackie' charCodeAt:3) equals: 99.
  952. self assert: ('jackie' charCodeAt:4) equals: 107.
  953. self assert: ('jackie' charCodeAt:5) equals: 105.
  954. self assert: ('jackie' charCodeAt:6) equals: 101
  955. !
  956. testCollect
  957. | newCollection |
  958. newCollection := 'hheelllloo'.
  959. self
  960. assertSameContents: (self collection collect: [ :each |
  961. each, each ])
  962. as: newCollection
  963. !
  964. testCopyFromTo
  965. self assert: ('jackie' copyFrom: 1 to: 3) equals: 'jac'.
  966. self assert: ('jackie' copyFrom: 4 to: 6) equals: 'kie'.
  967. !
  968. testCopyWithoutAll
  969. self
  970. assert: ('*hello* *world*' copyWithoutAll: '*')
  971. equals: 'hello world'
  972. !
  973. testDetect
  974. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  975. self
  976. should: [ self collection detect: [ :each | each = 6 ] ]
  977. raise: Error
  978. !
  979. testEquality
  980. self assert: 'hello' equals: 'hello'.
  981. self deny: 'hello' = 'world'.
  982. "Test for issue 459"
  983. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  984. self assert: 'hello' equals: 'hello' yourself.
  985. self assert: 'hello' yourself equals: 'hello'.
  986. "test JS falsy value"
  987. self deny: '' = 0
  988. !
  989. testIdentity
  990. self assert: 'hello' == 'hello'.
  991. self deny: 'hello' == 'world'.
  992. self assert: 'hello' == 'hello' yourself.
  993. self assert: 'hello' yourself == 'hello'.
  994. "test JS falsy value"
  995. self deny: '' == 0
  996. !
  997. testIdentityHash
  998. self assert: 'foo' identityHash equals: 'foo' identityHash.
  999. self deny: ('foo' identityHash = 'bar' identityHash)
  1000. !
  1001. testIncludesSubString
  1002. self assert: ('amber' includesSubString: 'ber').
  1003. self deny: ('amber' includesSubString: 'zork').
  1004. !
  1005. testIsVowel
  1006. |vowel consonant|
  1007. vowel := 'u'.
  1008. consonant := 'z'.
  1009. self assert: vowel isVowel equals: true.
  1010. self assert: consonant isVowel equals: false
  1011. !
  1012. testJoin
  1013. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  1014. !
  1015. testRemoveAll
  1016. self should: [ self collection removeAll ] raise: Error
  1017. !
  1018. testReversed
  1019. self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
  1020. !
  1021. testSelect
  1022. | newCollection |
  1023. newCollection := 'o'.
  1024. self
  1025. assertSameContents: (self collection select: [ :each |
  1026. each = 'o' ])
  1027. as: newCollection
  1028. !
  1029. testSize
  1030. self assert: 'smalltalk' size equals: 9.
  1031. self assert: '' size equals: 0
  1032. !
  1033. testStreamContents
  1034. self
  1035. assert: (String streamContents: [ :aStream |
  1036. aStream
  1037. nextPutAll: 'hello'; space;
  1038. nextPutAll: 'world' ])
  1039. equals: 'hello world'
  1040. !
  1041. testSubStrings
  1042. self assert: ('jackiechan' subStrings: 'ie') equals: #( 'jack' 'chan' ).
  1043. !
  1044. testTrim
  1045. self assert: ' jackie' trimLeft equals: 'jackie'.
  1046. self assert: 'jackie ' trimRight equals: 'jackie'.
  1047. ! !
  1048. !StringTest class methodsFor: 'fixture'!
  1049. collectionClass
  1050. ^ String
  1051. ! !
  1052. TestCase subclass: #ConsoleTranscriptTest
  1053. instanceVariableNames: ''
  1054. package: 'Kernel-Tests'!
  1055. !ConsoleTranscriptTest methodsFor: 'tests'!
  1056. testShow
  1057. | originalTranscript |
  1058. originalTranscript := Transcript current.
  1059. Transcript register: ConsoleTranscript new.
  1060. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  1061. self shouldnt: [ Transcript show: console ] raise: Error.
  1062. Transcript register: originalTranscript.
  1063. ! !
  1064. TestCase subclass: #JSObjectProxyTest
  1065. instanceVariableNames: ''
  1066. package: 'Kernel-Tests'!
  1067. !JSObjectProxyTest methodsFor: 'accessing'!
  1068. jsObject
  1069. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': void 0}>
  1070. ! !
  1071. !JSObjectProxyTest methodsFor: 'tests'!
  1072. testAtIfAbsent
  1073. | testObject |
  1074. testObject := self jsObject.
  1075. self assert: (testObject at: 'abc' ifAbsent: [ 'Property does not exist' ]) equals: 'Property does not exist'.
  1076. self assert: (testObject at: 'e' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  1077. self assert: (testObject at: 'a' ifAbsent: [ 'Property does not exist' ]) equals: 1.
  1078. self assert: (testObject at: 'f' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  1079. !
  1080. testAtIfPresent
  1081. | testObject |
  1082. testObject := self jsObject.
  1083. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  1084. self assert: (testObject at: 'e' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  1085. self assert: (testObject at: 'a' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello 1'.
  1086. self assert: (testObject at: 'f' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  1087. !
  1088. testAtIfPresentIfAbsent
  1089. | testObject |
  1090. testObject := self jsObject.
  1091. self assert: (testObject at: 'abc' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'not present'.
  1092. self assert: (testObject at: 'e' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  1093. self assert: (testObject at: 'a' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello 1'.
  1094. self assert: (testObject at: 'f' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  1095. !
  1096. testAtPut
  1097. | testObject |
  1098. testObject := self jsObject.
  1099. self assert: (testObject at: 'abc') ~= 'xyz'.
  1100. self assert: (testObject at: 'abc' put: 'xyz') equals: 'xyz'.
  1101. self assert: (testObject at: 'abc') equals: 'xyz'
  1102. !
  1103. testDNU
  1104. self should: [ self jsObject foo ] raise: MessageNotUnderstood
  1105. !
  1106. testMessageSend
  1107. self assert: self jsObject a equals: 1.
  1108. self assert: self jsObject b equals: 2.
  1109. self assert: (self jsObject c: 3) equals: 3
  1110. !
  1111. testMethodWithArguments
  1112. self assert: (self jsObject c: 1) equals: 1
  1113. !
  1114. testPrinting
  1115. self assert: self jsObject printString equals: '[object Object]'
  1116. !
  1117. testPropertyThatReturnsEmptyString
  1118. | object |
  1119. object := self jsObject.
  1120. self assert: object d equals: ''.
  1121. object d: 'hello'.
  1122. self assert: object d equals: 'hello'
  1123. !
  1124. testPropertyThatReturnsUndefined
  1125. | object |
  1126. object := self jsObject.
  1127. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  1128. self assert: object e isNil
  1129. !
  1130. testValue
  1131. | testObject |
  1132. testObject := self jsObject.
  1133. testObject at: 'value' put: 'aValue'.
  1134. self assert: testObject value equals: 'aValue'
  1135. !
  1136. testYourself
  1137. | object |
  1138. object := self jsObject
  1139. d: 'test';
  1140. yourself.
  1141. self assert: object d equals: 'test'
  1142. ! !
  1143. TestCase subclass: #JavaScriptExceptionTest
  1144. instanceVariableNames: ''
  1145. package: 'Kernel-Tests'!
  1146. !JavaScriptExceptionTest methodsFor: 'helpers'!
  1147. throwException
  1148. <throw 'test'>
  1149. ! !
  1150. !JavaScriptExceptionTest methodsFor: 'tests'!
  1151. testCatchingException
  1152. [ self throwException ]
  1153. on: Error
  1154. do: [ :error |
  1155. self assert: error exception = 'test' ]
  1156. !
  1157. testRaisingException
  1158. self should: [ self throwException ] raise: JavaScriptException
  1159. ! !
  1160. TestCase subclass: #MessageSendTest
  1161. instanceVariableNames: ''
  1162. package: 'Kernel-Tests'!
  1163. !MessageSendTest methodsFor: 'tests'!
  1164. testValue
  1165. | messageSend |
  1166. messageSend := MessageSend new
  1167. receiver: Object new;
  1168. selector: #asString;
  1169. yourself.
  1170. self assert: messageSend value equals: 'an Object'
  1171. !
  1172. testValueWithArguments
  1173. | messageSend |
  1174. messageSend := MessageSend new
  1175. receiver: 2;
  1176. selector: '+';
  1177. yourself.
  1178. self assert: (messageSend value: 3) equals: 5.
  1179. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  1180. ! !
  1181. TestCase subclass: #MethodInheritanceTest
  1182. instanceVariableNames: 'receiverTop receiverMiddle receiverBottom method performBlock'
  1183. package: 'Kernel-Tests'!
  1184. !MethodInheritanceTest methodsFor: 'accessing'!
  1185. codeGeneratorClass
  1186. ^ CodeGenerator
  1187. !
  1188. targetClassBottom
  1189. ^ JavaScriptException
  1190. !
  1191. targetClassMiddle
  1192. ^ Error
  1193. !
  1194. targetClassTop
  1195. ^ Object
  1196. ! !
  1197. !MethodInheritanceTest methodsFor: 'factory'!
  1198. compiler
  1199. ^ Compiler new
  1200. codeGeneratorClass: self codeGeneratorClass;
  1201. yourself
  1202. ! !
  1203. !MethodInheritanceTest methodsFor: 'initialization'!
  1204. setUp
  1205. receiverTop := self targetClassTop new.
  1206. receiverMiddle := self targetClassMiddle new.
  1207. receiverBottom := self targetClassBottom new.
  1208. method := nil.
  1209. performBlock := [ self error: 'performBlock not initialized' ]
  1210. !
  1211. tearDown
  1212. [ self deinstallTop ] on: Error do: [ ].
  1213. [ self deinstallMiddle ] on: Error do: [ ].
  1214. [ self deinstallBottom ] on: Error do: [ ]
  1215. ! !
  1216. !MethodInheritanceTest methodsFor: 'testing'!
  1217. deinstallBottom
  1218. self targetClassBottom removeCompiledMethod: method
  1219. !
  1220. deinstallMiddle
  1221. self targetClassMiddle removeCompiledMethod: method
  1222. !
  1223. deinstallTop
  1224. self targetClassTop removeCompiledMethod: method
  1225. !
  1226. installBottom: aString
  1227. method := self compiler install: aString forClass: self targetClassBottom protocol: 'tests'
  1228. !
  1229. installMiddle: aString
  1230. method := self compiler install: aString forClass: self targetClassMiddle protocol: 'tests'
  1231. !
  1232. installTop: aString
  1233. method := self compiler install: aString forClass: self targetClassTop protocol: 'tests'
  1234. !
  1235. shouldMNU
  1236. self shouldMNUTop.
  1237. self shouldMNUMiddle.
  1238. self shouldMNUBottom
  1239. !
  1240. shouldMNUBottom
  1241. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  1242. !
  1243. shouldMNUMiddle
  1244. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  1245. !
  1246. shouldMNUTop
  1247. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  1248. !
  1249. shouldReturn: anObject
  1250. | result |
  1251. result := performBlock value: receiverTop.
  1252. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1253. result := performBlock value: receiverMiddle.
  1254. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  1255. result := performBlock value: receiverBottom.
  1256. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  1257. !
  1258. shouldReturn: anObject and: anObject2 and: anObject3
  1259. | result |
  1260. result := performBlock value: receiverTop.
  1261. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1262. result := performBlock value: receiverMiddle.
  1263. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  1264. result := performBlock value: receiverBottom.
  1265. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  1266. ! !
  1267. !MethodInheritanceTest methodsFor: 'tests'!
  1268. testMNU11
  1269. performBlock := [ :x | x foo ].
  1270. self shouldMNU.
  1271. self installTop: 'foo ^ false'.
  1272. self installTop: 'foo ^ true'.
  1273. self deinstallTop.
  1274. self shouldMNU
  1275. !
  1276. testMNU22
  1277. performBlock := [ :x | x foo ].
  1278. self shouldMNU.
  1279. self installMiddle: 'foo ^ false'.
  1280. self installMiddle: 'foo ^ true'.
  1281. self deinstallMiddle.
  1282. self shouldMNU
  1283. !
  1284. testReturns1
  1285. performBlock := [ :x | x foo ].
  1286. self installTop: 'foo ^ false'.
  1287. self shouldReturn: false.
  1288. self installTop: 'foo ^ true'.
  1289. self shouldReturn: true
  1290. ! !
  1291. TestCase subclass: #NumberTest
  1292. instanceVariableNames: ''
  1293. package: 'Kernel-Tests'!
  1294. !NumberTest methodsFor: 'tests'!
  1295. testAbs
  1296. self assert: 4 abs equals: 4.
  1297. self assert: -4 abs equals: 4
  1298. !
  1299. testArithmetic
  1300. "We rely on JS here, so we won't test complex behavior, just check if
  1301. message sends are corrects"
  1302. self assert: 1.5 + 1 equals: 2.5.
  1303. self assert: 2 - 1 equals: 1.
  1304. self assert: -2 - 1 equals: -3.
  1305. self assert: 12 / 2 equals: 6.
  1306. self assert: 3 * 4 equals: 12.
  1307. self assert: 7 // 2 equals: 3.
  1308. self assert: 7 \\ 2 equals: 1.
  1309. "Simple parenthesis and execution order"
  1310. self assert: 1 + 2 * 3 equals: 9.
  1311. self assert: 1 + (2 * 3) equals: 7
  1312. !
  1313. testAsNumber
  1314. self assert: 3 asNumber equals: 3.
  1315. !
  1316. testCeiling
  1317. self assert: 1.2 ceiling equals: 2.
  1318. self assert: -1.2 ceiling equals: -1.
  1319. self assert: 1.0 ceiling equals: 1.
  1320. !
  1321. testComparison
  1322. self assert: 3 > 2.
  1323. self assert: 2 < 3.
  1324. self deny: 3 < 2.
  1325. self deny: 2 > 3.
  1326. self assert: 3 >= 3.
  1327. self assert: 3.1 >= 3.
  1328. self assert: 3 <= 3.
  1329. self assert: 3 <= 3.1
  1330. !
  1331. testCopying
  1332. self assert: 1 copy == 1.
  1333. self assert: 1 deepCopy == 1
  1334. !
  1335. testEquality
  1336. self assert: (1 = 1).
  1337. self assert: (0 = 0).
  1338. self deny: (1 = 0).
  1339. self assert: (1 yourself = 1).
  1340. self assert: (1 = 1 yourself).
  1341. self assert: (1 yourself = 1 yourself).
  1342. self deny: 0 = false.
  1343. self deny: false = 0.
  1344. self deny: '' = 0.
  1345. self deny: 0 = ''
  1346. !
  1347. testFloor
  1348. self assert: 1.2 floor equals: 1.
  1349. self assert: -1.2 floor equals: -2.
  1350. self assert: 1.0 floor equals: 1.
  1351. !
  1352. testHexNumbers
  1353. self assert: 16r9 equals: 9.
  1354. self assert: 16rA truncated equals: 10.
  1355. self assert: 16rB truncated equals: 11.
  1356. self assert: 16rC truncated equals: 12.
  1357. self assert: 16rD truncated equals: 13.
  1358. self assert: 16rE truncated equals: 14.
  1359. self assert: 16rF truncated equals: 15
  1360. !
  1361. testIdentity
  1362. self assert: 1 == 1.
  1363. self assert: 0 == 0.
  1364. self deny: 1 == 0.
  1365. self assert: 1 yourself == 1.
  1366. self assert: 1 == 1 yourself.
  1367. self assert: 1 yourself == 1 yourself.
  1368. self deny: 1 == 2
  1369. !
  1370. testInvalidHexNumbers
  1371. self should: [ 16rG ] raise: MessageNotUnderstood.
  1372. self should: [ 16rg ] raise: MessageNotUnderstood.
  1373. self should: [ 16rH ] raise: MessageNotUnderstood.
  1374. self should: [ 16rh ] raise: MessageNotUnderstood.
  1375. self should: [ 16rI ] raise: MessageNotUnderstood.
  1376. self should: [ 16ri ] raise: MessageNotUnderstood.
  1377. self should: [ 16rJ ] raise: MessageNotUnderstood.
  1378. self should: [ 16rj ] raise: MessageNotUnderstood.
  1379. self should: [ 16rK ] raise: MessageNotUnderstood.
  1380. self should: [ 16rk ] raise: MessageNotUnderstood.
  1381. self should: [ 16rL ] raise: MessageNotUnderstood.
  1382. self should: [ 16rl ] raise: MessageNotUnderstood.
  1383. self should: [ 16rM ] raise: MessageNotUnderstood.
  1384. self should: [ 16rm ] raise: MessageNotUnderstood.
  1385. self should: [ 16rN ] raise: MessageNotUnderstood.
  1386. self should: [ 16rn ] raise: MessageNotUnderstood.
  1387. self should: [ 16rO ] raise: MessageNotUnderstood.
  1388. self should: [ 16ro ] raise: MessageNotUnderstood.
  1389. self should: [ 16rP ] raise: MessageNotUnderstood.
  1390. self should: [ 16rp ] raise: MessageNotUnderstood.
  1391. self should: [ 16rQ ] raise: MessageNotUnderstood.
  1392. self should: [ 16rq ] raise: MessageNotUnderstood.
  1393. self should: [ 16rR ] raise: MessageNotUnderstood.
  1394. self should: [ 16rr ] raise: MessageNotUnderstood.
  1395. self should: [ 16rS ] raise: MessageNotUnderstood.
  1396. self should: [ 16rs ] raise: MessageNotUnderstood.
  1397. self should: [ 16rT ] raise: MessageNotUnderstood.
  1398. self should: [ 16rt ] raise: MessageNotUnderstood.
  1399. self should: [ 16rU ] raise: MessageNotUnderstood.
  1400. self should: [ 16ru ] raise: MessageNotUnderstood.
  1401. self should: [ 16rV ] raise: MessageNotUnderstood.
  1402. self should: [ 16rv ] raise: MessageNotUnderstood.
  1403. self should: [ 16rW ] raise: MessageNotUnderstood.
  1404. self should: [ 16rw ] raise: MessageNotUnderstood.
  1405. self should: [ 16rX ] raise: MessageNotUnderstood.
  1406. self should: [ 16rx ] raise: MessageNotUnderstood.
  1407. self should: [ 16rY ] raise: MessageNotUnderstood.
  1408. self should: [ 16ry ] raise: MessageNotUnderstood.
  1409. self should: [ 16rZ ] raise: MessageNotUnderstood.
  1410. self should: [ 16rz ] raise: MessageNotUnderstood.
  1411. self should: [ 16rABcdEfZ ] raise: MessageNotUnderstood.
  1412. !
  1413. testLog
  1414. self assert: 10000 log equals: 4.
  1415. self assert: (512 log: 2) equals: 9.
  1416. self assert: Number e ln equals: 1.
  1417. !
  1418. testMinMax
  1419. self assert: (2 max: 5) equals: 5.
  1420. self assert: (2 min: 5) equals: 2
  1421. !
  1422. testNegated
  1423. self assert: 3 negated equals: -3.
  1424. self assert: -3 negated equals: 3
  1425. !
  1426. testPrintShowingDecimalPlaces
  1427. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1428. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1429. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1430. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1431. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1432. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1433. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1434. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1435. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1436. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1437. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1438. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  1439. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  1440. !
  1441. testRaisedTo
  1442. self assert: (2 raisedTo: 4) equals: 16.
  1443. self assert: (2 raisedTo: 0) equals: 1.
  1444. self assert: (2 raisedTo: -3) equals: 0.125.
  1445. self assert: (4 raisedTo: 0.5) equals: 2.
  1446. self assert: 2 ** 4 equals: 16.
  1447. !
  1448. testRounded
  1449. self assert: 3 rounded equals: 3.
  1450. self assert: 3.212 rounded equals: 3.
  1451. self assert: 3.51 rounded equals: 4
  1452. !
  1453. testSign
  1454. self assert: 5 sign equals: 1.
  1455. self assert: 0 sign equals: 0.
  1456. self assert: -1.4 sign equals: -1.
  1457. !
  1458. testSqrt
  1459. self assert: 4 sqrt equals: 2.
  1460. self assert: 16 sqrt equals: 4
  1461. !
  1462. testSquared
  1463. self assert: 4 squared equals: 16
  1464. !
  1465. testTimesRepeat
  1466. | i |
  1467. i := 0.
  1468. 0 timesRepeat: [ i := i + 1 ].
  1469. self assert: i equals: 0.
  1470. 5 timesRepeat: [ i := i + 1 ].
  1471. self assert: i equals: 5
  1472. !
  1473. testTo
  1474. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  1475. !
  1476. testToBy
  1477. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  1478. self should: [ 1 to: 4 by: 0 ] raise: Error
  1479. !
  1480. testTrigonometry
  1481. self assert: 0 cos equals: 1.
  1482. self assert: 0 sin equals: 0.
  1483. self assert: 0 tan equals: 0.
  1484. self assert: 1 arcCos equals: 0.
  1485. self assert: 0 arcSin equals: 0.
  1486. self assert: 0 arcTan equals: 0.
  1487. !
  1488. testTruncated
  1489. self assert: 3 truncated equals: 3.
  1490. self assert: 3.212 truncated equals: 3.
  1491. self assert: 3.51 truncated equals: 3
  1492. ! !
  1493. Object subclass: #ObjectMock
  1494. instanceVariableNames: 'foo bar'
  1495. package: 'Kernel-Tests'!
  1496. !ObjectMock commentStamp!
  1497. ObjectMock is there only to perform tests on classes.!
  1498. !ObjectMock methodsFor: 'not yet classified'!
  1499. foo
  1500. ^ foo
  1501. !
  1502. foo: anObject
  1503. foo := anObject
  1504. ! !
  1505. TestCase subclass: #ObjectTest
  1506. instanceVariableNames: ''
  1507. package: 'Kernel-Tests'!
  1508. !ObjectTest methodsFor: 'tests'!
  1509. notDefined
  1510. <return void 0;>
  1511. !
  1512. testBasicAccess
  1513. | o |
  1514. o := Object new.
  1515. o basicAt: 'a' put: 1.
  1516. self assert: (o basicAt: 'a') equals: 1.
  1517. self assert: (o basicAt: 'b') equals: nil
  1518. !
  1519. testBasicPerform
  1520. | o |
  1521. o := Object new.
  1522. o basicAt: 'func' put: [ 'hello' ].
  1523. o basicAt: 'func2' put: [ :a | a + 1 ].
  1524. self assert: (o basicPerform: 'func') equals: 'hello'.
  1525. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1526. !
  1527. testDNU
  1528. self should: [ Object new foo ] raise: MessageNotUnderstood
  1529. !
  1530. testEquality
  1531. | o |
  1532. o := Object new.
  1533. self deny: o = Object new.
  1534. self assert: (o = o).
  1535. self assert: (o yourself = o).
  1536. self assert: (o = o yourself)
  1537. !
  1538. testHalt
  1539. self should: [ Object new halt ] raise: Error
  1540. !
  1541. testIdentity
  1542. | o |
  1543. o := Object new.
  1544. self deny: o == Object new.
  1545. self assert: o == o.
  1546. self assert: o yourself == o.
  1547. self assert: o == o yourself
  1548. !
  1549. testIfNil
  1550. self deny: Object new isNil.
  1551. self deny: (Object new ifNil: [ true ]) = true.
  1552. self assert: (Object new ifNotNil: [ true ]) equals: true.
  1553. self assert: (Object new ifNil: [ false ] ifNotNil: [ true ]) equals: true.
  1554. self assert: (Object new ifNotNil: [ true ] ifNil: [ false ]) equals: true
  1555. !
  1556. testInstVars
  1557. | o |
  1558. o := ObjectMock new.
  1559. self assert: (o instVarAt: #foo) equals: nil.
  1560. o instVarAt: #foo put: 1.
  1561. self assert: (o instVarAt: #foo) equals: 1.
  1562. self assert: (o instVarAt: 'foo') equals: 1
  1563. !
  1564. testNilUndefined
  1565. "nil in Smalltalk is the undefined object in JS"
  1566. self assert: self notDefined equals: nil
  1567. !
  1568. testYourself
  1569. | o |
  1570. o := ObjectMock new.
  1571. self assert: o yourself == o
  1572. !
  1573. testidentityHash
  1574. | o1 o2 |
  1575. o1 := Object new.
  1576. o2 := Object new.
  1577. self assert: o1 identityHash == o1 identityHash.
  1578. self deny: o1 identityHash == o2 identityHash
  1579. ! !
  1580. TestCase subclass: #PointTest
  1581. instanceVariableNames: ''
  1582. package: 'Kernel-Tests'!
  1583. !PointTest methodsFor: 'tests'!
  1584. testAccessing
  1585. self assert: (Point x: 3 y: 4) x equals: 3.
  1586. self assert: (Point x: 3 y: 4) y equals: 4.
  1587. self assert: (Point new x: 3) x equals: 3.
  1588. self assert: (Point new y: 4) y equals: 4
  1589. !
  1590. testArithmetic
  1591. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1592. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1593. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1594. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1595. !
  1596. testAt
  1597. self assert: 3@4 equals: (Point x: 3 y: 4)
  1598. !
  1599. testEgality
  1600. self assert: (3@4 = (3@4)).
  1601. self deny: 3@5 = (3@6)
  1602. !
  1603. testNew
  1604. self assert: (Point new x: 3) y equals: nil.
  1605. self deny: (Point new x: 3) x = 0.
  1606. self assert: (Point new y: 4) x equals: nil.
  1607. self deny: (Point new y: 4) y = 0
  1608. !
  1609. testTranslateBy
  1610. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1611. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1612. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1613. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1614. ! !
  1615. TestCase subclass: #QueueTest
  1616. instanceVariableNames: ''
  1617. package: 'Kernel-Tests'!
  1618. !QueueTest methodsFor: 'tests'!
  1619. testNextIfAbsent
  1620. | queue |
  1621. queue := Queue new.
  1622. queue nextPut: 'index1'.
  1623. self assert: (queue nextIfAbsent: 'empty') = 'index1'.
  1624. self deny: (queue nextIfAbsent: 'empty') = 'index1'
  1625. !
  1626. testQueueNext
  1627. | queue |
  1628. queue := Queue new.
  1629. queue
  1630. nextPut: 'index1';
  1631. nextPut: 'index2'.
  1632. self assert: queue next = 'index1'.
  1633. self deny: queue next = 'index'.
  1634. self should: [ queue next ] raise: Error
  1635. ! !
  1636. TestCase subclass: #RandomTest
  1637. instanceVariableNames: ''
  1638. package: 'Kernel-Tests'!
  1639. !RandomTest methodsFor: 'tests'!
  1640. testAtRandomNumber
  1641. |val|
  1642. 100 timesRepeat: [
  1643. val := 10 atRandom.
  1644. self assert: (val > 0).
  1645. self assert: (val <11)
  1646. ]
  1647. !
  1648. testAtRandomSequenceableCollection
  1649. |val|
  1650. 100 timesRepeat: [
  1651. val := 'abc' atRandom.
  1652. self assert: ((val = 'a') | (val = 'b') | (val = 'c' )).
  1653. ].
  1654. !
  1655. textNext
  1656. 10000 timesRepeat: [
  1657. | current next |
  1658. next := Random new next.
  1659. self assert: (next >= 0).
  1660. self assert: (next < 1).
  1661. self deny: current = next.
  1662. next = current ]
  1663. ! !
  1664. TestCase subclass: #SetTest
  1665. instanceVariableNames: ''
  1666. package: 'Kernel-Tests'!
  1667. !SetTest methodsFor: 'tests'!
  1668. testAddRemove
  1669. | set |
  1670. set := Set new.
  1671. self assert: set isEmpty.
  1672. set add: 3.
  1673. self assert: (set includes: 3).
  1674. set add: 5.
  1675. self assert: (set includes: 5).
  1676. set remove: 3.
  1677. self deny: (set includes: 3)
  1678. !
  1679. testAt
  1680. self should: [ Set new at: 1 put: 2 ] raise: Error
  1681. !
  1682. testCollect
  1683. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1684. !
  1685. testComparing
  1686. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1687. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1688. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1689. self deny: #(1 2) asSet = #(0 2) asSet
  1690. !
  1691. testPrintString
  1692. | set |
  1693. set := Set new.
  1694. self assert: set printString equals: 'a Set ()'.
  1695. set add: 1; add: 3.
  1696. self assert: set printString equals: 'a Set (1 3)'.
  1697. set add: 'foo'.
  1698. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1699. set remove: 1; remove: 3.
  1700. self assert: set printString equals: 'a Set (''foo'')'.
  1701. set add: 3.
  1702. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1703. set add: 3.
  1704. self assert: set printString equals: 'a Set (''foo'' 3)'
  1705. !
  1706. testSize
  1707. self assert: Set new size equals: 0.
  1708. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1709. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1710. !
  1711. testUnboxedObjects
  1712. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1713. !
  1714. testUnicity
  1715. | set |
  1716. set := Set new.
  1717. set add: 21.
  1718. set add: 'hello'.
  1719. set add: 21.
  1720. self assert: set size equals: 2.
  1721. set add: 'hello'.
  1722. self assert: set size equals: 2.
  1723. self assert: set asArray equals: #(21 'hello')
  1724. ! !
  1725. TestCase subclass: #StreamTest
  1726. instanceVariableNames: ''
  1727. package: 'Kernel-Tests'!
  1728. !StreamTest methodsFor: 'accessing'!
  1729. collectionClass
  1730. ^ self class collectionClass
  1731. !
  1732. newCollection
  1733. ^ self collectionClass new
  1734. !
  1735. newStream
  1736. ^ self collectionClass new stream
  1737. ! !
  1738. !StreamTest methodsFor: 'tests'!
  1739. testAtStartAtEnd
  1740. | stream |
  1741. stream := self newStream.
  1742. self assert: stream atStart.
  1743. self assert: stream atEnd.
  1744. stream nextPutAll: self newCollection.
  1745. self assert: stream atEnd.
  1746. self deny: stream atStart.
  1747. stream position: 1.
  1748. self deny: stream atEnd.
  1749. self deny: stream atStart
  1750. !
  1751. testContents
  1752. | stream |
  1753. stream := self newStream.
  1754. stream nextPutAll: self newCollection.
  1755. self assert: stream contents equals: self newCollection
  1756. !
  1757. testIsEmpty
  1758. | stream |
  1759. stream := self newStream.
  1760. self assert: stream isEmpty.
  1761. stream nextPutAll: self newCollection.
  1762. self deny: stream isEmpty
  1763. !
  1764. testPosition
  1765. | collection stream |
  1766. collection := self newCollection.
  1767. stream := self newStream.
  1768. stream nextPutAll: collection.
  1769. self assert: stream position equals: collection size.
  1770. stream position: 0.
  1771. self assert: stream position equals: 0.
  1772. stream next.
  1773. self assert: stream position equals: 1.
  1774. stream next.
  1775. self assert: stream position equals: 2
  1776. !
  1777. testReading
  1778. | stream collection |
  1779. collection := self newCollection.
  1780. stream := self newStream.
  1781. stream
  1782. nextPutAll: collection;
  1783. position: 0.
  1784. collection do: [ :each |
  1785. self assert: stream next equals: each ].
  1786. self assert: stream next isNil
  1787. !
  1788. testStreamContents
  1789. !
  1790. testWrite
  1791. | stream collection |
  1792. collection := self newCollection.
  1793. stream := self newStream.
  1794. collection do: [ :each | stream << each ].
  1795. self assert: stream contents equals: collection
  1796. !
  1797. testWriting
  1798. | stream collection |
  1799. collection := self newCollection.
  1800. stream := self newStream.
  1801. collection do: [ :each | stream nextPut: each ].
  1802. self assert: stream contents equals: collection.
  1803. stream := self newStream.
  1804. stream nextPutAll: collection.
  1805. self assert: stream contents equals: collection
  1806. ! !
  1807. !StreamTest class methodsFor: 'accessing'!
  1808. collectionClass
  1809. ^ nil
  1810. ! !
  1811. !StreamTest class methodsFor: 'testing'!
  1812. isAbstract
  1813. ^ self collectionClass isNil
  1814. ! !
  1815. StreamTest subclass: #ArrayStreamTest
  1816. instanceVariableNames: ''
  1817. package: 'Kernel-Tests'!
  1818. !ArrayStreamTest methodsFor: 'accessing'!
  1819. newCollection
  1820. ^ { true. 1. 3@4. 'foo' }
  1821. ! !
  1822. !ArrayStreamTest class methodsFor: 'accessing'!
  1823. collectionClass
  1824. ^ Array
  1825. ! !
  1826. StreamTest subclass: #StringStreamTest
  1827. instanceVariableNames: ''
  1828. package: 'Kernel-Tests'!
  1829. !StringStreamTest methodsFor: 'accessing'!
  1830. newCollection
  1831. ^ 'hello world'
  1832. ! !
  1833. !StringStreamTest class methodsFor: 'accessing'!
  1834. collectionClass
  1835. ^ String
  1836. ! !
  1837. TestCase subclass: #UndefinedTest
  1838. instanceVariableNames: ''
  1839. package: 'Kernel-Tests'!
  1840. !UndefinedTest methodsFor: 'tests'!
  1841. testCopying
  1842. self assert: nil copy equals: nil
  1843. !
  1844. testDeepCopy
  1845. self assert: nil deepCopy = nil
  1846. !
  1847. testIfNil
  1848. self assert: (nil ifNil: [ true ]) equals: true.
  1849. self deny: (nil ifNotNil: [ true ]) = true.
  1850. self assert: (nil ifNil: [ true ] ifNotNil: [ false ]) equals: true.
  1851. self deny: (nil ifNotNil: [ true ] ifNil: [ false ]) = true
  1852. !
  1853. testIsNil
  1854. self assert: nil isNil.
  1855. self deny: nil notNil.
  1856. ! !