Kernel-Tests.st 21 KB

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