Kernel-Tests.st 77 KB

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