Kernel-Tests.st 35 KB

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