Kernel-Tests.st 30 KB

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