Kernel-Tests.st 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. Smalltalk current createPackage: 'Kernel-Tests'!
  2. TestCase subclass: #AnnouncementSubscriptionTest
  3. instanceVariableNames: ''
  4. package: 'Kernel-Tests'!
  5. !AnnouncementSubscriptionTest methodsFor: 'tests'!
  6. testHandlesAnnouncement
  7. | subscription announcementClass1 announcementClass2 classBuilder |
  8. classBuilder := ClassBuilder new.
  9. announcementClass1 := classBuilder basicAddSubclassOf: SystemAnnouncement named: 'TestAnnouncement1' instanceVariableNames: #() package: 'Kernel-Tests'.
  10. subscription := AnnouncementSubscription new announcementClass: SystemAnnouncement.
  11. "Test whether the same class triggers the announcement"
  12. self assert: (subscription handlesAnnouncement: SystemAnnouncement) equals: true.
  13. "Test whether a subclass triggers the announcement"
  14. self assert: (subscription handlesAnnouncement: announcementClass1) equals: true.
  15. "Test whether an unrelated class does not trigger the announcement"
  16. self assert: (subscription handlesAnnouncement: Object) equals: false.
  17. classBuilder basicRemoveClass: announcementClass1.
  18. ! !
  19. TestCase subclass: #BlockClosureTest
  20. instanceVariableNames: ''
  21. package: 'Kernel-Tests'!
  22. !BlockClosureTest methodsFor: 'tests'!
  23. testCanClearInterval
  24. self shouldnt: [([Error new signal] valueWithInterval: 0) clearInterval] raise: Error
  25. !
  26. testCanClearTimeout
  27. self shouldnt: [([Error new signal] valueWithTimeout: 0) clearTimeout] raise: Error
  28. !
  29. testCompiledSource
  30. self assert: ([1+1] compiledSource includesSubString: 'function')
  31. !
  32. testCurrySelf
  33. | curriedMethod array |
  34. curriedMethod := [ :selfarg :x | selfarg at: x ] currySelf asCompiledMethod: 'foo:'.
  35. array := #(3 1 4).
  36. ClassBuilder new installMethod: curriedMethod forClass: Array category: '**test helper'.
  37. [ self assert: (array foo: 2) equals: 1 ]
  38. ensure: [ Array removeCompiledMethod: curriedMethod ]
  39. !
  40. testEnsure
  41. self assert: ([3] ensure: [4]) equals: 3
  42. !
  43. testEnsureRaises
  44. self should: [[Error new signal] ensure: [true]] raise: Error
  45. !
  46. testExceptionSemantics
  47. "See https://github.com/NicolasPetton/amber/issues/314"
  48. self timeout: 100.
  49. (self async: [
  50. [
  51. self assert: true.
  52. Error signal.
  53. "The following should *not* be run"
  54. self deny: true.
  55. self finished.
  56. ] on: Error do: [ :ex | self finished ]
  57. ]) valueWithTimeout: 0
  58. !
  59. testNewWithValues
  60. <
  61. function theTestPrototype() {this.name = "theTestPrototype";}
  62. function theTestConstructor(arg1, arg2, arg3) {}
  63. theTestConstructor.prototype = new theTestPrototype;
  64. var theWrappedConstructor = _st(theTestConstructor);
  65. var theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);
  66. self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
  67. "newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
  68. function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
  69. self._should_raise_(_st(constructionShouldFail), smalltalk.Error);
  70. >
  71. !
  72. testNumArgs
  73. self assert: [] numArgs equals: 0.
  74. self assert: [:a :b | ] numArgs equals: 2
  75. !
  76. testOnDo
  77. self assert: ([Error new signal] on: Error do: [:ex | true])
  78. !
  79. testValue
  80. self assert: ([1+1] value) equals: 2.
  81. self assert: ([:x | x +1] value: 2) equals: 3.
  82. self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
  83. "Arguments are optional in Amber. This isn't ANSI compliant."
  84. self assert: ([:a :b :c | 1] value) equals: 1
  85. !
  86. testValueWithPossibleArguments
  87. self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
  88. self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
  89. self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  90. !
  91. testWhileFalse
  92. | i |
  93. i := 0.
  94. [i > 5] whileFalse: [i := i + 1].
  95. self assert: i equals: 6.
  96. i := 0.
  97. [i := i + 1. i > 5] whileFalse.
  98. self assert: i equals: 6
  99. !
  100. testWhileTrue
  101. | i |
  102. i := 0.
  103. [i < 5] whileTrue: [i := i + 1].
  104. self assert: i equals: 5.
  105. i := 0.
  106. [i := i + 1. i < 5] whileTrue.
  107. self assert: i equals: 5
  108. ! !
  109. TestCase subclass: #BooleanTest
  110. instanceVariableNames: ''
  111. package: 'Kernel-Tests'!
  112. !BooleanTest methodsFor: 'tests'!
  113. testEquality
  114. "We're on top of JS...just be sure to check the basics!!"
  115. self deny: 0 = false.
  116. self deny: false = 0.
  117. self deny: '' = false.
  118. self deny: false = ''.
  119. self assert: (true = true).
  120. self deny: false = true.
  121. self deny: true = false.
  122. self assert: (false = false).
  123. "JS may do some type coercing after sending a message"
  124. self assert: (true yourself = true).
  125. self assert: (true yourself = true yourself)
  126. !
  127. testIdentity
  128. "We're on top of JS...just be sure to check the basics!!"
  129. self deny: 0 == false.
  130. self deny: false == 0.
  131. self deny: '' == false.
  132. self deny: false == ''.
  133. self assert: true == true.
  134. self deny: false == true.
  135. self deny: true == false.
  136. self assert: false == false.
  137. "JS may do some type coercing after sending a message"
  138. self assert: true yourself == true.
  139. self assert: true yourself == true yourself
  140. !
  141. testIfTrueIfFalse
  142. self assert: (true ifTrue: ['alternative block']) equals: 'alternative block'.
  143. self assert: (true ifFalse: ['alternative block']) equals: nil.
  144. self assert: (false ifTrue: ['alternative block']) equals: nil.
  145. self assert: (false ifFalse: ['alternative block']) equals: 'alternative block'.
  146. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block2'.
  147. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block'.
  148. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block'.
  149. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block2'.
  150. !
  151. testIfTrueIfFalseWithBoxing
  152. self assert: (true yourself ifTrue: ['alternative block']) equals: 'alternative block'.
  153. self assert: (true yourself ifFalse: ['alternative block']) equals: nil.
  154. self assert: (false yourself ifTrue: ['alternative block']) equals: nil.
  155. self assert: (false yourself ifFalse: ['alternative block']) equals: 'alternative block'.
  156. self assert: (false yourself ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block2'.
  157. self assert: (false yourself ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block'.
  158. self assert: (true yourself ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block'.
  159. self assert: (true yourself ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block2'.
  160. !
  161. testLogic
  162. "Trivial logic table"
  163. self assert: (true & true);
  164. deny: (true & false);
  165. deny: (false & true);
  166. deny: (false & false).
  167. self assert: (true | true);
  168. assert: (true | false);
  169. assert: (false | true);
  170. deny: (false | false).
  171. "Checking that expressions work fine too"
  172. self assert: (true & (1 > 0));
  173. deny: ((1 > 0) & false);
  174. deny: ((1 > 0) & (1 > 2)).
  175. self assert: (false | (1 > 0));
  176. assert: ((1 > 0) | false);
  177. assert: ((1 > 0) | (1 > 2))
  178. !
  179. testLogicKeywords
  180. "Trivial logic table"
  181. self
  182. assert: (true and: [ true]);
  183. deny: (true and: [ false ]);
  184. deny: (false and: [ true ]);
  185. deny: (false and: [ false ]).
  186. self
  187. assert: (true or: [ true ]);
  188. assert: (true or: [ false ]);
  189. assert: (false or: [ true ]);
  190. deny: (false or: [ false ]).
  191. "Checking that expressions work fine too"
  192. self
  193. assert: (true and: [ 1 > 0 ]);
  194. deny: ((1 > 0) and: [ false ]);
  195. deny: ((1 > 0) and: [ 1 > 2 ]).
  196. self
  197. assert: (false or: [ 1 > 0 ]);
  198. assert: ((1 > 0) or: [ false ]);
  199. assert: ((1 > 0) or: [ 1 > 2 ])
  200. !
  201. testNonBooleanError
  202. self should: [ '' ifTrue: [] ifFalse: [] ] raise: NonBooleanReceiver
  203. ! !
  204. TestCase subclass: #ClassBuilderTest
  205. instanceVariableNames: 'builder theClass'
  206. package: 'Kernel-Tests'!
  207. !ClassBuilderTest methodsFor: 'running'!
  208. setUp
  209. builder := ClassBuilder new
  210. !
  211. tearDown
  212. theClass ifNotNil: [Smalltalk current removeClass: theClass. theClass := nil]
  213. ! !
  214. !ClassBuilderTest methodsFor: 'tests'!
  215. testClassCopy
  216. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  217. self assert: theClass superclass == ObjectMock superclass.
  218. self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
  219. self assert: theClass name equals: 'ObjectMock2'.
  220. self assert: theClass package == ObjectMock package.
  221. self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
  222. !
  223. testClassMigration
  224. | instance oldClass |
  225. oldClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  226. instance := (Smalltalk current at: 'ObjectMock2') new.
  227. "Change the superclass of ObjectMock2"
  228. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  229. instanceVariableNames: ''
  230. package: 'Kernel-Tests'.
  231. self deny: oldClass == ObjectMock2.
  232. self assert: ObjectMock2 superclass == ObjectMock.
  233. self assert: ObjectMock2 instanceVariableNames isEmpty.
  234. self assert: ObjectMock2 selectors equals: oldClass selectors.
  235. self assert: ObjectMock2 comment equals: oldClass comment.
  236. self assert: ObjectMock2 package name equals: 'Kernel-Tests'.
  237. self deny: instance class == ObjectMock2.
  238. "Commeting this out. Tests implementation detail."
  239. "self assert: instance class name equals: 'OldObjectMock2'."
  240. self assert: (Smalltalk current at: instance class name) isNil.
  241. Smalltalk current removeClass: ObjectMock2
  242. !
  243. testClassMigrationWithClassInstanceVariables
  244. builder copyClass: ObjectMock named: 'ObjectMock2'.
  245. ObjectMock2 class instanceVariableNames: 'foo bar'.
  246. "Change the superclass of ObjectMock2"
  247. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  248. instanceVariableNames: ''
  249. package: 'Kernel-Tests'.
  250. self assert: ObjectMock2 class instanceVariableNames equals: #('foo' 'bar').
  251. Smalltalk current removeClass: ObjectMock2
  252. !
  253. testClassMigrationWithSubclasses
  254. builder copyClass: ObjectMock named: 'ObjectMock2'.
  255. ObjectMock2 subclass: 'ObjectMock3' instanceVariableNames: '' package: 'Kernel-Tests'.
  256. ObjectMock3 subclass: 'ObjectMock4' instanceVariableNames: '' package: 'Kernel-Tests'.
  257. "Change the superclass of ObjectMock2"
  258. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  259. instanceVariableNames: ''
  260. package: 'Kernel-Tests'.
  261. self assert: (ObjectMock subclasses includes: ObjectMock2).
  262. self assert: (ObjectMock2 subclasses includes: ObjectMock3).
  263. self assert: (ObjectMock3 subclasses includes: ObjectMock4).
  264. ObjectMock allSubclasses do: [ :each | Smalltalk current removeClass: each ]
  265. !
  266. testInstanceVariableNames
  267. self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
  268. ! !
  269. TestCase subclass: #CollectionTest
  270. instanceVariableNames: ''
  271. package: 'Kernel-Tests'!
  272. !CollectionTest methodsFor: 'accessing'!
  273. collection
  274. ^ self collectionClass withAll: self defaultValues
  275. !
  276. collectionClass
  277. ^ self class collectionClass
  278. !
  279. collectionWithDuplicates
  280. ^ self collectionClass withAll: #('a' 'b' 'c' 1 2 1 'a')
  281. !
  282. defaultValues
  283. ^ #(1 2 3 -4)
  284. ! !
  285. !CollectionTest methodsFor: 'convenience'!
  286. assertSameContents: aCollection as: anotherCollection
  287. self assert: (aCollection size = anotherCollection size).
  288. aCollection do: [ :each |
  289. self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
  290. ! !
  291. !CollectionTest methodsFor: 'testing'!
  292. isCollectionReadOnly
  293. ^ false
  294. ! !
  295. !CollectionTest methodsFor: 'tests'!
  296. testAsArray
  297. self
  298. assertSameContents: self collection
  299. as: self collection asArray
  300. !
  301. testAsOrderedCollection
  302. self
  303. assertSameContents: self collection
  304. as: self collection asOrderedCollection
  305. !
  306. testAsSet
  307. | c set |
  308. c := self collectionWithDuplicates.
  309. set := c asSet.
  310. self assert: set size equals: 5.
  311. c do: [ :each |
  312. self assert: (set includes: each) ]
  313. !
  314. testCollect
  315. | newCollection |
  316. newCollection := #(1 2 3 4).
  317. self
  318. assertSameContents: (self collection collect: [ :each |
  319. each abs ])
  320. as: newCollection
  321. !
  322. testDetect
  323. self assert: (self collection detect: [ :each | each < 0 ]) equals: -4.
  324. self
  325. should: [ self collection detect: [ :each | each = 6 ] ]
  326. raise: Error
  327. !
  328. testDo
  329. | newCollection |
  330. newCollection := OrderedCollection new.
  331. self collection do: [ :each |
  332. newCollection add: each ].
  333. self
  334. assertSameContents: self collection
  335. as: newCollection
  336. !
  337. testIsEmpty
  338. self assert: self collectionClass new isEmpty.
  339. self deny: self collection isEmpty
  340. !
  341. testSelect
  342. | newCollection |
  343. newCollection := #(2 -4).
  344. self
  345. assertSameContents: (self collection select: [ :each |
  346. each even ])
  347. as: newCollection
  348. !
  349. testSize
  350. self assert: self collectionClass new size equals: 0.
  351. self assert: self collection size equals: 4
  352. ! !
  353. !CollectionTest class methodsFor: 'accessing'!
  354. collectionClass
  355. ^ nil
  356. ! !
  357. !CollectionTest class methodsFor: 'testing'!
  358. isAbstract
  359. ^ self collectionClass isNil
  360. ! !
  361. CollectionTest subclass: #IndexableCollectionTest
  362. instanceVariableNames: ''
  363. package: 'Kernel-Tests'!
  364. !IndexableCollectionTest methodsFor: 'tests'!
  365. testAt
  366. self assert: (self collection at: 4) equals: -4.
  367. self should: [ self collection at: 5 ] raise: Error
  368. !
  369. testAtIfAbsent
  370. self assert: (self collection at: (self collection size + 1) ifAbsent: [ 'none' ]) equals: 'none'
  371. !
  372. testContains
  373. | collection |
  374. collection := self collection.
  375. self assert: (self collection contains: [ :each | each = self collection first ]).
  376. self deny: (self collection contains: [ :each | each = Object new ])
  377. !
  378. testIndexOf
  379. self assert: (self collection indexOf: 2) equals: 2.
  380. self should: [ self collection indexOf: 999 ] raise: Error.
  381. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  382. !
  383. testWithIndexDo
  384. | collection |
  385. collection := self collection.
  386. self collection withIndexDo: [ :each :index |
  387. self assert: (collection at: index) equals: each ]
  388. ! !
  389. IndexableCollectionTest subclass: #HashedCollectionTest
  390. instanceVariableNames: ''
  391. package: 'Kernel-Tests'!
  392. !HashedCollectionTest methodsFor: 'accessing'!
  393. collection
  394. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  395. !
  396. collectionWithDuplicates
  397. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
  398. ! !
  399. !HashedCollectionTest methodsFor: 'tests'!
  400. testAsDictionary
  401. self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
  402. !
  403. testAt
  404. self assert: (self collection at: 'a') equals: 2.
  405. self should: [ self collection at: 5 ] raise: Error
  406. !
  407. testContains
  408. | collection |
  409. collection := self collection.
  410. self assert: (self collection contains: [ :each | each = self collection values first ]).
  411. self deny: (self collection contains: [ :each | each = Object new ])
  412. !
  413. testFrom
  414. "Accept a collection of associations."
  415. | associations |
  416. associations := { 'a' -> 1. 'b' -> 2 }.
  417. self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  418. !
  419. testIndexOf
  420. self assert: (self collection indexOf: 2) equals: 'a'.
  421. self should: [ self collection indexOf: 999 ] raise: Error.
  422. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  423. !
  424. testNewFromPairs
  425. "Accept an array in which all odd indexes are keys and evens are values."
  426. | flattenedAssociations |
  427. flattenedAssociations := { 'a'. 1. 'b'. 2 }.
  428. self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  429. ! !
  430. !HashedCollectionTest class methodsFor: 'accessing'!
  431. collectionClass
  432. ^ HashedCollection
  433. ! !
  434. HashedCollectionTest subclass: #DictionaryTest
  435. instanceVariableNames: ''
  436. package: 'Kernel-Tests'!
  437. !DictionaryTest methodsFor: 'accessing'!
  438. collection
  439. ^ Dictionary new
  440. at: 1 put: 1;
  441. at: 'a' put: 2;
  442. at: true put: 3;
  443. at: 4 put: -4;
  444. yourself
  445. !
  446. collectionWithDuplicates
  447. ^ Dictionary new
  448. at: 1 put: 1;
  449. at: 'a' put: 2;
  450. at: true put: 3;
  451. at: 4 put: -4;
  452. at: 'b' put: 1;
  453. at: 3 put: 3;
  454. at: false put: 12;
  455. yourself
  456. ! !
  457. !DictionaryTest methodsFor: 'tests'!
  458. testAccessing
  459. | d |
  460. d := Dictionary new.
  461. d at: 'hello' put: 'world'.
  462. self assert: (d at: 'hello') equals: 'world'.
  463. self assert: (d at: 'hello' ifAbsent: [nil]) equals: 'world'.
  464. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  465. self assert: (d includesKey: 'hello').
  466. self deny: (d includesKey: 'foo').
  467. d at: 1 put: 2.
  468. self assert: (d at: 1) equals: 2.
  469. d at: 1@3 put: 3.
  470. self assert: (d at: 1@3) equals: 3.
  471. self assert: (d includesKey: 1@3).
  472. self deny: (d includesKey: 3@1)
  473. !
  474. testAsHashedCollection
  475. self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
  476. !
  477. testDynamicDictionaries
  478. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  479. !
  480. testEquality
  481. | d1 d2 |
  482. self assert: (Dictionary new = Dictionary new).
  483. d1 := Dictionary new at: 1 put: 2; yourself.
  484. d2 := Dictionary new at: 1 put: 2; yourself.
  485. self assert: (d1 = d2).
  486. d2 := Dictionary new at: 1 put: 3; yourself.
  487. self deny: d1 = d2.
  488. d2 := Dictionary new at: 2 put: 2; yourself.
  489. self deny: d1 = d2.
  490. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  491. self deny: d1 = d2.
  492. !
  493. testIfAbsent
  494. | d visited |
  495. visited := false.
  496. d := Dictionary new.
  497. d at: 'hello' ifAbsent: [ visited := true ].
  498. self assert: visited.
  499. !
  500. testIfPresent
  501. | d visited absent |
  502. visited := false.
  503. d := Dictionary new.
  504. d at: 'hello' put: 'world'.
  505. d at: 'hello' ifPresent: [ :value | visited := value ].
  506. self assert: visited equals: 'world'.
  507. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  508. self assert: absent isNil.
  509. !
  510. testIfPresentIfAbsent
  511. | d visited |
  512. visited := false.
  513. d := Dictionary new.
  514. d at: 'hello' put: 'world'.
  515. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  516. self assert: visited equals: 'world'.
  517. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  518. self assert: visited.
  519. !
  520. testKeys
  521. | d |
  522. d := Dictionary new.
  523. d at: 1 put: 2.
  524. d at: 2 put: 3.
  525. d at: 3 put: 4.
  526. self assert: d keys equals: #(1 2 3)
  527. !
  528. testPointKey
  529. | d |
  530. d := Dictionary new.
  531. d at: 1@1 put: 'foo'.
  532. self assert: (d at: 1@1) equals: 'foo'.
  533. d at: 1@1 put: 'bar'.
  534. self assert: (d at: 1@1) equals: 'bar'.
  535. d removeKey: 1@1.
  536. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  537. self deny: (d includesKey: 1@1)
  538. !
  539. testPrintString
  540. self
  541. assert: (Dictionary new
  542. at:'firstname' put: 'James';
  543. at:'lastname' put: 'Bond';
  544. printString)
  545. equals: 'a Dictionary (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  546. !
  547. testRemoveKey
  548. | d key |
  549. d := Dictionary new.
  550. d at: 1 put: 2.
  551. d at: 2 put: 3.
  552. d at: 3 put: 4.
  553. key := 2.
  554. self assert: d keys equals: #(1 2 3).
  555. d removeKey: key.
  556. self assert: d keys equals: #(1 3).
  557. self assert: d values equals: #(2 4).
  558. self deny: (d includesKey: 2)
  559. !
  560. testRemoveKeyIfAbsent
  561. | d key |
  562. d := Dictionary new.
  563. d at: 1 put: 2.
  564. d at: 2 put: 3.
  565. d at: 3 put: 4.
  566. key := 2.
  567. self assert: (d removeKey: key) equals: 3.
  568. key := 3.
  569. self assert: (d removeKey: key ifAbsent: [42]) equals: 4.
  570. key := 'why'.
  571. self assert: (d removeKey: key ifAbsent: [42] ) equals: 42.
  572. !
  573. testSize
  574. | d |
  575. d := Dictionary new.
  576. self assert: d size equals: 0.
  577. d at: 1 put: 2.
  578. self assert: d size equals: 1.
  579. d at: 2 put: 3.
  580. self assert: d size equals: 2.
  581. !
  582. testValues
  583. | d |
  584. d := Dictionary new.
  585. d at: 1 put: 2.
  586. d at: 2 put: 3.
  587. d at: 3 put: 4.
  588. self assert: d values equals: #(2 3 4)
  589. ! !
  590. !DictionaryTest class methodsFor: 'accessing'!
  591. collectionClass
  592. ^ Dictionary
  593. ! !
  594. IndexableCollectionTest subclass: #SequenceableCollectionTest
  595. instanceVariableNames: ''
  596. package: 'Kernel-Tests'!
  597. SequenceableCollectionTest subclass: #ArrayTest
  598. instanceVariableNames: ''
  599. package: 'Kernel-Tests'!
  600. !ArrayTest methodsFor: 'tests'!
  601. testAddFirst
  602. self assert: (#(2 3 4 5) addFirst: 1; yourself) equals: #(1 2 3 4 5)
  603. !
  604. testAtIfAbsent
  605. | array |
  606. array := #('hello' 'world').
  607. self assert: (array at: 1) equals: 'hello'.
  608. self assert: (array at: 2) equals: 'world'.
  609. self assert: (array at: 2 ifAbsent: ['not found']) equals: 'world'.
  610. self assert: (array at: 0 ifAbsent: ['not found']) equals: 'not found'.
  611. self assert: (array at: -10 ifAbsent: ['not found']) equals: 'not found'.
  612. self assert: (array at: 3 ifAbsent: ['not found']) equals: 'not found'.
  613. !
  614. testFirstN
  615. self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
  616. !
  617. testIfEmpty
  618. self assert: ( '' ifEmpty: ['zork'] ) equals: 'zork'
  619. !
  620. testPrintString
  621. | array |
  622. array := Array new.
  623. self assert: array printString equals: 'an Array ()'.
  624. array add: 1; add: 3.
  625. self assert: array printString equals: 'an Array (1 3)'.
  626. array add: 'foo'.
  627. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  628. array remove: 1; remove: 3.
  629. self assert: array printString equals: 'an Array (''foo'')'.
  630. array addLast: 3.
  631. self assert: array printString equals: 'an Array (''foo'' 3)'.
  632. array addLast: 3.
  633. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  634. !
  635. testRemoveFromTo
  636. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  637. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  638. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  639. !
  640. testRemoveIndex
  641. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  642. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  643. self assert: (#('hello') removeIndex: 1) equals: #()
  644. ! !
  645. !ArrayTest class methodsFor: 'accessing'!
  646. collectionClass
  647. ^ Array
  648. ! !
  649. SequenceableCollectionTest subclass: #StringTest
  650. instanceVariableNames: ''
  651. package: 'Kernel-Tests'!
  652. !StringTest methodsFor: 'accessing'!
  653. collection
  654. ^'hello'
  655. !
  656. collectionWithDuplicates
  657. ^ 'abbaerte'
  658. ! !
  659. !StringTest methodsFor: 'tests'!
  660. testAddRemove
  661. self should: ['hello' add: 'a'] raise: Error.
  662. self should: ['hello' remove: 'h'] raise: Error
  663. !
  664. testAsArray
  665. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  666. !
  667. testAsNumber
  668. self assert: '3' asNumber equals: 3.
  669. self assert: '-3' asNumber equals: -3.
  670. self assert: '-1.5' asNumber equals: -1.5.
  671. !
  672. testAt
  673. self assert: ('hello' at: 1) equals: 'h'.
  674. self assert: ('hello' at: 5) equals: 'o'.
  675. self assert: ('hello' at: 6 ifAbsent: [nil]) equals: nil
  676. !
  677. testAtPut
  678. "String instances are read-only"
  679. self should: ['hello' at: 1 put: 'a'] raise: Error
  680. !
  681. testCollect
  682. | newCollection |
  683. newCollection := 'hheelllloo'.
  684. self
  685. assertSameContents: (self collection collect: [ :each |
  686. each, each ])
  687. as: newCollection
  688. !
  689. testCopyWithoutAll
  690. self
  691. assert: ('*hello* *world*' copyWithoutAll: '*')
  692. equals: 'hello world'
  693. !
  694. testDetect
  695. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  696. self
  697. should: [ self collection detect: [ :each | each = 6 ] ]
  698. raise: Error
  699. !
  700. testEquality
  701. self assert: 'hello' equals: 'hello'.
  702. self deny: 'hello' = 'world'.
  703. "Test for issue 459"
  704. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  705. self assert: 'hello' equals: 'hello' yourself.
  706. self assert: 'hello' yourself equals: 'hello'.
  707. "test JS falsy value"
  708. self deny: '' = 0
  709. !
  710. testIdentity
  711. self assert: 'hello' == 'hello'.
  712. self deny: 'hello' == 'world'.
  713. self assert: 'hello' == 'hello' yourself.
  714. self assert: 'hello' yourself == 'hello'.
  715. "test JS falsy value"
  716. self deny: '' == 0
  717. !
  718. testIncludesSubString
  719. self assert: ('amber' includesSubString: 'ber').
  720. self deny: ('amber' includesSubString: 'zork').
  721. !
  722. testIndexOf
  723. self assert: (self collection indexOf: 'e') equals: 2.
  724. self should: [ self collection indexOf: 999 ] raise: Error.
  725. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  726. !
  727. testJoin
  728. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  729. !
  730. testSelect
  731. | newCollection |
  732. newCollection := 'o'.
  733. self
  734. assertSameContents: (self collection select: [ :each |
  735. each = 'o' ])
  736. as: newCollection
  737. !
  738. testSize
  739. self assert: 'smalltalk' size equals: 9.
  740. self assert: '' size equals: 0
  741. !
  742. testStreamContents
  743. self
  744. assert: (String streamContents: [ :aStream |
  745. aStream
  746. nextPutAll: 'hello'; space;
  747. nextPutAll: 'world' ])
  748. equals: 'hello world'
  749. !
  750. testCapitalized
  751. self assert: 'test' capitalized equals: 'Test'.
  752. self assert: 'Test' capitalized equals: 'Test'.
  753. self assert: '' capitalized equals: ''.
  754. self assert: 'Test' isCapitalized equals: true.
  755. self assert: 'test' isCapitalized equals: false.
  756. ! !
  757. !StringTest class methodsFor: 'accessing'!
  758. collectionClass
  759. ^ String
  760. ! !
  761. TestCase subclass: #ConsoleTranscriptTest
  762. instanceVariableNames: ''
  763. package: 'Kernel-Tests'!
  764. !ConsoleTranscriptTest methodsFor: 'tests'!
  765. testShow
  766. | originalTranscript |
  767. originalTranscript := Transcript current.
  768. Transcript register: ConsoleTranscript new.
  769. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  770. self shouldnt: [ Transcript show: console ] raise: Error.
  771. Transcript register: originalTranscript.
  772. ! !
  773. TestCase subclass: #JSObjectProxyTest
  774. instanceVariableNames: ''
  775. package: 'Kernel-Tests'!
  776. !JSObjectProxyTest methodsFor: 'accessing'!
  777. jsObject
  778. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': void 0}>
  779. ! !
  780. !JSObjectProxyTest methodsFor: 'tests'!
  781. testAtIfAbsent
  782. | testObject |
  783. testObject := self jsObject.
  784. self assert: (testObject at: 'abc' ifAbsent: ['Property does not exist']) equals: 'Property does not exist'.
  785. self assert: (testObject at: 'e' ifAbsent: ['Property does not exist']) equals: nil.
  786. self assert: (testObject at: 'a' ifAbsent: ['Property does not exist']) equals: 1.
  787. self assert: (testObject at: 'f' ifAbsent: ['Property does not exist']) equals: nil.
  788. !
  789. testAtIfPresent
  790. | testObject |
  791. testObject := self jsObject.
  792. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  793. self assert: (testObject at: 'e' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello nil'.
  794. self assert: (testObject at: 'a' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello 1'.
  795. self assert: (testObject at: 'f' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello nil'.
  796. !
  797. testAtIfPresentIfAbsent
  798. | testObject |
  799. testObject := self jsObject.
  800. self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'not present'.
  801. self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  802. self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello 1'.
  803. self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  804. !
  805. testDNU
  806. self should: [self jsObject foo] raise: MessageNotUnderstood
  807. !
  808. testMessageSend
  809. self assert: self jsObject a equals: 1.
  810. self assert: self jsObject b equals: 2.
  811. self assert: (self jsObject c: 3) equals: 3
  812. !
  813. testMethodWithArguments
  814. self assert: (self jsObject c: 1) equals: 1
  815. !
  816. testPrinting
  817. self assert: self jsObject printString equals: '[object Object]'
  818. !
  819. testPropertyThatReturnsEmptyString
  820. | object |
  821. object := self jsObject.
  822. self assert: object d equals: ''.
  823. object d: 'hello'.
  824. self assert: object d equals: 'hello'
  825. !
  826. testPropertyThatReturnsUndefined
  827. | object |
  828. object := self jsObject.
  829. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  830. self assert: object e isNil
  831. !
  832. testValue
  833. | testObject |
  834. testObject := self jsObject.
  835. self assert: testObject value printString equals: '[object Object]'.
  836. testObject at: 'value' put: 'aValue'.
  837. self assert: testObject value equals: 'aValue'
  838. !
  839. testYourself
  840. | object |
  841. object := self jsObject
  842. d: 'test';
  843. yourself.
  844. self assert: object d equals: 'test'
  845. ! !
  846. TestCase subclass: #JavaScriptExceptionTest
  847. instanceVariableNames: ''
  848. package: 'Kernel-Tests'!
  849. !JavaScriptExceptionTest methodsFor: 'helpers'!
  850. throwException
  851. <throw 'test'>
  852. ! !
  853. !JavaScriptExceptionTest methodsFor: 'tests'!
  854. testCatchingException
  855. [ self throwException ]
  856. on: Error
  857. do: [ :error |
  858. self assert: error exception = 'test' ]
  859. !
  860. testRaisingException
  861. self should: [ self throwException ] raise: JavaScriptException
  862. ! !
  863. TestCase subclass: #MessageSendTest
  864. instanceVariableNames: ''
  865. package: 'Kernel-Tests'!
  866. !MessageSendTest methodsFor: 'tests'!
  867. testValue
  868. | messageSend |
  869. messageSend := MessageSend new
  870. receiver: Object new;
  871. selector: #asString;
  872. yourself.
  873. self assert: messageSend value equals: 'an Object'
  874. !
  875. testValueWithArguments
  876. | messageSend |
  877. messageSend := MessageSend new
  878. receiver: 2;
  879. selector: '+';
  880. yourself.
  881. self assert: (messageSend value: 3) equals: 5.
  882. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  883. ! !
  884. TestCase subclass: #MethodInheritanceTest
  885. instanceVariableNames: 'receiverTop receiverMiddle receiverBottom method performBlock'
  886. package: 'Kernel-Tests'!
  887. !MethodInheritanceTest methodsFor: 'accessing'!
  888. codeGeneratorClass
  889. ^ CodeGenerator
  890. !
  891. targetClassBottom
  892. ^ JavaScriptException
  893. !
  894. targetClassMiddle
  895. ^ Error
  896. !
  897. targetClassTop
  898. ^ Object
  899. ! !
  900. !MethodInheritanceTest methodsFor: 'factory'!
  901. compiler
  902. ^ Compiler new
  903. codeGeneratorClass: self codeGeneratorClass;
  904. yourself
  905. ! !
  906. !MethodInheritanceTest methodsFor: 'initialization'!
  907. setUp
  908. receiverTop := self targetClassTop new.
  909. receiverMiddle := self targetClassMiddle new.
  910. receiverBottom := self targetClassBottom new.
  911. method := nil.
  912. performBlock := [ self error: 'performBlock not initialized' ]
  913. !
  914. tearDown
  915. [ self deinstallTop ] on: Error do: [].
  916. [ self deinstallMiddle ] on: Error do: [].
  917. [ self deinstallBottom ] on: Error do: []
  918. ! !
  919. !MethodInheritanceTest methodsFor: 'testing'!
  920. deinstallBottom
  921. self targetClassBottom removeCompiledMethod: method
  922. !
  923. deinstallMiddle
  924. self targetClassMiddle removeCompiledMethod: method
  925. !
  926. deinstallTop
  927. self targetClassTop removeCompiledMethod: method
  928. !
  929. installBottom: aString
  930. method := self compiler install: aString forClass: self targetClassBottom category: 'tests'
  931. !
  932. installMiddle: aString
  933. method := self compiler install: aString forClass: self targetClassMiddle category: 'tests'
  934. !
  935. installTop: aString
  936. method := self compiler install: aString forClass: self targetClassTop category: 'tests'
  937. !
  938. shouldMNU
  939. self shouldMNUTop.
  940. self shouldMNUMiddle.
  941. self shouldMNUBottom
  942. !
  943. shouldMNUBottom
  944. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  945. !
  946. shouldMNUMiddle
  947. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  948. !
  949. shouldMNUTop
  950. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  951. !
  952. shouldReturn: anObject
  953. | result |
  954. result := performBlock value: receiverTop.
  955. self assert: { 'top'. anObject } equals: { 'top'. result }.
  956. result := performBlock value: receiverMiddle.
  957. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  958. result := performBlock value: receiverBottom.
  959. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  960. !
  961. shouldReturn: anObject and: anObject2 and: anObject3
  962. | result |
  963. result := performBlock value: receiverTop.
  964. self assert: { 'top'. anObject } equals: { 'top'. result }.
  965. result := performBlock value: receiverMiddle.
  966. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  967. result := performBlock value: receiverBottom.
  968. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  969. ! !
  970. !MethodInheritanceTest methodsFor: 'tests'!
  971. testMNU11
  972. performBlock := [ :x | x foo ].
  973. self shouldMNU.
  974. self installTop: 'foo ^ false'.
  975. self installTop: 'foo ^ true'.
  976. self deinstallTop.
  977. self shouldMNU
  978. !
  979. testMNU22
  980. performBlock := [ :x | x foo ].
  981. self shouldMNU.
  982. self installMiddle: 'foo ^ false'.
  983. self installMiddle: 'foo ^ true'.
  984. self deinstallMiddle.
  985. self shouldMNU
  986. !
  987. testReturns1
  988. performBlock := [ :x | x foo ].
  989. self installTop: 'foo ^ false'.
  990. self shouldReturn: false.
  991. self installTop: 'foo ^ true'.
  992. self shouldReturn: true
  993. ! !
  994. TestCase subclass: #NumberTest
  995. instanceVariableNames: ''
  996. package: 'Kernel-Tests'!
  997. !NumberTest methodsFor: 'tests'!
  998. testAbs
  999. self assert: 4 abs equals: 4.
  1000. self assert: -4 abs equals: 4
  1001. !
  1002. testArithmetic
  1003. "We rely on JS here, so we won't test complex behavior, just check if
  1004. message sends are corrects"
  1005. self assert: 1.5 + 1 equals: 2.5.
  1006. self assert: 2 - 1 equals: 1.
  1007. self assert: -2 - 1 equals: -3.
  1008. self assert: 12 / 2 equals: 6.
  1009. self assert: 3 * 4 equals: 12.
  1010. self assert: 7 // 2 equals: 3.
  1011. self assert: 7 \\ 2 equals: 1.
  1012. "Simple parenthesis and execution order"
  1013. self assert: 1 + 2 * 3 equals: 9.
  1014. self assert: 1 + (2 * 3) equals: 7
  1015. !
  1016. testAsNumber
  1017. self assert: 3 asNumber equals: 3.
  1018. !
  1019. testCeiling
  1020. self assert: 1.2 ceiling equals: 2.
  1021. self assert: -1.2 ceiling equals: -1.
  1022. self assert: 1.0 ceiling equals: 1.
  1023. !
  1024. testComparison
  1025. self assert: 3 > 2.
  1026. self assert: 2 < 3.
  1027. self deny: 3 < 2.
  1028. self deny: 2 > 3.
  1029. self assert: 3 >= 3.
  1030. self assert: 3.1 >= 3.
  1031. self assert: 3 <= 3.
  1032. self assert: 3 <= 3.1
  1033. !
  1034. testCopying
  1035. self assert: 1 copy == 1.
  1036. self assert: 1 deepCopy == 1
  1037. !
  1038. testEquality
  1039. self assert: (1 = 1).
  1040. self assert: (0 = 0).
  1041. self deny: (1 = 0).
  1042. self assert: (1 yourself = 1).
  1043. self assert: (1 = 1 yourself).
  1044. self assert: (1 yourself = 1 yourself).
  1045. self deny: 0 = false.
  1046. self deny: false = 0.
  1047. self deny: '' = 0.
  1048. self deny: 0 = ''
  1049. !
  1050. testFloor
  1051. self assert: 1.2 floor equals: 1.
  1052. self assert: -1.2 floor equals: -2.
  1053. self assert: 1.0 floor equals: 1.
  1054. !
  1055. testHexNumbers
  1056. self assert: 16r9 equals: 9.
  1057. self assert: 16rA truncated equals: 10.
  1058. self assert: 16rB truncated equals: 11.
  1059. self assert: 16rC truncated equals: 12.
  1060. self assert: 16rD truncated equals: 13.
  1061. self assert: 16rE truncated equals: 14.
  1062. self assert: 16rF truncated equals: 15
  1063. !
  1064. testIdentity
  1065. self assert: 1 == 1.
  1066. self assert: 0 == 0.
  1067. self deny: 1 == 0.
  1068. self assert: 1 yourself == 1.
  1069. self assert: 1 == 1 yourself.
  1070. self assert: 1 yourself == 1 yourself.
  1071. self deny: 1 == 2
  1072. !
  1073. testInvalidHexNumbers
  1074. self should: [16rG] raise: MessageNotUnderstood.
  1075. self should: [16rg] raise: MessageNotUnderstood.
  1076. self should: [16rH] raise: MessageNotUnderstood.
  1077. self should: [16rh] raise: MessageNotUnderstood.
  1078. self should: [16rI] raise: MessageNotUnderstood.
  1079. self should: [16ri] raise: MessageNotUnderstood.
  1080. self should: [16rJ] raise: MessageNotUnderstood.
  1081. self should: [16rj] raise: MessageNotUnderstood.
  1082. self should: [16rK] raise: MessageNotUnderstood.
  1083. self should: [16rk] raise: MessageNotUnderstood.
  1084. self should: [16rL] raise: MessageNotUnderstood.
  1085. self should: [16rl] raise: MessageNotUnderstood.
  1086. self should: [16rM] raise: MessageNotUnderstood.
  1087. self should: [16rm] raise: MessageNotUnderstood.
  1088. self should: [16rN] raise: MessageNotUnderstood.
  1089. self should: [16rn] raise: MessageNotUnderstood.
  1090. self should: [16rO] raise: MessageNotUnderstood.
  1091. self should: [16ro] raise: MessageNotUnderstood.
  1092. self should: [16rP] raise: MessageNotUnderstood.
  1093. self should: [16rp] raise: MessageNotUnderstood.
  1094. self should: [16rQ] raise: MessageNotUnderstood.
  1095. self should: [16rq] raise: MessageNotUnderstood.
  1096. self should: [16rR] raise: MessageNotUnderstood.
  1097. self should: [16rr] raise: MessageNotUnderstood.
  1098. self should: [16rS] raise: MessageNotUnderstood.
  1099. self should: [16rs] raise: MessageNotUnderstood.
  1100. self should: [16rT] raise: MessageNotUnderstood.
  1101. self should: [16rt] raise: MessageNotUnderstood.
  1102. self should: [16rU] raise: MessageNotUnderstood.
  1103. self should: [16ru] raise: MessageNotUnderstood.
  1104. self should: [16rV] raise: MessageNotUnderstood.
  1105. self should: [16rv] raise: MessageNotUnderstood.
  1106. self should: [16rW] raise: MessageNotUnderstood.
  1107. self should: [16rw] raise: MessageNotUnderstood.
  1108. self should: [16rX] raise: MessageNotUnderstood.
  1109. self should: [16rx] raise: MessageNotUnderstood.
  1110. self should: [16rY] raise: MessageNotUnderstood.
  1111. self should: [16ry] raise: MessageNotUnderstood.
  1112. self should: [16rZ] raise: MessageNotUnderstood.
  1113. self should: [16rz] raise: MessageNotUnderstood.
  1114. self should: [16rABcdEfZ] raise: MessageNotUnderstood.
  1115. !
  1116. testMinMax
  1117. self assert: (2 max: 5) equals: 5.
  1118. self assert: (2 min: 5) equals: 2
  1119. !
  1120. testNegated
  1121. self assert: 3 negated equals: -3.
  1122. self assert: -3 negated equals: 3
  1123. !
  1124. testPrintShowingDecimalPlaces
  1125. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1126. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1127. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1128. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1129. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1130. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1131. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1132. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1133. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1134. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1135. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1136. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  1137. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  1138. !
  1139. testRounded
  1140. self assert: 3 rounded equals: 3.
  1141. self assert: 3.212 rounded equals: 3.
  1142. self assert: 3.51 rounded equals: 4
  1143. !
  1144. testSqrt
  1145. self assert: 4 sqrt equals: 2.
  1146. self assert: 16 sqrt equals: 4
  1147. !
  1148. testSquared
  1149. self assert: 4 squared equals: 16
  1150. !
  1151. testTimesRepeat
  1152. | i |
  1153. i := 0.
  1154. 0 timesRepeat: [i := i + 1].
  1155. self assert: i equals: 0.
  1156. 5 timesRepeat: [i := i + 1].
  1157. self assert: i equals: 5
  1158. !
  1159. testTo
  1160. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  1161. !
  1162. testToBy
  1163. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  1164. self should: [1 to: 4 by: 0] raise: Error
  1165. !
  1166. testTruncated
  1167. self assert: 3 truncated equals: 3.
  1168. self assert: 3.212 truncated equals: 3.
  1169. self assert: 3.51 truncated equals: 3
  1170. !
  1171. testTrigonometry
  1172. self assert: 0 cos equals: 1.
  1173. self assert: 0 sin equals: 0.
  1174. self assert: 0 tan equals: 0.
  1175. self assert: 1 arcCos equals: 0.
  1176. self assert: 0 arcSin equals: 0.
  1177. self assert: 0 arcTan equals: 0.
  1178. !
  1179. testLog
  1180. self assert: 10000 log equals: 4.
  1181. self assert: (512 log: 2) equals: 9.
  1182. self assert: Number e ln equals: 1.
  1183. !
  1184. testRaisedTo
  1185. self assert: (2 raisedTo: 4) equals: 16.
  1186. self assert: (2 raisedTo: 0) equals: 1.
  1187. self assert: (2 raisedTo: -3) equals: 0.125.
  1188. self assert: (4 raisedTo: 0.5) equals: 2.
  1189. self assert: 2 ** 4 equals: 16.
  1190. !
  1191. testSign
  1192. self assert: 5 sign equals: 1.
  1193. self assert: 0 sign equals: 0.
  1194. self assert: -1.4 sign equals: -1.
  1195. ! !
  1196. Object subclass: #ObjectMock
  1197. instanceVariableNames: 'foo bar'
  1198. package: 'Kernel-Tests'!
  1199. !ObjectMock commentStamp!
  1200. ObjectMock is there only to perform tests on classes.!
  1201. !ObjectMock methodsFor: 'not yet classified'!
  1202. foo
  1203. ^foo
  1204. !
  1205. foo: anObject
  1206. foo := anObject
  1207. ! !
  1208. TestCase subclass: #ObjectTest
  1209. instanceVariableNames: ''
  1210. package: 'Kernel-Tests'!
  1211. !ObjectTest methodsFor: 'tests'!
  1212. notDefined
  1213. <return void 0;>
  1214. !
  1215. testBasicAccess
  1216. | o |
  1217. o := Object new.
  1218. o basicAt: 'a' put: 1.
  1219. self assert: (o basicAt: 'a') equals: 1.
  1220. self assert: (o basicAt: 'b') equals: nil
  1221. !
  1222. testBasicPerform
  1223. | o |
  1224. o := Object new.
  1225. o basicAt: 'func' put: ['hello'].
  1226. o basicAt: 'func2' put: [:a | a + 1].
  1227. self assert: (o basicPerform: 'func') equals: 'hello'.
  1228. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1229. !
  1230. testDNU
  1231. self should: [Object new foo] raise: MessageNotUnderstood
  1232. !
  1233. testEquality
  1234. | o |
  1235. o := Object new.
  1236. self deny: o = Object new.
  1237. self assert: (o = o).
  1238. self assert: (o yourself = o).
  1239. self assert: (o = o yourself)
  1240. !
  1241. testHalt
  1242. self should: [Object new halt] raise: Error
  1243. !
  1244. testIdentity
  1245. | o |
  1246. o := Object new.
  1247. self deny: o == Object new.
  1248. self assert: o == o.
  1249. self assert: o yourself == o.
  1250. self assert: o == o yourself
  1251. !
  1252. testIfNil
  1253. self deny: Object new isNil.
  1254. self deny: (Object new ifNil: [true]) = true.
  1255. self assert: (Object new ifNotNil: [true]) equals: true.
  1256. self assert: (Object new ifNil: [false] ifNotNil: [true]) equals: true.
  1257. self assert: (Object new ifNotNil: [true] ifNil: [false]) equals: true
  1258. !
  1259. testInstVars
  1260. | o |
  1261. o := ObjectMock new.
  1262. self assert: (o instVarAt: #foo) equals: nil.
  1263. o instVarAt: #foo put: 1.
  1264. self assert: (o instVarAt: #foo) equals: 1.
  1265. self assert: (o instVarAt: 'foo') equals: 1
  1266. !
  1267. testNilUndefined
  1268. "nil in Smalltalk is the undefined object in JS"
  1269. self assert: self notDefined equals: nil
  1270. !
  1271. testYourself
  1272. | o |
  1273. o := ObjectMock new.
  1274. self assert: o yourself == o
  1275. !
  1276. testidentityHash
  1277. | o1 o2 |
  1278. o1 := Object new.
  1279. o2 := Object new.
  1280. self assert: o1 identityHash == o1 identityHash.
  1281. self deny: o1 identityHash == o2 identityHash
  1282. ! !
  1283. TestCase subclass: #PointTest
  1284. instanceVariableNames: ''
  1285. package: 'Kernel-Tests'!
  1286. !PointTest methodsFor: 'tests'!
  1287. testAccessing
  1288. self assert: (Point x: 3 y: 4) x equals: 3.
  1289. self assert: (Point x: 3 y: 4) y equals: 4.
  1290. self assert: (Point new x: 3) x equals: 3.
  1291. self assert: (Point new y: 4) y equals: 4
  1292. !
  1293. testArithmetic
  1294. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1295. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1296. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1297. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1298. !
  1299. testAt
  1300. self assert: 3@4 equals: (Point x: 3 y: 4)
  1301. !
  1302. testEgality
  1303. self assert: (3@4 = (3@4)).
  1304. self deny: 3@5 = (3@6)
  1305. !
  1306. testTranslateBy
  1307. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1308. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1309. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1310. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1311. ! !
  1312. TestCase subclass: #RandomTest
  1313. instanceVariableNames: ''
  1314. package: 'Kernel-Tests'!
  1315. !RandomTest methodsFor: 'tests'!
  1316. textNext
  1317. 10000 timesRepeat: [
  1318. | current next |
  1319. next := Random new next.
  1320. self assert: (next >= 0).
  1321. self assert: (next < 1).
  1322. self deny: current = next.
  1323. next = current]
  1324. ! !
  1325. TestCase subclass: #SetTest
  1326. instanceVariableNames: ''
  1327. package: 'Kernel-Tests'!
  1328. !SetTest methodsFor: 'tests'!
  1329. testAddRemove
  1330. | set |
  1331. set := Set new.
  1332. self assert: set isEmpty.
  1333. set add: 3.
  1334. self assert: (set includes: 3).
  1335. set add: 5.
  1336. self assert: (set includes: 5).
  1337. set remove: 3.
  1338. self deny: (set includes: 3)
  1339. !
  1340. testAt
  1341. self should: [Set new at: 1 put: 2] raise: Error
  1342. !
  1343. testCollect
  1344. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1345. !
  1346. testComparing
  1347. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1348. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1349. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1350. self deny: #(1 2) asSet = #(0 2) asSet
  1351. !
  1352. testPrintString
  1353. | set |
  1354. set := Set new.
  1355. self assert: set printString equals: 'a Set ()'.
  1356. set add: 1; add: 3.
  1357. self assert: set printString equals: 'a Set (1 3)'.
  1358. set add: 'foo'.
  1359. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1360. set remove: 1; remove: 3.
  1361. self assert: set printString equals: 'a Set (''foo'')'.
  1362. set add: 3.
  1363. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1364. set add: 3.
  1365. self assert: set printString equals: 'a Set (''foo'' 3)'
  1366. !
  1367. testSize
  1368. self assert: Set new size equals: 0.
  1369. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1370. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1371. !
  1372. testUnboxedObjects
  1373. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1374. !
  1375. testUnicity
  1376. | set |
  1377. set := Set new.
  1378. set add: 21.
  1379. set add: 'hello'.
  1380. set add: 21.
  1381. self assert: set size equals: 2.
  1382. set add: 'hello'.
  1383. self assert: set size equals: 2.
  1384. self assert: set asArray equals: #(21 'hello')
  1385. ! !
  1386. TestCase subclass: #StreamTest
  1387. instanceVariableNames: ''
  1388. package: 'Kernel-Tests'!
  1389. !StreamTest methodsFor: 'accessing'!
  1390. collectionClass
  1391. ^ self class collectionClass
  1392. !
  1393. newCollection
  1394. ^ self collectionClass new
  1395. !
  1396. newStream
  1397. ^ self collectionClass new stream
  1398. ! !
  1399. !StreamTest methodsFor: 'tests'!
  1400. testAtStartAtEnd
  1401. | stream |
  1402. stream := self newStream.
  1403. self assert: stream atStart.
  1404. self assert: stream atEnd.
  1405. stream nextPutAll: self newCollection.
  1406. self assert: stream atEnd.
  1407. self deny: stream atStart.
  1408. stream position: 1.
  1409. self deny: stream atEnd.
  1410. self deny: stream atStart
  1411. !
  1412. testContents
  1413. | stream |
  1414. stream := self newStream.
  1415. stream nextPutAll: self newCollection.
  1416. self assert: stream contents equals: self newCollection
  1417. !
  1418. testIsEmpty
  1419. | stream |
  1420. stream := self newStream.
  1421. self assert: stream isEmpty.
  1422. stream nextPutAll: self newCollection.
  1423. self deny: stream isEmpty
  1424. !
  1425. testPosition
  1426. | collection stream |
  1427. collection := self newCollection.
  1428. stream := self newStream.
  1429. stream nextPutAll: collection.
  1430. self assert: stream position equals: collection size.
  1431. stream position: 0.
  1432. self assert: stream position equals: 0.
  1433. stream next.
  1434. self assert: stream position equals: 1.
  1435. stream next.
  1436. self assert: stream position equals: 2
  1437. !
  1438. testReading
  1439. | stream collection |
  1440. collection := self newCollection.
  1441. stream := self newStream.
  1442. stream
  1443. nextPutAll: collection;
  1444. position: 0.
  1445. collection do: [ :each |
  1446. self assert: stream next equals: each ].
  1447. self assert: stream next isNil
  1448. !
  1449. testStreamContents
  1450. !
  1451. testWrite
  1452. | stream collection |
  1453. collection := self newCollection.
  1454. stream := self newStream.
  1455. collection do: [ :each | stream << each ].
  1456. self assert: stream contents equals: collection
  1457. !
  1458. testWriting
  1459. | stream collection |
  1460. collection := self newCollection.
  1461. stream := self newStream.
  1462. collection do: [ :each | stream nextPut: each ].
  1463. self assert: stream contents equals: collection.
  1464. stream := self newStream.
  1465. stream nextPutAll: collection.
  1466. self assert: stream contents equals: collection
  1467. ! !
  1468. !StreamTest class methodsFor: 'accessing'!
  1469. collectionClass
  1470. ^ nil
  1471. ! !
  1472. !StreamTest class methodsFor: 'testing'!
  1473. isAbstract
  1474. ^ self collectionClass isNil
  1475. ! !
  1476. StreamTest subclass: #ArrayStreamTest
  1477. instanceVariableNames: ''
  1478. package: 'Kernel-Tests'!
  1479. !ArrayStreamTest methodsFor: 'accessing'!
  1480. newCollection
  1481. ^ { true. 1. 3@4. 'foo' }
  1482. ! !
  1483. !ArrayStreamTest class methodsFor: 'accessing'!
  1484. collectionClass
  1485. ^ Array
  1486. ! !
  1487. StreamTest subclass: #StringStreamTest
  1488. instanceVariableNames: ''
  1489. package: 'Kernel-Tests'!
  1490. !StringStreamTest methodsFor: 'accessing'!
  1491. newCollection
  1492. ^ 'hello world'
  1493. ! !
  1494. !StringStreamTest class methodsFor: 'accessing'!
  1495. collectionClass
  1496. ^ String
  1497. ! !
  1498. TestCase subclass: #UndefinedTest
  1499. instanceVariableNames: ''
  1500. package: 'Kernel-Tests'!
  1501. !UndefinedTest methodsFor: 'tests'!
  1502. testCopying
  1503. self assert: nil copy equals: nil
  1504. !
  1505. testDeepCopy
  1506. self assert: nil deepCopy = nil
  1507. !
  1508. testIfNil
  1509. self assert: (nil ifNil: [true]) equals: true.
  1510. self deny: (nil ifNotNil: [true]) = true.
  1511. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1512. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1513. !
  1514. testIsNil
  1515. self assert: nil isNil.
  1516. self deny: nil notNil.
  1517. ! !