Kernel-Tests.st 35 KB

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