Kernel-Tests.st 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. testAt
  384. self assert: (self collection at: 'a') equals: 2.
  385. self should: [ self collection at: 5 ] raise: Error
  386. !
  387. testContains
  388. | collection |
  389. collection := self collection.
  390. self assert: (self collection contains: [ :each | each = self collection values first ]).
  391. self deny: (self collection contains: [ :each | each = Object new ])
  392. !
  393. testIndexOf
  394. self assert: (self collection indexOf: 2) equals: 'a'.
  395. self should: [ self collection indexOf: 999 ] raise: Error.
  396. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  397. ! !
  398. !HashedCollectionTest class methodsFor: 'accessing'!
  399. collectionClass
  400. ^ HashedCollection
  401. ! !
  402. HashedCollectionTest subclass: #DictionaryTest
  403. instanceVariableNames: ''
  404. package: 'Kernel-Tests'!
  405. !DictionaryTest methodsFor: 'accessing'!
  406. collection
  407. ^ Dictionary new
  408. at: 1 put: 1;
  409. at: 'a' put: 2;
  410. at: true put: 3;
  411. at: 4 put: -4;
  412. yourself
  413. !
  414. collectionWithDuplicates
  415. ^ Dictionary new
  416. at: 1 put: 1;
  417. at: 'a' put: 2;
  418. at: true put: 3;
  419. at: 4 put: -4;
  420. at: 'b' put: 1;
  421. at: 3 put: 3;
  422. at: false put: 12;
  423. yourself
  424. ! !
  425. !DictionaryTest methodsFor: 'tests'!
  426. testAccessing
  427. | d |
  428. d := Dictionary new.
  429. d at: 'hello' put: 'world'.
  430. self assert: (d at: 'hello') equals: 'world'.
  431. self assert: (d at: 'hello' ifAbsent: [nil]) equals: 'world'.
  432. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  433. self assert: (d includesKey: 'hello').
  434. self deny: (d includesKey: 'foo').
  435. d at: 1 put: 2.
  436. self assert: (d at: 1) equals: 2.
  437. d at: 1@3 put: 3.
  438. self assert: (d at: 1@3) equals: 3.
  439. self assert: (d includesKey: 1@3).
  440. self deny: (d includesKey: 3@1)
  441. !
  442. testDynamicDictionaries
  443. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  444. !
  445. testEquality
  446. | d1 d2 |
  447. self assert: (Dictionary new = Dictionary new).
  448. d1 := Dictionary new at: 1 put: 2; yourself.
  449. d2 := Dictionary new at: 1 put: 2; yourself.
  450. self assert: (d1 = d2).
  451. d2 := Dictionary new at: 1 put: 3; yourself.
  452. self deny: d1 = d2.
  453. d2 := Dictionary new at: 2 put: 2; yourself.
  454. self deny: d1 = d2.
  455. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  456. self deny: d1 = d2.
  457. !
  458. testIfAbsent
  459. | d visited |
  460. visited := false.
  461. d := Dictionary new.
  462. d at: 'hello' ifAbsent: [ visited := true ].
  463. self assert: visited.
  464. !
  465. testIfPresent
  466. | d visited absent |
  467. visited := false.
  468. d := Dictionary new.
  469. d at: 'hello' put: 'world'.
  470. d at: 'hello' ifPresent: [ :value | visited := value ].
  471. self assert: visited equals: 'world'.
  472. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  473. self assert: absent isNil.
  474. !
  475. testIfPresentIfAbsent
  476. | d visited |
  477. visited := false.
  478. d := Dictionary new.
  479. d at: 'hello' put: 'world'.
  480. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  481. self assert: visited equals: 'world'.
  482. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  483. self assert: visited.
  484. !
  485. testKeys
  486. | d |
  487. d := Dictionary new.
  488. d at: 1 put: 2.
  489. d at: 2 put: 3.
  490. d at: 3 put: 4.
  491. self assert: d keys equals: #(1 2 3)
  492. !
  493. testPointKey
  494. | d |
  495. d := Dictionary new.
  496. d at: 1@1 put: 'foo'.
  497. self assert: (d at: 1@1) equals: 'foo'.
  498. d at: 1@1 put: 'bar'.
  499. self assert: (d at: 1@1) equals: 'bar'.
  500. d removeKey: 1@1.
  501. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  502. self deny: (d includesKey: 1@1)
  503. !
  504. testPrintString
  505. self
  506. assert: (Dictionary new
  507. at:'firstname' put: 'James';
  508. at:'lastname' put: 'Bond';
  509. printString)
  510. equals: 'a Dictionary (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  511. !
  512. testRemoveKey
  513. | d key |
  514. d := Dictionary new.
  515. d at: 1 put: 2.
  516. d at: 2 put: 3.
  517. d at: 3 put: 4.
  518. key := 2.
  519. self assert: d keys equals: #(1 2 3).
  520. d removeKey: key.
  521. self assert: d keys equals: #(1 3).
  522. self assert: d values equals: #(2 4).
  523. self deny: (d includesKey: 2)
  524. !
  525. testRemoveKeyIfAbsent
  526. | d key |
  527. d := Dictionary new.
  528. d at: 1 put: 2.
  529. d at: 2 put: 3.
  530. d at: 3 put: 4.
  531. key := 2.
  532. self assert: (d removeKey: key) equals: 3.
  533. key := 3.
  534. self assert: (d removeKey: key ifAbsent: [42]) equals: 4.
  535. key := 'why'.
  536. self assert: (d removeKey: key ifAbsent: [42] ) equals: 42.
  537. !
  538. testSize
  539. | d |
  540. d := Dictionary new.
  541. self assert: d size equals: 0.
  542. d at: 1 put: 2.
  543. self assert: d size equals: 1.
  544. d at: 2 put: 3.
  545. self assert: d size equals: 2.
  546. !
  547. testValues
  548. | d |
  549. d := Dictionary new.
  550. d at: 1 put: 2.
  551. d at: 2 put: 3.
  552. d at: 3 put: 4.
  553. self assert: d values equals: #(2 3 4)
  554. ! !
  555. !DictionaryTest class methodsFor: 'accessing'!
  556. collectionClass
  557. ^ Dictionary
  558. ! !
  559. IndexableCollectionTest subclass: #SequenceableCollectionTest
  560. instanceVariableNames: ''
  561. package: 'Kernel-Tests'!
  562. SequenceableCollectionTest subclass: #ArrayTest
  563. instanceVariableNames: ''
  564. package: 'Kernel-Tests'!
  565. !ArrayTest methodsFor: 'tests'!
  566. testAddFirst
  567. self assert: (#(2 3 4 5) addFirst: 1; yourself) equals: #(1 2 3 4 5)
  568. !
  569. testAtIfAbsent
  570. | array |
  571. array := #('hello' 'world').
  572. self assert: (array at: 1) equals: 'hello'.
  573. self assert: (array at: 2) equals: 'world'.
  574. self assert: (array at: 2 ifAbsent: ['not found']) equals: 'world'.
  575. self assert: (array at: 0 ifAbsent: ['not found']) equals: 'not found'.
  576. self assert: (array at: -10 ifAbsent: ['not found']) equals: 'not found'.
  577. self assert: (array at: 3 ifAbsent: ['not found']) equals: 'not found'.
  578. !
  579. testFirstN
  580. self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
  581. !
  582. testIfEmpty
  583. self assert: ( '' ifEmpty: ['zork'] ) equals: 'zork'
  584. !
  585. testPrintString
  586. | array |
  587. array := Array new.
  588. self assert: array printString equals: 'an Array ()'.
  589. array add: 1; add: 3.
  590. self assert: array printString equals: 'an Array (1 3)'.
  591. array add: 'foo'.
  592. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  593. array remove: 1; remove: 3.
  594. self assert: array printString equals: 'an Array (''foo'')'.
  595. array addLast: 3.
  596. self assert: array printString equals: 'an Array (''foo'' 3)'.
  597. array addLast: 3.
  598. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  599. ! !
  600. !ArrayTest class methodsFor: 'accessing'!
  601. collectionClass
  602. ^ Array
  603. ! !
  604. SequenceableCollectionTest subclass: #StringTest
  605. instanceVariableNames: ''
  606. package: 'Kernel-Tests'!
  607. !StringTest methodsFor: 'accessing'!
  608. collection
  609. ^'hello'
  610. !
  611. collectionWithDuplicates
  612. ^ 'abbaerte'
  613. ! !
  614. !StringTest methodsFor: 'tests'!
  615. testAddRemove
  616. self should: ['hello' add: 'a'] raise: Error.
  617. self should: ['hello' remove: 'h'] raise: Error
  618. !
  619. testAsArray
  620. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  621. !
  622. testAt
  623. self assert: ('hello' at: 1) equals: 'h'.
  624. self assert: ('hello' at: 5) equals: 'o'.
  625. self assert: ('hello' at: 6 ifAbsent: [nil]) equals: nil
  626. !
  627. testAtPut
  628. "String instances are read-only"
  629. self should: ['hello' at: 1 put: 'a'] raise: Error
  630. !
  631. testCollect
  632. | newCollection |
  633. newCollection := 'hheelllloo'.
  634. self
  635. assertSameContents: (self collection collect: [ :each |
  636. each, each ])
  637. as: newCollection
  638. !
  639. testCopyWithoutAll
  640. self
  641. assert: ('*hello* *world*' copyWithoutAll: '*')
  642. equals: 'hello world'
  643. !
  644. testDetect
  645. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  646. self
  647. should: [ self collection detect: [ :each | each = 6 ] ]
  648. raise: Error
  649. !
  650. testEquality
  651. self assert: 'hello' equals: 'hello'.
  652. self deny: 'hello' = 'world'.
  653. "Test for issue 459"
  654. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  655. self assert: 'hello' equals: 'hello' yourself.
  656. self assert: 'hello' yourself equals: 'hello'.
  657. "test JS falsy value"
  658. self deny: '' = 0
  659. !
  660. testIdentity
  661. self assert: 'hello' == 'hello'.
  662. self deny: 'hello' == 'world'.
  663. self assert: 'hello' == 'hello' yourself.
  664. self assert: 'hello' yourself == 'hello'.
  665. "test JS falsy value"
  666. self deny: '' == 0
  667. !
  668. testIncludesSubString
  669. self assert: ('amber' includesSubString: 'ber').
  670. self deny: ('amber' includesSubString: 'zork').
  671. !
  672. testIndexOf
  673. self assert: (self collection indexOf: 'e') equals: 2.
  674. self should: [ self collection indexOf: 999 ] raise: Error.
  675. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  676. !
  677. testJoin
  678. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  679. !
  680. testSelect
  681. | newCollection |
  682. newCollection := 'o'.
  683. self
  684. assertSameContents: (self collection select: [ :each |
  685. each = 'o' ])
  686. as: newCollection
  687. !
  688. testSize
  689. self assert: 'smalltalk' size equals: 9.
  690. self assert: '' size equals: 0
  691. !
  692. testStreamContents
  693. self
  694. assert: (String streamContents: [ :aStream |
  695. aStream
  696. nextPutAll: 'hello'; space;
  697. nextPutAll: 'world' ])
  698. equals: 'hello world'
  699. ! !
  700. !StringTest class methodsFor: 'accessing'!
  701. collectionClass
  702. ^ String
  703. ! !
  704. TestCase subclass: #ConsoleTranscriptTest
  705. instanceVariableNames: ''
  706. package: 'Kernel-Tests'!
  707. !ConsoleTranscriptTest methodsFor: 'tests'!
  708. testShow
  709. | originalTranscript |
  710. originalTranscript := Transcript current.
  711. Transcript register: ConsoleTranscript new.
  712. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  713. self shouldnt: [ Transcript show: console ] raise: Error.
  714. Transcript register: originalTranscript.
  715. ! !
  716. TestCase subclass: #JSObjectProxyTest
  717. instanceVariableNames: ''
  718. package: 'Kernel-Tests'!
  719. !JSObjectProxyTest methodsFor: 'accessing'!
  720. jsObject
  721. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined}>
  722. ! !
  723. !JSObjectProxyTest methodsFor: 'tests'!
  724. testAtIfAbsent
  725. | testObject |
  726. testObject := self jsObject.
  727. self assert: (testObject at: 'abc' ifAbsent: ['Property does not exist']) equals: 'Property does not exist'.
  728. self assert: (testObject at: 'e' ifAbsent: ['Property does not exist']) equals: nil.
  729. self assert: (testObject at: 'a' ifAbsent: ['Property does not exist']) equals: 1.
  730. self assert: (testObject at: 'f' ifAbsent: ['Property does not exist']) equals: nil.
  731. !
  732. testAtIfPresent
  733. | testObject |
  734. testObject := self jsObject.
  735. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  736. self assert: (testObject at: 'e' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello nil'.
  737. self assert: (testObject at: 'a' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello 1'.
  738. self assert: (testObject at: 'f' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello nil'.
  739. !
  740. testAtIfPresentIfAbsent
  741. | testObject |
  742. testObject := self jsObject.
  743. self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'not present'.
  744. self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  745. self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello 1'.
  746. self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  747. !
  748. testDNU
  749. self should: [self jsObject foo] raise: MessageNotUnderstood
  750. !
  751. testMessageSend
  752. self assert: self jsObject a equals: 1.
  753. self assert: self jsObject b equals: 2.
  754. self assert: (self jsObject c: 3) equals: 3
  755. !
  756. testMethodWithArguments
  757. self assert: (self jsObject c: 1) equals: 1
  758. !
  759. testPrinting
  760. self assert: self jsObject printString equals: '[object Object]'
  761. !
  762. testPropertyThatReturnsEmptyString
  763. | object |
  764. object := self jsObject.
  765. self assert: object d equals: ''.
  766. object d: 'hello'.
  767. self assert: object d equals: 'hello'
  768. !
  769. testPropertyThatReturnsUndefined
  770. | object |
  771. object := self jsObject.
  772. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  773. self assert: object e isNil
  774. !
  775. testValue
  776. | testObject |
  777. testObject := self jsObject.
  778. self assert: testObject value printString equals: '[object Object]'.
  779. testObject at: 'value' put: 'aValue'.
  780. self assert: testObject value equals: 'aValue'
  781. !
  782. testYourself
  783. | object |
  784. object := self jsObject
  785. d: 'test';
  786. yourself.
  787. self assert: object d equals: 'test'
  788. ! !
  789. TestCase subclass: #JavaScriptExceptionTest
  790. instanceVariableNames: ''
  791. package: 'Kernel-Tests'!
  792. !JavaScriptExceptionTest methodsFor: 'helpers'!
  793. throwException
  794. <throw 'test'>
  795. ! !
  796. !JavaScriptExceptionTest methodsFor: 'tests'!
  797. testCatchingException
  798. [ self throwException ]
  799. on: Error
  800. do: [ :error |
  801. self assert: error exception = 'test' ]
  802. !
  803. testRaisingException
  804. self should: [ self throwException ] raise: JavaScriptException
  805. ! !
  806. TestCase subclass: #MessageSendTest
  807. instanceVariableNames: ''
  808. package: 'Kernel-Tests'!
  809. !MessageSendTest methodsFor: 'tests'!
  810. testValue
  811. | messageSend |
  812. messageSend := MessageSend new
  813. receiver: Object new;
  814. selector: #asString;
  815. yourself.
  816. self assert: messageSend value equals: 'an Object'
  817. !
  818. testValueWithArguments
  819. | messageSend |
  820. messageSend := MessageSend new
  821. receiver: 2;
  822. selector: '+';
  823. yourself.
  824. self assert: (messageSend value: 3) equals: 5.
  825. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  826. ! !
  827. TestCase subclass: #NumberTest
  828. instanceVariableNames: ''
  829. package: 'Kernel-Tests'!
  830. !NumberTest methodsFor: 'tests'!
  831. testAbs
  832. self assert: 4 abs equals: 4.
  833. self assert: -4 abs equals: 4
  834. !
  835. testArithmetic
  836. "We rely on JS here, so we won't test complex behavior, just check if
  837. message sends are corrects"
  838. self assert: 1.5 + 1 equals: 2.5.
  839. self assert: 2 - 1 equals: 1.
  840. self assert: -2 - 1 equals: -3.
  841. self assert: 12 / 2 equals: 6.
  842. self assert: 3 * 4 equals: 12.
  843. "Simple parenthesis and execution order"
  844. self assert: 1 + 2 * 3 equals: 9.
  845. self assert: 1 + (2 * 3) equals: 7
  846. !
  847. testComparison
  848. self assert: 3 > 2.
  849. self assert: 2 < 3.
  850. self deny: 3 < 2.
  851. self deny: 2 > 3.
  852. self assert: 3 >= 3.
  853. self assert: 3.1 >= 3.
  854. self assert: 3 <= 3.
  855. self assert: 3 <= 3.1
  856. !
  857. testCopying
  858. self assert: 1 copy == 1.
  859. self assert: 1 deepCopy == 1
  860. !
  861. testEquality
  862. self assert: (1 = 1).
  863. self assert: (0 = 0).
  864. self deny: (1 = 0).
  865. self assert: (1 yourself = 1).
  866. self assert: (1 = 1 yourself).
  867. self assert: (1 yourself = 1 yourself).
  868. self deny: 0 = false.
  869. self deny: false = 0.
  870. self deny: '' = 0.
  871. self deny: 0 = ''
  872. !
  873. testHexNumbers
  874. self assert: 16r9 equals: 9.
  875. self assert: 16rA truncated equals: 10.
  876. self assert: 16rB truncated equals: 11.
  877. self assert: 16rC truncated equals: 12.
  878. self assert: 16rD truncated equals: 13.
  879. self assert: 16rE truncated equals: 14.
  880. self assert: 16rF truncated equals: 15
  881. !
  882. testIdentity
  883. self assert: 1 == 1.
  884. self assert: 0 == 0.
  885. self deny: 1 == 0.
  886. self assert: 1 yourself == 1.
  887. self assert: 1 == 1 yourself.
  888. self assert: 1 yourself == 1 yourself.
  889. self deny: 1 == 2
  890. !
  891. testInvalidHexNumbers
  892. self should: [16rG] raise: MessageNotUnderstood.
  893. self should: [16rg] raise: MessageNotUnderstood.
  894. self should: [16rH] raise: MessageNotUnderstood.
  895. self should: [16rh] raise: MessageNotUnderstood.
  896. self should: [16rI] raise: MessageNotUnderstood.
  897. self should: [16ri] raise: MessageNotUnderstood.
  898. self should: [16rJ] raise: MessageNotUnderstood.
  899. self should: [16rj] raise: MessageNotUnderstood.
  900. self should: [16rK] raise: MessageNotUnderstood.
  901. self should: [16rk] raise: MessageNotUnderstood.
  902. self should: [16rL] raise: MessageNotUnderstood.
  903. self should: [16rl] raise: MessageNotUnderstood.
  904. self should: [16rM] raise: MessageNotUnderstood.
  905. self should: [16rm] raise: MessageNotUnderstood.
  906. self should: [16rN] raise: MessageNotUnderstood.
  907. self should: [16rn] raise: MessageNotUnderstood.
  908. self should: [16rO] raise: MessageNotUnderstood.
  909. self should: [16ro] raise: MessageNotUnderstood.
  910. self should: [16rP] raise: MessageNotUnderstood.
  911. self should: [16rp] raise: MessageNotUnderstood.
  912. self should: [16rQ] raise: MessageNotUnderstood.
  913. self should: [16rq] raise: MessageNotUnderstood.
  914. self should: [16rR] raise: MessageNotUnderstood.
  915. self should: [16rr] raise: MessageNotUnderstood.
  916. self should: [16rS] raise: MessageNotUnderstood.
  917. self should: [16rs] raise: MessageNotUnderstood.
  918. self should: [16rT] raise: MessageNotUnderstood.
  919. self should: [16rt] raise: MessageNotUnderstood.
  920. self should: [16rU] raise: MessageNotUnderstood.
  921. self should: [16ru] raise: MessageNotUnderstood.
  922. self should: [16rV] raise: MessageNotUnderstood.
  923. self should: [16rv] raise: MessageNotUnderstood.
  924. self should: [16rW] raise: MessageNotUnderstood.
  925. self should: [16rw] raise: MessageNotUnderstood.
  926. self should: [16rX] raise: MessageNotUnderstood.
  927. self should: [16rx] raise: MessageNotUnderstood.
  928. self should: [16rY] raise: MessageNotUnderstood.
  929. self should: [16ry] raise: MessageNotUnderstood.
  930. self should: [16rZ] raise: MessageNotUnderstood.
  931. self should: [16rz] raise: MessageNotUnderstood.
  932. self should: [16rABcdEfZ] raise: MessageNotUnderstood.
  933. !
  934. testMinMax
  935. self assert: (2 max: 5) equals: 5.
  936. self assert: (2 min: 5) equals: 2
  937. !
  938. testNegated
  939. self assert: 3 negated equals: -3.
  940. self assert: -3 negated equals: 3
  941. !
  942. testPrintShowingDecimalPlaces
  943. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  944. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  945. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  946. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  947. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  948. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  949. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  950. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  951. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  952. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  953. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  954. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  955. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  956. !
  957. testRounded
  958. self assert: 3 rounded equals: 3.
  959. self assert: 3.212 rounded equals: 3.
  960. self assert: 3.51 rounded equals: 4
  961. !
  962. testSqrt
  963. self assert: 4 sqrt equals: 2.
  964. self assert: 16 sqrt equals: 4
  965. !
  966. testSquared
  967. self assert: 4 squared equals: 16
  968. !
  969. testTimesRepeat
  970. | i |
  971. i := 0.
  972. 0 timesRepeat: [i := i + 1].
  973. self assert: i equals: 0.
  974. 5 timesRepeat: [i := i + 1].
  975. self assert: i equals: 5
  976. !
  977. testTo
  978. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  979. !
  980. testToBy
  981. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  982. self should: [1 to: 4 by: 0] raise: Error
  983. !
  984. testTruncated
  985. self assert: 3 truncated equals: 3.
  986. self assert: 3.212 truncated equals: 3.
  987. self assert: 3.51 truncated equals: 3
  988. ! !
  989. Object subclass: #ObjectMock
  990. instanceVariableNames: 'foo bar'
  991. package: 'Kernel-Tests'!
  992. !ObjectMock commentStamp!
  993. ObjectMock is there only to perform tests on classes.!
  994. !ObjectMock methodsFor: 'not yet classified'!
  995. foo
  996. ^foo
  997. !
  998. foo: anObject
  999. foo := anObject
  1000. ! !
  1001. TestCase subclass: #ObjectTest
  1002. instanceVariableNames: ''
  1003. package: 'Kernel-Tests'!
  1004. !ObjectTest methodsFor: 'tests'!
  1005. notDefined
  1006. <return undefined;>
  1007. !
  1008. testBasicAccess
  1009. | o |
  1010. o := Object new.
  1011. o basicAt: 'a' put: 1.
  1012. self assert: (o basicAt: 'a') equals: 1.
  1013. self assert: (o basicAt: 'b') equals: nil
  1014. !
  1015. testBasicPerform
  1016. | o |
  1017. o := Object new.
  1018. o basicAt: 'func' put: ['hello'].
  1019. o basicAt: 'func2' put: [:a | a + 1].
  1020. self assert: (o basicPerform: 'func') equals: 'hello'.
  1021. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1022. !
  1023. testDNU
  1024. self should: [Object new foo] raise: MessageNotUnderstood
  1025. !
  1026. testEquality
  1027. | o |
  1028. o := Object new.
  1029. self deny: o = Object new.
  1030. self assert: (o = o).
  1031. self assert: (o yourself = o).
  1032. self assert: (o = o yourself)
  1033. !
  1034. testHalt
  1035. self should: [Object new halt] raise: Error
  1036. !
  1037. testIdentity
  1038. | o |
  1039. o := Object new.
  1040. self deny: o == Object new.
  1041. self assert: o == o.
  1042. self assert: o yourself == o.
  1043. self assert: o == o yourself
  1044. !
  1045. testIfNil
  1046. self deny: Object new isNil.
  1047. self deny: (Object new ifNil: [true]) = true.
  1048. self assert: (Object new ifNotNil: [true]) equals: true.
  1049. self assert: (Object new ifNil: [false] ifNotNil: [true]) equals: true.
  1050. self assert: (Object new ifNotNil: [true] ifNil: [false]) equals: true
  1051. !
  1052. testInstVars
  1053. | o |
  1054. o := ObjectMock new.
  1055. self assert: (o instVarAt: #foo) equals: nil.
  1056. o instVarAt: #foo put: 1.
  1057. self assert: (o instVarAt: #foo) equals: 1.
  1058. self assert: (o instVarAt: 'foo') equals: 1
  1059. !
  1060. testNilUndefined
  1061. "nil in Smalltalk is the undefined object in JS"
  1062. self assert: self notDefined equals: nil
  1063. !
  1064. testYourself
  1065. | o |
  1066. o := ObjectMock new.
  1067. self assert: o yourself == o
  1068. !
  1069. testidentityHash
  1070. | o1 o2 |
  1071. o1 := Object new.
  1072. o2 := Object new.
  1073. self assert: o1 identityHash == o1 identityHash.
  1074. self deny: o1 identityHash == o2 identityHash
  1075. ! !
  1076. TestCase subclass: #PackageTest
  1077. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  1078. package: 'Kernel-Tests'!
  1079. !PackageTest methodsFor: 'running'!
  1080. setUp
  1081. backUpCommitPathJs := Package defaultCommitPathJs.
  1082. backUpCommitPathSt := Package defaultCommitPathSt.
  1083. Package resetCommitPaths.
  1084. zorkPackage := Package new name: 'Zork'.
  1085. grulPackage := Package new
  1086. name: 'Grul';
  1087. commitPathJs: 'server/grul/js';
  1088. commitPathSt: 'grul/st';
  1089. yourself
  1090. !
  1091. tearDown
  1092. Package
  1093. defaultCommitPathJs: backUpCommitPathJs;
  1094. defaultCommitPathSt: backUpCommitPathSt
  1095. ! !
  1096. !PackageTest methodsFor: 'tests'!
  1097. testGrulCommitPathJsShouldBeServerGrulJs
  1098. self assert: grulPackage commitPathJs equals: 'server/grul/js'
  1099. !
  1100. testGrulCommitPathStShouldBeGrulSt
  1101. self assert: grulPackage commitPathSt equals: 'grul/st'
  1102. !
  1103. testZorkCommitPathJsShouldBeJs
  1104. self assert: zorkPackage commitPathJs equals: 'js'
  1105. !
  1106. testZorkCommitPathStShouldBeSt
  1107. self assert: zorkPackage commitPathSt equals: 'st'
  1108. ! !
  1109. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  1110. instanceVariableNames: ''
  1111. package: 'Kernel-Tests'!
  1112. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  1113. setUp
  1114. super setUp.
  1115. Package
  1116. defaultCommitPathJs: 'javascripts/';
  1117. defaultCommitPathSt: 'smalltalk/'.
  1118. ! !
  1119. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  1120. testGrulCommitPathJsShouldBeServerGrulJs
  1121. self assert: grulPackage commitPathJs equals: 'server/grul/js'
  1122. !
  1123. testGrulCommitPathStShouldBeGrulSt
  1124. self assert: grulPackage commitPathSt equals: 'grul/st'
  1125. !
  1126. testZorkCommitPathJsShouldBeJavascript
  1127. self assert: zorkPackage commitPathJs equals: 'javascripts/'
  1128. !
  1129. testZorkCommitPathStShouldBeSmalltalk
  1130. self assert: zorkPackage commitPathSt equals: 'smalltalk/'
  1131. ! !
  1132. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  1133. shouldInheritSelectors
  1134. ^ false
  1135. ! !
  1136. TestCase subclass: #PointTest
  1137. instanceVariableNames: ''
  1138. package: 'Kernel-Tests'!
  1139. !PointTest methodsFor: 'tests'!
  1140. testAccessing
  1141. self assert: (Point x: 3 y: 4) x equals: 3.
  1142. self assert: (Point x: 3 y: 4) y equals: 4.
  1143. self assert: (Point new x: 3) x equals: 3.
  1144. self assert: (Point new y: 4) y equals: 4
  1145. !
  1146. testArithmetic
  1147. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1148. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1149. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1150. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1151. !
  1152. testAt
  1153. self assert: 3@4 equals: (Point x: 3 y: 4)
  1154. !
  1155. testEgality
  1156. self assert: (3@4 = (3@4)).
  1157. self deny: 3@5 = (3@6)
  1158. !
  1159. testTranslateBy
  1160. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1161. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1162. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1163. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1164. ! !
  1165. TestCase subclass: #RandomTest
  1166. instanceVariableNames: ''
  1167. package: 'Kernel-Tests'!
  1168. !RandomTest methodsFor: 'tests'!
  1169. textNext
  1170. 10000 timesRepeat: [
  1171. | current next |
  1172. next := Random new next.
  1173. self assert: (next >= 0).
  1174. self assert: (next < 1).
  1175. self deny: current = next.
  1176. next = current]
  1177. ! !
  1178. TestCase subclass: #SetTest
  1179. instanceVariableNames: ''
  1180. package: 'Kernel-Tests'!
  1181. !SetTest methodsFor: 'tests'!
  1182. testAddRemove
  1183. | set |
  1184. set := Set new.
  1185. self assert: set isEmpty.
  1186. set add: 3.
  1187. self assert: (set includes: 3).
  1188. set add: 5.
  1189. self assert: (set includes: 5).
  1190. set remove: 3.
  1191. self deny: (set includes: 3)
  1192. !
  1193. testAt
  1194. self should: [Set new at: 1 put: 2] raise: Error
  1195. !
  1196. testCollect
  1197. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1198. !
  1199. testComparing
  1200. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1201. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1202. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1203. self deny: #(1 2) asSet = #(0 2) asSet
  1204. !
  1205. testPrintString
  1206. | set |
  1207. set := Set new.
  1208. self assert: set printString equals: 'a Set ()'.
  1209. set add: 1; add: 3.
  1210. self assert: set printString equals: 'a Set (1 3)'.
  1211. set add: 'foo'.
  1212. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1213. set remove: 1; remove: 3.
  1214. self assert: set printString equals: 'a Set (''foo'')'.
  1215. set add: 3.
  1216. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1217. set add: 3.
  1218. self assert: set printString equals: 'a Set (''foo'' 3)'
  1219. !
  1220. testSize
  1221. self assert: Set new size equals: 0.
  1222. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1223. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1224. !
  1225. testUnicity
  1226. | set |
  1227. set := Set new.
  1228. set add: 21.
  1229. set add: 'hello'.
  1230. set add: 21.
  1231. self assert: set size equals: 2.
  1232. set add: 'hello'.
  1233. self assert: set size equals: 2.
  1234. self assert: set asArray equals: #(21 'hello')
  1235. ! !
  1236. TestCase subclass: #StreamTest
  1237. instanceVariableNames: ''
  1238. package: 'Kernel-Tests'!
  1239. !StreamTest methodsFor: 'accessing'!
  1240. collectionClass
  1241. ^ self class collectionClass
  1242. !
  1243. newCollection
  1244. ^ self collectionClass new
  1245. !
  1246. newStream
  1247. ^ self collectionClass new stream
  1248. ! !
  1249. !StreamTest methodsFor: 'tests'!
  1250. testAtStartAtEnd
  1251. | stream |
  1252. stream := self newStream.
  1253. self assert: stream atStart.
  1254. self assert: stream atEnd.
  1255. stream nextPutAll: self newCollection.
  1256. self assert: stream atEnd.
  1257. self deny: stream atStart.
  1258. stream position: 1.
  1259. self deny: stream atEnd.
  1260. self deny: stream atStart
  1261. !
  1262. testContents
  1263. | stream |
  1264. stream := self newStream.
  1265. stream nextPutAll: self newCollection.
  1266. self assert: stream contents equals: self newCollection
  1267. !
  1268. testIsEmpty
  1269. | stream |
  1270. stream := self newStream.
  1271. self assert: stream isEmpty.
  1272. stream nextPutAll: self newCollection.
  1273. self deny: stream isEmpty
  1274. !
  1275. testPosition
  1276. | collection stream |
  1277. collection := self newCollection.
  1278. stream := self newStream.
  1279. stream nextPutAll: collection.
  1280. self assert: stream position equals: collection size.
  1281. stream position: 0.
  1282. self assert: stream position equals: 0.
  1283. stream next.
  1284. self assert: stream position equals: 1.
  1285. stream next.
  1286. self assert: stream position equals: 2
  1287. !
  1288. testReading
  1289. | stream collection |
  1290. collection := self newCollection.
  1291. stream := self newStream.
  1292. stream
  1293. nextPutAll: collection;
  1294. position: 0.
  1295. collection do: [ :each |
  1296. self assert: stream next equals: each ].
  1297. self assert: stream next isNil
  1298. !
  1299. testStreamContents
  1300. !
  1301. testWrite
  1302. | stream collection |
  1303. collection := self newCollection.
  1304. stream := self newStream.
  1305. collection do: [ :each | stream << each ].
  1306. self assert: stream contents equals: collection
  1307. !
  1308. testWriting
  1309. | stream collection |
  1310. collection := self newCollection.
  1311. stream := self newStream.
  1312. collection do: [ :each | stream nextPut: each ].
  1313. self assert: stream contents equals: collection.
  1314. stream := self newStream.
  1315. stream nextPutAll: collection.
  1316. self assert: stream contents equals: collection
  1317. ! !
  1318. !StreamTest class methodsFor: 'accessing'!
  1319. collectionClass
  1320. ^ nil
  1321. ! !
  1322. !StreamTest class methodsFor: 'testing'!
  1323. isAbstract
  1324. ^ self collectionClass isNil
  1325. ! !
  1326. StreamTest subclass: #ArrayStreamTest
  1327. instanceVariableNames: ''
  1328. package: 'Kernel-Tests'!
  1329. !ArrayStreamTest methodsFor: 'accessing'!
  1330. newCollection
  1331. ^ { true. 1. 3@4. 'foo' }
  1332. ! !
  1333. !ArrayStreamTest class methodsFor: 'accessing'!
  1334. collectionClass
  1335. ^ Array
  1336. ! !
  1337. StreamTest subclass: #StringStreamTest
  1338. instanceVariableNames: ''
  1339. package: 'Kernel-Tests'!
  1340. !StringStreamTest methodsFor: 'accessing'!
  1341. newCollection
  1342. ^ 'hello world'
  1343. ! !
  1344. !StringStreamTest class methodsFor: 'accessing'!
  1345. collectionClass
  1346. ^ String
  1347. ! !
  1348. TestCase subclass: #UndefinedTest
  1349. instanceVariableNames: ''
  1350. package: 'Kernel-Tests'!
  1351. !UndefinedTest methodsFor: 'tests'!
  1352. testCopying
  1353. self assert: nil copy equals: nil
  1354. !
  1355. testDeepCopy
  1356. self assert: nil deepCopy = nil
  1357. !
  1358. testIfNil
  1359. self assert: (nil ifNil: [true]) equals: true.
  1360. self deny: (nil ifNotNil: [true]) = true.
  1361. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1362. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1363. !
  1364. testIsNil
  1365. self assert: nil isNil.
  1366. self deny: nil notNil.
  1367. ! !