Kernel-Tests.st 40 KB

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