Kernel-Tests.st 63 KB

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