Kernel-Tests.st 39 KB

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