1
0

Kernel-Tests.st 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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;}}>
  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 deny: ('body' asJQuery hasClass: 'amber').
  645. 'body' asJQuery addClass: 'amber'.
  646. self assert: ('body' asJQuery hasClass: 'amber').
  647. 'body' asJQuery removeClass: 'amber'.
  648. self deny: ('body' asJQuery hasClass: 'amber').
  649. !
  650. testPrinting
  651. self assert: self jsObject printString = '[object Object]'
  652. !
  653. testPropertyThatReturnsEmptyString
  654. <document.location.hash = ''>.
  655. self assert: '' equals: document location hash.
  656. document location hash: 'test'.
  657. self assert: '#test' equals: document location hash.
  658. !
  659. testYourself
  660. |body|
  661. body := 'body' asJQuery
  662. addClass: 'amber';
  663. yourself.
  664. self assert: (body hasClass: 'amber').
  665. body removeClass: 'amber'.
  666. self deny: (body hasClass: 'amber').
  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. self assert: nil = undefined
  847. !
  848. testYourself
  849. | o |
  850. o := ObjectMock new.
  851. self assert: o yourself == o
  852. !
  853. testidentityHash
  854. | o1 o2 |
  855. o1 := Object new.
  856. o2 := Object new.
  857. self assert: o1 identityHash == o1 identityHash.
  858. self deny: o1 identityHash == o2 identityHash
  859. ! !
  860. TestCase subclass: #PackageTest
  861. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  862. package: 'Kernel-Tests'!
  863. !PackageTest methodsFor: 'running'!
  864. setUp
  865. backUpCommitPathJs := Package defaultCommitPathJs.
  866. backUpCommitPathSt := Package defaultCommitPathSt.
  867. Package resetCommitPaths.
  868. zorkPackage := Package new name: 'Zork'.
  869. grulPackage := Package new
  870. name: 'Grul';
  871. commitPathJs: 'server/grul/js';
  872. commitPathSt: 'grul/st';
  873. yourself
  874. !
  875. tearDown
  876. Package
  877. defaultCommitPathJs: backUpCommitPathJs;
  878. defaultCommitPathSt: backUpCommitPathSt
  879. ! !
  880. !PackageTest methodsFor: 'tests'!
  881. testGrulCommitPathJsShouldBeServerGrulJs
  882. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  883. !
  884. testGrulCommitPathStShouldBeGrulSt
  885. self assert: 'grul/st' equals: grulPackage commitPathSt
  886. !
  887. testZorkCommitPathJsShouldBeJs
  888. self assert: 'js' equals: zorkPackage commitPathJs
  889. !
  890. testZorkCommitPathStShouldBeSt
  891. self assert: 'st' equals: zorkPackage commitPathSt
  892. ! !
  893. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  894. instanceVariableNames: ''
  895. package: 'Kernel-Tests'!
  896. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  897. setUp
  898. super setUp.
  899. Package
  900. defaultCommitPathJs: 'javascripts/';
  901. defaultCommitPathSt: 'smalltalk/'.
  902. ! !
  903. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  904. testGrulCommitPathJsShouldBeServerGrulJs
  905. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  906. !
  907. testGrulCommitPathStShouldBeGrulSt
  908. self assert: 'grul/st' equals: grulPackage commitPathSt
  909. !
  910. testZorkCommitPathJsShouldBeJavascript
  911. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  912. !
  913. testZorkCommitPathStShouldBeSmalltalk
  914. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  915. ! !
  916. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  917. shouldInheritSelectors
  918. ^ false
  919. ! !
  920. TestCase subclass: #PointTest
  921. instanceVariableNames: ''
  922. package: 'Kernel-Tests'!
  923. !PointTest methodsFor: 'tests'!
  924. testAccessing
  925. self assert: (Point x: 3 y: 4) x equals: 3.
  926. self assert: (Point x: 3 y: 4) y equals: 4.
  927. self assert: (Point new x: 3) x equals: 3.
  928. self assert: (Point new y: 4) y equals: 4
  929. !
  930. testArithmetic
  931. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  932. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  933. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  934. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  935. !
  936. testAt
  937. self assert: 3@4 equals: (Point x: 3 y: 4)
  938. !
  939. testEgality
  940. self assert: 3@4 = (3@4).
  941. self deny: 3@5 = (3@6)
  942. !
  943. testTranslateBy
  944. self assert: 3@4 equals: (3@3 translateBy: 0@1).
  945. self assert: 3@2 equals: (3@3 translateBy: 0@1 negated).
  946. self assert: 5@6 equals: (3@3 translateBy: 2@3).
  947. self assert: 0@3 equals: (3@3 translateBy: 3 negated @0).
  948. ! !
  949. TestCase subclass: #RandomTest
  950. instanceVariableNames: ''
  951. package: 'Kernel-Tests'!
  952. !RandomTest methodsFor: 'tests'!
  953. textNext
  954. 10000 timesRepeat: [
  955. | current next |
  956. next := Random new next.
  957. self assert: (next >= 0).
  958. self assert: (next < 1).
  959. self deny: current = next.
  960. next = current]
  961. ! !
  962. TestCase subclass: #SetTest
  963. instanceVariableNames: ''
  964. package: 'Kernel-Tests'!
  965. !SetTest methodsFor: 'tests'!
  966. testAddRemove
  967. | set |
  968. set := Set new.
  969. self assert: set isEmpty.
  970. set add: 3.
  971. self assert: (set includes: 3).
  972. set add: 5.
  973. self assert: (set includes: 5).
  974. set remove: 3.
  975. self deny: (set includes: 3)
  976. !
  977. testAt
  978. self should: [Set new at: 1 put: 2] raise: Error
  979. !
  980. testPrintString
  981. | set |
  982. set := Set new.
  983. self assert: 'a Set ()' equals: ( set printString ).
  984. set add: 1; add: 3.
  985. self assert: 'a Set (1 3)' equals: ( set printString ).
  986. set add: 'foo'.
  987. self assert: 'a Set (1 3 ''foo'')' equals: ( set printString ).
  988. set remove: 1; remove: 3.
  989. self assert: 'a Set (''foo'')' equals: ( set printString ).
  990. set add: 3.
  991. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  992. set add: 3.
  993. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  994. !
  995. testSize
  996. self assert: Set new size equals: 0.
  997. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  998. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  999. !
  1000. testUnicity
  1001. | set |
  1002. set := Set new.
  1003. set add: 21.
  1004. set add: 'hello'.
  1005. set add: 21.
  1006. self assert: set size = 2.
  1007. set add: 'hello'.
  1008. self assert: set size = 2.
  1009. self assert: set asArray equals: #(21 'hello')
  1010. ! !
  1011. TestCase subclass: #UndefinedTest
  1012. instanceVariableNames: ''
  1013. package: 'Kernel-Tests'!
  1014. !UndefinedTest methodsFor: 'tests'!
  1015. testCopying
  1016. self assert: nil copy equals: nil
  1017. !
  1018. testDeepCopy
  1019. self assert: nil deepCopy = nil
  1020. !
  1021. testIfNil
  1022. self assert: (nil ifNil: [true]) equals: true.
  1023. self deny: (nil ifNotNil: [true]) = true.
  1024. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1025. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1026. !
  1027. testIsNil
  1028. self assert: nil isNil.
  1029. self deny: nil notNil.
  1030. ! !