Kernel-Tests.st 59 KB

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