Kernel-Tests.st 74 KB

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