Kernel-Tests.st 58 KB

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