Kernel-Tests.st 28 KB

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