Kernel-Tests.st 28 KB

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