Kernel-Tests.st 39 KB

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