Kernel-Tests.st 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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: '', 'e': null}>
  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. testPropertyThatReturnsUndefined
  665. | object |
  666. object := self jsObject.
  667. self shouldnt: [ object e ] raise: MessageNotUnderstood.
  668. self assert: object e isNil
  669. !
  670. testYourself
  671. | object |
  672. object := self jsObject
  673. d: 'test';
  674. yourself.
  675. self assert: object d equals: 'test'
  676. ! !
  677. TestCase subclass: #NumberTest
  678. instanceVariableNames: ''
  679. package: 'Kernel-Tests'!
  680. !NumberTest methodsFor: 'tests'!
  681. testAbs
  682. self assert: 4 abs = 4.
  683. self assert: -4 abs = 4
  684. !
  685. testArithmetic
  686. "We rely on JS here, so we won't test complex behavior, just check if
  687. message sends are corrects"
  688. self assert: 1.5 + 1 = 2.5.
  689. self assert: 2 - 1 = 1.
  690. self assert: -2 - 1 = -3.
  691. self assert: 12 / 2 = 6.
  692. self assert: 3 * 4 = 12.
  693. "Simple parenthesis and execution order"
  694. self assert: 1 + 2 * 3 = 9.
  695. self assert: 1 + (2 * 3) = 7
  696. !
  697. testComparison
  698. self assert: 3 > 2.
  699. self assert: 2 < 3.
  700. self deny: 3 < 2.
  701. self deny: 2 > 3.
  702. self assert: 3 >= 3.
  703. self assert: 3.1 >= 3.
  704. self assert: 3 <= 3.
  705. self assert: 3 <= 3.1
  706. !
  707. testCopying
  708. self assert: 1 copy == 1.
  709. self assert: 1 deepCopy == 1
  710. !
  711. testEquality
  712. self assert: 1 = 1.
  713. self assert: 0 = 0.
  714. self deny: 1 = 0.
  715. self assert: 1 yourself = 1.
  716. self assert: 1 = 1 yourself.
  717. self assert: 1 yourself = 1 yourself.
  718. self deny: 0 = false.
  719. self deny: false = 0.
  720. self deny: '' = 0.
  721. self deny: 0 = ''
  722. !
  723. testIdentity
  724. self assert: 1 == 1.
  725. self assert: 0 == 0.
  726. self deny: 1 == 0.
  727. self assert: 1 yourself == 1.
  728. self assert: 1 == 1 yourself.
  729. self assert: 1 yourself == 1 yourself.
  730. self deny: 1 == 2
  731. !
  732. testMinMax
  733. self assert: (2 max: 5) equals: 5.
  734. self assert: (2 min: 5) equals: 2
  735. !
  736. testNegated
  737. self assert: 3 negated = -3.
  738. self assert: -3 negated = 3
  739. !
  740. testPrintShowingDecimalPlaces
  741. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  742. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  743. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  744. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  745. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  746. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  747. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  748. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  749. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  750. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  751. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  752. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  753. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  754. !
  755. testRounded
  756. self assert: 3 rounded = 3.
  757. self assert: 3.212 rounded = 3.
  758. self assert: 3.51 rounded = 4
  759. !
  760. testSqrt
  761. self assert: 4 sqrt = 2.
  762. self assert: 16 sqrt = 4
  763. !
  764. testSquared
  765. self assert: 4 squared = 16
  766. !
  767. testTimesRepeat
  768. | i |
  769. i := 0.
  770. 0 timesRepeat: [i := i + 1].
  771. self assert: i equals: 0.
  772. 5 timesRepeat: [i := i + 1].
  773. self assert: i equals: 5
  774. !
  775. testTo
  776. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  777. !
  778. testToBy
  779. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  780. self should: [1 to: 4 by: 0] raise: Error
  781. !
  782. testTruncated
  783. self assert: 3 truncated = 3.
  784. self assert: 3.212 truncated = 3.
  785. self assert: 3.51 truncated = 3
  786. ! !
  787. Object subclass: #ObjectMock
  788. instanceVariableNames: 'foo bar'
  789. package: 'Kernel-Tests'!
  790. !ObjectMock methodsFor: 'not yet classified'!
  791. foo
  792. ^foo
  793. !
  794. foo: anObject
  795. foo := anObject
  796. ! !
  797. TestCase subclass: #ObjectTest
  798. instanceVariableNames: ''
  799. package: 'Kernel-Tests'!
  800. !ObjectTest methodsFor: 'tests'!
  801. testBasicAccess
  802. | o |
  803. o := Object new.
  804. o basicAt: 'a' put: 1.
  805. self assert: (o basicAt: 'a') equals: 1.
  806. self assert: (o basicAt: 'b') equals: nil
  807. !
  808. testBasicPerform
  809. | o |
  810. o := Object new.
  811. o basicAt: 'func' put: ['hello'].
  812. o basicAt: 'func2' put: [:a | a + 1].
  813. self assert: (o basicPerform: 'func') equals: 'hello'.
  814. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  815. !
  816. testDNU
  817. self should: [Object new foo] raise: MessageNotUnderstood
  818. !
  819. testEquality
  820. | o |
  821. o := Object new.
  822. self deny: o = Object new.
  823. self assert: o = o.
  824. self assert: o yourself = o.
  825. self assert: o = o yourself
  826. !
  827. testHalt
  828. self should: [Object new halt] raise: Error
  829. !
  830. testIdentity
  831. | o |
  832. o := Object new.
  833. self deny: o == Object new.
  834. self assert: o == o.
  835. self assert: o yourself == o.
  836. self assert: o == o yourself
  837. !
  838. testIfNil
  839. self deny: Object new isNil.
  840. self deny: (Object new ifNil: [true]) = true.
  841. self assert: (Object new ifNotNil: [true]) = true.
  842. self assert: (Object new ifNil: [false] ifNotNil: [true]) = true.
  843. self assert: (Object new ifNotNil: [true] ifNil: [false]) = true
  844. !
  845. testInstVars
  846. | o |
  847. o := ObjectMock new.
  848. self assert: (o instVarAt: #foo) equals: nil.
  849. o instVarAt: #foo put: 1.
  850. self assert: (o instVarAt: #foo) equals: 1.
  851. self assert: (o instVarAt: 'foo') equals: 1
  852. !
  853. testNilUndefined
  854. "nil in Smalltalk is the undefined object in JS"
  855. | notDefined |
  856. <notDefined = undefined>.
  857. self assert: nil = notDefined
  858. !
  859. testYourself
  860. | o |
  861. o := ObjectMock new.
  862. self assert: o yourself == o
  863. !
  864. testidentityHash
  865. | o1 o2 |
  866. o1 := Object new.
  867. o2 := Object new.
  868. self assert: o1 identityHash == o1 identityHash.
  869. self deny: o1 identityHash == o2 identityHash
  870. ! !
  871. TestCase subclass: #PackageTest
  872. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  873. package: 'Kernel-Tests'!
  874. !PackageTest methodsFor: 'running'!
  875. setUp
  876. backUpCommitPathJs := Package defaultCommitPathJs.
  877. backUpCommitPathSt := Package defaultCommitPathSt.
  878. Package resetCommitPaths.
  879. zorkPackage := Package new name: 'Zork'.
  880. grulPackage := Package new
  881. name: 'Grul';
  882. commitPathJs: 'server/grul/js';
  883. commitPathSt: 'grul/st';
  884. yourself
  885. !
  886. tearDown
  887. Package
  888. defaultCommitPathJs: backUpCommitPathJs;
  889. defaultCommitPathSt: backUpCommitPathSt
  890. ! !
  891. !PackageTest methodsFor: 'tests'!
  892. testGrulCommitPathJsShouldBeServerGrulJs
  893. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  894. !
  895. testGrulCommitPathStShouldBeGrulSt
  896. self assert: 'grul/st' equals: grulPackage commitPathSt
  897. !
  898. testZorkCommitPathJsShouldBeJs
  899. self assert: 'js' equals: zorkPackage commitPathJs
  900. !
  901. testZorkCommitPathStShouldBeSt
  902. self assert: 'st' equals: zorkPackage commitPathSt
  903. ! !
  904. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  905. instanceVariableNames: ''
  906. package: 'Kernel-Tests'!
  907. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  908. setUp
  909. super setUp.
  910. Package
  911. defaultCommitPathJs: 'javascripts/';
  912. defaultCommitPathSt: 'smalltalk/'.
  913. ! !
  914. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  915. testGrulCommitPathJsShouldBeServerGrulJs
  916. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  917. !
  918. testGrulCommitPathStShouldBeGrulSt
  919. self assert: 'grul/st' equals: grulPackage commitPathSt
  920. !
  921. testZorkCommitPathJsShouldBeJavascript
  922. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  923. !
  924. testZorkCommitPathStShouldBeSmalltalk
  925. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  926. ! !
  927. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  928. shouldInheritSelectors
  929. ^ false
  930. ! !
  931. TestCase subclass: #PointTest
  932. instanceVariableNames: ''
  933. package: 'Kernel-Tests'!
  934. !PointTest methodsFor: 'tests'!
  935. testAccessing
  936. self assert: (Point x: 3 y: 4) x equals: 3.
  937. self assert: (Point x: 3 y: 4) y equals: 4.
  938. self assert: (Point new x: 3) x equals: 3.
  939. self assert: (Point new y: 4) y equals: 4
  940. !
  941. testArithmetic
  942. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  943. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  944. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  945. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  946. !
  947. testAt
  948. self assert: 3@4 equals: (Point x: 3 y: 4)
  949. !
  950. testEgality
  951. self assert: 3@4 = (3@4).
  952. self deny: 3@5 = (3@6)
  953. !
  954. testTranslateBy
  955. self assert: 3@4 equals: (3@3 translateBy: 0@1).
  956. self assert: 3@2 equals: (3@3 translateBy: 0@1 negated).
  957. self assert: 5@6 equals: (3@3 translateBy: 2@3).
  958. self assert: 0@3 equals: (3@3 translateBy: 3 negated @0).
  959. ! !
  960. TestCase subclass: #RandomTest
  961. instanceVariableNames: ''
  962. package: 'Kernel-Tests'!
  963. !RandomTest methodsFor: 'tests'!
  964. textNext
  965. 10000 timesRepeat: [
  966. | current next |
  967. next := Random new next.
  968. self assert: (next >= 0).
  969. self assert: (next < 1).
  970. self deny: current = next.
  971. next = current]
  972. ! !
  973. TestCase subclass: #SetTest
  974. instanceVariableNames: ''
  975. package: 'Kernel-Tests'!
  976. !SetTest methodsFor: 'tests'!
  977. testAddRemove
  978. | set |
  979. set := Set new.
  980. self assert: set isEmpty.
  981. set add: 3.
  982. self assert: (set includes: 3).
  983. set add: 5.
  984. self assert: (set includes: 5).
  985. set remove: 3.
  986. self deny: (set includes: 3)
  987. !
  988. testAt
  989. self should: [Set new at: 1 put: 2] raise: Error
  990. !
  991. testPrintString
  992. | set |
  993. set := Set new.
  994. self assert: 'a Set ()' equals: ( set printString ).
  995. set add: 1; add: 3.
  996. self assert: 'a Set (1 3)' equals: ( set printString ).
  997. set add: 'foo'.
  998. self assert: 'a Set (1 3 ''foo'')' equals: ( set printString ).
  999. set remove: 1; remove: 3.
  1000. self assert: 'a Set (''foo'')' equals: ( set printString ).
  1001. set add: 3.
  1002. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  1003. set add: 3.
  1004. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  1005. !
  1006. testSize
  1007. self assert: Set new size equals: 0.
  1008. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  1009. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  1010. !
  1011. testUnicity
  1012. | set |
  1013. set := Set new.
  1014. set add: 21.
  1015. set add: 'hello'.
  1016. set add: 21.
  1017. self assert: set size = 2.
  1018. set add: 'hello'.
  1019. self assert: set size = 2.
  1020. self assert: set asArray equals: #(21 'hello')
  1021. ! !
  1022. TestCase subclass: #UndefinedTest
  1023. instanceVariableNames: ''
  1024. package: 'Kernel-Tests'!
  1025. !UndefinedTest methodsFor: 'tests'!
  1026. testCopying
  1027. self assert: nil copy equals: nil
  1028. !
  1029. testDeepCopy
  1030. self assert: nil deepCopy = nil
  1031. !
  1032. testIfNil
  1033. self assert: (nil ifNil: [true]) equals: true.
  1034. self deny: (nil ifNotNil: [true]) = true.
  1035. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  1036. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  1037. !
  1038. testIsNil
  1039. self assert: nil isNil.
  1040. self deny: nil notNil.
  1041. ! !