Kernel-Tests.st 41 KB

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