Kernel-Tests.st 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. testRemoveKey
  187. | d key |
  188. d := Dictionary new.
  189. d at: 1 put: 2.
  190. d at: 2 put: 3.
  191. d at: 3 put: 4.
  192. key := 2.
  193. self assert: d keys = #(1 2 3).
  194. d removeKey: key.
  195. self assert: d keys = #(1 3).
  196. self assert: d values = #(2 4).
  197. self deny: (d includesKey: 2)
  198. !
  199. testRemoveKeyIfAbsent
  200. | d key |
  201. d := Dictionary new.
  202. d at: 1 put: 2.
  203. d at: 2 put: 3.
  204. d at: 3 put: 4.
  205. key := 2.
  206. self assert: (d removeKey: key) = 3.
  207. key := 3.
  208. self assert: (d removeKey: key ifAbsent: [42]) = 4.
  209. key := 'why'.
  210. self assert: (d removeKey: key ifAbsent: [42] ) = 42.
  211. !
  212. testSize
  213. | d |
  214. d := Dictionary new.
  215. self assert: d size = 0.
  216. d at: 1 put: 2.
  217. self assert: d size = 1.
  218. d at: 2 put: 3.
  219. self assert: d size = 2.
  220. !
  221. testValues
  222. | d |
  223. d := Dictionary new.
  224. d at: 1 put: 2.
  225. d at: 2 put: 3.
  226. d at: 3 put: 4.
  227. self assert: d values = #(2 3 4)
  228. ! !
  229. TestCase subclass: #JSObjectProxyTest
  230. instanceVariableNames: ''
  231. package: 'Kernel-Tests'!
  232. !JSObjectProxyTest methodsFor: 'accessing'!
  233. jsObject
  234. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}}>
  235. ! !
  236. !JSObjectProxyTest methodsFor: 'tests'!
  237. testDNU
  238. self should: [self jsObject foo] raise: MessageNotUnderstood
  239. !
  240. testMessageSend
  241. self assert: self jsObject a equals: 1.
  242. self assert: self jsObject b equals: 2.
  243. self assert: (self jsObject c: 3) equals: 3
  244. !
  245. testMethodWithArguments
  246. self deny: ('body' asJQuery hasClass: 'amber').
  247. 'body' asJQuery addClass: 'amber'.
  248. self assert: ('body' asJQuery hasClass: 'amber').
  249. 'body' asJQuery removeClass: 'amber'.
  250. self deny: ('body' asJQuery hasClass: 'amber').
  251. !
  252. testPrinting
  253. self assert: self jsObject printString = '[object Object]'
  254. !
  255. testPropertyThatReturnsEmptyString
  256. <document.location.hash = ''>.
  257. self assert: '' equals: document location hash.
  258. document location hash: 'test'.
  259. self assert: '#test' equals: document location hash.
  260. !
  261. testYourself
  262. |body|
  263. body := 'body' asJQuery
  264. addClass: 'amber';
  265. yourself.
  266. self assert: (body hasClass: 'amber').
  267. body removeClass: 'amber'.
  268. self deny: (body hasClass: 'amber').
  269. ! !
  270. TestCase subclass: #NumberTest
  271. instanceVariableNames: ''
  272. package: 'Kernel-Tests'!
  273. !NumberTest methodsFor: 'tests'!
  274. testArithmetic
  275. "We rely on JS here, so we won't test complex behavior, just check if
  276. message sends are corrects"
  277. self assert: 1.5 + 1 = 2.5.
  278. self assert: 2 - 1 = 1.
  279. self assert: -2 - 1 = -3.
  280. self assert: 12 / 2 = 6.
  281. self assert: 3 * 4 = 12.
  282. "Simple parenthesis and execution order"
  283. self assert: 1 + 2 * 3 = 9.
  284. self assert: 1 + (2 * 3) = 7
  285. !
  286. testComparison
  287. self assert: 3 > 2.
  288. self assert: 2 < 3.
  289. self deny: 3 < 2.
  290. self deny: 2 > 3.
  291. self assert: 3 >= 3.
  292. self assert: 3.1 >= 3.
  293. self assert: 3 <= 3.
  294. self assert: 3 <= 3.1
  295. !
  296. testCopying
  297. self assert: 1 copy == 1.
  298. self assert: 1 deepCopy == 1
  299. !
  300. testEquality
  301. self assert: 1 = 1.
  302. self assert: 0 = 0.
  303. self deny: 1 = 0.
  304. self assert: 1 yourself = 1.
  305. self assert: 1 = 1 yourself.
  306. self assert: 1 yourself = 1 yourself.
  307. self deny: 0 = false.
  308. self deny: false = 0.
  309. self deny: '' = 0.
  310. self deny: 0 = ''
  311. !
  312. testIdentity
  313. self assert: 1 == 1.
  314. self assert: 0 == 0.
  315. self deny: 1 == 0.
  316. self assert: 1 yourself == 1.
  317. self assert: 1 == 1 yourself.
  318. self assert: 1 yourself == 1 yourself.
  319. self deny: 1 == 2
  320. !
  321. testMinMax
  322. self assert: (2 max: 5) equals: 5.
  323. self assert: (2 min: 5) equals: 2
  324. !
  325. testNegated
  326. self assert: 3 negated = -3.
  327. self assert: -3 negated = 3
  328. !
  329. testPrintShowingDecimalPlaces
  330. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  331. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  332. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  333. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  334. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  335. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  336. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  337. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  338. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  339. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  340. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  341. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  342. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  343. !
  344. testRounded
  345. self assert: 3 rounded = 3.
  346. self assert: 3.212 rounded = 3.
  347. self assert: 3.51 rounded = 4
  348. !
  349. testSqrt
  350. self assert: 4 sqrt = 2.
  351. self assert: 16 sqrt = 4
  352. !
  353. testSquared
  354. self assert: 4 squared = 16
  355. !
  356. testTimesRepeat
  357. | i |
  358. i := 0.
  359. 0 timesRepeat: [i := i + 1].
  360. self assert: i equals: 0.
  361. 5 timesRepeat: [i := i + 1].
  362. self assert: i equals: 5
  363. !
  364. testTo
  365. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  366. !
  367. testToBy
  368. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  369. self should: [1 to: 4 by: 0] raise: Error
  370. !
  371. testTruncated
  372. self assert: 3 truncated = 3.
  373. self assert: 3.212 truncated = 3.
  374. self assert: 3.51 truncated = 3
  375. ! !
  376. Object subclass: #ObjectMock
  377. instanceVariableNames: 'foo bar'
  378. package: 'Kernel-Tests'!
  379. !ObjectMock methodsFor: 'not yet classified'!
  380. foo
  381. ^foo
  382. !
  383. foo: anObject
  384. foo := anObject
  385. ! !
  386. TestCase subclass: #ObjectTest
  387. instanceVariableNames: ''
  388. package: 'Kernel-Tests'!
  389. !ObjectTest methodsFor: 'tests'!
  390. testBasicAccess
  391. | o |
  392. o := Object new.
  393. o basicAt: 'a' put: 1.
  394. self assert: (o basicAt: 'a') equals: 1.
  395. self assert: (o basicAt: 'b') equals: nil
  396. !
  397. testBasicPerform
  398. | o |
  399. o := Object new.
  400. o basicAt: 'func' put: ['hello'].
  401. o basicAt: 'func2' put: [:a | a + 1].
  402. self assert: (o basicPerform: 'func') equals: 'hello'.
  403. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  404. !
  405. testDNU
  406. self should: [Object new foo] raise: MessageNotUnderstood
  407. !
  408. testEquality
  409. | o |
  410. o := Object new.
  411. self deny: o = Object new.
  412. self assert: o = o.
  413. self assert: o yourself = o.
  414. self assert: o = o yourself
  415. !
  416. testHalt
  417. self should: [Object new halt] raise: Error
  418. !
  419. testIdentity
  420. | o |
  421. o := Object new.
  422. self deny: o == Object new.
  423. self assert: o == o
  424. !
  425. testIfNil
  426. self deny: Object new isNil.
  427. self deny: (Object new ifNil: [true]) = true.
  428. self assert: (Object new ifNotNil: [true]) = true.
  429. self assert: (Object new ifNil: [false] ifNotNil: [true]) = true.
  430. self assert: (Object new ifNotNil: [true] ifNil: [false]) = true
  431. !
  432. testInstVars
  433. | o |
  434. o := ObjectMock new.
  435. self assert: (o instVarAt: #foo) equals: nil.
  436. o instVarAt: #foo put: 1.
  437. self assert: (o instVarAt: #foo) equals: 1.
  438. self assert: (o instVarAt: 'foo') equals: 1
  439. !
  440. testNilUndefined
  441. "nil in Smalltalk is the undefined object in JS"
  442. self assert: nil = undefined
  443. !
  444. testYourself
  445. | o |
  446. o := ObjectMock new.
  447. self assert: o yourself == o
  448. !
  449. testidentityHash
  450. | o1 o2 |
  451. o1 := Object new.
  452. o2 := Object new.
  453. self assert: o1 identityHash == o1 identityHash.
  454. self deny: o1 identityHash == o2 identityHash
  455. ! !
  456. TestCase subclass: #PackageTest
  457. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  458. package: 'Kernel-Tests'!
  459. !PackageTest methodsFor: 'running'!
  460. setUp
  461. backUpCommitPathJs := Package defaultCommitPathJs.
  462. backUpCommitPathSt := Package defaultCommitPathSt.
  463. Package resetCommitPaths.
  464. zorkPackage := Package new name: 'Zork'.
  465. grulPackage := Package new
  466. name: 'Grul';
  467. commitPathJs: 'server/grul/js';
  468. commitPathSt: 'grul/st';
  469. yourself
  470. !
  471. tearDown
  472. Package
  473. defaultCommitPathJs: backUpCommitPathJs;
  474. defaultCommitPathSt: backUpCommitPathSt
  475. ! !
  476. !PackageTest methodsFor: 'tests'!
  477. testGrulCommitPathJsShouldBeServerGrulJs
  478. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  479. !
  480. testGrulCommitPathStShouldBeGrulSt
  481. self assert: 'grul/st' equals: grulPackage commitPathSt
  482. !
  483. testZorkCommitPathJsShouldBeJs
  484. self assert: 'js' equals: zorkPackage commitPathJs
  485. !
  486. testZorkCommitPathStShouldBeSt
  487. self assert: 'st' equals: zorkPackage commitPathSt
  488. ! !
  489. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  490. instanceVariableNames: ''
  491. package: 'Kernel-Tests'!
  492. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  493. setUp
  494. super setUp.
  495. Package
  496. defaultCommitPathJs: 'javascripts/';
  497. defaultCommitPathSt: 'smalltalk/'.
  498. ! !
  499. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  500. testGrulCommitPathJsShouldBeServerGrulJs
  501. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  502. !
  503. testGrulCommitPathStShouldBeGrulSt
  504. self assert: 'grul/st' equals: grulPackage commitPathSt
  505. !
  506. testZorkCommitPathJsShouldBeJavascript
  507. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  508. !
  509. testZorkCommitPathStShouldBeSmalltalk
  510. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  511. ! !
  512. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  513. shouldInheritSelectors
  514. ^ false
  515. ! !
  516. TestCase subclass: #PointTest
  517. instanceVariableNames: ''
  518. package: 'Kernel-Tests'!
  519. !PointTest methodsFor: 'tests'!
  520. testAccessing
  521. self assert: (Point x: 3 y: 4) x equals: 3.
  522. self assert: (Point x: 3 y: 4) y equals: 4.
  523. self assert: (Point new x: 3) x equals: 3.
  524. self assert: (Point new y: 4) y equals: 4
  525. !
  526. testArithmetic
  527. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  528. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  529. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  530. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  531. !
  532. testAt
  533. self assert: 3@4 equals: (Point x: 3 y: 4)
  534. !
  535. testEgality
  536. self assert: 3@4 = (3@4).
  537. self deny: 3@5 = (3@6)
  538. !
  539. testTranslateBy
  540. self assert: 3@4 equals: (3@3 translateBy: 0@1).
  541. self assert: 3@2 equals: (3@3 translateBy: 0@1 negated).
  542. self assert: 5@6 equals: (3@3 translateBy: 2@3).
  543. self assert: 0@3 equals: (3@3 translateBy: 3 negated @0).
  544. ! !
  545. TestCase subclass: #RandomTest
  546. instanceVariableNames: ''
  547. package: 'Kernel-Tests'!
  548. !RandomTest methodsFor: 'tests'!
  549. textNext
  550. 10000 timesRepeat: [
  551. | current next |
  552. next := Random new next.
  553. self assert: (next >= 0).
  554. self assert: (next < 1).
  555. self deny: current = next.
  556. next = current]
  557. ! !
  558. TestCase subclass: #SetTest
  559. instanceVariableNames: ''
  560. package: 'Kernel-Tests'!
  561. !SetTest methodsFor: 'tests'!
  562. testAddRemove
  563. | set |
  564. set := Set new.
  565. self assert: set isEmpty.
  566. set add: 3.
  567. self assert: (set includes: 3).
  568. set add: 5.
  569. self assert: (set includes: 5).
  570. set remove: 3.
  571. self deny: (set includes: 3)
  572. !
  573. testAt
  574. self should: [Set new at: 1 put: 2] raise: Error
  575. !
  576. testSize
  577. self assert: Set new size equals: 0.
  578. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  579. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  580. !
  581. testUnicity
  582. | set |
  583. set := Set new.
  584. set add: 21.
  585. set add: 'hello'.
  586. set add: 21.
  587. self assert: set size = 2.
  588. set add: 'hello'.
  589. self assert: set size = 2.
  590. self assert: set asArray equals: #(21 'hello')
  591. ! !
  592. TestCase subclass: #StringTest
  593. instanceVariableNames: ''
  594. package: 'Kernel-Tests'!
  595. !StringTest methodsFor: 'tests'!
  596. testAddRemove
  597. self should: ['hello' add: 'a'] raise: Error.
  598. self should: ['hello' remove: 'h'] raise: Error
  599. !
  600. testAsArray
  601. self assert: 'hello' asArray = #('h' 'e' 'l' 'l' 'o').
  602. !
  603. testAt
  604. self assert: ('hello' at: 1) = 'h'.
  605. self assert: ('hello' at: 5) = 'o'.
  606. self assert: ('hello' at: 6 ifAbsent: [nil]) = nil
  607. !
  608. testAtPut
  609. "String instances are read-only"
  610. self should: ['hello' at: 1 put: 'a'] raise: Error
  611. !
  612. testCopyWithoutAll
  613. self
  614. assert: 'hello world'
  615. equals: ('*hello* *world*' copyWithoutAll: '*')
  616. !
  617. testEquality
  618. self assert: 'hello' = 'hello'.
  619. self deny: 'hello' = 'world'.
  620. self assert: 'hello' = 'hello' yourself.
  621. self assert: 'hello' yourself = 'hello'.
  622. "test JS falsy value"
  623. self deny: '' = 0
  624. !
  625. testIncludesSubString
  626. self assert: ('amber' includesSubString: 'ber').
  627. self deny: ('amber' includesSubString: 'zork').
  628. !
  629. testJoin
  630. self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
  631. !
  632. testSize
  633. self assert: 'smalltalk' size equals: 9.
  634. self assert: '' size equals: 0
  635. !
  636. testStreamContents
  637. self
  638. assert: 'hello world'
  639. equals: (String streamContents: [:aStream| aStream
  640. nextPutAll: 'hello'; space;
  641. nextPutAll: 'world'])
  642. ! !
  643. TestCase subclass: #SymbolTest
  644. instanceVariableNames: ''
  645. package: 'Kernel-Tests'!
  646. !SymbolTest methodsFor: 'tests'!
  647. testAsString
  648. self assert: #hello asString equals: 'hello'
  649. !
  650. testAsSymbol
  651. self assert: #hello == #hello asSymbol
  652. !
  653. testAt
  654. self assert: (#hello at: 1) = 'h'.
  655. self assert: (#hello at: 5) = 'o'.
  656. self assert: (#hello at: 6 ifAbsent: [nil]) = nil
  657. !
  658. testAtPut
  659. "Symbol instances are read-only"
  660. self should: ['hello' at: 1 put: 'a'] raise: Error
  661. !
  662. testComparing
  663. self assert: #ab > #aa.
  664. self deny: #ab > #ba.
  665. self assert: #ab < #ba.
  666. self deny: #bb < #ba.
  667. self assert: #ab >= #aa.
  668. self deny: #ab >= #ba.
  669. self assert: #ab <= #ba.
  670. self deny: #bb <= #ba
  671. !
  672. testCopying
  673. self assert: #hello copy == #hello.
  674. self assert: #hello deepCopy == #hello
  675. !
  676. testEquality
  677. self assert: #hello = #hello.
  678. self deny: #hello = #world.
  679. self assert: #hello = #hello yourself.
  680. self assert: #hello yourself = #hello.
  681. self deny: #hello = 'hello'.
  682. self deny: 'hello' = #hello.
  683. !
  684. testIdentity
  685. self assert: #hello == #hello.
  686. self deny: #hello == #world.
  687. self assert: #hello = #hello yourself.
  688. self assert: #hello yourself = #hello asString asSymbol
  689. !
  690. testIsSymbolIsString
  691. self assert: #hello isSymbol.
  692. self deny: 'hello' isSymbol.
  693. self deny: #hello isString.
  694. self assert: 'hello' isString
  695. !
  696. testSize
  697. self assert: #a size equals: 1.
  698. self assert: #aaaaa size equals: 5
  699. ! !
  700. TestCase subclass: #UndefinedTest
  701. instanceVariableNames: ''
  702. package: 'Kernel-Tests'!
  703. !UndefinedTest methodsFor: 'tests'!
  704. testCopying
  705. self assert: nil copy equals: nil
  706. !
  707. testDeepCopy
  708. self assert: nil deepCopy = nil
  709. !
  710. testIfNil
  711. self assert: (nil ifNil: [true]) equals: true.
  712. self deny: (nil ifNotNil: [true]) = true.
  713. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  714. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  715. !
  716. testIsNil
  717. self assert: nil isNil.
  718. self deny: nil notNil.
  719. ! !