1
0

Kernel-Tests.st 39 KB

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