Kernel-Tests.st 53 KB

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