Kernel-Tests.st 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. Smalltalk current createPackage: 'Kernel-Tests' properties: #{}!
  2. TestCase subclass: #ArrayTest
  3. instanceVariableNames: ''
  4. package: 'Kernel-Tests'!
  5. !ArrayTest methodsFor: 'testing'!
  6. testFirstN
  7. self assert: {1. 2. 3} equals: ({1. 2. 3. 4. 5} first: 3).
  8. !
  9. testIfEmpty
  10. self assert: 'zork' equals: ( '' ifEmpty: ['zork'] )
  11. ! !
  12. TestCase subclass: #BlockClosureTest
  13. instanceVariableNames: ''
  14. package: 'Kernel-Tests'!
  15. !BlockClosureTest methodsFor: 'tests'!
  16. testCompiledSource
  17. self assert: ([1+1] compiledSource includesSubString: 'function')
  18. !
  19. testEnsure
  20. self assert: ([Error new] ensure: [true])
  21. !
  22. testNumArgs
  23. self assert: [] numArgs equals: 0.
  24. self assert: [:a :b | ] numArgs equals: 2
  25. !
  26. testOnDo
  27. self assert: ([Error new signal] on: Error do: [:ex | true])
  28. !
  29. testValue
  30. self assert: ([1+1] value) equals: 2.
  31. self assert: ([:x | x +1] value: 2) equals: 3.
  32. self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
  33. "Arguments are optional in Amber. This isn't ANSI compliant."
  34. self assert: ([:a :b :c | 1] value) equals: 1
  35. !
  36. testValueWithPossibleArguments
  37. self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
  38. self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
  39. self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  40. !
  41. testWhileFalse
  42. | i |
  43. i := 0.
  44. [i > 5] whileFalse: [i := i + 1].
  45. self assert: i equals: 6.
  46. i := 0.
  47. [i := i + 1. i > 5] whileFalse.
  48. self assert: i equals: 6
  49. !
  50. testWhileTrue
  51. | i |
  52. i := 0.
  53. [i < 5] whileTrue: [i := i + 1].
  54. self assert: i equals: 5.
  55. i := 0.
  56. [i := i + 1. i < 5] whileTrue.
  57. self assert: i equals: 5
  58. ! !
  59. TestCase subclass: #BooleanTest
  60. instanceVariableNames: ''
  61. package: 'Kernel-Tests'!
  62. !BooleanTest methodsFor: 'tests'!
  63. testEquality
  64. "We're on top of JS...just be sure to check the basics!!"
  65. self deny: 0 = false.
  66. self deny: false = 0.
  67. self deny: '' = false.
  68. self deny: false = ''.
  69. self assert: true = true.
  70. self deny: false = true.
  71. self deny: true = false.
  72. self assert: false = false.
  73. "JS may do some type coercing after sending a message"
  74. self assert: true yourself = true.
  75. self assert: true yourself = true yourself
  76. !
  77. testIfTrueIfFalse
  78. self assert: (true ifTrue: ['alternative block']) = 'alternative block'.
  79. self assert: (true ifFalse: ['alternative block']) = nil.
  80. self assert: (false ifTrue: ['alternative block']) = nil.
  81. self assert: (false ifFalse: ['alternative block']) = 'alternative block'.
  82. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block2'.
  83. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block'.
  84. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block'.
  85. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block2'.
  86. !
  87. testLogic
  88. "Trivial logic table"
  89. self assert: (true & true); deny: (true & false); deny: (false & true); deny: (false & false).
  90. self assert: (true | true); assert: (true | false); assert: (false | true); deny: (false | false).
  91. "Checking that expressions work fine too"
  92. self assert: (true & (1 > 0)); deny: ((1 > 0) & false); deny: ((1 > 0) & (1 > 2)).
  93. self assert: (false | (1 > 0)); assert: ((1 > 0) | false); assert: ((1 > 0) | (1 > 2))
  94. !
  95. testLogicKeywords
  96. "Trivial logic table"
  97. self
  98. assert: (true and: [ true]);
  99. deny: (true and: [ false ]);
  100. deny: (false and: [ true ]);
  101. deny: (false and: [ false ]).
  102. self
  103. assert: (true or: [ true ]);
  104. assert: (true or: [ false ]);
  105. assert: (false or: [ true ]);
  106. deny: (false or: [ false ]).
  107. "Checking that expressions work fine too"
  108. self
  109. assert: (true and: [ 1 > 0 ]);
  110. deny: ((1 > 0) and: [ false ]);
  111. deny: ((1 > 0) and: [ 1 > 2 ]).
  112. self
  113. assert: (false or: [ 1 > 0 ]);
  114. assert: ((1 > 0) or: [ false ]);
  115. assert: ((1 > 0) or: [ 1 > 2 ])
  116. ! !
  117. TestCase subclass: #ClassBuilderTest
  118. instanceVariableNames: 'builder theClass'
  119. package: 'Kernel-Tests'!
  120. !ClassBuilderTest methodsFor: 'running'!
  121. setUp
  122. builder := ClassBuilder new
  123. !
  124. tearDown
  125. theClass ifNotNil: [Smalltalk current removeClass: theClass. theClass := nil]
  126. !
  127. testClassCopy
  128. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  129. self assert: theClass superclass == ObjectMock superclass.
  130. self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
  131. self assert: theClass name equals: 'ObjectMock2'.
  132. self assert: theClass package == ObjectMock package.
  133. self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
  134. !
  135. testInstanceVariableNames
  136. self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
  137. ! !
  138. TestCase subclass: #DictionaryTest
  139. instanceVariableNames: ''
  140. package: 'Kernel-Tests'!
  141. !DictionaryTest methodsFor: 'tests'!
  142. testAccessing
  143. | d |
  144. d := Dictionary new.
  145. d at: 'hello' put: 'world'.
  146. self assert: (d at: 'hello') = 'world'.
  147. self assert: (d at: 'hello' ifAbsent: [nil]) = 'world'.
  148. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  149. d at: 1 put: 2.
  150. self assert: (d at: 1) = 2.
  151. d at: 1@3 put: 3.
  152. self assert: (d at: 1@3) = 3
  153. !
  154. testDynamicDictionaries
  155. self assert: #{'hello' -> 1} asDictionary = (Dictionary with: 'hello' -> 1)
  156. !
  157. testEquality
  158. | d1 d2 |
  159. self assert: Dictionary new = Dictionary new.
  160. d1 := Dictionary new at: 1 put: 2; yourself.
  161. d2 := Dictionary new at: 1 put: 2; yourself.
  162. self assert: d1 = d2.
  163. d2 := Dictionary new at: 1 put: 3; yourself.
  164. self deny: d1 = d2.
  165. d2 := Dictionary new at: 2 put: 2; yourself.
  166. self deny: d1 = d2.
  167. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  168. self deny: d1 = d2.
  169. !
  170. testKeys
  171. | d |
  172. d := Dictionary new.
  173. d at: 1 put: 2.
  174. d at: 2 put: 3.
  175. d at: 3 put: 4.
  176. self assert: d keys = #(1 2 3)
  177. !
  178. testPrintString
  179. self
  180. assert: 'a Dictionary(''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  181. equals: (Dictionary new
  182. at:'firstname' put: 'James';
  183. at:'lastname' put: 'Bond';
  184. printString)
  185. !
  186. testSize
  187. | d |
  188. d := Dictionary new.
  189. self assert: d size = 0.
  190. d at: 1 put: 2.
  191. self assert: d size = 1.
  192. d at: 2 put: 3.
  193. self assert: d size = 2.
  194. !
  195. testValues
  196. | d |
  197. d := Dictionary new.
  198. d at: 1 put: 2.
  199. d at: 2 put: 3.
  200. d at: 3 put: 4.
  201. self assert: d values = #(2 3 4)
  202. ! !
  203. TestCase subclass: #JSObjectProxyTest
  204. instanceVariableNames: ''
  205. package: 'Kernel-Tests'!
  206. !JSObjectProxyTest methodsFor: 'accessing'!
  207. jsObject
  208. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}}>
  209. ! !
  210. !JSObjectProxyTest methodsFor: 'tests'!
  211. testDNU
  212. self should: [self jsObject foo] raise: MessageNotUnderstood
  213. !
  214. testMessageSend
  215. self assert: self jsObject a equals: 1.
  216. self assert: self jsObject b equals: 2.
  217. self assert: (self jsObject c: 3) equals: 3
  218. !
  219. testMethodWithArguments
  220. self deny: ('body' asJQuery hasClass: 'amber').
  221. 'body' asJQuery addClass: 'amber'.
  222. self assert: ('body' asJQuery hasClass: 'amber').
  223. 'body' asJQuery removeClass: 'amber'.
  224. self deny: ('body' asJQuery hasClass: 'amber').
  225. !
  226. testPrinting
  227. self assert: self jsObject printString = '[object Object]'
  228. !
  229. testPropertyThatReturnsEmptyString
  230. <document.location.hash = ''>.
  231. self assert: '' equals: document location hash.
  232. document location hash: 'test'.
  233. self assert: '#test' equals: document location hash.
  234. !
  235. testYourself
  236. |body|
  237. body := 'body' asJQuery
  238. addClass: 'amber';
  239. yourself.
  240. self assert: (body hasClass: 'amber').
  241. body removeClass: 'amber'.
  242. self deny: (body hasClass: 'amber').
  243. ! !
  244. TestCase subclass: #NumberTest
  245. instanceVariableNames: ''
  246. package: 'Kernel-Tests'!
  247. !NumberTest methodsFor: 'tests'!
  248. testArithmetic
  249. "We rely on JS here, so we won't test complex behavior, just check if
  250. message sends are corrects"
  251. self assert: 1.5 + 1 = 2.5.
  252. self assert: 2 - 1 = 1.
  253. self assert: -2 - 1 = -3.
  254. self assert: 12 / 2 = 6.
  255. self assert: 3 * 4 = 12.
  256. "Simple parenthesis and execution order"
  257. self assert: 1 + 2 * 3 = 9.
  258. self assert: 1 + (2 * 3) = 7
  259. !
  260. testComparison
  261. self assert: 3 > 2.
  262. self assert: 2 < 3.
  263. self deny: 3 < 2.
  264. self deny: 2 > 3.
  265. self assert: 3 >= 3.
  266. self assert: 3.1 >= 3.
  267. self assert: 3 <= 3.
  268. self assert: 3 <= 3.1
  269. !
  270. testCopying
  271. self assert: 1 copy == 1.
  272. self assert: 1 deepCopy == 1
  273. !
  274. testEquality
  275. self assert: 1 = 1.
  276. self assert: 0 = 0.
  277. self deny: 1 = 0.
  278. self assert: 1 yourself = 1.
  279. self assert: 1 = 1 yourself.
  280. self assert: 1 yourself = 1 yourself.
  281. self deny: 0 = false.
  282. self deny: false = 0.
  283. self deny: '' = 0.
  284. self deny: 0 = ''
  285. !
  286. testIdentity
  287. self assert: 1 == 1.
  288. self assert: 0 == 0.
  289. self deny: 1 == 0.
  290. self assert: 1 yourself == 1.
  291. self assert: 1 == 1 yourself.
  292. self assert: 1 yourself == 1 yourself.
  293. self deny: 1 == 2
  294. !
  295. testMinMax
  296. self assert: (2 max: 5) equals: 5.
  297. self assert: (2 min: 5) equals: 2
  298. !
  299. testNegated
  300. self assert: 3 negated = -3.
  301. self assert: -3 negated = 3
  302. !
  303. testPrintShowingDecimalPlaces
  304. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  305. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  306. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  307. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  308. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  309. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  310. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  311. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  312. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  313. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  314. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  315. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  316. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  317. !
  318. testRounded
  319. self assert: 3 rounded = 3.
  320. self assert: 3.212 rounded = 3.
  321. self assert: 3.51 rounded = 4
  322. !
  323. testSqrt
  324. self assert: 4 sqrt = 2.
  325. self assert: 16 sqrt = 4
  326. !
  327. testSquared
  328. self assert: 4 squared = 16
  329. !
  330. testTimesRepeat
  331. | i |
  332. i := 0.
  333. 0 timesRepeat: [i := i + 1].
  334. self assert: i equals: 0.
  335. 5 timesRepeat: [i := i + 1].
  336. self assert: i equals: 5
  337. !
  338. testTo
  339. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  340. !
  341. testToBy
  342. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  343. self should: [1 to: 4 by: 0] raise: Error
  344. !
  345. testTruncated
  346. self assert: 3 truncated = 3.
  347. self assert: 3.212 truncated = 3.
  348. self assert: 3.51 truncated = 3
  349. ! !
  350. Object subclass: #ObjectMock
  351. instanceVariableNames: 'foo bar'
  352. package: 'Kernel-Tests'!
  353. !ObjectMock methodsFor: 'not yet classified'!
  354. foo
  355. ^foo
  356. !
  357. foo: anObject
  358. foo := anObject
  359. ! !
  360. TestCase subclass: #ObjectTest
  361. instanceVariableNames: ''
  362. package: 'Kernel-Tests'!
  363. !ObjectTest methodsFor: 'tests'!
  364. testBasicAccess
  365. | o |
  366. o := Object new.
  367. o basicAt: 'a' put: 1.
  368. self assert: (o basicAt: 'a') equals: 1.
  369. self assert: (o basicAt: 'b') equals: nil
  370. !
  371. testBasicPerform
  372. | o |
  373. o := Object new.
  374. o basicAt: 'func' put: ['hello'].
  375. o basicAt: 'func2' put: [:a | a + 1].
  376. self assert: (o basicPerform: 'func') equals: 'hello'.
  377. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  378. !
  379. testDNU
  380. self should: [Object new foo] raise: MessageNotUnderstood
  381. !
  382. testEquality
  383. | o |
  384. o := Object new.
  385. self deny: o = Object new.
  386. self assert: o = o.
  387. self assert: o yourself = o.
  388. self assert: o = o yourself
  389. !
  390. testHalt
  391. self should: [Object new halt] raise: Error
  392. !
  393. testIdentity
  394. | o |
  395. o := Object new.
  396. self deny: o == Object new.
  397. self assert: o == o
  398. !
  399. testIfNil
  400. self deny: Object new isNil.
  401. self deny: (Object new ifNil: [true]) = true.
  402. self assert: (Object new ifNotNil: [true]) = true.
  403. self assert: (Object new ifNil: [false] ifNotNil: [true]) = true.
  404. self assert: (Object new ifNotNil: [true] ifNil: [false]) = true
  405. !
  406. testInstVars
  407. | o |
  408. o := ObjectMock new.
  409. self assert: (o instVarAt: #foo) equals: nil.
  410. o instVarAt: #foo put: 1.
  411. self assert: (o instVarAt: #foo) equals: 1.
  412. self assert: (o instVarAt: 'foo') equals: 1
  413. !
  414. testNilUndefined
  415. "nil in Smalltalk is the undefined object in JS"
  416. self assert: nil = undefined
  417. !
  418. testYourself
  419. | o |
  420. o := ObjectMock new.
  421. self assert: o yourself == o
  422. !
  423. testidentityHash
  424. | o1 o2 |
  425. o1 := Object new.
  426. o2 := Object new.
  427. self assert: o1 identityHash == o1 identityHash.
  428. self deny: o1 identityHash == o2 identityHash
  429. ! !
  430. TestCase subclass: #PackageTest
  431. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  432. package: 'Kernel-Tests'!
  433. !PackageTest methodsFor: 'running'!
  434. setUp
  435. backUpCommitPathJs := Package defaultCommitPathJs.
  436. backUpCommitPathSt := Package defaultCommitPathSt.
  437. Package resetCommitPaths.
  438. zorkPackage := Package new name: 'Zork'.
  439. grulPackage := Package new
  440. name: 'Grul';
  441. commitPathJs: 'server/grul/js';
  442. commitPathSt: 'grul/st';
  443. yourself
  444. !
  445. tearDown
  446. Package
  447. defaultCommitPathJs: backUpCommitPathJs;
  448. defaultCommitPathSt: backUpCommitPathSt
  449. ! !
  450. !PackageTest methodsFor: 'tests'!
  451. testGrulCommitPathJsShouldBeServerGrulJs
  452. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  453. !
  454. testGrulCommitPathStShouldBeGrulSt
  455. self assert: 'grul/st' equals: grulPackage commitPathSt
  456. !
  457. testZorkCommitPathJsShouldBeJs
  458. self assert: 'js' equals: zorkPackage commitPathJs
  459. !
  460. testZorkCommitPathStShouldBeSt
  461. self assert: 'st' equals: zorkPackage commitPathSt
  462. ! !
  463. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  464. instanceVariableNames: ''
  465. package: 'Kernel-Tests'!
  466. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  467. setUp
  468. super setUp.
  469. Package
  470. defaultCommitPathJs: 'javascripts/';
  471. defaultCommitPathSt: 'smalltalk/'.
  472. ! !
  473. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  474. testGrulCommitPathJsShouldBeServerGrulJs
  475. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  476. !
  477. testGrulCommitPathStShouldBeGrulSt
  478. self assert: 'grul/st' equals: grulPackage commitPathSt
  479. !
  480. testZorkCommitPathJsShouldBeJavascript
  481. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  482. !
  483. testZorkCommitPathStShouldBeSmalltalk
  484. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  485. ! !
  486. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  487. shouldInheritSelectors
  488. ^ false
  489. ! !
  490. TestCase subclass: #PointTest
  491. instanceVariableNames: ''
  492. package: 'Kernel-Tests'!
  493. !PointTest methodsFor: 'tests'!
  494. testAccessing
  495. self assert: (Point x: 3 y: 4) x equals: 3.
  496. self assert: (Point x: 3 y: 4) y equals: 4.
  497. self assert: (Point new x: 3) x equals: 3.
  498. self assert: (Point new y: 4) y equals: 4
  499. !
  500. testArithmetic
  501. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  502. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  503. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  504. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  505. !
  506. testAt
  507. self assert: 3@4 equals: (Point x: 3 y: 4)
  508. !
  509. testEgality
  510. self assert: 3@4 = (3@4).
  511. self deny: 3@5 = (3@6)
  512. !
  513. testTranslateBy
  514. self assert: 3@4 equals: (3@3 translateBy: 0@1).
  515. self assert: 3@2 equals: (3@3 translateBy: 0@1 negated).
  516. self assert: 5@6 equals: (3@3 translateBy: 2@3).
  517. self assert: 0@3 equals: (3@3 translateBy: 3 negated @0).
  518. ! !
  519. TestCase subclass: #RandomTest
  520. instanceVariableNames: ''
  521. package: 'Kernel-Tests'!
  522. !RandomTest methodsFor: 'tests'!
  523. textNext
  524. 10000 timesRepeat: [
  525. | current next |
  526. next := Random new next.
  527. self assert: (next >= 0).
  528. self assert: (next < 1).
  529. self deny: current = next.
  530. next = current]
  531. ! !
  532. TestCase subclass: #SetTest
  533. instanceVariableNames: ''
  534. package: 'Kernel-Tests'!
  535. !SetTest methodsFor: 'tests'!
  536. testAddRemove
  537. | set |
  538. set := Set new.
  539. self assert: set isEmpty.
  540. set add: 3.
  541. self assert: (set includes: 3).
  542. set add: 5.
  543. self assert: (set includes: 5).
  544. set remove: 3.
  545. self deny: (set includes: 3)
  546. !
  547. testAt
  548. self should: [Set new at: 1 put: 2] raise: Error
  549. !
  550. testSize
  551. self assert: Set new size equals: 0.
  552. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  553. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  554. !
  555. testUnicity
  556. | set |
  557. set := Set new.
  558. set add: 21.
  559. set add: 'hello'.
  560. set add: 21.
  561. self assert: set size = 2.
  562. set add: 'hello'.
  563. self assert: set size = 2.
  564. self assert: set asArray equals: #(21 'hello')
  565. ! !
  566. TestCase subclass: #StringTest
  567. instanceVariableNames: ''
  568. package: 'Kernel-Tests'!
  569. !StringTest methodsFor: 'tests'!
  570. testAddRemove
  571. self should: ['hello' add: 'a'] raise: Error.
  572. self should: ['hello' remove: 'h'] raise: Error
  573. !
  574. testAsArray
  575. self assert: 'hello' asArray = #('h' 'e' 'l' 'l' 'o').
  576. !
  577. testAt
  578. self assert: ('hello' at: 1) = 'h'.
  579. self assert: ('hello' at: 5) = 'o'.
  580. self assert: ('hello' at: 6 ifAbsent: [nil]) = nil
  581. !
  582. testAtPut
  583. "String instances are read-only"
  584. self should: ['hello' at: 1 put: 'a'] raise: Error
  585. !
  586. testCopyWithoutAll
  587. self
  588. assert: 'hello world'
  589. equals: ('*hello* *world*' copyWithoutAll: '*')
  590. !
  591. testEquality
  592. self assert: 'hello' = 'hello'.
  593. self deny: 'hello' = 'world'.
  594. self assert: 'hello' = 'hello' yourself.
  595. self assert: 'hello' yourself = 'hello'.
  596. "test JS falsy value"
  597. self deny: '' = 0
  598. !
  599. testIncludesSubString
  600. self assert: ('amber' includesSubString: 'ber').
  601. self deny: ('amber' includesSubString: 'zork').
  602. !
  603. testJoin
  604. self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
  605. !
  606. testSize
  607. self assert: 'smalltalk' size equals: 9.
  608. self assert: '' size equals: 0
  609. !
  610. testStreamContents
  611. self
  612. assert: 'hello world'
  613. equals: (String streamContents: [:aStream| aStream
  614. nextPutAll: 'hello'; space;
  615. nextPutAll: 'world'])
  616. ! !
  617. TestCase subclass: #SymbolTest
  618. instanceVariableNames: ''
  619. package: 'Kernel-Tests'!
  620. !SymbolTest methodsFor: 'tests'!
  621. testAsString
  622. self assert: #hello asString equals: 'hello'
  623. !
  624. testAsSymbol
  625. self assert: #hello == #hello asSymbol
  626. !
  627. testAt
  628. self assert: (#hello at: 1) = 'h'.
  629. self assert: (#hello at: 5) = 'o'.
  630. self assert: (#hello at: 6 ifAbsent: [nil]) = nil
  631. !
  632. testAtPut
  633. "Symbol instances are read-only"
  634. self should: ['hello' at: 1 put: 'a'] raise: Error
  635. !
  636. testComparing
  637. self assert: #ab > #aa.
  638. self deny: #ab > #ba.
  639. self assert: #ab < #ba.
  640. self deny: #bb < #ba.
  641. self assert: #ab >= #aa.
  642. self deny: #ab >= #ba.
  643. self assert: #ab <= #ba.
  644. self deny: #bb <= #ba
  645. !
  646. testCopying
  647. self assert: #hello copy == #hello.
  648. self assert: #hello deepCopy == #hello
  649. !
  650. testEquality
  651. self assert: #hello = #hello.
  652. self deny: #hello = #world.
  653. self assert: #hello = #hello yourself.
  654. self assert: #hello yourself = #hello.
  655. self deny: #hello = 'hello'.
  656. self deny: 'hello' = #hello.
  657. !
  658. testIdentity
  659. self assert: #hello == #hello.
  660. self deny: #hello == #world.
  661. self assert: #hello = #hello yourself.
  662. self assert: #hello yourself = #hello asString asSymbol
  663. !
  664. testIsSymbolIsString
  665. self assert: #hello isSymbol.
  666. self deny: 'hello' isSymbol.
  667. self deny: #hello isString.
  668. self assert: 'hello' isString
  669. !
  670. testSize
  671. self assert: #a size equals: 1.
  672. self assert: #aaaaa size equals: 5
  673. ! !
  674. TestCase subclass: #UndefinedTest
  675. instanceVariableNames: ''
  676. package: 'Kernel-Tests'!
  677. !UndefinedTest methodsFor: 'tests'!
  678. testCopying
  679. self assert: nil copy equals: nil
  680. !
  681. testDeepCopy
  682. self assert: nil deepCopy = nil
  683. !
  684. testIfNil
  685. self assert: (nil ifNil: [true]) equals: true.
  686. self deny: (nil ifNotNil: [true]) = true.
  687. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  688. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  689. !
  690. testIsNil
  691. self assert: nil isNil.
  692. self deny: nil notNil.
  693. ! !