Kernel-Tests.st 28 KB

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