Kernel-Tests.st 36 KB

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