Kernel-Tests.st 52 KB

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