Kernel-Tests.st 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. testPrintString
  1141. | set |
  1142. set := Set new.
  1143. self assert: 'a Set ()' equals: ( set printString ).
  1144. set add: 1; add: 3.
  1145. self assert: 'a Set (1 3)' equals: ( set printString ).
  1146. set add: 'foo'.
  1147. self assert: 'a Set (1 3 ''foo'')' equals: ( set printString ).
  1148. set remove: 1; remove: 3.
  1149. self assert: 'a Set (''foo'')' equals: ( set printString ).
  1150. set add: 3.
  1151. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  1152. set add: 3.
  1153. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  1154. !
  1155. testSize
  1156. self assert: Set new size equals: 0.
  1157. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1158. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1159. !
  1160. testUnicity
  1161. | set |
  1162. set := Set new.
  1163. set add: 21.
  1164. set add: 'hello'.
  1165. set add: 21.
  1166. self assert: set size = 2.
  1167. set add: 'hello'.
  1168. self assert: set size = 2.
  1169. self assert: set asArray equals: #(21 'hello')
  1170. ! !
  1171. TestCase subclass: #UndefinedTest
  1172. instanceVariableNames: ''
  1173. package: 'Kernel-Tests'!
  1174. !UndefinedTest methodsFor: 'tests'!
  1175. testCopying
  1176. self assert: nil copy equals: nil
  1177. !
  1178. testDeepCopy
  1179. self assert: nil deepCopy = nil
  1180. !
  1181. testIfNil
  1182. self assert: (nil ifNil: [true]) equals: true.
  1183. self deny: (nil ifNotNil: [true]) = true.
  1184. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1185. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1186. !
  1187. testIsNil
  1188. self assert: nil isNil.
  1189. self deny: nil notNil.
  1190. ! !