1
0

Kernel-Tests.st 20 KB

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