Kernel-Tests.st 55 KB

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