Kernel-Tests.st 64 KB

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