Kernel-Tests.st 41 KB

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