1
0

Kernel-Tests.st 34 KB

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