Kernel-Tests.st 20 KB

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