Kernel-Tests.st 51 KB

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