Kernel-Tests.st 45 KB

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