1
0

Kernel-Tests.st 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  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. testAsDictionary
  384. self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
  385. !
  386. testAt
  387. self assert: (self collection at: 'a') equals: 2.
  388. self should: [ self collection at: 5 ] raise: Error
  389. !
  390. testContains
  391. | collection |
  392. collection := self collection.
  393. self assert: (self collection contains: [ :each | each = self collection values first ]).
  394. self deny: (self collection contains: [ :each | each = Object new ])
  395. !
  396. testFrom
  397. "Accept a collection of associations."
  398. | associations |
  399. associations := { 'a' -> 1. 'b' -> 2 }.
  400. self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  401. !
  402. testIndexOf
  403. self assert: (self collection indexOf: 2) equals: 'a'.
  404. self should: [ self collection indexOf: 999 ] raise: Error.
  405. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  406. !
  407. testNewFromPairs
  408. "Accept an array in which all odd indexes are keys and evens are values."
  409. | flattenedAssociations |
  410. flattenedAssociations := { 'a'. 1. 'b'. 2 }.
  411. self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
  412. ! !
  413. !HashedCollectionTest class methodsFor: 'accessing'!
  414. collectionClass
  415. ^ HashedCollection
  416. ! !
  417. HashedCollectionTest subclass: #DictionaryTest
  418. instanceVariableNames: ''
  419. package: 'Kernel-Tests'!
  420. !DictionaryTest methodsFor: 'accessing'!
  421. collection
  422. ^ Dictionary new
  423. at: 1 put: 1;
  424. at: 'a' put: 2;
  425. at: true put: 3;
  426. at: 4 put: -4;
  427. yourself
  428. !
  429. collectionWithDuplicates
  430. ^ Dictionary new
  431. at: 1 put: 1;
  432. at: 'a' put: 2;
  433. at: true put: 3;
  434. at: 4 put: -4;
  435. at: 'b' put: 1;
  436. at: 3 put: 3;
  437. at: false put: 12;
  438. yourself
  439. ! !
  440. !DictionaryTest methodsFor: 'tests'!
  441. testAccessing
  442. | d |
  443. d := Dictionary new.
  444. d at: 'hello' put: 'world'.
  445. self assert: (d at: 'hello') equals: 'world'.
  446. self assert: (d at: 'hello' ifAbsent: [nil]) equals: 'world'.
  447. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  448. self assert: (d includesKey: 'hello').
  449. self deny: (d includesKey: 'foo').
  450. d at: 1 put: 2.
  451. self assert: (d at: 1) equals: 2.
  452. d at: 1@3 put: 3.
  453. self assert: (d at: 1@3) equals: 3.
  454. self assert: (d includesKey: 1@3).
  455. self deny: (d includesKey: 3@1)
  456. !
  457. testAsHashedCollection
  458. self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
  459. !
  460. testDynamicDictionaries
  461. self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
  462. !
  463. testEquality
  464. | d1 d2 |
  465. self assert: (Dictionary new = Dictionary new).
  466. d1 := Dictionary new at: 1 put: 2; yourself.
  467. d2 := Dictionary new at: 1 put: 2; yourself.
  468. self assert: (d1 = d2).
  469. d2 := Dictionary new at: 1 put: 3; yourself.
  470. self deny: d1 = d2.
  471. d2 := Dictionary new at: 2 put: 2; yourself.
  472. self deny: d1 = d2.
  473. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  474. self deny: d1 = d2.
  475. !
  476. testIfAbsent
  477. | d visited |
  478. visited := false.
  479. d := Dictionary new.
  480. d at: 'hello' ifAbsent: [ visited := true ].
  481. self assert: visited.
  482. !
  483. testIfPresent
  484. | d visited absent |
  485. visited := false.
  486. d := Dictionary new.
  487. d at: 'hello' put: 'world'.
  488. d at: 'hello' ifPresent: [ :value | visited := value ].
  489. self assert: visited equals: 'world'.
  490. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  491. self assert: absent isNil.
  492. !
  493. testIfPresentIfAbsent
  494. | d visited |
  495. visited := false.
  496. d := Dictionary new.
  497. d at: 'hello' put: 'world'.
  498. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  499. self assert: visited equals: 'world'.
  500. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  501. self assert: visited.
  502. !
  503. testKeys
  504. | d |
  505. d := Dictionary new.
  506. d at: 1 put: 2.
  507. d at: 2 put: 3.
  508. d at: 3 put: 4.
  509. self assert: d keys equals: #(1 2 3)
  510. !
  511. testPointKey
  512. | d |
  513. d := Dictionary new.
  514. d at: 1@1 put: 'foo'.
  515. self assert: (d at: 1@1) equals: 'foo'.
  516. d at: 1@1 put: 'bar'.
  517. self assert: (d at: 1@1) equals: 'bar'.
  518. d removeKey: 1@1.
  519. self assert: (d at: 1@1 ifAbsent: [ 'baz' ]) equals: 'baz'.
  520. self deny: (d includesKey: 1@1)
  521. !
  522. testPrintString
  523. self
  524. assert: (Dictionary new
  525. at:'firstname' put: 'James';
  526. at:'lastname' put: 'Bond';
  527. printString)
  528. equals: 'a Dictionary (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  529. !
  530. testRemoveKey
  531. | d key |
  532. d := Dictionary new.
  533. d at: 1 put: 2.
  534. d at: 2 put: 3.
  535. d at: 3 put: 4.
  536. key := 2.
  537. self assert: d keys equals: #(1 2 3).
  538. d removeKey: key.
  539. self assert: d keys equals: #(1 3).
  540. self assert: d values equals: #(2 4).
  541. self deny: (d includesKey: 2)
  542. !
  543. testRemoveKeyIfAbsent
  544. | d key |
  545. d := Dictionary new.
  546. d at: 1 put: 2.
  547. d at: 2 put: 3.
  548. d at: 3 put: 4.
  549. key := 2.
  550. self assert: (d removeKey: key) equals: 3.
  551. key := 3.
  552. self assert: (d removeKey: key ifAbsent: [42]) equals: 4.
  553. key := 'why'.
  554. self assert: (d removeKey: key ifAbsent: [42] ) equals: 42.
  555. !
  556. testSize
  557. | d |
  558. d := Dictionary new.
  559. self assert: d size equals: 0.
  560. d at: 1 put: 2.
  561. self assert: d size equals: 1.
  562. d at: 2 put: 3.
  563. self assert: d size equals: 2.
  564. !
  565. testValues
  566. | d |
  567. d := Dictionary new.
  568. d at: 1 put: 2.
  569. d at: 2 put: 3.
  570. d at: 3 put: 4.
  571. self assert: d values equals: #(2 3 4)
  572. ! !
  573. !DictionaryTest class methodsFor: 'accessing'!
  574. collectionClass
  575. ^ Dictionary
  576. ! !
  577. IndexableCollectionTest subclass: #SequenceableCollectionTest
  578. instanceVariableNames: ''
  579. package: 'Kernel-Tests'!
  580. SequenceableCollectionTest subclass: #ArrayTest
  581. instanceVariableNames: ''
  582. package: 'Kernel-Tests'!
  583. !ArrayTest methodsFor: 'tests'!
  584. testAtIfAbsent
  585. | array |
  586. array := #('hello' 'world').
  587. self assert: (array at: 1) equals: 'hello'.
  588. self assert: (array at: 2) equals: 'world'.
  589. self assert: (array at: 2 ifAbsent: ['not found']) equals: 'world'.
  590. self assert: (array at: 0 ifAbsent: ['not found']) equals: 'not found'.
  591. self assert: (array at: -10 ifAbsent: ['not found']) equals: 'not found'.
  592. self assert: (array at: 3 ifAbsent: ['not found']) equals: 'not found'.
  593. !
  594. testFirstN
  595. self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
  596. !
  597. testIfEmpty
  598. self assert: ( '' ifEmpty: ['zork'] ) equals: 'zork'
  599. !
  600. testPrintString
  601. | array |
  602. array := Array new.
  603. self assert: array printString equals: 'an Array ()'.
  604. array add: 1; add: 3.
  605. self assert: array printString equals: 'an Array (1 3)'.
  606. array add: 'foo'.
  607. self assert: array printString equals: 'an Array (1 3 ''foo'')'.
  608. array remove: 1; remove: 3.
  609. self assert: array printString equals: 'an Array (''foo'')'.
  610. array addLast: 3.
  611. self assert: array printString equals: 'an Array (''foo'' 3)'.
  612. array addLast: 3.
  613. self assert: array printString equals: 'an Array (''foo'' 3 3)'.
  614. !
  615. testRemoveFromTo
  616. self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
  617. self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
  618. self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
  619. !
  620. testRemoveIndex
  621. self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
  622. self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
  623. self assert: (#('hello') removeIndex: 1) equals: #()
  624. ! !
  625. !ArrayTest class methodsFor: 'accessing'!
  626. collectionClass
  627. ^ Array
  628. ! !
  629. SequenceableCollectionTest subclass: #StringTest
  630. instanceVariableNames: ''
  631. package: 'Kernel-Tests'!
  632. !StringTest methodsFor: 'accessing'!
  633. collection
  634. ^'hello'
  635. !
  636. collectionWithDuplicates
  637. ^ 'abbaerte'
  638. ! !
  639. !StringTest methodsFor: 'tests'!
  640. testAddRemove
  641. self should: ['hello' add: 'a'] raise: Error.
  642. self should: ['hello' remove: 'h'] raise: Error
  643. !
  644. testAsArray
  645. self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
  646. !
  647. testAt
  648. self assert: ('hello' at: 1) equals: 'h'.
  649. self assert: ('hello' at: 5) equals: 'o'.
  650. self assert: ('hello' at: 6 ifAbsent: [nil]) equals: nil
  651. !
  652. testAtPut
  653. "String instances are read-only"
  654. self should: ['hello' at: 1 put: 'a'] raise: Error
  655. !
  656. testCollect
  657. | newCollection |
  658. newCollection := 'hheelllloo'.
  659. self
  660. assertSameContents: (self collection collect: [ :each |
  661. each, each ])
  662. as: newCollection
  663. !
  664. testCopyWithoutAll
  665. self
  666. assert: ('*hello* *world*' copyWithoutAll: '*')
  667. equals: 'hello world'
  668. !
  669. testDetect
  670. self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
  671. self
  672. should: [ self collection detect: [ :each | each = 6 ] ]
  673. raise: Error
  674. !
  675. testEquality
  676. self assert: 'hello' equals: 'hello'.
  677. self deny: 'hello' = 'world'.
  678. "Test for issue 459"
  679. self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
  680. self assert: 'hello' equals: 'hello' yourself.
  681. self assert: 'hello' yourself equals: 'hello'.
  682. "test JS falsy value"
  683. self deny: '' = 0
  684. !
  685. testIdentity
  686. self assert: 'hello' == 'hello'.
  687. self deny: 'hello' == 'world'.
  688. self assert: 'hello' == 'hello' yourself.
  689. self assert: 'hello' yourself == 'hello'.
  690. "test JS falsy value"
  691. self deny: '' == 0
  692. !
  693. testIncludesSubString
  694. self assert: ('amber' includesSubString: 'ber').
  695. self deny: ('amber' includesSubString: 'zork').
  696. !
  697. testIndexOf
  698. self assert: (self collection indexOf: 'e') equals: 2.
  699. self should: [ self collection indexOf: 999 ] raise: Error.
  700. self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
  701. !
  702. testJoin
  703. self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
  704. !
  705. testSelect
  706. | newCollection |
  707. newCollection := 'o'.
  708. self
  709. assertSameContents: (self collection select: [ :each |
  710. each = 'o' ])
  711. as: newCollection
  712. !
  713. testSize
  714. self assert: 'smalltalk' size equals: 9.
  715. self assert: '' size equals: 0
  716. !
  717. testStreamContents
  718. self
  719. assert: (String streamContents: [ :aStream |
  720. aStream
  721. nextPutAll: 'hello'; space;
  722. nextPutAll: 'world' ])
  723. equals: 'hello world'
  724. ! !
  725. !StringTest class methodsFor: 'accessing'!
  726. collectionClass
  727. ^ String
  728. ! !
  729. TestCase subclass: #ConsoleTranscriptTest
  730. instanceVariableNames: ''
  731. package: 'Kernel-Tests'!
  732. !ConsoleTranscriptTest methodsFor: 'tests'!
  733. testShow
  734. | originalTranscript |
  735. originalTranscript := Transcript current.
  736. Transcript register: ConsoleTranscript new.
  737. self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
  738. self shouldnt: [ Transcript show: console ] raise: Error.
  739. Transcript register: originalTranscript.
  740. ! !
  741. TestCase subclass: #JSObjectProxyTest
  742. instanceVariableNames: ''
  743. package: 'Kernel-Tests'!
  744. !JSObjectProxyTest methodsFor: 'accessing'!
  745. jsObject
  746. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined}>
  747. ! !
  748. !JSObjectProxyTest methodsFor: 'tests'!
  749. testAtIfAbsent
  750. | testObject |
  751. testObject := self jsObject.
  752. self assert: (testObject at: 'abc' ifAbsent: ['Property does not exist']) equals: 'Property does not exist'.
  753. self assert: (testObject at: 'e' ifAbsent: ['Property does not exist']) equals: nil.
  754. self assert: (testObject at: 'a' ifAbsent: ['Property does not exist']) equals: 1.
  755. self assert: (testObject at: 'f' ifAbsent: ['Property does not exist']) equals: nil.
  756. !
  757. testAtIfPresent
  758. | testObject |
  759. testObject := self jsObject.
  760. self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
  761. self assert: (testObject at: 'e' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello nil'.
  762. self assert: (testObject at: 'a' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello 1'.
  763. self assert: (testObject at: 'f' ifPresent: [:x | 'hello ',x asString ]) equals: 'hello nil'.
  764. !
  765. testAtIfPresentIfAbsent
  766. | testObject |
  767. testObject := self jsObject.
  768. self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'not present'.
  769. self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  770. self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello 1'.
  771. self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
  772. !
  773. testDNU
  774. self should: [self jsObject foo] raise: MessageNotUnderstood
  775. !
  776. testMessageSend
  777. self assert: self jsObject a equals: 1.
  778. self assert: self jsObject b equals: 2.
  779. self assert: (self jsObject c: 3) equals: 3
  780. !
  781. testMethodWithArguments
  782. self assert: (self jsObject c: 1) equals: 1
  783. !
  784. testPrinting
  785. self assert: self jsObject printString equals: '[object Object]'
  786. !
  787. testPropertyThatReturnsEmptyString
  788. | object |
  789. object := self jsObject.
  790. self assert: object d equals: ''.
  791. object d: 'hello'.
  792. self assert: object d equals: 'hello'
  793. !
  794. testPropertyThatReturnsUndefined
  795. | object |
  796. object := self jsObject.
  797. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  798. self assert: object e isNil
  799. !
  800. testValue
  801. | testObject |
  802. testObject := self jsObject.
  803. self assert: testObject value printString equals: '[object Object]'.
  804. testObject at: 'value' put: 'aValue'.
  805. self assert: testObject value equals: 'aValue'
  806. !
  807. testYourself
  808. | object |
  809. object := self jsObject
  810. d: 'test';
  811. yourself.
  812. self assert: object d equals: 'test'
  813. ! !
  814. TestCase subclass: #JavaScriptExceptionTest
  815. instanceVariableNames: ''
  816. package: 'Kernel-Tests'!
  817. !JavaScriptExceptionTest methodsFor: 'helpers'!
  818. throwException
  819. <throw 'test'>
  820. ! !
  821. !JavaScriptExceptionTest methodsFor: 'tests'!
  822. testCatchingException
  823. [ self throwException ]
  824. on: Error
  825. do: [ :error |
  826. self assert: error exception = 'test' ]
  827. !
  828. testRaisingException
  829. self should: [ self throwException ] raise: JavaScriptException
  830. ! !
  831. TestCase subclass: #MessageSendTest
  832. instanceVariableNames: ''
  833. package: 'Kernel-Tests'!
  834. !MessageSendTest methodsFor: 'tests'!
  835. testValue
  836. | messageSend |
  837. messageSend := MessageSend new
  838. receiver: Object new;
  839. selector: #asString;
  840. yourself.
  841. self assert: messageSend value equals: 'an Object'
  842. !
  843. testValueWithArguments
  844. | messageSend |
  845. messageSend := MessageSend new
  846. receiver: 2;
  847. selector: '+';
  848. yourself.
  849. self assert: (messageSend value: 3) equals: 5.
  850. self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
  851. ! !
  852. TestCase subclass: #MethodInheritanceTest
  853. instanceVariableNames: 'receiverTop receiverMiddle receiverBottom method performBlock'
  854. package: 'Kernel-Tests'!
  855. !MethodInheritanceTest methodsFor: 'accessing'!
  856. codeGeneratorClass
  857. ^ CodeGenerator
  858. !
  859. targetClassBottom
  860. ^ JavaScriptException
  861. !
  862. targetClassMiddle
  863. ^ Error
  864. !
  865. targetClassTop
  866. ^ Object
  867. ! !
  868. !MethodInheritanceTest methodsFor: 'factory'!
  869. compiler
  870. ^ Compiler new
  871. codeGeneratorClass: self codeGeneratorClass;
  872. yourself
  873. ! !
  874. !MethodInheritanceTest methodsFor: 'initialization'!
  875. setUp
  876. receiverTop := self targetClassTop new.
  877. receiverMiddle := self targetClassMiddle new.
  878. receiverBottom := self targetClassBottom new.
  879. method := nil.
  880. performBlock := [ self error: 'performBlock not initialized' ]
  881. !
  882. tearDown
  883. [ self deinstallTop ] on: Error do: [].
  884. [ self deinstallMiddle ] on: Error do: [].
  885. [ self deinstallBottom ] on: Error do: []
  886. ! !
  887. !MethodInheritanceTest methodsFor: 'testing'!
  888. deinstallBottom
  889. self targetClassBottom removeCompiledMethod: method
  890. !
  891. deinstallMiddle
  892. self targetClassMiddle removeCompiledMethod: method
  893. !
  894. deinstallTop
  895. self targetClassTop removeCompiledMethod: method
  896. !
  897. installBottom: aString
  898. method := self compiler install: aString forClass: self targetClassBottom category: 'tests'
  899. !
  900. installMiddle: aString
  901. method := self compiler install: aString forClass: self targetClassMiddle category: 'tests'
  902. !
  903. installTop: aString
  904. method := self compiler install: aString forClass: self targetClassTop category: 'tests'
  905. !
  906. shouldMNU
  907. self shouldMNUTop.
  908. self shouldMNUMiddle.
  909. self shouldMNUBottom
  910. !
  911. shouldMNUBottom
  912. self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
  913. !
  914. shouldMNUMiddle
  915. self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
  916. !
  917. shouldMNUTop
  918. self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
  919. !
  920. shouldReturn: anObject
  921. | result |
  922. result := performBlock value: receiverTop.
  923. self assert: { 'top'. anObject } equals: { 'top'. result }.
  924. result := performBlock value: receiverMiddle.
  925. self assert: { 'middle'. anObject } equals: { 'middle'. result }.
  926. result := performBlock value: receiverBottom.
  927. self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
  928. !
  929. shouldReturn: anObject and: anObject2 and: anObject3
  930. | result |
  931. result := performBlock value: receiverTop.
  932. self assert: { 'top'. anObject } equals: { 'top'. result }.
  933. result := performBlock value: receiverMiddle.
  934. self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
  935. result := performBlock value: receiverBottom.
  936. self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
  937. ! !
  938. !MethodInheritanceTest methodsFor: 'tests'!
  939. testMNU11
  940. "Breaks other tests. Commenting out"
  941. "performBlock := [ :x | x foo ].
  942. self shouldMNU.
  943. self installTop: 'foo ^ false'.
  944. self installTop: 'foo ^ true'.
  945. self deinstallTop.
  946. self shouldMNU"
  947. !
  948. testMNU22
  949. performBlock := [ :x | x foo ].
  950. self shouldMNU.
  951. self installMiddle: 'foo ^ false'.
  952. self installMiddle: 'foo ^ true'.
  953. self deinstallMiddle.
  954. self shouldMNU
  955. !
  956. testReturns1
  957. "Breaks other tests. Commenting out"
  958. "performBlock := [ :x | x foo ].
  959. self installTop: 'foo ^ false'.
  960. self shouldReturn: false.
  961. self installTop: 'foo ^ true'.
  962. self shouldReturn: true"
  963. ! !
  964. TestCase subclass: #NumberTest
  965. instanceVariableNames: ''
  966. package: 'Kernel-Tests'!
  967. !NumberTest methodsFor: 'tests'!
  968. testAbs
  969. self assert: 4 abs equals: 4.
  970. self assert: -4 abs equals: 4
  971. !
  972. testArithmetic
  973. "We rely on JS here, so we won't test complex behavior, just check if
  974. message sends are corrects"
  975. self assert: 1.5 + 1 equals: 2.5.
  976. self assert: 2 - 1 equals: 1.
  977. self assert: -2 - 1 equals: -3.
  978. self assert: 12 / 2 equals: 6.
  979. self assert: 3 * 4 equals: 12.
  980. "Simple parenthesis and execution order"
  981. self assert: 1 + 2 * 3 equals: 9.
  982. self assert: 1 + (2 * 3) equals: 7
  983. !
  984. testComparison
  985. self assert: 3 > 2.
  986. self assert: 2 < 3.
  987. self deny: 3 < 2.
  988. self deny: 2 > 3.
  989. self assert: 3 >= 3.
  990. self assert: 3.1 >= 3.
  991. self assert: 3 <= 3.
  992. self assert: 3 <= 3.1
  993. !
  994. testCopying
  995. self assert: 1 copy == 1.
  996. self assert: 1 deepCopy == 1
  997. !
  998. testEquality
  999. self assert: (1 = 1).
  1000. self assert: (0 = 0).
  1001. self deny: (1 = 0).
  1002. self assert: (1 yourself = 1).
  1003. self assert: (1 = 1 yourself).
  1004. self assert: (1 yourself = 1 yourself).
  1005. self deny: 0 = false.
  1006. self deny: false = 0.
  1007. self deny: '' = 0.
  1008. self deny: 0 = ''
  1009. !
  1010. testHexNumbers
  1011. self assert: 16r9 equals: 9.
  1012. self assert: 16rA truncated equals: 10.
  1013. self assert: 16rB truncated equals: 11.
  1014. self assert: 16rC truncated equals: 12.
  1015. self assert: 16rD truncated equals: 13.
  1016. self assert: 16rE truncated equals: 14.
  1017. self assert: 16rF truncated equals: 15
  1018. !
  1019. testIdentity
  1020. self assert: 1 == 1.
  1021. self assert: 0 == 0.
  1022. self deny: 1 == 0.
  1023. self assert: 1 yourself == 1.
  1024. self assert: 1 == 1 yourself.
  1025. self assert: 1 yourself == 1 yourself.
  1026. self deny: 1 == 2
  1027. !
  1028. testInvalidHexNumbers
  1029. self should: [16rG] raise: MessageNotUnderstood.
  1030. self should: [16rg] raise: MessageNotUnderstood.
  1031. self should: [16rH] raise: MessageNotUnderstood.
  1032. self should: [16rh] raise: MessageNotUnderstood.
  1033. self should: [16rI] raise: MessageNotUnderstood.
  1034. self should: [16ri] raise: MessageNotUnderstood.
  1035. self should: [16rJ] raise: MessageNotUnderstood.
  1036. self should: [16rj] raise: MessageNotUnderstood.
  1037. self should: [16rK] raise: MessageNotUnderstood.
  1038. self should: [16rk] raise: MessageNotUnderstood.
  1039. self should: [16rL] raise: MessageNotUnderstood.
  1040. self should: [16rl] raise: MessageNotUnderstood.
  1041. self should: [16rM] raise: MessageNotUnderstood.
  1042. self should: [16rm] raise: MessageNotUnderstood.
  1043. self should: [16rN] raise: MessageNotUnderstood.
  1044. self should: [16rn] raise: MessageNotUnderstood.
  1045. self should: [16rO] raise: MessageNotUnderstood.
  1046. self should: [16ro] raise: MessageNotUnderstood.
  1047. self should: [16rP] raise: MessageNotUnderstood.
  1048. self should: [16rp] raise: MessageNotUnderstood.
  1049. self should: [16rQ] raise: MessageNotUnderstood.
  1050. self should: [16rq] raise: MessageNotUnderstood.
  1051. self should: [16rR] raise: MessageNotUnderstood.
  1052. self should: [16rr] raise: MessageNotUnderstood.
  1053. self should: [16rS] raise: MessageNotUnderstood.
  1054. self should: [16rs] raise: MessageNotUnderstood.
  1055. self should: [16rT] raise: MessageNotUnderstood.
  1056. self should: [16rt] raise: MessageNotUnderstood.
  1057. self should: [16rU] raise: MessageNotUnderstood.
  1058. self should: [16ru] raise: MessageNotUnderstood.
  1059. self should: [16rV] raise: MessageNotUnderstood.
  1060. self should: [16rv] raise: MessageNotUnderstood.
  1061. self should: [16rW] raise: MessageNotUnderstood.
  1062. self should: [16rw] raise: MessageNotUnderstood.
  1063. self should: [16rX] raise: MessageNotUnderstood.
  1064. self should: [16rx] raise: MessageNotUnderstood.
  1065. self should: [16rY] raise: MessageNotUnderstood.
  1066. self should: [16ry] raise: MessageNotUnderstood.
  1067. self should: [16rZ] raise: MessageNotUnderstood.
  1068. self should: [16rz] raise: MessageNotUnderstood.
  1069. self should: [16rABcdEfZ] raise: MessageNotUnderstood.
  1070. !
  1071. testMinMax
  1072. self assert: (2 max: 5) equals: 5.
  1073. self assert: (2 min: 5) equals: 2
  1074. !
  1075. testNegated
  1076. self assert: 3 negated equals: -3.
  1077. self assert: -3 negated equals: 3
  1078. !
  1079. testPrintShowingDecimalPlaces
  1080. self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
  1081. self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
  1082. self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
  1083. self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
  1084. self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
  1085. self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
  1086. self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
  1087. self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
  1088. self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
  1089. self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
  1090. self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
  1091. self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
  1092. self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
  1093. !
  1094. testRounded
  1095. self assert: 3 rounded equals: 3.
  1096. self assert: 3.212 rounded equals: 3.
  1097. self assert: 3.51 rounded equals: 4
  1098. !
  1099. testSqrt
  1100. self assert: 4 sqrt equals: 2.
  1101. self assert: 16 sqrt equals: 4
  1102. !
  1103. testSquared
  1104. self assert: 4 squared equals: 16
  1105. !
  1106. testTimesRepeat
  1107. | i |
  1108. i := 0.
  1109. 0 timesRepeat: [i := i + 1].
  1110. self assert: i equals: 0.
  1111. 5 timesRepeat: [i := i + 1].
  1112. self assert: i equals: 5
  1113. !
  1114. testTo
  1115. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  1116. !
  1117. testToBy
  1118. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  1119. self should: [1 to: 4 by: 0] raise: Error
  1120. !
  1121. testTruncated
  1122. self assert: 3 truncated equals: 3.
  1123. self assert: 3.212 truncated equals: 3.
  1124. self assert: 3.51 truncated equals: 3
  1125. ! !
  1126. Object subclass: #ObjectMock
  1127. instanceVariableNames: 'foo bar'
  1128. package: 'Kernel-Tests'!
  1129. !ObjectMock commentStamp!
  1130. ObjectMock is there only to perform tests on classes.!
  1131. !ObjectMock methodsFor: 'not yet classified'!
  1132. foo
  1133. ^foo
  1134. !
  1135. foo: anObject
  1136. foo := anObject
  1137. ! !
  1138. TestCase subclass: #ObjectTest
  1139. instanceVariableNames: ''
  1140. package: 'Kernel-Tests'!
  1141. !ObjectTest methodsFor: 'tests'!
  1142. notDefined
  1143. <return undefined;>
  1144. !
  1145. testBasicAccess
  1146. | o |
  1147. o := Object new.
  1148. o basicAt: 'a' put: 1.
  1149. self assert: (o basicAt: 'a') equals: 1.
  1150. self assert: (o basicAt: 'b') equals: nil
  1151. !
  1152. testBasicPerform
  1153. | o |
  1154. o := Object new.
  1155. o basicAt: 'func' put: ['hello'].
  1156. o basicAt: 'func2' put: [:a | a + 1].
  1157. self assert: (o basicPerform: 'func') equals: 'hello'.
  1158. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  1159. !
  1160. testDNU
  1161. self should: [Object new foo] raise: MessageNotUnderstood
  1162. !
  1163. testEquality
  1164. | o |
  1165. o := Object new.
  1166. self deny: o = Object new.
  1167. self assert: (o = o).
  1168. self assert: (o yourself = o).
  1169. self assert: (o = o yourself)
  1170. !
  1171. testHalt
  1172. self should: [Object new halt] raise: Error
  1173. !
  1174. testIdentity
  1175. | o |
  1176. o := Object new.
  1177. self deny: o == Object new.
  1178. self assert: o == o.
  1179. self assert: o yourself == o.
  1180. self assert: o == o yourself
  1181. !
  1182. testIfNil
  1183. self deny: Object new isNil.
  1184. self deny: (Object new ifNil: [true]) = true.
  1185. self assert: (Object new ifNotNil: [true]) equals: true.
  1186. self assert: (Object new ifNil: [false] ifNotNil: [true]) equals: true.
  1187. self assert: (Object new ifNotNil: [true] ifNil: [false]) equals: true
  1188. !
  1189. testInstVars
  1190. | o |
  1191. o := ObjectMock new.
  1192. self assert: (o instVarAt: #foo) equals: nil.
  1193. o instVarAt: #foo put: 1.
  1194. self assert: (o instVarAt: #foo) equals: 1.
  1195. self assert: (o instVarAt: 'foo') equals: 1
  1196. !
  1197. testNilUndefined
  1198. "nil in Smalltalk is the undefined object in JS"
  1199. self assert: self notDefined equals: nil
  1200. !
  1201. testYourself
  1202. | o |
  1203. o := ObjectMock new.
  1204. self assert: o yourself == o
  1205. !
  1206. testidentityHash
  1207. | o1 o2 |
  1208. o1 := Object new.
  1209. o2 := Object new.
  1210. self assert: o1 identityHash == o1 identityHash.
  1211. self deny: o1 identityHash == o2 identityHash
  1212. ! !
  1213. TestCase subclass: #PackageTest
  1214. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  1215. package: 'Kernel-Tests'!
  1216. !PackageTest methodsFor: 'running'!
  1217. setUp
  1218. backUpCommitPathJs := Package defaultCommitPathJs.
  1219. backUpCommitPathSt := Package defaultCommitPathSt.
  1220. Package resetCommitPaths.
  1221. zorkPackage := Package new name: 'Zork'.
  1222. grulPackage := Package new
  1223. name: 'Grul';
  1224. commitPathJs: 'server/grul/js';
  1225. commitPathSt: 'grul/st';
  1226. yourself
  1227. !
  1228. tearDown
  1229. Package
  1230. defaultCommitPathJs: backUpCommitPathJs;
  1231. defaultCommitPathSt: backUpCommitPathSt
  1232. ! !
  1233. !PackageTest methodsFor: 'tests'!
  1234. testGrulCommitPathJsShouldBeServerGrulJs
  1235. self assert: grulPackage commitPathJs equals: 'server/grul/js'
  1236. !
  1237. testGrulCommitPathStShouldBeGrulSt
  1238. self assert: grulPackage commitPathSt equals: 'grul/st'
  1239. !
  1240. testZorkCommitPathJsShouldBeJs
  1241. self assert: zorkPackage commitPathJs equals: 'js'
  1242. !
  1243. testZorkCommitPathStShouldBeSt
  1244. self assert: zorkPackage commitPathSt equals: 'st'
  1245. ! !
  1246. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  1247. instanceVariableNames: ''
  1248. package: 'Kernel-Tests'!
  1249. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  1250. setUp
  1251. super setUp.
  1252. Package
  1253. defaultCommitPathJs: 'javascripts/';
  1254. defaultCommitPathSt: 'smalltalk/'.
  1255. ! !
  1256. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  1257. testGrulCommitPathJsShouldBeServerGrulJs
  1258. self assert: grulPackage commitPathJs equals: 'server/grul/js'
  1259. !
  1260. testGrulCommitPathStShouldBeGrulSt
  1261. self assert: grulPackage commitPathSt equals: 'grul/st'
  1262. !
  1263. testZorkCommitPathJsShouldBeJavascript
  1264. self assert: zorkPackage commitPathJs equals: 'javascripts/'
  1265. !
  1266. testZorkCommitPathStShouldBeSmalltalk
  1267. self assert: zorkPackage commitPathSt equals: 'smalltalk/'
  1268. ! !
  1269. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  1270. shouldInheritSelectors
  1271. ^ false
  1272. ! !
  1273. TestCase subclass: #PointTest
  1274. instanceVariableNames: ''
  1275. package: 'Kernel-Tests'!
  1276. !PointTest methodsFor: 'tests'!
  1277. testAccessing
  1278. self assert: (Point x: 3 y: 4) x equals: 3.
  1279. self assert: (Point x: 3 y: 4) y equals: 4.
  1280. self assert: (Point new x: 3) x equals: 3.
  1281. self assert: (Point new y: 4) y equals: 4
  1282. !
  1283. testArithmetic
  1284. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  1285. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  1286. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  1287. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  1288. !
  1289. testAt
  1290. self assert: 3@4 equals: (Point x: 3 y: 4)
  1291. !
  1292. testEgality
  1293. self assert: (3@4 = (3@4)).
  1294. self deny: 3@5 = (3@6)
  1295. !
  1296. testTranslateBy
  1297. self assert: (3@3 translateBy: 0@1) equals: 3@4.
  1298. self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
  1299. self assert: (3@3 translateBy: 2@3) equals: 5@6.
  1300. self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
  1301. ! !
  1302. TestCase subclass: #RandomTest
  1303. instanceVariableNames: ''
  1304. package: 'Kernel-Tests'!
  1305. !RandomTest methodsFor: 'tests'!
  1306. textNext
  1307. 10000 timesRepeat: [
  1308. | current next |
  1309. next := Random new next.
  1310. self assert: (next >= 0).
  1311. self assert: (next < 1).
  1312. self deny: current = next.
  1313. next = current]
  1314. ! !
  1315. TestCase subclass: #SetTest
  1316. instanceVariableNames: ''
  1317. package: 'Kernel-Tests'!
  1318. !SetTest methodsFor: 'tests'!
  1319. testAddRemove
  1320. | set |
  1321. set := Set new.
  1322. self assert: set isEmpty.
  1323. set add: 3.
  1324. self assert: (set includes: 3).
  1325. set add: 5.
  1326. self assert: (set includes: 5).
  1327. set remove: 3.
  1328. self deny: (set includes: 3)
  1329. !
  1330. testAt
  1331. self should: [Set new at: 1 put: 2] raise: Error
  1332. !
  1333. testCollect
  1334. self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
  1335. !
  1336. testComparing
  1337. self assert: #(0 2) asSet equals: #(0 2) asSet.
  1338. self assert: #(2 0) asSet equals: #(0 2) asSet.
  1339. self deny: #(0 2 3) asSet = #(0 2) asSet.
  1340. self deny: #(1 2) asSet = #(0 2) asSet
  1341. !
  1342. testPrintString
  1343. | set |
  1344. set := Set new.
  1345. self assert: set printString equals: 'a Set ()'.
  1346. set add: 1; add: 3.
  1347. self assert: set printString equals: 'a Set (1 3)'.
  1348. set add: 'foo'.
  1349. self assert: set printString equals: 'a Set (1 3 ''foo'')'.
  1350. set remove: 1; remove: 3.
  1351. self assert: set printString equals: 'a Set (''foo'')'.
  1352. set add: 3.
  1353. self assert: set printString equals: 'a Set (''foo'' 3)'.
  1354. set add: 3.
  1355. self assert: set printString equals: 'a Set (''foo'' 3)'
  1356. !
  1357. testSize
  1358. self assert: Set new size equals: 0.
  1359. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1360. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1361. !
  1362. testUnboxedObjects
  1363. self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
  1364. !
  1365. testUnicity
  1366. | set |
  1367. set := Set new.
  1368. set add: 21.
  1369. set add: 'hello'.
  1370. set add: 21.
  1371. self assert: set size equals: 2.
  1372. set add: 'hello'.
  1373. self assert: set size equals: 2.
  1374. self assert: set asArray equals: #(21 'hello')
  1375. ! !
  1376. TestCase subclass: #StreamTest
  1377. instanceVariableNames: ''
  1378. package: 'Kernel-Tests'!
  1379. !StreamTest methodsFor: 'accessing'!
  1380. collectionClass
  1381. ^ self class collectionClass
  1382. !
  1383. newCollection
  1384. ^ self collectionClass new
  1385. !
  1386. newStream
  1387. ^ self collectionClass new stream
  1388. ! !
  1389. !StreamTest methodsFor: 'tests'!
  1390. testAtStartAtEnd
  1391. | stream |
  1392. stream := self newStream.
  1393. self assert: stream atStart.
  1394. self assert: stream atEnd.
  1395. stream nextPutAll: self newCollection.
  1396. self assert: stream atEnd.
  1397. self deny: stream atStart.
  1398. stream position: 1.
  1399. self deny: stream atEnd.
  1400. self deny: stream atStart
  1401. !
  1402. testContents
  1403. | stream |
  1404. stream := self newStream.
  1405. stream nextPutAll: self newCollection.
  1406. self assert: stream contents equals: self newCollection
  1407. !
  1408. testIsEmpty
  1409. | stream |
  1410. stream := self newStream.
  1411. self assert: stream isEmpty.
  1412. stream nextPutAll: self newCollection.
  1413. self deny: stream isEmpty
  1414. !
  1415. testPosition
  1416. | collection stream |
  1417. collection := self newCollection.
  1418. stream := self newStream.
  1419. stream nextPutAll: collection.
  1420. self assert: stream position equals: collection size.
  1421. stream position: 0.
  1422. self assert: stream position equals: 0.
  1423. stream next.
  1424. self assert: stream position equals: 1.
  1425. stream next.
  1426. self assert: stream position equals: 2
  1427. !
  1428. testReading
  1429. | stream collection |
  1430. collection := self newCollection.
  1431. stream := self newStream.
  1432. stream
  1433. nextPutAll: collection;
  1434. position: 0.
  1435. collection do: [ :each |
  1436. self assert: stream next equals: each ].
  1437. self assert: stream next isNil
  1438. !
  1439. testStreamContents
  1440. !
  1441. testWrite
  1442. | stream collection |
  1443. collection := self newCollection.
  1444. stream := self newStream.
  1445. collection do: [ :each | stream << each ].
  1446. self assert: stream contents equals: collection
  1447. !
  1448. testWriting
  1449. | stream collection |
  1450. collection := self newCollection.
  1451. stream := self newStream.
  1452. collection do: [ :each | stream nextPut: each ].
  1453. self assert: stream contents equals: collection.
  1454. stream := self newStream.
  1455. stream nextPutAll: collection.
  1456. self assert: stream contents equals: collection
  1457. ! !
  1458. !StreamTest class methodsFor: 'accessing'!
  1459. collectionClass
  1460. ^ nil
  1461. ! !
  1462. !StreamTest class methodsFor: 'testing'!
  1463. isAbstract
  1464. ^ self collectionClass isNil
  1465. ! !
  1466. StreamTest subclass: #ArrayStreamTest
  1467. instanceVariableNames: ''
  1468. package: 'Kernel-Tests'!
  1469. !ArrayStreamTest methodsFor: 'accessing'!
  1470. newCollection
  1471. ^ { true. 1. 3@4. 'foo' }
  1472. ! !
  1473. !ArrayStreamTest class methodsFor: 'accessing'!
  1474. collectionClass
  1475. ^ Array
  1476. ! !
  1477. StreamTest subclass: #StringStreamTest
  1478. instanceVariableNames: ''
  1479. package: 'Kernel-Tests'!
  1480. !StringStreamTest methodsFor: 'accessing'!
  1481. newCollection
  1482. ^ 'hello world'
  1483. ! !
  1484. !StringStreamTest class methodsFor: 'accessing'!
  1485. collectionClass
  1486. ^ String
  1487. ! !
  1488. TestCase subclass: #UndefinedTest
  1489. instanceVariableNames: ''
  1490. package: 'Kernel-Tests'!
  1491. !UndefinedTest methodsFor: 'tests'!
  1492. testCopying
  1493. self assert: nil copy equals: nil
  1494. !
  1495. testDeepCopy
  1496. self assert: nil deepCopy = nil
  1497. !
  1498. testIfNil
  1499. self assert: (nil ifNil: [true]) equals: true.
  1500. self deny: (nil ifNotNil: [true]) = true.
  1501. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1502. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1503. !
  1504. testIsNil
  1505. self assert: nil isNil.
  1506. self deny: nil notNil.
  1507. ! !