1
0

Kernel-Tests.st 28 KB

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