1
0

Kernel-Tests.st 31 KB

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