Kernel-Tests.st 58 KB

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