Kernel-Tests.st 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  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. testFourth
  650. self assert: (self collection fourth) equals: (self collection at: 4)
  651. !
  652. testLast
  653. self assert: (self collection last) equals: (self collection at: self collection size)
  654. !
  655. testSecond
  656. self assert: (self collection second) equals: (self collection at: 2)
  657. !
  658. testThird
  659. self assert: (self collection third) equals: (self collection at: 3)
  660. ! !
  661. SequenceableCollectionTest subclass: #ArrayTest
  662. instanceVariableNames: ''
  663. package: 'Kernel-Tests'!
  664. !ArrayTest methodsFor: 'tests'!
  665. testAdd
  666. | array |
  667. array := self collection.
  668. array add: 6.
  669. self assert: array last equals: 6
  670. !
  671. testAddFirst
  672. self assert: (self collection addFirst: 0; yourself) first equals: 0
  673. !
  674. testAtIfAbsent
  675. | array |
  676. array := #('hello' 'world').
  677. self assert: (array at: 1) equals: 'hello'.
  678. self assert: (array at: 2) equals: 'world'.
  679. self assert: (array at: 2 ifAbsent: [ 'not found' ]) equals: 'world'.
  680. self assert: (array at: 0 ifAbsent: [ 'not found' ]) equals: 'not found'.
  681. self assert: (array at: -10 ifAbsent: [ 'not found' ]) equals: 'not found'.
  682. self assert: (array at: 3 ifAbsent: [ 'not found' ]) equals: 'not found'.
  683. !
  684. testFirstN
  685. self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
  686. !
  687. testIfEmpty
  688. self assert: ( '' ifEmpty: [ 'zork' ] ) equals: 'zork'
  689. !
  690. testPrintString
  691. | array |
  692. array := Array new.
  693. self assert: array printString equals: 'an Array ()'.
  694. array add: 1; add: 3.
  695. self assert: array printString equals: 'an Array (1 3)'.
  696. array add: 'foo'.
  697. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  698. array remove: 1; remove: 3.
  699. self assert: array printString equals: 'an Array (''foo'')'.
  700. array addLast: 3.
  701. self assert: array printString equals: 'an Array (''foo'' 3)'.
  702. array addLast: 3.
  703. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  704. !
  705. testRemove
  706. | array |
  707. array := #(1 2 3 4 5).
  708. array remove: 3.
  709. self assert: array equals: #(1 2 4 5).
  710. self should: [ array remove: 3 ] raise: Error
  711. !
  712. testRemoveFromTo
  713. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  714. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  715. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  716. !
  717. testRemoveIndex
  718. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  719. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  720. self assert: (#('hello') removeIndex: 1) equals: #()
  721. !
  722. testRemoveLast
  723. | array |
  724. array := #(1 2).
  725. array removeLast.
  726. self assert: array last equals: 1
  727. !
  728. testReversed
  729. |array|
  730. array := #(5 4 3 2 1).
  731. self assert: (array reversed) equals: #(1 2 3 4 5)
  732. !
  733. testSort
  734. | array |
  735. array := #(3 1 4 5 2).
  736. array sort.
  737. self assert: array equals: #(1 2 3 4 5)
  738. ! !
  739. !ArrayTest class methodsFor: 'accessing'!
  740. collectionClass
  741. ^ Array
  742. ! !
  743. SequenceableCollectionTest subclass: #StringTest
  744. instanceVariableNames: ''
  745. package: 'Kernel-Tests'!
  746. !StringTest methodsFor: 'accessing'!
  747. collection
  748. ^ 'hello'
  749. !
  750. collectionWithDuplicates
  751. ^ 'abbaerte'
  752. ! !
  753. !StringTest methodsFor: 'tests'!
  754. testAddRemove
  755. self should: [ 'hello' add: 'a' ] raise: Error.
  756. self should: [ 'hello' remove: 'h' ] raise: Error
  757. !
  758. testAsArray
  759. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  760. !
  761. testAsLowerCase
  762. self assert: 'JACKIE' asLowercase equals: 'jackie'.
  763. !
  764. testAsNumber
  765. self assert: '3' asNumber equals: 3.
  766. self assert: '-3' asNumber equals: -3.
  767. self assert: '-1.5' asNumber equals: -1.5.
  768. !
  769. testAsUpperCase
  770. self assert: 'jackie' asUppercase equals: 'JACKIE'.
  771. !
  772. testAsciiValue
  773. | characterA characterU |
  774. characterA := 'A'.
  775. characterU := 'U'.
  776. self assert: (characterA asciiValue) equals:65.
  777. self assert: (characterU asciiValue) equals:85
  778. !
  779. testAt
  780. self assert: ('hello' at: 1) equals: 'h'.
  781. self assert: ('hello' at: 5) equals: 'o'.
  782. self assert: ('hello' at: 6 ifAbsent: [ nil ]) equals: nil
  783. !
  784. testAtPut
  785. "String instances are read-only"
  786. self should: [ 'hello' at: 1 put: 'a' ] raise: Error
  787. !
  788. testCapitalized
  789. self assert: 'test' capitalized equals: 'Test'.
  790. self assert: 'Test' capitalized equals: 'Test'.
  791. self assert: '' capitalized equals: ''.
  792. self assert: 'Test' isCapitalized equals: true.
  793. self assert: 'test' isCapitalized equals: false.
  794. !
  795. testCharCodeAt
  796. self assert: ('jackie' charCodeAt:1) equals: 106.
  797. self assert: ('jackie' charCodeAt:2) equals: 97.
  798. self assert: ('jackie' charCodeAt:3) equals: 99.
  799. self assert: ('jackie' charCodeAt:4) equals: 107.
  800. self assert: ('jackie' charCodeAt:5) equals: 105.
  801. self assert: ('jackie' charCodeAt:6) equals: 101
  802. !
  803. testCollect
  804. | newCollection |
  805. newCollection := 'hheelllloo'.
  806. self
  807. assertSameContents: (self collection collect: [ :each |
  808. each, each ])
  809. as: newCollection
  810. !
  811. testCopyFromTo
  812. self assert: ('jackie' copyFrom: 1 to: 3) equals: 'jac'.
  813. self assert: ('jackie' copyFrom: 4 to: 6) equals: 'kie'.
  814. !
  815. testCopyWithoutAll
  816. self
  817. assert: ('*hello* *world*' copyWithoutAll: '*')
  818. equals: 'hello world'
  819. !
  820. testDetect
  821. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  822. self
  823. should: [ self collection detect: [ :each | each = 6 ] ]
  824. raise: Error
  825. !
  826. testEquality
  827. self assert: 'hello' equals: 'hello'.
  828. self deny: 'hello' = 'world'.
  829. "Test for issue 459"
  830. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  831. self assert: 'hello' equals: 'hello' yourself.
  832. self assert: 'hello' yourself equals: 'hello'.
  833. "test JS falsy value"
  834. self deny: '' = 0
  835. !
  836. testIdentity
  837. self assert: 'hello' == 'hello'.
  838. self deny: 'hello' == 'world'.
  839. self assert: 'hello' == 'hello' yourself.
  840. self assert: 'hello' yourself == 'hello'.
  841. "test JS falsy value"
  842. self deny: '' == 0
  843. !
  844. testIdentityHash
  845. self assert: 'foo' identityHash equals: 'foo' identityHash.
  846. self deny: ('foo' identityHash = 'bar' identityHash)
  847. !
  848. testIncludesSubString
  849. self assert: ('amber' includesSubString: 'ber').
  850. self deny: ('amber' includesSubString: 'zork').
  851. !
  852. testIndexOf
  853. self assert: (self collection indexOf: 'e') equals: 2.
  854. self should: [ self collection indexOf: 999 ] raise: Error.
  855. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  856. !
  857. testIsVowel
  858. |vowel consonant|
  859. vowel := 'u'.
  860. consonant := 'z'.
  861. self assert: vowel isVowel equals: true.
  862. self assert: consonant isVowel equals: false
  863. !
  864. testJoin
  865. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  866. !
  867. testRemoveAll
  868. self should: [ self collection removeAll ] raise: Error
  869. !
  870. testReversed
  871. self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
  872. !
  873. testSelect
  874. | newCollection |
  875. newCollection := 'o'.
  876. self
  877. assertSameContents: (self collection select: [ :each |
  878. each = 'o' ])
  879. as: newCollection
  880. !
  881. testSize
  882. self assert: 'smalltalk' size equals: 9.
  883. self assert: '' size equals: 0
  884. !
  885. testStreamContents
  886. self
  887. assert: (String streamContents: [ :aStream |
  888. aStream
  889. nextPutAll: 'hello'; space;
  890. nextPutAll: 'world' ])
  891. equals: 'hello world'
  892. !
  893. testSubStrings
  894. self assert: ('jackiechan' subStrings: 'ie') equals: #( 'jack' 'chan' ).
  895. !
  896. testTrim
  897. self assert: ' jackie' trimLeft equals: 'jackie'.
  898. self assert: 'jackie ' trimRight equals: 'jackie'.
  899. ! !
  900. !StringTest class methodsFor: 'accessing'!
  901. collectionClass
  902. ^ String
  903. ! !
  904. TestCase subclass: #ConsoleTranscriptTest
  905. instanceVariableNames: ''
  906. package: 'Kernel-Tests'!
  907. !ConsoleTranscriptTest methodsFor: 'tests'!
  908. testShow
  909. | originalTranscript |
  910. originalTranscript := Transcript current.
  911. Transcript register: ConsoleTranscript new.
  912. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  913. self shouldnt: [ Transcript show: console ] raise: Error.
  914. Transcript register: originalTranscript.
  915. ! !
  916. TestCase subclass: #JSObjectProxyTest
  917. instanceVariableNames: ''
  918. package: 'Kernel-Tests'!
  919. !JSObjectProxyTest methodsFor: 'accessing'!
  920. jsObject
  921. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': void 0}>
  922. ! !
  923. !JSObjectProxyTest methodsFor: 'tests'!
  924. testAtIfAbsent
  925. | testObject |
  926. testObject := self jsObject.
  927. self assert: (testObject at: 'abc' ifAbsent: [ 'Property does not exist' ]) equals: 'Property does not exist'.
  928. self assert: (testObject at: 'e' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  929. self assert: (testObject at: 'a' ifAbsent: [ 'Property does not exist' ]) equals: 1.
  930. self assert: (testObject at: 'f' ifAbsent: [ 'Property does not exist' ]) equals: nil.
  931. !
  932. testAtIfPresent
  933. | testObject |
  934. testObject := self jsObject.
  935. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  936. self assert: (testObject at: 'e' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  937. self assert: (testObject at: 'a' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello 1'.
  938. self assert: (testObject at: 'f' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
  939. !
  940. testAtIfPresentIfAbsent
  941. | testObject |
  942. testObject := self jsObject.
  943. self assert: (testObject at: 'abc' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'not present'.
  944. self assert: (testObject at: 'e' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  945. self assert: (testObject at: 'a' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello 1'.
  946. self assert: (testObject at: 'f' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
  947. !
  948. testAtPut
  949. | testObject |
  950. testObject := self jsObject.
  951. self assert: (testObject at: 'abc') ~= 'xyz'.
  952. self assert: (testObject at: 'abc' put: 'xyz') equals: 'xyz'.
  953. self assert: (testObject at: 'abc') equals: 'xyz'
  954. !
  955. testDNU
  956. self should: [ self jsObject foo ] raise: MessageNotUnderstood
  957. !
  958. testMessageSend
  959. self assert: self jsObject a equals: 1.
  960. self assert: self jsObject b equals: 2.
  961. self assert: (self jsObject c: 3) equals: 3
  962. !
  963. testMethodWithArguments
  964. self assert: (self jsObject c: 1) equals: 1
  965. !
  966. testPrinting
  967. self assert: self jsObject printString equals: '[object Object]'
  968. !
  969. testPropertyThatReturnsEmptyString
  970. | object |
  971. object := self jsObject.
  972. self assert: object d equals: ''.
  973. object d: 'hello'.
  974. self assert: object d equals: 'hello'
  975. !
  976. testPropertyThatReturnsUndefined
  977. | object |
  978. object := self jsObject.
  979. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  980. self assert: object e isNil
  981. !
  982. testValue
  983. | testObject |
  984. testObject := self jsObject.
  985. testObject at: 'value' put: 'aValue'.
  986. self assert: testObject value equals: 'aValue'
  987. !
  988. testYourself
  989. | object |
  990. object := self jsObject
  991. d: 'test';
  992. yourself.
  993. self assert: object d equals: 'test'
  994. ! !
  995. TestCase subclass: #JavaScriptExceptionTest
  996. instanceVariableNames: ''
  997. package: 'Kernel-Tests'!
  998. !JavaScriptExceptionTest methodsFor: 'helpers'!
  999. throwException
  1000. <throw 'test'>
  1001. ! !
  1002. !JavaScriptExceptionTest methodsFor: 'tests'!
  1003. testCatchingException
  1004. [ self throwException ]
  1005. on: Error
  1006. do: [ :error |
  1007. self assert: error exception = 'test' ]
  1008. !
  1009. testRaisingException
  1010. self should: [ self throwException ] raise: JavaScriptException
  1011. ! !
  1012. TestCase subclass: #MessageSendTest
  1013. instanceVariableNames: ''
  1014. package: 'Kernel-Tests'!
  1015. !MessageSendTest methodsFor: 'tests'!
  1016. testValue
  1017. | messageSend |
  1018. messageSend := MessageSend new
  1019. receiver: Object new;
  1020. selector: #asString;
  1021. yourself.
  1022. self assert: messageSend value equals: 'an Object'
  1023. !
  1024. testValueWithArguments
  1025. | messageSend |
  1026. messageSend := MessageSend new
  1027. receiver: 2;
  1028. selector: '+';
  1029. yourself.
  1030. self assert: (messageSend value: 3) equals: 5.
  1031. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  1032. ! !
  1033. TestCase subclass: #MethodInheritanceTest
  1034. instanceVariableNames: 'receiverTop receiverMiddle receiverBottom method performBlock'
  1035. package: 'Kernel-Tests'!
  1036. !MethodInheritanceTest methodsFor: 'accessing'!
  1037. codeGeneratorClass
  1038. ^ CodeGenerator
  1039. !
  1040. targetClassBottom
  1041. ^ JavaScriptException
  1042. !
  1043. targetClassMiddle
  1044. ^ Error
  1045. !
  1046. targetClassTop
  1047. ^ Object
  1048. ! !
  1049. !MethodInheritanceTest methodsFor: 'factory'!
  1050. compiler
  1051. ^ Compiler new
  1052. codeGeneratorClass: self codeGeneratorClass;
  1053. yourself
  1054. ! !
  1055. !MethodInheritanceTest methodsFor: 'initialization'!
  1056. setUp
  1057. receiverTop := self targetClassTop new.
  1058. receiverMiddle := self targetClassMiddle new.
  1059. receiverBottom := self targetClassBottom new.
  1060. method := nil.
  1061. performBlock := [ self error: 'performBlock not initialized' ]
  1062. !
  1063. tearDown
  1064. [ self deinstallTop ] on: Error do: [ ].
  1065. [ self deinstallMiddle ] on: Error do: [ ].
  1066. [ self deinstallBottom ] on: Error do: [ ]
  1067. ! !
  1068. !MethodInheritanceTest methodsFor: 'testing'!
  1069. deinstallBottom
  1070. self targetClassBottom removeCompiledMethod: method
  1071. !
  1072. deinstallMiddle
  1073. self targetClassMiddle removeCompiledMethod: method
  1074. !
  1075. deinstallTop
  1076. self targetClassTop removeCompiledMethod: method
  1077. !
  1078. installBottom: aString
  1079. method := self compiler install: aString forClass: self targetClassBottom protocol: 'tests'
  1080. !
  1081. installMiddle: aString
  1082. method := self compiler install: aString forClass: self targetClassMiddle protocol: 'tests'
  1083. !
  1084. installTop: aString
  1085. method := self compiler install: aString forClass: self targetClassTop protocol: 'tests'
  1086. !
  1087. shouldMNU
  1088. self shouldMNUTop.
  1089. self shouldMNUMiddle.
  1090. self shouldMNUBottom
  1091. !
  1092. shouldMNUBottom
  1093. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  1094. !
  1095. shouldMNUMiddle
  1096. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  1097. !
  1098. shouldMNUTop
  1099. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  1100. !
  1101. shouldReturn: anObject
  1102. | result |
  1103. result := performBlock value: receiverTop.
  1104. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1105. result := performBlock value: receiverMiddle.
  1106. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  1107. result := performBlock value: receiverBottom.
  1108. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  1109. !
  1110. shouldReturn: anObject and: anObject2 and: anObject3
  1111. | result |
  1112. result := performBlock value: receiverTop.
  1113. self assert: { 'top'. anObject } equals: { 'top'. result }.
  1114. result := performBlock value: receiverMiddle.
  1115. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  1116. result := performBlock value: receiverBottom.
  1117. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  1118. ! !
  1119. !MethodInheritanceTest methodsFor: 'tests'!
  1120. testMNU11
  1121. performBlock := [ :x | x foo ].
  1122. self shouldMNU.
  1123. self installTop: 'foo ^ false'.
  1124. self installTop: 'foo ^ true'.
  1125. self deinstallTop.
  1126. self shouldMNU
  1127. !
  1128. testMNU22
  1129. performBlock := [ :x | x foo ].
  1130. self shouldMNU.
  1131. self installMiddle: 'foo ^ false'.
  1132. self installMiddle: 'foo ^ true'.
  1133. self deinstallMiddle.
  1134. self shouldMNU
  1135. !
  1136. testReturns1
  1137. performBlock := [ :x | x foo ].
  1138. self installTop: 'foo ^ false'.
  1139. self shouldReturn: false.
  1140. self installTop: 'foo ^ true'.
  1141. self shouldReturn: true
  1142. ! !
  1143. TestCase subclass: #NumberTest
  1144. instanceVariableNames: ''
  1145. package: 'Kernel-Tests'!
  1146. !NumberTest methodsFor: 'tests'!
  1147. testAbs
  1148. self assert: 4 abs equals: 4.
  1149. self assert: -4 abs equals: 4
  1150. !
  1151. testArithmetic
  1152. "We rely on JS here, so we won't test complex behavior, just check if
  1153. message sends are corrects"
  1154. self assert: 1.5 + 1 equals: 2.5.
  1155. self assert: 2 - 1 equals: 1.
  1156. self assert: -2 - 1 equals: -3.
  1157. self assert: 12 / 2 equals: 6.
  1158. self assert: 3 * 4 equals: 12.
  1159. self assert: 7 // 2 equals: 3.
  1160. self assert: 7 \\ 2 equals: 1.
  1161. "Simple parenthesis and execution order"
  1162. self assert: 1 + 2 * 3 equals: 9.
  1163. self assert: 1 + (2 * 3) equals: 7
  1164. !
  1165. testAsNumber
  1166. self assert: 3 asNumber equals: 3.
  1167. !
  1168. testCeiling
  1169. self assert: 1.2 ceiling equals: 2.
  1170. self assert: -1.2 ceiling equals: -1.
  1171. self assert: 1.0 ceiling equals: 1.
  1172. !
  1173. testComparison
  1174. self assert: 3 > 2.
  1175. self assert: 2 < 3.
  1176. self deny: 3 < 2.
  1177. self deny: 2 > 3.
  1178. self assert: 3 >= 3.
  1179. self assert: 3.1 >= 3.
  1180. self assert: 3 <= 3.
  1181. self assert: 3 <= 3.1
  1182. !
  1183. testCopying
  1184. self assert: 1 copy == 1.
  1185. self assert: 1 deepCopy == 1
  1186. !
  1187. testEquality
  1188. self assert: (1 = 1).
  1189. self assert: (0 = 0).
  1190. self deny: (1 = 0).
  1191. self assert: (1 yourself = 1).
  1192. self assert: (1 = 1 yourself).
  1193. self assert: (1 yourself = 1 yourself).
  1194. self deny: 0 = false.
  1195. self deny: false = 0.
  1196. self deny: '' = 0.
  1197. self deny: 0 = ''
  1198. !
  1199. testFloor
  1200. self assert: 1.2 floor equals: 1.
  1201. self assert: -1.2 floor equals: -2.
  1202. self assert: 1.0 floor equals: 1.
  1203. !
  1204. testHexNumbers
  1205. self assert: 16r9 equals: 9.
  1206. self assert: 16rA truncated equals: 10.
  1207. self assert: 16rB truncated equals: 11.
  1208. self assert: 16rC truncated equals: 12.
  1209. self assert: 16rD truncated equals: 13.
  1210. self assert: 16rE truncated equals: 14.
  1211. self assert: 16rF truncated equals: 15
  1212. !
  1213. testIdentity
  1214. self assert: 1 == 1.
  1215. self assert: 0 == 0.
  1216. self deny: 1 == 0.
  1217. self assert: 1 yourself == 1.
  1218. self assert: 1 == 1 yourself.
  1219. self assert: 1 yourself == 1 yourself.
  1220. self deny: 1 == 2
  1221. !
  1222. testInvalidHexNumbers
  1223. self should: [ 16rG ] raise: MessageNotUnderstood.
  1224. self should: [ 16rg ] raise: MessageNotUnderstood.
  1225. self should: [ 16rH ] raise: MessageNotUnderstood.
  1226. self should: [ 16rh ] raise: MessageNotUnderstood.
  1227. self should: [ 16rI ] raise: MessageNotUnderstood.
  1228. self should: [ 16ri ] raise: MessageNotUnderstood.
  1229. self should: [ 16rJ ] raise: MessageNotUnderstood.
  1230. self should: [ 16rj ] raise: MessageNotUnderstood.
  1231. self should: [ 16rK ] raise: MessageNotUnderstood.
  1232. self should: [ 16rk ] raise: MessageNotUnderstood.
  1233. self should: [ 16rL ] raise: MessageNotUnderstood.
  1234. self should: [ 16rl ] raise: MessageNotUnderstood.
  1235. self should: [ 16rM ] raise: MessageNotUnderstood.
  1236. self should: [ 16rm ] raise: MessageNotUnderstood.
  1237. self should: [ 16rN ] raise: MessageNotUnderstood.
  1238. self should: [ 16rn ] raise: MessageNotUnderstood.
  1239. self should: [ 16rO ] raise: MessageNotUnderstood.
  1240. self should: [ 16ro ] raise: MessageNotUnderstood.
  1241. self should: [ 16rP ] raise: MessageNotUnderstood.
  1242. self should: [ 16rp ] raise: MessageNotUnderstood.
  1243. self should: [ 16rQ ] raise: MessageNotUnderstood.
  1244. self should: [ 16rq ] raise: MessageNotUnderstood.
  1245. self should: [ 16rR ] raise: MessageNotUnderstood.
  1246. self should: [ 16rr ] raise: MessageNotUnderstood.
  1247. self should: [ 16rS ] raise: MessageNotUnderstood.
  1248. self should: [ 16rs ] raise: MessageNotUnderstood.
  1249. self should: [ 16rT ] raise: MessageNotUnderstood.
  1250. self should: [ 16rt ] raise: MessageNotUnderstood.
  1251. self should: [ 16rU ] raise: MessageNotUnderstood.
  1252. self should: [ 16ru ] raise: MessageNotUnderstood.
  1253. self should: [ 16rV ] raise: MessageNotUnderstood.
  1254. self should: [ 16rv ] raise: MessageNotUnderstood.
  1255. self should: [ 16rW ] raise: MessageNotUnderstood.
  1256. self should: [ 16rw ] raise: MessageNotUnderstood.
  1257. self should: [ 16rX ] raise: MessageNotUnderstood.
  1258. self should: [ 16rx ] raise: MessageNotUnderstood.
  1259. self should: [ 16rY ] raise: MessageNotUnderstood.
  1260. self should: [ 16ry ] raise: MessageNotUnderstood.
  1261. self should: [ 16rZ ] raise: MessageNotUnderstood.
  1262. self should: [ 16rz ] raise: MessageNotUnderstood.
  1263. self should: [ 16rABcdEfZ ] raise: MessageNotUnderstood.
  1264. !
  1265. testLog
  1266. self assert: 10000 log equals: 4.
  1267. self assert: (512 log: 2) equals: 9.
  1268. self assert: Number e ln equals: 1.
  1269. !
  1270. testMinMax
  1271. self assert: (2 max: 5) equals: 5.
  1272. self assert: (2 min: 5) equals: 2
  1273. !
  1274. testNegated
  1275. self assert: 3 negated equals: -3.
  1276. self assert: -3 negated equals: 3
  1277. !
  1278. testPrintShowingDecimalPlaces
  1279. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1280. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1281. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1282. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1283. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1284. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1285. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1286. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1287. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1288. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1289. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1290. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  1291. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  1292. !
  1293. testRaisedTo
  1294. self assert: (2 raisedTo: 4) equals: 16.
  1295. self assert: (2 raisedTo: 0) equals: 1.
  1296. self assert: (2 raisedTo: -3) equals: 0.125.
  1297. self assert: (4 raisedTo: 0.5) equals: 2.
  1298. self assert: 2 ** 4 equals: 16.
  1299. !
  1300. testRounded
  1301. self assert: 3 rounded equals: 3.
  1302. self assert: 3.212 rounded equals: 3.
  1303. self assert: 3.51 rounded equals: 4
  1304. !
  1305. testSign
  1306. self assert: 5 sign equals: 1.
  1307. self assert: 0 sign equals: 0.
  1308. self assert: -1.4 sign equals: -1.
  1309. !
  1310. testSqrt
  1311. self assert: 4 sqrt equals: 2.
  1312. self assert: 16 sqrt equals: 4
  1313. !
  1314. testSquared
  1315. self assert: 4 squared equals: 16
  1316. !
  1317. testTimesRepeat
  1318. | i |
  1319. i := 0.
  1320. 0 timesRepeat: [ i := i + 1 ].
  1321. self assert: i equals: 0.
  1322. 5 timesRepeat: [ i := i + 1 ].
  1323. self assert: i equals: 5
  1324. !
  1325. testTo
  1326. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  1327. !
  1328. testToBy
  1329. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  1330. self should: [ 1 to: 4 by: 0 ] raise: Error
  1331. !
  1332. testTrigonometry
  1333. self assert: 0 cos equals: 1.
  1334. self assert: 0 sin equals: 0.
  1335. self assert: 0 tan equals: 0.
  1336. self assert: 1 arcCos equals: 0.
  1337. self assert: 0 arcSin equals: 0.
  1338. self assert: 0 arcTan equals: 0.
  1339. !
  1340. testTruncated
  1341. self assert: 3 truncated equals: 3.
  1342. self assert: 3.212 truncated equals: 3.
  1343. self assert: 3.51 truncated equals: 3
  1344. ! !
  1345. Object subclass: #ObjectMock
  1346. instanceVariableNames: 'foo bar'
  1347. package: 'Kernel-Tests'!
  1348. !ObjectMock commentStamp!
  1349. ObjectMock is there only to perform tests on classes.!
  1350. !ObjectMock methodsFor: 'not yet classified'!
  1351. foo
  1352. ^ foo
  1353. !
  1354. foo: anObject
  1355. foo := anObject
  1356. ! !
  1357. TestCase subclass: #ObjectTest
  1358. instanceVariableNames: ''
  1359. package: 'Kernel-Tests'!
  1360. !ObjectTest methodsFor: 'tests'!
  1361. notDefined
  1362. <return void 0;>
  1363. !
  1364. testBasicAccess
  1365. | o |
  1366. o := Object new.
  1367. o basicAt: 'a' put: 1.
  1368. self assert: (o basicAt: 'a') equals: 1.
  1369. self assert: (o basicAt: 'b') equals: nil
  1370. !
  1371. testBasicPerform
  1372. | o |
  1373. o := Object new.
  1374. o basicAt: 'func' put: [ 'hello' ].
  1375. o basicAt: 'func2' put: [ :a | a + 1 ].
  1376. self assert: (o basicPerform: 'func') equals: 'hello'.
  1377. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1378. !
  1379. testDNU
  1380. self should: [ Object new foo ] raise: MessageNotUnderstood
  1381. !
  1382. testEquality
  1383. | o |
  1384. o := Object new.
  1385. self deny: o = Object new.
  1386. self assert: (o = o).
  1387. self assert: (o yourself = o).
  1388. self assert: (o = o yourself)
  1389. !
  1390. testHalt
  1391. self should: [ Object new halt ] raise: Error
  1392. !
  1393. testIdentity
  1394. | o |
  1395. o := Object new.
  1396. self deny: o == Object new.
  1397. self assert: o == o.
  1398. self assert: o yourself == o.
  1399. self assert: o == o yourself
  1400. !
  1401. testIfNil
  1402. self deny: Object new isNil.
  1403. self deny: (Object new ifNil: [ true ]) = true.
  1404. self assert: (Object new ifNotNil: [ true ]) equals: true.
  1405. self assert: (Object new ifNil: [ false ] ifNotNil: [ true ]) equals: true.
  1406. self assert: (Object new ifNotNil: [ true ] ifNil: [ false ]) equals: true
  1407. !
  1408. testInstVars
  1409. | o |
  1410. o := ObjectMock new.
  1411. self assert: (o instVarAt: #foo) equals: nil.
  1412. o instVarAt: #foo put: 1.
  1413. self assert: (o instVarAt: #foo) equals: 1.
  1414. self assert: (o instVarAt: 'foo') equals: 1
  1415. !
  1416. testNilUndefined
  1417. "nil in Smalltalk is the undefined object in JS"
  1418. self assert: self notDefined equals: nil
  1419. !
  1420. testYourself
  1421. | o |
  1422. o := ObjectMock new.
  1423. self assert: o yourself == o
  1424. !
  1425. testidentityHash
  1426. | o1 o2 |
  1427. o1 := Object new.
  1428. o2 := Object new.
  1429. self assert: o1 identityHash == o1 identityHash.
  1430. self deny: o1 identityHash == o2 identityHash
  1431. ! !
  1432. TestCase subclass: #PointTest
  1433. instanceVariableNames: ''
  1434. package: 'Kernel-Tests'!
  1435. !PointTest methodsFor: 'tests'!
  1436. testAccessing
  1437. self assert: (Point x: 3 y: 4) x equals: 3.
  1438. self assert: (Point x: 3 y: 4) y equals: 4.
  1439. self assert: (Point new x: 3) x equals: 3.
  1440. self assert: (Point new y: 4) y equals: 4
  1441. !
  1442. testArithmetic
  1443. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1444. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1445. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1446. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1447. !
  1448. testAt
  1449. self assert: 3@4 equals: (Point x: 3 y: 4)
  1450. !
  1451. testEgality
  1452. self assert: (3@4 = (3@4)).
  1453. self deny: 3@5 = (3@6)
  1454. !
  1455. testNew
  1456. self assert: (Point new x: 3) y equals: nil.
  1457. self deny: (Point new x: 3) x = 0.
  1458. self assert: (Point new y: 4) x equals: nil.
  1459. self deny: (Point new y: 4) y = 0
  1460. !
  1461. testTranslateBy
  1462. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1463. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1464. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1465. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1466. ! !
  1467. TestCase subclass: #QueueTest
  1468. instanceVariableNames: ''
  1469. package: 'Kernel-Tests'!
  1470. !QueueTest methodsFor: 'tests'!
  1471. testNextIfAbsent
  1472. | queue |
  1473. queue := Queue new.
  1474. queue nextPut: 'index1'.
  1475. self assert: (queue nextIfAbsent: 'empty') = 'index1'.
  1476. self deny: (queue nextIfAbsent: 'empty') = 'index1'
  1477. !
  1478. testQueueNext
  1479. | queue |
  1480. queue := Queue new.
  1481. queue
  1482. nextPut: 'index1';
  1483. nextPut: 'index2'.
  1484. self assert: queue next = 'index1'.
  1485. self deny: queue next = 'index'.
  1486. self should: [ queue next ] raise: Error
  1487. ! !
  1488. TestCase subclass: #RandomTest
  1489. instanceVariableNames: ''
  1490. package: 'Kernel-Tests'!
  1491. !RandomTest methodsFor: 'tests'!
  1492. testAtRandomNumber
  1493. |val|
  1494. 100 timesRepeat: [
  1495. val := 10 atRandom.
  1496. self assert: (val > 0).
  1497. self assert: (val <11)
  1498. ]
  1499. !
  1500. testAtRandomSequenceableCollection
  1501. |val|
  1502. 100 timesRepeat: [
  1503. val := 'abc' atRandom.
  1504. self assert: ((val = 'a') | (val = 'b') | (val = 'c' )).
  1505. ].
  1506. !
  1507. textNext
  1508. 10000 timesRepeat: [
  1509. | current next |
  1510. next := Random new next.
  1511. self assert: (next >= 0).
  1512. self assert: (next < 1).
  1513. self deny: current = next.
  1514. next = current ]
  1515. ! !
  1516. TestCase subclass: #SetTest
  1517. instanceVariableNames: ''
  1518. package: 'Kernel-Tests'!
  1519. !SetTest methodsFor: 'tests'!
  1520. testAddRemove
  1521. | set |
  1522. set := Set new.
  1523. self assert: set isEmpty.
  1524. set add: 3.
  1525. self assert: (set includes: 3).
  1526. set add: 5.
  1527. self assert: (set includes: 5).
  1528. set remove: 3.
  1529. self deny: (set includes: 3)
  1530. !
  1531. testAt
  1532. self should: [ Set new at: 1 put: 2 ] raise: Error
  1533. !
  1534. testCollect
  1535. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1536. !
  1537. testComparing
  1538. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1539. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1540. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1541. self deny: #(1 2) asSet = #(0 2) asSet
  1542. !
  1543. testPrintString
  1544. | set |
  1545. set := Set new.
  1546. self assert: set printString equals: 'a Set ()'.
  1547. set add: 1; add: 3.
  1548. self assert: set printString equals: 'a Set (1 3)'.
  1549. set add: 'foo'.
  1550. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1551. set remove: 1; remove: 3.
  1552. self assert: set printString equals: 'a Set (''foo'')'.
  1553. set add: 3.
  1554. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1555. set add: 3.
  1556. self assert: set printString equals: 'a Set (''foo'' 3)'
  1557. !
  1558. testSize
  1559. self assert: Set new size equals: 0.
  1560. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1561. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1562. !
  1563. testUnboxedObjects
  1564. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1565. !
  1566. testUnicity
  1567. | set |
  1568. set := Set new.
  1569. set add: 21.
  1570. set add: 'hello'.
  1571. set add: 21.
  1572. self assert: set size equals: 2.
  1573. set add: 'hello'.
  1574. self assert: set size equals: 2.
  1575. self assert: set asArray equals: #(21 'hello')
  1576. ! !
  1577. TestCase subclass: #StreamTest
  1578. instanceVariableNames: ''
  1579. package: 'Kernel-Tests'!
  1580. !StreamTest methodsFor: 'accessing'!
  1581. collectionClass
  1582. ^ self class collectionClass
  1583. !
  1584. newCollection
  1585. ^ self collectionClass new
  1586. !
  1587. newStream
  1588. ^ self collectionClass new stream
  1589. ! !
  1590. !StreamTest methodsFor: 'tests'!
  1591. testAtStartAtEnd
  1592. | stream |
  1593. stream := self newStream.
  1594. self assert: stream atStart.
  1595. self assert: stream atEnd.
  1596. stream nextPutAll: self newCollection.
  1597. self assert: stream atEnd.
  1598. self deny: stream atStart.
  1599. stream position: 1.
  1600. self deny: stream atEnd.
  1601. self deny: stream atStart
  1602. !
  1603. testContents
  1604. | stream |
  1605. stream := self newStream.
  1606. stream nextPutAll: self newCollection.
  1607. self assert: stream contents equals: self newCollection
  1608. !
  1609. testIsEmpty
  1610. | stream |
  1611. stream := self newStream.
  1612. self assert: stream isEmpty.
  1613. stream nextPutAll: self newCollection.
  1614. self deny: stream isEmpty
  1615. !
  1616. testPosition
  1617. | collection stream |
  1618. collection := self newCollection.
  1619. stream := self newStream.
  1620. stream nextPutAll: collection.
  1621. self assert: stream position equals: collection size.
  1622. stream position: 0.
  1623. self assert: stream position equals: 0.
  1624. stream next.
  1625. self assert: stream position equals: 1.
  1626. stream next.
  1627. self assert: stream position equals: 2
  1628. !
  1629. testReading
  1630. | stream collection |
  1631. collection := self newCollection.
  1632. stream := self newStream.
  1633. stream
  1634. nextPutAll: collection;
  1635. position: 0.
  1636. collection do: [ :each |
  1637. self assert: stream next equals: each ].
  1638. self assert: stream next isNil
  1639. !
  1640. testStreamContents
  1641. !
  1642. testWrite
  1643. | stream collection |
  1644. collection := self newCollection.
  1645. stream := self newStream.
  1646. collection do: [ :each | stream << each ].
  1647. self assert: stream contents equals: collection
  1648. !
  1649. testWriting
  1650. | stream collection |
  1651. collection := self newCollection.
  1652. stream := self newStream.
  1653. collection do: [ :each | stream nextPut: each ].
  1654. self assert: stream contents equals: collection.
  1655. stream := self newStream.
  1656. stream nextPutAll: collection.
  1657. self assert: stream contents equals: collection
  1658. ! !
  1659. !StreamTest class methodsFor: 'accessing'!
  1660. collectionClass
  1661. ^ nil
  1662. ! !
  1663. !StreamTest class methodsFor: 'testing'!
  1664. isAbstract
  1665. ^ self collectionClass isNil
  1666. ! !
  1667. StreamTest subclass: #ArrayStreamTest
  1668. instanceVariableNames: ''
  1669. package: 'Kernel-Tests'!
  1670. !ArrayStreamTest methodsFor: 'accessing'!
  1671. newCollection
  1672. ^ { true. 1. 3@4. 'foo' }
  1673. ! !
  1674. !ArrayStreamTest class methodsFor: 'accessing'!
  1675. collectionClass
  1676. ^ Array
  1677. ! !
  1678. StreamTest subclass: #StringStreamTest
  1679. instanceVariableNames: ''
  1680. package: 'Kernel-Tests'!
  1681. !StringStreamTest methodsFor: 'accessing'!
  1682. newCollection
  1683. ^ 'hello world'
  1684. ! !
  1685. !StringStreamTest class methodsFor: 'accessing'!
  1686. collectionClass
  1687. ^ String
  1688. ! !
  1689. TestCase subclass: #UndefinedTest
  1690. instanceVariableNames: ''
  1691. package: 'Kernel-Tests'!
  1692. !UndefinedTest methodsFor: 'tests'!
  1693. testCopying
  1694. self assert: nil copy equals: nil
  1695. !
  1696. testDeepCopy
  1697. self assert: nil deepCopy = nil
  1698. !
  1699. testIfNil
  1700. self assert: (nil ifNil: [ true ]) equals: true.
  1701. self deny: (nil ifNotNil: [ true ]) = true.
  1702. self assert: (nil ifNil: [ true ] ifNotNil: [ false ]) equals: true.
  1703. self deny: (nil ifNotNil: [ true ] ifNil: [ false ]) = true
  1704. !
  1705. testIsNil
  1706. self assert: nil isNil.
  1707. self deny: nil notNil.
  1708. ! !