Kernel-Tests.st 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. |body|
  658. body := 'body' asJQuery
  659. addClass: 'amber';
  660. yourself.
  661. self assert: (body hasClass: 'amber').
  662. body removeClass: 'amber'.
  663. self deny: (body hasClass: 'amber').
  664. ! !
  665. TestCase subclass: #NumberTest
  666. instanceVariableNames: ''
  667. package: 'Kernel-Tests'!
  668. !NumberTest methodsFor: 'tests'!
  669. testAbs
  670. self assert: 4 abs = 4.
  671. self assert: -4 abs = 4
  672. !
  673. testArithmetic
  674. "We rely on JS here, so we won't test complex behavior, just check if
  675. message sends are corrects"
  676. self assert: 1.5 + 1 = 2.5.
  677. self assert: 2 - 1 = 1.
  678. self assert: -2 - 1 = -3.
  679. self assert: 12 / 2 = 6.
  680. self assert: 3 * 4 = 12.
  681. "Simple parenthesis and execution order"
  682. self assert: 1 + 2 * 3 = 9.
  683. self assert: 1 + (2 * 3) = 7
  684. !
  685. testComparison
  686. self assert: 3 > 2.
  687. self assert: 2 < 3.
  688. self deny: 3 < 2.
  689. self deny: 2 > 3.
  690. self assert: 3 >= 3.
  691. self assert: 3.1 >= 3.
  692. self assert: 3 <= 3.
  693. self assert: 3 <= 3.1
  694. !
  695. testCopying
  696. self assert: 1 copy == 1.
  697. self assert: 1 deepCopy == 1
  698. !
  699. testEquality
  700. self assert: 1 = 1.
  701. self assert: 0 = 0.
  702. self deny: 1 = 0.
  703. self assert: 1 yourself = 1.
  704. self assert: 1 = 1 yourself.
  705. self assert: 1 yourself = 1 yourself.
  706. self deny: 0 = false.
  707. self deny: false = 0.
  708. self deny: '' = 0.
  709. self deny: 0 = ''
  710. !
  711. testIdentity
  712. self assert: 1 == 1.
  713. self assert: 0 == 0.
  714. self deny: 1 == 0.
  715. self assert: 1 yourself == 1.
  716. self assert: 1 == 1 yourself.
  717. self assert: 1 yourself == 1 yourself.
  718. self deny: 1 == 2
  719. !
  720. testMinMax
  721. self assert: (2 max: 5) equals: 5.
  722. self assert: (2 min: 5) equals: 2
  723. !
  724. testNegated
  725. self assert: 3 negated = -3.
  726. self assert: -3 negated = 3
  727. !
  728. testPrintShowingDecimalPlaces
  729. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  730. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  731. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  732. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  733. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  734. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  735. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  736. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  737. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  738. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  739. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  740. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  741. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  742. !
  743. testRounded
  744. self assert: 3 rounded = 3.
  745. self assert: 3.212 rounded = 3.
  746. self assert: 3.51 rounded = 4
  747. !
  748. testSqrt
  749. self assert: 4 sqrt = 2.
  750. self assert: 16 sqrt = 4
  751. !
  752. testSquared
  753. self assert: 4 squared = 16
  754. !
  755. testTimesRepeat
  756. | i |
  757. i := 0.
  758. 0 timesRepeat: [i := i + 1].
  759. self assert: i equals: 0.
  760. 5 timesRepeat: [i := i + 1].
  761. self assert: i equals: 5
  762. !
  763. testTo
  764. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  765. !
  766. testToBy
  767. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  768. self should: [1 to: 4 by: 0] raise: Error
  769. !
  770. testTruncated
  771. self assert: 3 truncated = 3.
  772. self assert: 3.212 truncated = 3.
  773. self assert: 3.51 truncated = 3
  774. ! !
  775. Object subclass: #ObjectMock
  776. instanceVariableNames: 'foo bar'
  777. package: 'Kernel-Tests'!
  778. !ObjectMock methodsFor: 'not yet classified'!
  779. foo
  780. ^foo
  781. !
  782. foo: anObject
  783. foo := anObject
  784. ! !
  785. TestCase subclass: #ObjectTest
  786. instanceVariableNames: ''
  787. package: 'Kernel-Tests'!
  788. !ObjectTest methodsFor: 'tests'!
  789. testBasicAccess
  790. | o |
  791. o := Object new.
  792. o basicAt: 'a' put: 1.
  793. self assert: (o basicAt: 'a') equals: 1.
  794. self assert: (o basicAt: 'b') equals: nil
  795. !
  796. testBasicPerform
  797. | o |
  798. o := Object new.
  799. o basicAt: 'func' put: ['hello'].
  800. o basicAt: 'func2' put: [:a | a + 1].
  801. self assert: (o basicPerform: 'func') equals: 'hello'.
  802. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  803. !
  804. testDNU
  805. self should: [Object new foo] raise: MessageNotUnderstood
  806. !
  807. testEquality
  808. | o |
  809. o := Object new.
  810. self deny: o = Object new.
  811. self assert: o = o.
  812. self assert: o yourself = o.
  813. self assert: o = o yourself
  814. !
  815. testHalt
  816. self should: [Object new halt] raise: Error
  817. !
  818. testIdentity
  819. | o |
  820. o := Object new.
  821. self deny: o == Object new.
  822. self assert: o == o.
  823. self assert: o yourself == o.
  824. self assert: o == o yourself
  825. !
  826. testIfNil
  827. self deny: Object new isNil.
  828. self deny: (Object new ifNil: [true]) = true.
  829. self assert: (Object new ifNotNil: [true]) = true.
  830. self assert: (Object new ifNil: [false] ifNotNil: [true]) = true.
  831. self assert: (Object new ifNotNil: [true] ifNil: [false]) = true
  832. !
  833. testInstVars
  834. | o |
  835. o := ObjectMock new.
  836. self assert: (o instVarAt: #foo) equals: nil.
  837. o instVarAt: #foo put: 1.
  838. self assert: (o instVarAt: #foo) equals: 1.
  839. self assert: (o instVarAt: 'foo') equals: 1
  840. !
  841. testNilUndefined
  842. "nil in Smalltalk is the undefined object in JS"
  843. self assert: nil = undefined
  844. !
  845. testYourself
  846. | o |
  847. o := ObjectMock new.
  848. self assert: o yourself == o
  849. !
  850. testidentityHash
  851. | o1 o2 |
  852. o1 := Object new.
  853. o2 := Object new.
  854. self assert: o1 identityHash == o1 identityHash.
  855. self deny: o1 identityHash == o2 identityHash
  856. ! !
  857. TestCase subclass: #PackageTest
  858. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  859. package: 'Kernel-Tests'!
  860. !PackageTest methodsFor: 'running'!
  861. setUp
  862. backUpCommitPathJs := Package defaultCommitPathJs.
  863. backUpCommitPathSt := Package defaultCommitPathSt.
  864. Package resetCommitPaths.
  865. zorkPackage := Package new name: 'Zork'.
  866. grulPackage := Package new
  867. name: 'Grul';
  868. commitPathJs: 'server/grul/js';
  869. commitPathSt: 'grul/st';
  870. yourself
  871. !
  872. tearDown
  873. Package
  874. defaultCommitPathJs: backUpCommitPathJs;
  875. defaultCommitPathSt: backUpCommitPathSt
  876. ! !
  877. !PackageTest methodsFor: 'tests'!
  878. testGrulCommitPathJsShouldBeServerGrulJs
  879. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  880. !
  881. testGrulCommitPathStShouldBeGrulSt
  882. self assert: 'grul/st' equals: grulPackage commitPathSt
  883. !
  884. testZorkCommitPathJsShouldBeJs
  885. self assert: 'js' equals: zorkPackage commitPathJs
  886. !
  887. testZorkCommitPathStShouldBeSt
  888. self assert: 'st' equals: zorkPackage commitPathSt
  889. ! !
  890. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  891. instanceVariableNames: ''
  892. package: 'Kernel-Tests'!
  893. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  894. setUp
  895. super setUp.
  896. Package
  897. defaultCommitPathJs: 'javascripts/';
  898. defaultCommitPathSt: 'smalltalk/'.
  899. ! !
  900. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  901. testGrulCommitPathJsShouldBeServerGrulJs
  902. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  903. !
  904. testGrulCommitPathStShouldBeGrulSt
  905. self assert: 'grul/st' equals: grulPackage commitPathSt
  906. !
  907. testZorkCommitPathJsShouldBeJavascript
  908. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  909. !
  910. testZorkCommitPathStShouldBeSmalltalk
  911. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  912. ! !
  913. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  914. shouldInheritSelectors
  915. ^ false
  916. ! !
  917. TestCase subclass: #PointTest
  918. instanceVariableNames: ''
  919. package: 'Kernel-Tests'!
  920. !PointTest methodsFor: 'tests'!
  921. testAccessing
  922. self assert: (Point x: 3 y: 4) x equals: 3.
  923. self assert: (Point x: 3 y: 4) y equals: 4.
  924. self assert: (Point new x: 3) x equals: 3.
  925. self assert: (Point new y: 4) y equals: 4
  926. !
  927. testArithmetic
  928. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  929. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  930. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  931. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  932. !
  933. testAt
  934. self assert: 3@4 equals: (Point x: 3 y: 4)
  935. !
  936. testEgality
  937. self assert: 3@4 = (3@4).
  938. self deny: 3@5 = (3@6)
  939. !
  940. testTranslateBy
  941. self assert: 3@4 equals: (3@3 translateBy: 0@1).
  942. self assert: 3@2 equals: (3@3 translateBy: 0@1 negated).
  943. self assert: 5@6 equals: (3@3 translateBy: 2@3).
  944. self assert: 0@3 equals: (3@3 translateBy: 3 negated @0).
  945. ! !
  946. TestCase subclass: #RandomTest
  947. instanceVariableNames: ''
  948. package: 'Kernel-Tests'!
  949. !RandomTest methodsFor: 'tests'!
  950. textNext
  951. 10000 timesRepeat: [
  952. | current next |
  953. next := Random new next.
  954. self assert: (next >= 0).
  955. self assert: (next < 1).
  956. self deny: current = next.
  957. next = current]
  958. ! !
  959. TestCase subclass: #SetTest
  960. instanceVariableNames: ''
  961. package: 'Kernel-Tests'!
  962. !SetTest methodsFor: 'tests'!
  963. testAddRemove
  964. | set |
  965. set := Set new.
  966. self assert: set isEmpty.
  967. set add: 3.
  968. self assert: (set includes: 3).
  969. set add: 5.
  970. self assert: (set includes: 5).
  971. set remove: 3.
  972. self deny: (set includes: 3)
  973. !
  974. testAt
  975. self should: [Set new at: 1 put: 2] raise: Error
  976. !
  977. testPrintString
  978. | set |
  979. set := Set new.
  980. self assert: 'a Set ()' equals: ( set printString ).
  981. set add: 1; add: 3.
  982. self assert: 'a Set (1 3)' equals: ( set printString ).
  983. set add: 'foo'.
  984. self assert: 'a Set (1 3 ''foo'')' equals: ( set printString ).
  985. set remove: 1; remove: 3.
  986. self assert: 'a Set (''foo'')' equals: ( set printString ).
  987. set add: 3.
  988. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  989. set add: 3.
  990. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  991. !
  992. testSize
  993. self assert: Set new size equals: 0.
  994. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  995. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  996. !
  997. testUnicity
  998. | set |
  999. set := Set new.
  1000. set add: 21.
  1001. set add: 'hello'.
  1002. set add: 21.
  1003. self assert: set size = 2.
  1004. set add: 'hello'.
  1005. self assert: set size = 2.
  1006. self assert: set asArray equals: #(21 'hello')
  1007. ! !
  1008. TestCase subclass: #UndefinedTest
  1009. instanceVariableNames: ''
  1010. package: 'Kernel-Tests'!
  1011. !UndefinedTest methodsFor: 'tests'!
  1012. testCopying
  1013. self assert: nil copy equals: nil
  1014. !
  1015. testDeepCopy
  1016. self assert: nil deepCopy = nil
  1017. !
  1018. testIfNil
  1019. self assert: (nil ifNil: [true]) equals: true.
  1020. self deny: (nil ifNotNil: [true]) = true.
  1021. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1022. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1023. !
  1024. testIsNil
  1025. self assert: nil isNil.
  1026. self deny: nil notNil.
  1027. ! !