1
0

Kernel-Tests.st 27 KB

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