Kernel-Tests.st 62 KB

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