Kernel-Tests.st 27 KB

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