1
0

Kernel-Tests.st 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. testNumArgs
  43. self assert: [] numArgs equals: 0.
  44. self assert: [:a :b | ] numArgs equals: 2
  45. !
  46. testOnDo
  47. self assert: ([Error new signal] on: Error do: [:ex | true])
  48. !
  49. testValue
  50. self assert: ([1+1] value) equals: 2.
  51. self assert: ([:x | x +1] value: 2) equals: 3.
  52. self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
  53. "Arguments are optional in Amber. This isn't ANSI compliant."
  54. self assert: ([:a :b :c | 1] value) equals: 1
  55. !
  56. testValueWithPossibleArguments
  57. self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
  58. self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
  59. self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  60. !
  61. testWhileFalse
  62. | i |
  63. i := 0.
  64. [i > 5] whileFalse: [i := i + 1].
  65. self assert: i equals: 6.
  66. i := 0.
  67. [i := i + 1. i > 5] whileFalse.
  68. self assert: i equals: 6
  69. !
  70. testWhileTrue
  71. | i |
  72. i := 0.
  73. [i < 5] whileTrue: [i := i + 1].
  74. self assert: i equals: 5.
  75. i := 0.
  76. [i := i + 1. i < 5] whileTrue.
  77. self assert: i equals: 5
  78. ! !
  79. TestCase subclass: #BooleanTest
  80. instanceVariableNames: ''
  81. package: 'Kernel-Tests'!
  82. !BooleanTest methodsFor: 'tests'!
  83. testEquality
  84. "We're on top of JS...just be sure to check the basics!!"
  85. self deny: 0 = false.
  86. self deny: false = 0.
  87. self deny: '' = false.
  88. self deny: false = ''.
  89. self assert: (true = true).
  90. self deny: false = true.
  91. self deny: true = false.
  92. self assert: (false = false).
  93. "JS may do some type coercing after sending a message"
  94. self assert: (true yourself = true).
  95. self assert: (true yourself = true yourself)
  96. !
  97. testIdentity
  98. "We're on top of JS...just be sure to check the basics!!"
  99. self deny: 0 == false.
  100. self deny: false == 0.
  101. self deny: '' == false.
  102. self deny: false == ''.
  103. self assert: true == true.
  104. self deny: false == true.
  105. self deny: true == false.
  106. self assert: false == false.
  107. "JS may do some type coercing after sending a message"
  108. self assert: true yourself == true.
  109. self assert: true yourself == true yourself
  110. !
  111. testIfTrueIfFalse
  112. self assert: (true ifTrue: ['alternative block']) equals: 'alternative block'.
  113. self assert: (true ifFalse: ['alternative block']) equals: nil.
  114. self assert: (false ifTrue: ['alternative block']) equals: nil.
  115. self assert: (false ifFalse: ['alternative block']) equals: 'alternative block'.
  116. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block2'.
  117. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block'.
  118. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block'.
  119. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block2'.
  120. !
  121. testIfTrueIfFalseWithBoxing
  122. self assert: (true yourself ifTrue: ['alternative block']) equals: 'alternative block'.
  123. self assert: (true yourself ifFalse: ['alternative block']) equals: nil.
  124. self assert: (false yourself ifTrue: ['alternative block']) equals: nil.
  125. self assert: (false yourself ifFalse: ['alternative block']) equals: 'alternative block'.
  126. self assert: (false yourself ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block2'.
  127. self assert: (false yourself ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block'.
  128. self assert: (true yourself ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block'.
  129. self assert: (true yourself ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block2'.
  130. !
  131. testLogic
  132. "Trivial logic table"
  133. self assert: (true & true);
  134. deny: (true & false);
  135. deny: (false & true);
  136. deny: (false & false).
  137. self assert: (true | true);
  138. assert: (true | false);
  139. assert: (false | true);
  140. deny: (false | false).
  141. "Checking that expressions work fine too"
  142. self assert: (true & (1 > 0));
  143. deny: ((1 > 0) & false);
  144. deny: ((1 > 0) & (1 > 2)).
  145. self assert: (false | (1 > 0));
  146. assert: ((1 > 0) | false);
  147. assert: ((1 > 0) | (1 > 2))
  148. !
  149. testLogicKeywords
  150. "Trivial logic table"
  151. self
  152. assert: (true and: [ true]);
  153. deny: (true and: [ false ]);
  154. deny: (false and: [ true ]);
  155. deny: (false and: [ false ]).
  156. self
  157. assert: (true or: [ true ]);
  158. assert: (true or: [ false ]);
  159. assert: (false or: [ true ]);
  160. deny: (false or: [ false ]).
  161. "Checking that expressions work fine too"
  162. self
  163. assert: (true and: [ 1 > 0 ]);
  164. deny: ((1 > 0) and: [ false ]);
  165. deny: ((1 > 0) and: [ 1 > 2 ]).
  166. self
  167. assert: (false or: [ 1 > 0 ]);
  168. assert: ((1 > 0) or: [ false ]);
  169. assert: ((1 > 0) or: [ 1 > 2 ])
  170. !
  171. testNonBooleanError
  172. self should: [ '' ifTrue: [] ifFalse: [] ] raise: NonBooleanReceiver
  173. ! !
  174. TestCase subclass: #ClassBuilderTest
  175. instanceVariableNames: 'builder theClass'
  176. package: 'Kernel-Tests'!
  177. !ClassBuilderTest methodsFor: 'running'!
  178. setUp
  179. builder := ClassBuilder new
  180. !
  181. tearDown
  182. theClass ifNotNil: [Smalltalk current removeClass: theClass. theClass := nil]
  183. ! !
  184. !ClassBuilderTest methodsFor: 'tests'!
  185. testClassCopy
  186. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  187. self assert: theClass superclass == ObjectMock superclass.
  188. self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
  189. self assert: theClass name equals: 'ObjectMock2'.
  190. self assert: theClass package == ObjectMock package.
  191. self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
  192. !
  193. testClassMigration
  194. | instance oldClass |
  195. oldClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  196. instance := (Smalltalk current at: 'ObjectMock2') new.
  197. "Change the superclass of ObjectMock2"
  198. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  199. instanceVariableNames: ''
  200. package: 'Kernel-Tests'.
  201. self deny: oldClass == ObjectMock2.
  202. self assert: ObjectMock2 superclass == ObjectMock.
  203. self assert: ObjectMock2 instanceVariableNames isEmpty.
  204. self assert: ObjectMock2 selectors equals: oldClass selectors.
  205. self assert: ObjectMock2 comment equals: oldClass comment.
  206. self assert: ObjectMock2 package name equals: 'Kernel-Tests'.
  207. self deny: instance class == ObjectMock2.
  208. "Commeting this out. Tests implementation detail."
  209. "self assert: instance class name equals: 'OldObjectMock2'."
  210. self assert: (Smalltalk current at: instance class name) isNil.
  211. Smalltalk current removeClass: ObjectMock2
  212. !
  213. testClassMigrationWithClassInstanceVariables
  214. builder copyClass: ObjectMock named: 'ObjectMock2'.
  215. ObjectMock2 class instanceVariableNames: 'foo bar'.
  216. "Change the superclass of ObjectMock2"
  217. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  218. instanceVariableNames: ''
  219. package: 'Kernel-Tests'.
  220. self assert: ObjectMock2 class instanceVariableNames equals: #('foo' 'bar').
  221. Smalltalk current removeClass: ObjectMock2
  222. !
  223. testClassMigrationWithSubclasses
  224. builder copyClass: ObjectMock named: 'ObjectMock2'.
  225. ObjectMock2 subclass: 'ObjectMock3' instanceVariableNames: '' package: 'Kernel-Tests'.
  226. ObjectMock3 subclass: 'ObjectMock4' instanceVariableNames: '' package: 'Kernel-Tests'.
  227. "Change the superclass of ObjectMock2"
  228. ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
  229. instanceVariableNames: ''
  230. package: 'Kernel-Tests'.
  231. self assert: (ObjectMock subclasses includes: ObjectMock2).
  232. self assert: (ObjectMock2 subclasses includes: ObjectMock3).
  233. self assert: (ObjectMock3 subclasses includes: ObjectMock4).
  234. ObjectMock allSubclasses do: [ :each | Smalltalk current removeClass: each ]
  235. !
  236. testInstanceVariableNames
  237. self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
  238. ! !
  239. TestCase subclass: #CollectionTest
  240. instanceVariableNames: ''
  241. package: 'Kernel-Tests'!
  242. !CollectionTest methodsFor: 'accessing'!
  243. collection
  244. ^ self collectionClass withAll: self defaultValues
  245. !
  246. collectionClass
  247. ^ self class collectionClass
  248. !
  249. collectionWithDuplicates
  250. ^ self collectionClass withAll: #('a' 'b' 'c' 1 2 1 'a')
  251. !
  252. defaultValues
  253. ^ #(1 2 3 -4)
  254. ! !
  255. !CollectionTest methodsFor: 'convenience'!
  256. assertSameContents: aCollection as: anotherCollection
  257. self assert: (aCollection size = anotherCollection size).
  258. aCollection do: [ :each |
  259. self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
  260. ! !
  261. !CollectionTest methodsFor: 'testing'!
  262. isCollectionReadOnly
  263. ^ false
  264. ! !
  265. !CollectionTest methodsFor: 'tests'!
  266. testAsArray
  267. self
  268. assertSameContents: self collection
  269. as: self collection asArray
  270. !
  271. testAsOrderedCollection
  272. self
  273. assertSameContents: self collection
  274. as: self collection asOrderedCollection
  275. !
  276. testAsSet
  277. | c set |
  278. c := self collectionWithDuplicates.
  279. set := c asSet.
  280. self assert: set size equals: 5.
  281. c do: [ :each |
  282. self assert: (set includes: each) ]
  283. !
  284. testCollect
  285. | newCollection |
  286. newCollection := #(1 2 3 4).
  287. self
  288. assertSameContents: (self collection collect: [ :each |
  289. each abs ])
  290. as: newCollection
  291. !
  292. testDetect
  293. self assert: (self collection detect: [ :each | each < 0 ]) equals: -4.
  294. self
  295. should: [ self collection detect: [ :each | each = 6 ] ]
  296. raise: Error
  297. !
  298. testDo
  299. | newCollection |
  300. newCollection := OrderedCollection new.
  301. self collection do: [ :each |
  302. newCollection add: each ].
  303. self
  304. assertSameContents: self collection
  305. as: newCollection
  306. !
  307. testIsEmpty
  308. self assert: self collectionClass new isEmpty.
  309. self deny: self collection isEmpty
  310. !
  311. testSelect
  312. | newCollection |
  313. newCollection := #(2 -4).
  314. self
  315. assertSameContents: (self collection select: [ :each |
  316. each even ])
  317. as: newCollection
  318. !
  319. testSize
  320. self assert: self collectionClass new size equals: 0.
  321. self assert: self collection size equals: 4
  322. ! !
  323. !CollectionTest class methodsFor: 'accessing'!
  324. collectionClass
  325. ^ nil
  326. ! !
  327. !CollectionTest class methodsFor: 'testing'!
  328. isAbstract
  329. ^ self collectionClass isNil
  330. ! !
  331. CollectionTest subclass: #HashedCollectionTest
  332. instanceVariableNames: ''
  333. package: 'Kernel-Tests'!
  334. !HashedCollectionTest methodsFor: 'accessing'!
  335. collection
  336. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
  337. !
  338. collectionWithDuplicates
  339. ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
  340. ! !
  341. !HashedCollectionTest methodsFor: 'tests'!
  342. testIndexOf
  343. self assert: (self collection indexOf: 2) equals: 'a'.
  344. self should: [ self collection indexOf: 999 ] raise: Error.
  345. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  346. ! !
  347. !HashedCollectionTest class methodsFor: 'accessing'!
  348. collectionClass
  349. ^ HashedCollection
  350. ! !
  351. HashedCollectionTest subclass: #DictionaryTest
  352. instanceVariableNames: ''
  353. package: 'Kernel-Tests'!
  354. !DictionaryTest methodsFor: 'accessing'!
  355. collection
  356. ^ Dictionary new
  357. at: 1 put: 1;
  358. at: 'a' put: 2;
  359. at: true put: 3;
  360. at: 4 put: -4;
  361. yourself
  362. !
  363. collectionWithDuplicates
  364. ^ Dictionary new
  365. at: 1 put: 1;
  366. at: 'a' put: 2;
  367. at: true put: 3;
  368. at: 4 put: -4;
  369. at: 'b' put: 1;
  370. at: 3 put: 3;
  371. at: false put: 12;
  372. yourself
  373. ! !
  374. !DictionaryTest methodsFor: 'tests'!
  375. testAccessing
  376. | d |
  377. d := Dictionary new.
  378. d at: 'hello' put: 'world'.
  379. self assert: (d at: 'hello') equals: 'world'.
  380. self assert: (d at: 'hello' ifAbsent: [nil]) equals: 'world'.
  381. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  382. self assert: (d includesKey: 'hello').
  383. self deny: (d includesKey: 'foo').
  384. d at: 1 put: 2.
  385. self assert: (d at: 1) equals: 2.
  386. d at: 1@3 put: 3.
  387. self assert: (d at: 1@3) equals: 3.
  388. self assert: (d includesKey: 1@3).
  389. self deny: (d includesKey: 3@1)
  390. !
  391. testDynamicDictionaries
  392. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  393. !
  394. testEquality
  395. | d1 d2 |
  396. self assert: (Dictionary new = Dictionary new).
  397. d1 := Dictionary new at: 1 put: 2; yourself.
  398. d2 := Dictionary new at: 1 put: 2; yourself.
  399. self assert: (d1 = d2).
  400. d2 := Dictionary new at: 1 put: 3; yourself.
  401. self deny: d1 = d2.
  402. d2 := Dictionary new at: 2 put: 2; yourself.
  403. self deny: d1 = d2.
  404. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  405. self deny: d1 = d2.
  406. !
  407. testIfAbsent
  408. | d visited |
  409. visited := false.
  410. d := Dictionary new.
  411. d at: 'hello' ifAbsent: [ visited := true ].
  412. self assert: visited.
  413. !
  414. testIfPresent
  415. | d visited absent |
  416. visited := false.
  417. d := Dictionary new.
  418. d at: 'hello' put: 'world'.
  419. d at: 'hello' ifPresent: [ :value | visited := value ].
  420. self assert: visited equals: 'world'.
  421. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  422. self assert: absent isNil.
  423. !
  424. testIfPresentIfAbsent
  425. | d visited |
  426. visited := false.
  427. d := Dictionary new.
  428. d at: 'hello' put: 'world'.
  429. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  430. self assert: visited equals: 'world'.
  431. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  432. self assert: visited.
  433. !
  434. testKeys
  435. | d |
  436. d := Dictionary new.
  437. d at: 1 put: 2.
  438. d at: 2 put: 3.
  439. d at: 3 put: 4.
  440. self assert: d keys equals: #(1 2 3)
  441. !
  442. testPointKey
  443. | d |
  444. d := Dictionary new.
  445. d at: 1@1 put: 'foo'.
  446. self assert: (d at: 1@1) equals: 'foo'.
  447. d at: 1@1 put: 'bar'.
  448. self assert: (d at: 1@1) equals: 'bar'.
  449. d removeKey: 1@1.
  450. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  451. self deny: (d includesKey: 1@1)
  452. !
  453. testPrintString
  454. self
  455. assert: (Dictionary new
  456. at:'firstname' put: 'James';
  457. at:'lastname' put: 'Bond';
  458. printString)
  459. equals: 'a Dictionary(''firstname''->''James'' , ''lastname''->''Bond'')'
  460. !
  461. testRemoveKey
  462. | d key |
  463. d := Dictionary new.
  464. d at: 1 put: 2.
  465. d at: 2 put: 3.
  466. d at: 3 put: 4.
  467. key := 2.
  468. self assert: d keys equals: #(1 2 3).
  469. d removeKey: key.
  470. self assert: d keys equals: #(1 3).
  471. self assert: d values equals: #(2 4).
  472. self deny: (d includesKey: 2)
  473. !
  474. testRemoveKeyIfAbsent
  475. | d key |
  476. d := Dictionary new.
  477. d at: 1 put: 2.
  478. d at: 2 put: 3.
  479. d at: 3 put: 4.
  480. key := 2.
  481. self assert: (d removeKey: key) equals: 3.
  482. key := 3.
  483. self assert: (d removeKey: key ifAbsent: [42]) equals: 4.
  484. key := 'why'.
  485. self assert: (d removeKey: key ifAbsent: [42] ) equals: 42.
  486. !
  487. testSize
  488. | d |
  489. d := Dictionary new.
  490. self assert: d size equals: 0.
  491. d at: 1 put: 2.
  492. self assert: d size equals: 1.
  493. d at: 2 put: 3.
  494. self assert: d size equals: 2.
  495. !
  496. testValues
  497. | d |
  498. d := Dictionary new.
  499. d at: 1 put: 2.
  500. d at: 2 put: 3.
  501. d at: 3 put: 4.
  502. self assert: d values equals: #(2 3 4)
  503. ! !
  504. !DictionaryTest class methodsFor: 'accessing'!
  505. collectionClass
  506. ^ Dictionary
  507. ! !
  508. CollectionTest subclass: #SequenceableCollectionTest
  509. instanceVariableNames: ''
  510. package: 'Kernel-Tests'!
  511. !SequenceableCollectionTest methodsFor: 'tests'!
  512. testAt
  513. self assert: (self collection at: 4) equals: -4.
  514. self should: [ self collection at: 5 ] raise: Error
  515. !
  516. testAtIfAbsent
  517. self assert: (self collection at: (self collection size + 1) ifAbsent: [ 'none' ]) equals: 'none'
  518. ! !
  519. SequenceableCollectionTest subclass: #ArrayTest
  520. instanceVariableNames: ''
  521. package: 'Kernel-Tests'!
  522. !ArrayTest methodsFor: 'tests'!
  523. testAtIfAbsent
  524. | array |
  525. array := #('hello' 'world').
  526. self assert: (array at: 1) equals: 'hello'.
  527. self assert: (array at: 2) equals: 'world'.
  528. self assert: (array at: 2 ifAbsent: ['not found']) equals: 'world'.
  529. self assert: (array at: 0 ifAbsent: ['not found']) equals: 'not found'.
  530. self assert: (array at: -10 ifAbsent: ['not found']) equals: 'not found'.
  531. self assert: (array at: 3 ifAbsent: ['not found']) equals: 'not found'.
  532. !
  533. testFirstN
  534. self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
  535. !
  536. testIfEmpty
  537. self assert: ( '' ifEmpty: ['zork'] ) equals: 'zork'
  538. !
  539. testPrintString
  540. | array |
  541. array := Array new.
  542. self assert: array printString equals: 'a Array ()'.
  543. array add: 1; add: 3.
  544. self assert: array printString equals: 'a Array (1 3)'.
  545. array add: 'foo'.
  546. self assert: array printString equals: 'a Array (1 3 ''foo'')'.
  547. array remove: 1; remove: 3.
  548. self assert: array printString equals: 'a Array (''foo'')'.
  549. array addLast: 3.
  550. self assert: array printString equals: 'a Array (''foo'' 3)'.
  551. array addLast: 3.
  552. self assert: array printString equals: 'a Array (''foo'' 3 3)'.
  553. ! !
  554. !ArrayTest class methodsFor: 'accessing'!
  555. collectionClass
  556. ^ Array
  557. ! !
  558. SequenceableCollectionTest subclass: #StringTest
  559. instanceVariableNames: ''
  560. package: 'Kernel-Tests'!
  561. !StringTest methodsFor: 'accessing'!
  562. collection
  563. ^'hello'
  564. !
  565. collectionWithDuplicates
  566. ^ 'abbaerte'
  567. ! !
  568. !StringTest methodsFor: 'tests'!
  569. testAddRemove
  570. self should: ['hello' add: 'a'] raise: Error.
  571. self should: ['hello' remove: 'h'] raise: Error
  572. !
  573. testAsArray
  574. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  575. !
  576. testAt
  577. self assert: ('hello' at: 1) equals: 'h'.
  578. self assert: ('hello' at: 5) equals: 'o'.
  579. self assert: ('hello' at: 6 ifAbsent: [nil]) equals: nil
  580. !
  581. testAtPut
  582. "String instances are read-only"
  583. self should: ['hello' at: 1 put: 'a'] raise: Error
  584. !
  585. testCollect
  586. | newCollection |
  587. newCollection := 'hheelllloo'.
  588. self
  589. assertSameContents: (self collection collect: [ :each |
  590. each, each ])
  591. as: newCollection
  592. !
  593. testCopyWithoutAll
  594. self
  595. assert: ('*hello* *world*' copyWithoutAll: '*')
  596. equals: 'hello world'
  597. !
  598. testDetect
  599. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  600. self
  601. should: [ self collection detect: [ :each | each = 6 ] ]
  602. raise: Error
  603. !
  604. testEquality
  605. self assert: 'hello' equals: 'hello'.
  606. self deny: 'hello' = 'world'.
  607. self assert: 'hello' equals: 'hello' yourself.
  608. self assert: 'hello' yourself equals: 'hello'.
  609. "test JS falsy value"
  610. self deny: '' = 0
  611. !
  612. testIdentity
  613. self assert: 'hello' == 'hello'.
  614. self deny: 'hello' == 'world'.
  615. self assert: 'hello' == 'hello' yourself.
  616. self assert: 'hello' yourself == 'hello'.
  617. "test JS falsy value"
  618. self deny: '' == 0
  619. !
  620. testIncludesSubString
  621. self assert: ('amber' includesSubString: 'ber').
  622. self deny: ('amber' includesSubString: 'zork').
  623. !
  624. testJoin
  625. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  626. !
  627. testSelect
  628. | newCollection |
  629. newCollection := 'o'.
  630. self
  631. assertSameContents: (self collection select: [ :each |
  632. each = 'o' ])
  633. as: newCollection
  634. !
  635. testSize
  636. self assert: 'smalltalk' size equals: 9.
  637. self assert: '' size equals: 0
  638. !
  639. testStreamContents
  640. self
  641. assert: (String streamContents: [ :aStream |
  642. aStream
  643. nextPutAll: 'hello'; space;
  644. nextPutAll: 'world' ])
  645. equals: 'hello world'
  646. ! !
  647. !StringTest class methodsFor: 'accessing'!
  648. collectionClass
  649. ^ String
  650. ! !
  651. SequenceableCollectionTest subclass: #SymbolTest
  652. instanceVariableNames: ''
  653. package: 'Kernel-Tests'!
  654. !SymbolTest methodsFor: 'accessing'!
  655. collection
  656. ^ #hello
  657. !
  658. collectionWithDuplicates
  659. ^ #phhaaarorra
  660. ! !
  661. !SymbolTest methodsFor: 'tests'!
  662. testAsString
  663. self assert: #hello asString equals: 'hello'
  664. !
  665. testAsSymbol
  666. self assert: #hello == #hello asSymbol
  667. !
  668. testAt
  669. self assert: (#hello at: 1) equals: 'h'.
  670. self assert: (#hello at: 5) equals: 'o'.
  671. self assert: (#hello at: 6 ifAbsent: [nil]) equals: nil
  672. !
  673. testAtPut
  674. "Symbol instances are read-only"
  675. self should: ['hello' at: 1 put: 'a'] raise: Error
  676. !
  677. testCollect
  678. | newCollection |
  679. newCollection := #hheelllloo.
  680. self
  681. assertSameContents: (self collection collect: [ :each |
  682. each, each ])
  683. as: newCollection
  684. !
  685. testComparing
  686. self assert: #ab > #aa.
  687. self deny: #ab > #ba.
  688. self assert: #ab < #ba.
  689. self deny: #bb < #ba.
  690. self assert: #ab >= #aa.
  691. self deny: #ab >= #ba.
  692. self assert: #ab <= #ba.
  693. self deny: #bb <= #ba
  694. !
  695. testCopying
  696. self assert: #hello copy == #hello.
  697. self assert: #hello deepCopy == #hello
  698. !
  699. testDetect
  700. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  701. self
  702. should: [ self collection detect: [ :each | each = 'z' ] ]
  703. raise: Error
  704. !
  705. testEquality
  706. self assert: (#hello = #hello).
  707. self deny: #hello = #world.
  708. self assert: (#hello = #hello yourself).
  709. self assert: (#hello yourself = #hello).
  710. self deny: #hello = 'hello'.
  711. self deny: 'hello' = #hello.
  712. !
  713. testIdentity
  714. self assert: #hello == #hello.
  715. self deny: #hello == #world.
  716. self assert: #hello = #hello yourself.
  717. self assert: #hello yourself = #hello asString asSymbol
  718. !
  719. testIsEmpty
  720. self deny: self collection isEmpty.
  721. self assert: '' asSymbol isEmpty
  722. !
  723. testIsSymbolIsString
  724. self assert: #hello isSymbol.
  725. self deny: 'hello' isSymbol.
  726. self deny: #hello isString.
  727. self assert: 'hello' isString
  728. !
  729. testSelect
  730. | newCollection |
  731. newCollection := 'o'.
  732. self
  733. assertSameContents: (self collection select: [ :each |
  734. each = 'o' ])
  735. as: newCollection
  736. !
  737. testSize
  738. self assert: #a size equals: 1.
  739. self assert: #aaaaa size equals: 5
  740. ! !
  741. !SymbolTest class methodsFor: 'accessing'!
  742. collectionClass
  743. ^ Symbol
  744. ! !
  745. TestCase subclass: #JSObjectProxyTest
  746. instanceVariableNames: ''
  747. package: 'Kernel-Tests'!
  748. !JSObjectProxyTest methodsFor: 'accessing'!
  749. jsObject
  750. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined}>
  751. ! !
  752. !JSObjectProxyTest methodsFor: 'tests'!
  753. testAtIfAbsent
  754. | testObject |
  755. testObject := self jsObject.
  756. self assert: (testObject at: 'abc' ifAbsent: ['Property does not exist']) equals: 'Property does not exist'.
  757. self assert: (testObject at: 'e' ifAbsent: ['Property does not exist']) equals: nil.
  758. self assert: (testObject at: 'a' ifAbsent: ['Property does not exist']) equals: 1.
  759. self assert: (testObject at: 'f' ifAbsent: ['Property does not exist']) equals: nil.
  760. !
  761. testAtIfPresent
  762. | testObject |
  763. testObject := self jsObject.
  764. self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString]) equals: nil.
  765. self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString]) equals: 'hello nil'.
  766. self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString]) equals: 'hello 1'.
  767. self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString]) equals: 'hello nil'.
  768. !
  769. testAtIfPresentIfAbsent
  770. | testObject |
  771. testObject := self jsObject.
  772. self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'not present'.
  773. self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  774. self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello 1'.
  775. self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  776. !
  777. testDNU
  778. self should: [self jsObject foo] raise: MessageNotUnderstood
  779. !
  780. testMessageSend
  781. self assert: self jsObject a equals: 1.
  782. self assert: self jsObject b equals: 2.
  783. self assert: (self jsObject c: 3) equals: 3
  784. !
  785. testMethodWithArguments
  786. self assert: (self jsObject c: 1) equals: 1
  787. !
  788. testPrinting
  789. self assert: self jsObject printString equals: '[object Object]'
  790. !
  791. testPropertyThatReturnsEmptyString
  792. | object |
  793. object := self jsObject.
  794. self assert: object d equals: ''.
  795. object d: 'hello'.
  796. self assert: object d equals: 'hello'
  797. !
  798. testPropertyThatReturnsUndefined
  799. | object |
  800. object := self jsObject.
  801. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  802. self assert: object e isNil
  803. !
  804. testValue
  805. | testObject |
  806. testObject := self jsObject.
  807. self assert: testObject value printString equals: '[object Object]'.
  808. testObject at: 'value' put: 'aValue'.
  809. self assert: testObject value equals: 'aValue'
  810. !
  811. testYourself
  812. | object |
  813. object := self jsObject
  814. d: 'test';
  815. yourself.
  816. self assert: object d equals: 'test'
  817. ! !
  818. TestCase subclass: #JavaScriptExceptionTest
  819. instanceVariableNames: ''
  820. package: 'Kernel-Tests'!
  821. !JavaScriptExceptionTest methodsFor: 'helpers'!
  822. throwException
  823. <throw 'test'>
  824. ! !
  825. !JavaScriptExceptionTest methodsFor: 'tests'!
  826. testCatchingException
  827. [ self throwException ]
  828. on: Error
  829. do: [ :error |
  830. self assert: error exception = 'test' ]
  831. !
  832. testRaisingException
  833. self should: [ self throwException ] raise: JavaScriptException
  834. ! !
  835. TestCase subclass: #NumberTest
  836. instanceVariableNames: ''
  837. package: 'Kernel-Tests'!
  838. !NumberTest methodsFor: 'tests'!
  839. testAbs
  840. self assert: 4 abs equals: 4.
  841. self assert: -4 abs equals: 4
  842. !
  843. testArithmetic
  844. "We rely on JS here, so we won't test complex behavior, just check if
  845. message sends are corrects"
  846. self assert: 1.5 + 1 equals: 2.5.
  847. self assert: 2 - 1 equals: 1.
  848. self assert: -2 - 1 equals: -3.
  849. self assert: 12 / 2 equals: 6.
  850. self assert: 3 * 4 equals: 12.
  851. "Simple parenthesis and execution order"
  852. self assert: 1 + 2 * 3 equals: 9.
  853. self assert: 1 + (2 * 3) equals: 7
  854. !
  855. testComparison
  856. self assert: 3 > 2.
  857. self assert: 2 < 3.
  858. self deny: 3 < 2.
  859. self deny: 2 > 3.
  860. self assert: 3 >= 3.
  861. self assert: 3.1 >= 3.
  862. self assert: 3 <= 3.
  863. self assert: 3 <= 3.1
  864. !
  865. testCopying
  866. self assert: 1 copy == 1.
  867. self assert: 1 deepCopy == 1
  868. !
  869. testEquality
  870. self assert: (1 = 1).
  871. self assert: (0 = 0).
  872. self deny: (1 = 0).
  873. self assert: (1 yourself = 1).
  874. self assert: (1 = 1 yourself).
  875. self assert: (1 yourself = 1 yourself).
  876. self deny: 0 = false.
  877. self deny: false = 0.
  878. self deny: '' = 0.
  879. self deny: 0 = ''
  880. !
  881. testHexNumbers
  882. self assert: 16r9 equals: 9.
  883. self assert: 16rA truncated equals: 10.
  884. self assert: 16rB truncated equals: 11.
  885. self assert: 16rC truncated equals: 12.
  886. self assert: 16rD truncated equals: 13.
  887. self assert: 16rE truncated equals: 14.
  888. self assert: 16rF truncated equals: 15
  889. !
  890. testIdentity
  891. self assert: 1 == 1.
  892. self assert: 0 == 0.
  893. self deny: 1 == 0.
  894. self assert: 1 yourself == 1.
  895. self assert: 1 == 1 yourself.
  896. self assert: 1 yourself == 1 yourself.
  897. self deny: 1 == 2
  898. !
  899. testInvalidHexNumbers
  900. self should: [16rG] raise: MessageNotUnderstood.
  901. self should: [16rg] raise: MessageNotUnderstood.
  902. self should: [16rH] raise: MessageNotUnderstood.
  903. self should: [16rh] raise: MessageNotUnderstood.
  904. self should: [16rI] raise: MessageNotUnderstood.
  905. self should: [16ri] raise: MessageNotUnderstood.
  906. self should: [16rJ] raise: MessageNotUnderstood.
  907. self should: [16rj] raise: MessageNotUnderstood.
  908. self should: [16rK] raise: MessageNotUnderstood.
  909. self should: [16rk] raise: MessageNotUnderstood.
  910. self should: [16rL] raise: MessageNotUnderstood.
  911. self should: [16rl] raise: MessageNotUnderstood.
  912. self should: [16rM] raise: MessageNotUnderstood.
  913. self should: [16rm] raise: MessageNotUnderstood.
  914. self should: [16rN] raise: MessageNotUnderstood.
  915. self should: [16rn] raise: MessageNotUnderstood.
  916. self should: [16rO] raise: MessageNotUnderstood.
  917. self should: [16ro] raise: MessageNotUnderstood.
  918. self should: [16rP] raise: MessageNotUnderstood.
  919. self should: [16rp] raise: MessageNotUnderstood.
  920. self should: [16rQ] raise: MessageNotUnderstood.
  921. self should: [16rq] raise: MessageNotUnderstood.
  922. self should: [16rR] raise: MessageNotUnderstood.
  923. self should: [16rr] raise: MessageNotUnderstood.
  924. self should: [16rS] raise: MessageNotUnderstood.
  925. self should: [16rs] raise: MessageNotUnderstood.
  926. self should: [16rT] raise: MessageNotUnderstood.
  927. self should: [16rt] raise: MessageNotUnderstood.
  928. self should: [16rU] raise: MessageNotUnderstood.
  929. self should: [16ru] raise: MessageNotUnderstood.
  930. self should: [16rV] raise: MessageNotUnderstood.
  931. self should: [16rv] raise: MessageNotUnderstood.
  932. self should: [16rW] raise: MessageNotUnderstood.
  933. self should: [16rw] raise: MessageNotUnderstood.
  934. self should: [16rX] raise: MessageNotUnderstood.
  935. self should: [16rx] raise: MessageNotUnderstood.
  936. self should: [16rY] raise: MessageNotUnderstood.
  937. self should: [16ry] raise: MessageNotUnderstood.
  938. self should: [16rZ] raise: MessageNotUnderstood.
  939. self should: [16rz] raise: MessageNotUnderstood.
  940. self should: [16rABcdEfZ] raise: MessageNotUnderstood.
  941. !
  942. testMinMax
  943. self assert: (2 max: 5) equals: 5.
  944. self assert: (2 min: 5) equals: 2
  945. !
  946. testNegated
  947. self assert: 3 negated equals: -3.
  948. self assert: -3 negated equals: 3
  949. !
  950. testPrintShowingDecimalPlaces
  951. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  952. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  953. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  954. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  955. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  956. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  957. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  958. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  959. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  960. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  961. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  962. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  963. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  964. !
  965. testRounded
  966. self assert: 3 rounded equals: 3.
  967. self assert: 3.212 rounded equals: 3.
  968. self assert: 3.51 rounded equals: 4
  969. !
  970. testSqrt
  971. self assert: 4 sqrt equals: 2.
  972. self assert: 16 sqrt equals: 4
  973. !
  974. testSquared
  975. self assert: 4 squared equals: 16
  976. !
  977. testTimesRepeat
  978. | i |
  979. i := 0.
  980. 0 timesRepeat: [i := i + 1].
  981. self assert: i equals: 0.
  982. 5 timesRepeat: [i := i + 1].
  983. self assert: i equals: 5
  984. !
  985. testTo
  986. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  987. !
  988. testToBy
  989. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  990. self should: [1 to: 4 by: 0] raise: Error
  991. !
  992. testTruncated
  993. self assert: 3 truncated equals: 3.
  994. self assert: 3.212 truncated equals: 3.
  995. self assert: 3.51 truncated equals: 3
  996. ! !
  997. Object subclass: #ObjectMock
  998. instanceVariableNames: 'foo bar'
  999. package: 'Kernel-Tests'!
  1000. !ObjectMock commentStamp!
  1001. ObjectMock is there only to perform tests on classes.!
  1002. !ObjectMock methodsFor: 'not yet classified'!
  1003. foo
  1004. ^foo
  1005. !
  1006. foo: anObject
  1007. foo := anObject
  1008. ! !
  1009. TestCase subclass: #ObjectTest
  1010. instanceVariableNames: ''
  1011. package: 'Kernel-Tests'!
  1012. !ObjectTest methodsFor: 'tests'!
  1013. notDefined
  1014. <return undefined;>
  1015. !
  1016. testBasicAccess
  1017. | o |
  1018. o := Object new.
  1019. o basicAt: 'a' put: 1.
  1020. self assert: (o basicAt: 'a') equals: 1.
  1021. self assert: (o basicAt: 'b') equals: nil
  1022. !
  1023. testBasicPerform
  1024. | o |
  1025. o := Object new.
  1026. o basicAt: 'func' put: ['hello'].
  1027. o basicAt: 'func2' put: [:a | a + 1].
  1028. self assert: (o basicPerform: 'func') equals: 'hello'.
  1029. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1030. !
  1031. testDNU
  1032. self should: [Object new foo] raise: MessageNotUnderstood
  1033. !
  1034. testEquality
  1035. | o |
  1036. o := Object new.
  1037. self deny: o = Object new.
  1038. self assert: (o = o).
  1039. self assert: (o yourself = o).
  1040. self assert: (o = o yourself)
  1041. !
  1042. testHalt
  1043. self should: [Object new halt] raise: Error
  1044. !
  1045. testIdentity
  1046. | o |
  1047. o := Object new.
  1048. self deny: o == Object new.
  1049. self assert: o == o.
  1050. self assert: o yourself == o.
  1051. self assert: o == o yourself
  1052. !
  1053. testIfNil
  1054. self deny: Object new isNil.
  1055. self deny: (Object new ifNil: [true]) = true.
  1056. self assert: (Object new ifNotNil: [true]) equals: true.
  1057. self assert: (Object new ifNil: [false] ifNotNil: [true]) equals: true.
  1058. self assert: (Object new ifNotNil: [true] ifNil: [false]) equals: true
  1059. !
  1060. testInstVars
  1061. | o |
  1062. o := ObjectMock new.
  1063. self assert: (o instVarAt: #foo) equals: nil.
  1064. o instVarAt: #foo put: 1.
  1065. self assert: (o instVarAt: #foo) equals: 1.
  1066. self assert: (o instVarAt: 'foo') equals: 1
  1067. !
  1068. testNilUndefined
  1069. "nil in Smalltalk is the undefined object in JS"
  1070. self assert: self notDefined equals: nil
  1071. !
  1072. testYourself
  1073. | o |
  1074. o := ObjectMock new.
  1075. self assert: o yourself == o
  1076. !
  1077. testidentityHash
  1078. | o1 o2 |
  1079. o1 := Object new.
  1080. o2 := Object new.
  1081. self assert: o1 identityHash == o1 identityHash.
  1082. self deny: o1 identityHash == o2 identityHash
  1083. ! !
  1084. TestCase subclass: #PackageTest
  1085. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  1086. package: 'Kernel-Tests'!
  1087. !PackageTest methodsFor: 'running'!
  1088. setUp
  1089. backUpCommitPathJs := Package defaultCommitPathJs.
  1090. backUpCommitPathSt := Package defaultCommitPathSt.
  1091. Package resetCommitPaths.
  1092. zorkPackage := Package new name: 'Zork'.
  1093. grulPackage := Package new
  1094. name: 'Grul';
  1095. commitPathJs: 'server/grul/js';
  1096. commitPathSt: 'grul/st';
  1097. yourself
  1098. !
  1099. tearDown
  1100. Package
  1101. defaultCommitPathJs: backUpCommitPathJs;
  1102. defaultCommitPathSt: backUpCommitPathSt
  1103. ! !
  1104. !PackageTest methodsFor: 'tests'!
  1105. testGrulCommitPathJsShouldBeServerGrulJs
  1106. self assert: grulPackage commitPathJs equals: 'server/grul/js'
  1107. !
  1108. testGrulCommitPathStShouldBeGrulSt
  1109. self assert: grulPackage commitPathSt equals: 'grul/st'
  1110. !
  1111. testZorkCommitPathJsShouldBeJs
  1112. self assert: zorkPackage commitPathJs equals: 'js'
  1113. !
  1114. testZorkCommitPathStShouldBeSt
  1115. self assert: zorkPackage commitPathSt equals: 'st'
  1116. ! !
  1117. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  1118. instanceVariableNames: ''
  1119. package: 'Kernel-Tests'!
  1120. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  1121. setUp
  1122. super setUp.
  1123. Package
  1124. defaultCommitPathJs: 'javascripts/';
  1125. defaultCommitPathSt: 'smalltalk/'.
  1126. ! !
  1127. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  1128. testGrulCommitPathJsShouldBeServerGrulJs
  1129. self assert: grulPackage commitPathJs equals: 'server/grul/js'
  1130. !
  1131. testGrulCommitPathStShouldBeGrulSt
  1132. self assert: grulPackage commitPathSt equals: 'grul/st'
  1133. !
  1134. testZorkCommitPathJsShouldBeJavascript
  1135. self assert: zorkPackage commitPathJs equals: 'javascripts/'
  1136. !
  1137. testZorkCommitPathStShouldBeSmalltalk
  1138. self assert: zorkPackage commitPathSt equals: 'smalltalk/'
  1139. ! !
  1140. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  1141. shouldInheritSelectors
  1142. ^ false
  1143. ! !
  1144. TestCase subclass: #PointTest
  1145. instanceVariableNames: ''
  1146. package: 'Kernel-Tests'!
  1147. !PointTest methodsFor: 'tests'!
  1148. testAccessing
  1149. self assert: (Point x: 3 y: 4) x equals: 3.
  1150. self assert: (Point x: 3 y: 4) y equals: 4.
  1151. self assert: (Point new x: 3) x equals: 3.
  1152. self assert: (Point new y: 4) y equals: 4
  1153. !
  1154. testArithmetic
  1155. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1156. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1157. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1158. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1159. !
  1160. testAt
  1161. self assert: 3@4 equals: (Point x: 3 y: 4)
  1162. !
  1163. testEgality
  1164. self assert: (3@4 = (3@4)).
  1165. self deny: 3@5 = (3@6)
  1166. !
  1167. testTranslateBy
  1168. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1169. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1170. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1171. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1172. ! !
  1173. TestCase subclass: #RandomTest
  1174. instanceVariableNames: ''
  1175. package: 'Kernel-Tests'!
  1176. !RandomTest methodsFor: 'tests'!
  1177. textNext
  1178. 10000 timesRepeat: [
  1179. | current next |
  1180. next := Random new next.
  1181. self assert: (next >= 0).
  1182. self assert: (next < 1).
  1183. self deny: current = next.
  1184. next = current]
  1185. ! !
  1186. TestCase subclass: #SetTest
  1187. instanceVariableNames: ''
  1188. package: 'Kernel-Tests'!
  1189. !SetTest methodsFor: 'tests'!
  1190. testAddRemove
  1191. | set |
  1192. set := Set new.
  1193. self assert: set isEmpty.
  1194. set add: 3.
  1195. self assert: (set includes: 3).
  1196. set add: 5.
  1197. self assert: (set includes: 5).
  1198. set remove: 3.
  1199. self deny: (set includes: 3)
  1200. !
  1201. testAt
  1202. self should: [Set new at: 1 put: 2] raise: Error
  1203. !
  1204. testCollect
  1205. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1206. !
  1207. testComparing
  1208. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1209. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1210. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1211. self deny: #(1 2) asSet = #(0 2) asSet
  1212. !
  1213. testPrintString
  1214. | set |
  1215. set := Set new.
  1216. self assert: set printString equals: 'a Set ()'.
  1217. set add: 1; add: 3.
  1218. self assert: set printString equals: 'a Set (1 3)'.
  1219. set add: 'foo'.
  1220. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1221. set remove: 1; remove: 3.
  1222. self assert: set printString equals: 'a Set (''foo'')'.
  1223. set add: 3.
  1224. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1225. set add: 3.
  1226. self assert: set printString equals: 'a Set (''foo'' 3)'
  1227. !
  1228. testSize
  1229. self assert: Set new size equals: 0.
  1230. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1231. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1232. !
  1233. testUnicity
  1234. | set |
  1235. set := Set new.
  1236. set add: 21.
  1237. set add: 'hello'.
  1238. set add: 21.
  1239. self assert: set size equals: 2.
  1240. set add: 'hello'.
  1241. self assert: set size equals: 2.
  1242. self assert: set asArray equals: #(21 'hello')
  1243. ! !
  1244. TestCase subclass: #UndefinedTest
  1245. instanceVariableNames: ''
  1246. package: 'Kernel-Tests'!
  1247. !UndefinedTest methodsFor: 'tests'!
  1248. testCopying
  1249. self assert: nil copy equals: nil
  1250. !
  1251. testDeepCopy
  1252. self assert: nil deepCopy = nil
  1253. !
  1254. testIfNil
  1255. self assert: (nil ifNil: [true]) equals: true.
  1256. self deny: (nil ifNotNil: [true]) = true.
  1257. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1258. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1259. !
  1260. testIsNil
  1261. self assert: nil isNil.
  1262. self deny: nil notNil.
  1263. ! !