2
0

Kernel-Tests.st 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. Smalltalk current createPackage: 'Kernel-Tests' properties: #{}!
  2. TestCase subclass: #BlockClosureTest
  3. instanceVariableNames: ''
  4. package: 'Kernel-Tests'!
  5. !BlockClosureTest methodsFor: 'tests'!
  6. testCompiledSource
  7. self assert: ([1+1] compiledSource includesSubString: 'function')
  8. !
  9. testEnsure
  10. self assert: ([Error new] ensure: [true])
  11. !
  12. testNumArgs
  13. self assert: [] numArgs equals: 0.
  14. self assert: [:a :b | ] numArgs equals: 2
  15. !
  16. testOnDo
  17. self assert: ([Error new signal] on: Error do: [:ex | true])
  18. !
  19. testValue
  20. self assert: ([1+1] value) equals: 2.
  21. self assert: ([:x | x +1] value: 2) equals: 3.
  22. self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
  23. "Arguments are optional in Amber. This isn't ANSI compliant."
  24. self assert: ([:a :b :c | 1] value) equals: 1
  25. !
  26. testValueWithPossibleArguments
  27. self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
  28. self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
  29. self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  30. !
  31. testWhileFalse
  32. | i |
  33. i := 0.
  34. [i > 5] whileFalse: [i := i + 1].
  35. self assert: i equals: 6.
  36. i := 0.
  37. [i := i + 1. i > 5] whileFalse.
  38. self assert: i equals: 6
  39. !
  40. testWhileTrue
  41. | i |
  42. i := 0.
  43. [i < 5] whileTrue: [i := i + 1].
  44. self assert: i equals: 5.
  45. i := 0.
  46. [i := i + 1. i < 5] whileTrue.
  47. self assert: i equals: 5
  48. ! !
  49. TestCase subclass: #BooleanTest
  50. instanceVariableNames: ''
  51. package: 'Kernel-Tests'!
  52. !BooleanTest methodsFor: 'tests'!
  53. testEquality
  54. "We're on top of JS...just be sure to check the basics!!"
  55. self deny: 0 = false.
  56. self deny: false = 0.
  57. self deny: '' = false.
  58. self deny: false = ''.
  59. self assert: true = true.
  60. self deny: false = true.
  61. self deny: true = false.
  62. self assert: false = false.
  63. "JS may do some type coercing after sending a message"
  64. self assert: true yourself = true.
  65. self assert: true yourself = true yourself
  66. !
  67. testIdentity
  68. "We're on top of JS...just be sure to check the basics!!"
  69. self deny: 0 == false.
  70. self deny: false == 0.
  71. self deny: '' == false.
  72. self deny: false == ''.
  73. self assert: true == true.
  74. self deny: false == true.
  75. self deny: true == false.
  76. self assert: false == false.
  77. "JS may do some type coercing after sending a message"
  78. self assert: true yourself == true.
  79. self assert: true yourself == true yourself
  80. !
  81. testIfTrueIfFalse
  82. self assert: (true ifTrue: ['alternative block']) = 'alternative block'.
  83. self assert: (true ifFalse: ['alternative block']) = nil.
  84. self assert: (false ifTrue: ['alternative block']) = nil.
  85. self assert: (false ifFalse: ['alternative block']) = 'alternative block'.
  86. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block2'.
  87. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block'.
  88. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block'.
  89. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block2'.
  90. !
  91. testLogic
  92. "Trivial logic table"
  93. self assert: (true & true); deny: (true & false); deny: (false & true); deny: (false & false).
  94. self assert: (true | true); assert: (true | false); assert: (false | true); deny: (false | false).
  95. "Checking that expressions work fine too"
  96. self assert: (true & (1 > 0)); deny: ((1 > 0) & false); deny: ((1 > 0) & (1 > 2)).
  97. self assert: (false | (1 > 0)); assert: ((1 > 0) | false); assert: ((1 > 0) | (1 > 2))
  98. !
  99. testLogicKeywords
  100. "Trivial logic table"
  101. self
  102. assert: (true and: [ true]);
  103. deny: (true and: [ false ]);
  104. deny: (false and: [ true ]);
  105. deny: (false and: [ false ]).
  106. self
  107. assert: (true or: [ true ]);
  108. assert: (true or: [ false ]);
  109. assert: (false or: [ true ]);
  110. deny: (false or: [ false ]).
  111. "Checking that expressions work fine too"
  112. self
  113. assert: (true and: [ 1 > 0 ]);
  114. deny: ((1 > 0) and: [ false ]);
  115. deny: ((1 > 0) and: [ 1 > 2 ]).
  116. self
  117. assert: (false or: [ 1 > 0 ]);
  118. assert: ((1 > 0) or: [ false ]);
  119. assert: ((1 > 0) or: [ 1 > 2 ])
  120. !
  121. testNonBooleanError
  122. self should: ['' ifTrue: [] ifFalse: []] raise: NonBooleanReceiver
  123. ! !
  124. TestCase subclass: #ClassBuilderTest
  125. instanceVariableNames: 'builder theClass'
  126. package: 'Kernel-Tests'!
  127. !ClassBuilderTest methodsFor: 'running'!
  128. setUp
  129. builder := ClassBuilder new
  130. !
  131. tearDown
  132. theClass ifNotNil: [Smalltalk current removeClass: theClass. theClass := nil]
  133. !
  134. testClassCopy
  135. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  136. self assert: theClass superclass == ObjectMock superclass.
  137. self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
  138. self assert: theClass name equals: 'ObjectMock2'.
  139. self assert: theClass package == ObjectMock package.
  140. self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
  141. !
  142. testInstanceVariableNames
  143. self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
  144. ! !
  145. TestCase subclass: #CollectionTest
  146. instanceVariableNames: ''
  147. package: 'Kernel-Tests'!
  148. !CollectionTest methodsFor: 'accessing'!
  149. collection
  150. ^ self collectionClass withAll: self defaultValues
  151. !
  152. collectionClass
  153. ^ self class collectionClass
  154. !
  155. collectionWithDuplicates
  156. ^ self collectionClass withAll: #('a' 'b' 'c' 1 2 1 'a')
  157. !
  158. defaultValues
  159. ^ #(1 2 3 -4)
  160. ! !
  161. !CollectionTest methodsFor: 'convenience'!
  162. assertSameContents: aCollection as: anotherCollection
  163. self assert: aCollection size = anotherCollection size.
  164. aCollection do: [ :each |
  165. self assert: (aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each) ]
  166. ! !
  167. !CollectionTest methodsFor: 'testing'!
  168. isCollectionReadOnly
  169. ^ false
  170. ! !
  171. !CollectionTest methodsFor: 'tests'!
  172. testAsArray
  173. self
  174. assertSameContents: self collection
  175. as: self collection asArray
  176. !
  177. testAsOrderedCollection
  178. self
  179. assertSameContents: self collection
  180. as: self collection asOrderedCollection
  181. !
  182. testAsSet
  183. | c set |
  184. c := self collectionWithDuplicates.
  185. set := c asSet.
  186. self assert: set size = 5.
  187. c do: [ :each |
  188. self assert: (set includes: each) ]
  189. !
  190. testCollect
  191. | newCollection |
  192. newCollection := #(1 2 3 4).
  193. self
  194. assertSameContents: (self collection collect: [ :each |
  195. each abs ])
  196. as: newCollection
  197. !
  198. testDetect
  199. self assert: (self collection detect: [ :each | each < 0 ]) = -4.
  200. self
  201. should: [ self collection detect: [ :each | each = 6 ] ]
  202. raise: Error
  203. !
  204. testDo
  205. | newCollection |
  206. newCollection := OrderedCollection new.
  207. self collection do: [ :each |
  208. newCollection add: each ].
  209. self
  210. assertSameContents: self collection
  211. as: newCollection
  212. !
  213. testIsEmpty
  214. self assert: self collectionClass new isEmpty.
  215. self deny: self collection isEmpty
  216. !
  217. testSelect
  218. | newCollection |
  219. newCollection := #(2 -4).
  220. self
  221. assertSameContents: (self collection select: [ :each |
  222. each even ])
  223. as: newCollection
  224. !
  225. testSize
  226. self assert: self collectionClass new size = 0.
  227. self assert: self collection size = 4
  228. ! !
  229. !CollectionTest class methodsFor: 'accessing'!
  230. collectionClass
  231. ^ nil
  232. ! !
  233. !CollectionTest class methodsFor: 'testing'!
  234. isAbstract
  235. ^ self collectionClass isNil
  236. ! !
  237. CollectionTest subclass: #HashedCollectionTest
  238. instanceVariableNames: ''
  239. package: 'Kernel-Tests'!
  240. !HashedCollectionTest methodsFor: 'accessing'!
  241. collection
  242. ^ #{ 'a' -> 1. 'b' -> 2. 'c' -> 3. 'd' -> -4 }
  243. !
  244. collectionWithDuplicates
  245. ^ #{ 'a' -> 1. 'b' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
  246. ! !
  247. !HashedCollectionTest class methodsFor: 'accessing'!
  248. collectionClass
  249. ^ HashedCollection
  250. ! !
  251. HashedCollectionTest subclass: #DictionaryTest
  252. instanceVariableNames: ''
  253. package: 'Kernel-Tests'!
  254. !DictionaryTest methodsFor: 'accessing'!
  255. collection
  256. ^ Dictionary new
  257. at: 1 put: 1;
  258. at: 'a' put: 2;
  259. at: true put: 3;
  260. at: 4 put: -4;
  261. yourself
  262. !
  263. collectionWithDuplicates
  264. ^ Dictionary new
  265. at: 1 put: 1;
  266. at: 'a' put: 2;
  267. at: true put: 3;
  268. at: 4 put: -4;
  269. at: 'b' put: 1;
  270. at: 3 put: 3;
  271. at: false put: 12;
  272. yourself
  273. ! !
  274. !DictionaryTest methodsFor: 'tests'!
  275. testAccessing
  276. | d |
  277. d := Dictionary new.
  278. d at: 'hello' put: 'world'.
  279. self assert: (d at: 'hello') = 'world'.
  280. self assert: (d at: 'hello' ifAbsent: [nil]) = 'world'.
  281. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  282. d at: 1 put: 2.
  283. self assert: (d at: 1) = 2.
  284. d at: 1@3 put: 3.
  285. self assert: (d at: 1@3) = 3
  286. !
  287. testDynamicDictionaries
  288. self assert: #{'hello' -> 1} asDictionary = (Dictionary with: 'hello' -> 1)
  289. !
  290. testEquality
  291. | d1 d2 |
  292. self assert: Dictionary new = Dictionary new.
  293. d1 := Dictionary new at: 1 put: 2; yourself.
  294. d2 := Dictionary new at: 1 put: 2; yourself.
  295. self assert: d1 = d2.
  296. d2 := Dictionary new at: 1 put: 3; yourself.
  297. self deny: d1 = d2.
  298. d2 := Dictionary new at: 2 put: 2; yourself.
  299. self deny: d1 = d2.
  300. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  301. self deny: d1 = d2.
  302. !
  303. testIfAbsent
  304. | d visited |
  305. visited := false.
  306. d := Dictionary new.
  307. d at: 'hello' ifAbsent: [ visited := true ].
  308. self assert: visited.
  309. !
  310. testIfPresent
  311. | d visited absent |
  312. visited := false.
  313. d := Dictionary new.
  314. d at: 'hello' put: 'world'.
  315. d at: 'hello' ifPresent: [ :value | visited := value ].
  316. self assert: visited = 'world'.
  317. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  318. self assert: absent isNil.
  319. !
  320. testIfPresentIfAbsent
  321. | d visited |
  322. visited := false.
  323. d := Dictionary new.
  324. d at: 'hello' put: 'world'.
  325. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  326. self assert: visited = 'world'.
  327. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  328. self assert: visited.
  329. !
  330. testKeys
  331. | d |
  332. d := Dictionary new.
  333. d at: 1 put: 2.
  334. d at: 2 put: 3.
  335. d at: 3 put: 4.
  336. self assert: d keys = #(1 2 3)
  337. !
  338. testPrintString
  339. self
  340. assert: 'a Dictionary(''firstname''->''James'' , ''lastname''->''Bond'')'
  341. equals: (Dictionary new
  342. at:'firstname' put: 'James';
  343. at:'lastname' put: 'Bond';
  344. printString)
  345. !
  346. testRemoveKey
  347. | d key |
  348. d := Dictionary new.
  349. d at: 1 put: 2.
  350. d at: 2 put: 3.
  351. d at: 3 put: 4.
  352. key := 2.
  353. self assert: d keys = #(1 2 3).
  354. d removeKey: key.
  355. self assert: d keys = #(1 3).
  356. self assert: d values = #(2 4).
  357. self deny: (d includesKey: 2)
  358. !
  359. testRemoveKeyIfAbsent
  360. | d key |
  361. d := Dictionary new.
  362. d at: 1 put: 2.
  363. d at: 2 put: 3.
  364. d at: 3 put: 4.
  365. key := 2.
  366. self assert: (d removeKey: key) = 3.
  367. key := 3.
  368. self assert: (d removeKey: key ifAbsent: [42]) = 4.
  369. key := 'why'.
  370. self assert: (d removeKey: key ifAbsent: [42] ) = 42.
  371. !
  372. testSize
  373. | d |
  374. d := Dictionary new.
  375. self assert: d size = 0.
  376. d at: 1 put: 2.
  377. self assert: d size = 1.
  378. d at: 2 put: 3.
  379. self assert: d size = 2.
  380. !
  381. testValues
  382. | d |
  383. d := Dictionary new.
  384. d at: 1 put: 2.
  385. d at: 2 put: 3.
  386. d at: 3 put: 4.
  387. self assert: d values = #(2 3 4)
  388. ! !
  389. !DictionaryTest class methodsFor: 'accessing'!
  390. collectionClass
  391. ^ Dictionary
  392. ! !
  393. CollectionTest subclass: #SequenceableCollectionTest
  394. instanceVariableNames: ''
  395. package: 'Kernel-Tests'!
  396. !SequenceableCollectionTest methodsFor: 'tests'!
  397. testAt
  398. self assert: (self collection at: 4) = -4.
  399. self should: [ self collection at: 5 ] raise: Error
  400. !
  401. testAtIfAbsent
  402. self assert: (self collection at: (self collection size + 1) ifAbsent: [ 'none' ]) = 'none'
  403. ! !
  404. SequenceableCollectionTest subclass: #ArrayTest
  405. instanceVariableNames: ''
  406. package: 'Kernel-Tests'!
  407. !ArrayTest methodsFor: 'testing'!
  408. testAtIfAbsent
  409. | array |
  410. array := #('hello' 'world').
  411. self assert: (array at: 1) equals: 'hello'.
  412. self assert: (array at: 2) equals: 'world'.
  413. self assert: (array at: 2 ifAbsent: ['not found']) equals: 'world'.
  414. self assert: (array at: 0 ifAbsent: ['not found']) equals: 'not found'.
  415. self assert: (array at: -10 ifAbsent: ['not found']) equals: 'not found'.
  416. self assert: (array at: 3 ifAbsent: ['not found']) equals: 'not found'.
  417. !
  418. testFirstN
  419. self assert: {1. 2. 3} equals: ({1. 2. 3. 4. 5} first: 3).
  420. !
  421. testIfEmpty
  422. self assert: 'zork' equals: ( '' ifEmpty: ['zork'] )
  423. !
  424. testPrintString
  425. | array |
  426. array := Array new.
  427. self assert: 'a Array ()' equals: ( array printString ).
  428. array add: 1; add: 3.
  429. self assert: 'a Array (1 3)' equals: ( array printString ).
  430. array add: 'foo'.
  431. self assert: 'a Array (1 3 ''foo'')' equals: ( array printString ).
  432. array remove: 1; remove: 3.
  433. self assert: 'a Array (''foo'')' equals: ( array printString ).
  434. array addLast: 3.
  435. self assert: 'a Array (''foo'' 3)' equals: ( array printString ).
  436. array addLast: 3.
  437. self assert: 'a Array (''foo'' 3 3)' equals: ( array printString ).
  438. ! !
  439. !ArrayTest class methodsFor: 'accessing'!
  440. collectionClass
  441. ^ Array
  442. ! !
  443. SequenceableCollectionTest subclass: #StringTest
  444. instanceVariableNames: ''
  445. package: 'Kernel-Tests'!
  446. !StringTest methodsFor: 'accessing'!
  447. collection
  448. ^'hello'
  449. !
  450. collectionWithDuplicates
  451. ^ 'abbaerte'
  452. ! !
  453. !StringTest methodsFor: 'tests'!
  454. testAddRemove
  455. self should: ['hello' add: 'a'] raise: Error.
  456. self should: ['hello' remove: 'h'] raise: Error
  457. !
  458. testAsArray
  459. self assert: 'hello' asArray = #('h' 'e' 'l' 'l' 'o').
  460. !
  461. testAt
  462. self assert: ('hello' at: 1) = 'h'.
  463. self assert: ('hello' at: 5) = 'o'.
  464. self assert: ('hello' at: 6 ifAbsent: [nil]) = nil
  465. !
  466. testAtPut
  467. "String instances are read-only"
  468. self should: ['hello' at: 1 put: 'a'] raise: Error
  469. !
  470. testCollect
  471. | newCollection |
  472. newCollection := 'hheelllloo'.
  473. self
  474. assertSameContents: (self collection collect: [ :each |
  475. each, each ])
  476. as: newCollection
  477. !
  478. testCopyWithoutAll
  479. self
  480. assert: 'hello world'
  481. equals: ('*hello* *world*' copyWithoutAll: '*')
  482. !
  483. testDetect
  484. self assert: (self collection detect: [ :each | each = 'h' ]) = 'h'.
  485. self
  486. should: [ self collection detect: [ :each | each = 6 ] ]
  487. raise: Error
  488. !
  489. testEquality
  490. self assert: 'hello' = 'hello'.
  491. self deny: 'hello' = 'world'.
  492. self assert: 'hello' = 'hello' yourself.
  493. self assert: 'hello' yourself = 'hello'.
  494. "test JS falsy value"
  495. self deny: '' = 0
  496. !
  497. testIdentity
  498. self assert: 'hello' == 'hello'.
  499. self deny: 'hello' == 'world'.
  500. self assert: 'hello' == 'hello' yourself.
  501. self assert: 'hello' yourself == 'hello'.
  502. "test JS falsy value"
  503. self deny: '' == 0
  504. !
  505. testIncludesSubString
  506. self assert: ('amber' includesSubString: 'ber').
  507. self deny: ('amber' includesSubString: 'zork').
  508. !
  509. testJoin
  510. self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
  511. !
  512. testSelect
  513. | newCollection |
  514. newCollection := 'o'.
  515. self
  516. assertSameContents: (self collection select: [ :each |
  517. each = 'o' ])
  518. as: newCollection
  519. !
  520. testSize
  521. self assert: 'smalltalk' size equals: 9.
  522. self assert: '' size equals: 0
  523. !
  524. testStreamContents
  525. self
  526. assert: 'hello world'
  527. equals: (String streamContents: [ :aStream |
  528. aStream
  529. nextPutAll: 'hello'; space;
  530. nextPutAll: 'world' ])
  531. ! !
  532. !StringTest class methodsFor: 'accessing'!
  533. collectionClass
  534. ^ String
  535. ! !
  536. SequenceableCollectionTest subclass: #SymbolTest
  537. instanceVariableNames: ''
  538. package: 'Kernel-Tests'!
  539. !SymbolTest methodsFor: 'accessing'!
  540. collection
  541. ^ #hello
  542. !
  543. collectionWithDuplicates
  544. ^ #phhaaarorra
  545. ! !
  546. !SymbolTest methodsFor: 'tests'!
  547. testAsString
  548. self assert: #hello asString equals: 'hello'
  549. !
  550. testAsSymbol
  551. self assert: #hello == #hello asSymbol
  552. !
  553. testAt
  554. self assert: (#hello at: 1) = 'h'.
  555. self assert: (#hello at: 5) = 'o'.
  556. self assert: (#hello at: 6 ifAbsent: [nil]) = nil
  557. !
  558. testAtPut
  559. "Symbol instances are read-only"
  560. self should: ['hello' at: 1 put: 'a'] raise: Error
  561. !
  562. testCollect
  563. | newCollection |
  564. newCollection := #hheelllloo.
  565. self
  566. assertSameContents: (self collection collect: [ :each |
  567. each, each ])
  568. as: newCollection
  569. !
  570. testComparing
  571. self assert: #ab > #aa.
  572. self deny: #ab > #ba.
  573. self assert: #ab < #ba.
  574. self deny: #bb < #ba.
  575. self assert: #ab >= #aa.
  576. self deny: #ab >= #ba.
  577. self assert: #ab <= #ba.
  578. self deny: #bb <= #ba
  579. !
  580. testCopying
  581. self assert: #hello copy == #hello.
  582. self assert: #hello deepCopy == #hello
  583. !
  584. testDetect
  585. self assert: (self collection detect: [ :each | each = 'h' ]) = 'h'.
  586. self
  587. should: [ self collection detect: [ :each | each = 'z' ] ]
  588. raise: Error
  589. !
  590. testEquality
  591. self assert: #hello = #hello.
  592. self deny: #hello = #world.
  593. self assert: #hello = #hello yourself.
  594. self assert: #hello yourself = #hello.
  595. self deny: #hello = 'hello'.
  596. self deny: 'hello' = #hello.
  597. !
  598. testIdentity
  599. self assert: #hello == #hello.
  600. self deny: #hello == #world.
  601. self assert: #hello = #hello yourself.
  602. self assert: #hello yourself = #hello asString asSymbol
  603. !
  604. testIsEmpty
  605. self deny: self collection isEmpty.
  606. self assert: '' asSymbol isEmpty
  607. !
  608. testIsSymbolIsString
  609. self assert: #hello isSymbol.
  610. self deny: 'hello' isSymbol.
  611. self deny: #hello isString.
  612. self assert: 'hello' isString
  613. !
  614. testSelect
  615. | newCollection |
  616. newCollection := 'o'.
  617. self
  618. assertSameContents: (self collection select: [ :each |
  619. each = 'o' ])
  620. as: newCollection
  621. !
  622. testSize
  623. self assert: #a size equals: 1.
  624. self assert: #aaaaa size equals: 5
  625. ! !
  626. !SymbolTest class methodsFor: 'accessing'!
  627. collectionClass
  628. ^ Symbol
  629. ! !
  630. TestCase subclass: #JSObjectProxyTest
  631. instanceVariableNames: ''
  632. package: 'Kernel-Tests'!
  633. !JSObjectProxyTest methodsFor: 'accessing'!
  634. jsObject
  635. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: ''}>
  636. ! !
  637. !JSObjectProxyTest methodsFor: 'tests'!
  638. testDNU
  639. self should: [self jsObject foo] raise: MessageNotUnderstood
  640. !
  641. testMessageSend
  642. self assert: self jsObject a equals: 1.
  643. self assert: self jsObject b equals: 2.
  644. self assert: (self jsObject c: 3) equals: 3
  645. !
  646. testMethodWithArguments
  647. self assert: (self jsObject c: 1) equals: 1
  648. !
  649. testPrinting
  650. self assert: self jsObject printString = '[object Object]'
  651. !
  652. testPropertyThatReturnsEmptyString
  653. | object |
  654. object := self jsObject.
  655. self assert: '' equals: object d.
  656. object d: 'hello'.
  657. self assert: 'hello' equals: object d
  658. !
  659. testYourself
  660. | object |
  661. object := self jsObject
  662. d: 'test';
  663. yourself.
  664. self assert: object d equals: 'test'
  665. ! !
  666. TestCase subclass: #NumberTest
  667. instanceVariableNames: ''
  668. package: 'Kernel-Tests'!
  669. !NumberTest methodsFor: 'tests'!
  670. testAbs
  671. self assert: 4 abs = 4.
  672. self assert: -4 abs = 4
  673. !
  674. testArithmetic
  675. "We rely on JS here, so we won't test complex behavior, just check if
  676. message sends are corrects"
  677. self assert: 1.5 + 1 = 2.5.
  678. self assert: 2 - 1 = 1.
  679. self assert: -2 - 1 = -3.
  680. self assert: 12 / 2 = 6.
  681. self assert: 3 * 4 = 12.
  682. "Simple parenthesis and execution order"
  683. self assert: 1 + 2 * 3 = 9.
  684. self assert: 1 + (2 * 3) = 7
  685. !
  686. testComparison
  687. self assert: 3 > 2.
  688. self assert: 2 < 3.
  689. self deny: 3 < 2.
  690. self deny: 2 > 3.
  691. self assert: 3 >= 3.
  692. self assert: 3.1 >= 3.
  693. self assert: 3 <= 3.
  694. self assert: 3 <= 3.1
  695. !
  696. testCopying
  697. self assert: 1 copy == 1.
  698. self assert: 1 deepCopy == 1
  699. !
  700. testEquality
  701. self assert: 1 = 1.
  702. self assert: 0 = 0.
  703. self deny: 1 = 0.
  704. self assert: 1 yourself = 1.
  705. self assert: 1 = 1 yourself.
  706. self assert: 1 yourself = 1 yourself.
  707. self deny: 0 = false.
  708. self deny: false = 0.
  709. self deny: '' = 0.
  710. self deny: 0 = ''
  711. !
  712. testIdentity
  713. self assert: 1 == 1.
  714. self assert: 0 == 0.
  715. self deny: 1 == 0.
  716. self assert: 1 yourself == 1.
  717. self assert: 1 == 1 yourself.
  718. self assert: 1 yourself == 1 yourself.
  719. self deny: 1 == 2
  720. !
  721. testMinMax
  722. self assert: (2 max: 5) equals: 5.
  723. self assert: (2 min: 5) equals: 2
  724. !
  725. testNegated
  726. self assert: 3 negated = -3.
  727. self assert: -3 negated = 3
  728. !
  729. testPrintShowingDecimalPlaces
  730. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  731. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  732. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  733. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  734. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  735. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  736. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  737. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  738. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  739. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  740. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  741. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  742. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  743. !
  744. testRounded
  745. self assert: 3 rounded = 3.
  746. self assert: 3.212 rounded = 3.
  747. self assert: 3.51 rounded = 4
  748. !
  749. testSqrt
  750. self assert: 4 sqrt = 2.
  751. self assert: 16 sqrt = 4
  752. !
  753. testSquared
  754. self assert: 4 squared = 16
  755. !
  756. testTimesRepeat
  757. | i |
  758. i := 0.
  759. 0 timesRepeat: [i := i + 1].
  760. self assert: i equals: 0.
  761. 5 timesRepeat: [i := i + 1].
  762. self assert: i equals: 5
  763. !
  764. testTo
  765. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  766. !
  767. testToBy
  768. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  769. self should: [1 to: 4 by: 0] raise: Error
  770. !
  771. testTruncated
  772. self assert: 3 truncated = 3.
  773. self assert: 3.212 truncated = 3.
  774. self assert: 3.51 truncated = 3
  775. ! !
  776. Object subclass: #ObjectMock
  777. instanceVariableNames: 'foo bar'
  778. package: 'Kernel-Tests'!
  779. !ObjectMock methodsFor: 'not yet classified'!
  780. foo
  781. ^foo
  782. !
  783. foo: anObject
  784. foo := anObject
  785. ! !
  786. TestCase subclass: #ObjectTest
  787. instanceVariableNames: ''
  788. package: 'Kernel-Tests'!
  789. !ObjectTest methodsFor: 'tests'!
  790. testBasicAccess
  791. | o |
  792. o := Object new.
  793. o basicAt: 'a' put: 1.
  794. self assert: (o basicAt: 'a') equals: 1.
  795. self assert: (o basicAt: 'b') equals: nil
  796. !
  797. testBasicPerform
  798. | o |
  799. o := Object new.
  800. o basicAt: 'func' put: ['hello'].
  801. o basicAt: 'func2' put: [:a | a + 1].
  802. self assert: (o basicPerform: 'func') equals: 'hello'.
  803. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  804. !
  805. testDNU
  806. self should: [Object new foo] raise: MessageNotUnderstood
  807. !
  808. testEquality
  809. | o |
  810. o := Object new.
  811. self deny: o = Object new.
  812. self assert: o = o.
  813. self assert: o yourself = o.
  814. self assert: o = o yourself
  815. !
  816. testHalt
  817. self should: [Object new halt] raise: Error
  818. !
  819. testIdentity
  820. | o |
  821. o := Object new.
  822. self deny: o == Object new.
  823. self assert: o == o.
  824. self assert: o yourself == o.
  825. self assert: o == o yourself
  826. !
  827. testIfNil
  828. self deny: Object new isNil.
  829. self deny: (Object new ifNil: [true]) = true.
  830. self assert: (Object new ifNotNil: [true]) = true.
  831. self assert: (Object new ifNil: [false] ifNotNil: [true]) = true.
  832. self assert: (Object new ifNotNil: [true] ifNil: [false]) = true
  833. !
  834. testInstVars
  835. | o |
  836. o := ObjectMock new.
  837. self assert: (o instVarAt: #foo) equals: nil.
  838. o instVarAt: #foo put: 1.
  839. self assert: (o instVarAt: #foo) equals: 1.
  840. self assert: (o instVarAt: 'foo') equals: 1
  841. !
  842. testNilUndefined
  843. "nil in Smalltalk is the undefined object in JS"
  844. | notDefined |
  845. notDefined := window at: '__this_is_undefined'.
  846. self assert: nil = notDefined
  847. !
  848. testYourself
  849. | o |
  850. o := ObjectMock new.
  851. self assert: o yourself == o
  852. !
  853. testidentityHash
  854. | o1 o2 |
  855. o1 := Object new.
  856. o2 := Object new.
  857. self assert: o1 identityHash == o1 identityHash.
  858. self deny: o1 identityHash == o2 identityHash
  859. ! !
  860. TestCase subclass: #PackageTest
  861. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  862. package: 'Kernel-Tests'!
  863. !PackageTest methodsFor: 'running'!
  864. setUp
  865. backUpCommitPathJs := Package defaultCommitPathJs.
  866. backUpCommitPathSt := Package defaultCommitPathSt.
  867. Package resetCommitPaths.
  868. zorkPackage := Package new name: 'Zork'.
  869. grulPackage := Package new
  870. name: 'Grul';
  871. commitPathJs: 'server/grul/js';
  872. commitPathSt: 'grul/st';
  873. yourself
  874. !
  875. tearDown
  876. Package
  877. defaultCommitPathJs: backUpCommitPathJs;
  878. defaultCommitPathSt: backUpCommitPathSt
  879. ! !
  880. !PackageTest methodsFor: 'tests'!
  881. testGrulCommitPathJsShouldBeServerGrulJs
  882. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  883. !
  884. testGrulCommitPathStShouldBeGrulSt
  885. self assert: 'grul/st' equals: grulPackage commitPathSt
  886. !
  887. testZorkCommitPathJsShouldBeJs
  888. self assert: 'js' equals: zorkPackage commitPathJs
  889. !
  890. testZorkCommitPathStShouldBeSt
  891. self assert: 'st' equals: zorkPackage commitPathSt
  892. ! !
  893. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  894. instanceVariableNames: ''
  895. package: 'Kernel-Tests'!
  896. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  897. setUp
  898. super setUp.
  899. Package
  900. defaultCommitPathJs: 'javascripts/';
  901. defaultCommitPathSt: 'smalltalk/'.
  902. ! !
  903. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  904. testGrulCommitPathJsShouldBeServerGrulJs
  905. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  906. !
  907. testGrulCommitPathStShouldBeGrulSt
  908. self assert: 'grul/st' equals: grulPackage commitPathSt
  909. !
  910. testZorkCommitPathJsShouldBeJavascript
  911. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  912. !
  913. testZorkCommitPathStShouldBeSmalltalk
  914. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  915. ! !
  916. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  917. shouldInheritSelectors
  918. ^ false
  919. ! !
  920. TestCase subclass: #PointTest
  921. instanceVariableNames: ''
  922. package: 'Kernel-Tests'!
  923. !PointTest methodsFor: 'tests'!
  924. testAccessing
  925. self assert: (Point x: 3 y: 4) x equals: 3.
  926. self assert: (Point x: 3 y: 4) y equals: 4.
  927. self assert: (Point new x: 3) x equals: 3.
  928. self assert: (Point new y: 4) y equals: 4
  929. !
  930. testArithmetic
  931. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  932. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  933. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  934. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  935. !
  936. testAt
  937. self assert: 3@4 equals: (Point x: 3 y: 4)
  938. !
  939. testEgality
  940. self assert: 3@4 = (3@4).
  941. self deny: 3@5 = (3@6)
  942. !
  943. testTranslateBy
  944. self assert: 3@4 equals: (3@3 translateBy: 0@1).
  945. self assert: 3@2 equals: (3@3 translateBy: 0@1 negated).
  946. self assert: 5@6 equals: (3@3 translateBy: 2@3).
  947. self assert: 0@3 equals: (3@3 translateBy: 3 negated @0).
  948. ! !
  949. TestCase subclass: #RandomTest
  950. instanceVariableNames: ''
  951. package: 'Kernel-Tests'!
  952. !RandomTest methodsFor: 'tests'!
  953. textNext
  954. 10000 timesRepeat: [
  955. | current next |
  956. next := Random new next.
  957. self assert: (next >= 0).
  958. self assert: (next < 1).
  959. self deny: current = next.
  960. next = current]
  961. ! !
  962. TestCase subclass: #SetTest
  963. instanceVariableNames: ''
  964. package: 'Kernel-Tests'!
  965. !SetTest methodsFor: 'tests'!
  966. testAddRemove
  967. | set |
  968. set := Set new.
  969. self assert: set isEmpty.
  970. set add: 3.
  971. self assert: (set includes: 3).
  972. set add: 5.
  973. self assert: (set includes: 5).
  974. set remove: 3.
  975. self deny: (set includes: 3)
  976. !
  977. testAt
  978. self should: [Set new at: 1 put: 2] raise: Error
  979. !
  980. testPrintString
  981. | set |
  982. set := Set new.
  983. self assert: 'a Set ()' equals: ( set printString ).
  984. set add: 1; add: 3.
  985. self assert: 'a Set (1 3)' equals: ( set printString ).
  986. set add: 'foo'.
  987. self assert: 'a Set (1 3 ''foo'')' equals: ( set printString ).
  988. set remove: 1; remove: 3.
  989. self assert: 'a Set (''foo'')' equals: ( set printString ).
  990. set add: 3.
  991. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  992. set add: 3.
  993. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  994. !
  995. testSize
  996. self assert: Set new size equals: 0.
  997. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  998. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  999. !
  1000. testUnicity
  1001. | set |
  1002. set := Set new.
  1003. set add: 21.
  1004. set add: 'hello'.
  1005. set add: 21.
  1006. self assert: set size = 2.
  1007. set add: 'hello'.
  1008. self assert: set size = 2.
  1009. self assert: set asArray equals: #(21 'hello')
  1010. ! !
  1011. TestCase subclass: #UndefinedTest
  1012. instanceVariableNames: ''
  1013. package: 'Kernel-Tests'!
  1014. !UndefinedTest methodsFor: 'tests'!
  1015. testCopying
  1016. self assert: nil copy equals: nil
  1017. !
  1018. testDeepCopy
  1019. self assert: nil deepCopy = nil
  1020. !
  1021. testIfNil
  1022. self assert: (nil ifNil: [true]) equals: true.
  1023. self deny: (nil ifNotNil: [true]) = true.
  1024. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1025. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1026. !
  1027. testIsNil
  1028. self assert: nil isNil.
  1029. self deny: nil notNil.
  1030. ! !