2
0

Kernel-Tests.st 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. testPrintString
  23. | array |
  24. array := Array new.
  25. self assert: 'a Array ()' equals: ( array printString ).
  26. array add: 1; add: 3.
  27. self assert: 'a Array (1 3)' equals: ( array printString ).
  28. array add: 'foo'.
  29. self assert: 'a Array (1 3 ''foo'')' equals: ( array printString ).
  30. array remove: 1; remove: 3.
  31. self assert: 'a Array (''foo'')' equals: ( array printString ).
  32. array addLast: 3.
  33. self assert: 'a Array (''foo'' 3)' equals: ( array printString ).
  34. array addLast: 3.
  35. self assert: 'a Array (''foo'' 3 3)' equals: ( array printString ).
  36. ! !
  37. TestCase subclass: #AssociationTest
  38. instanceVariableNames: ''
  39. package: 'Kernel-Tests'!
  40. !AssociationTest methodsFor: 'tests'!
  41. testClassCreation
  42. "Test if Associatin object created via the class method contains correct values"
  43. | anAssociation key value|
  44. key := 'KEY_STRING'.
  45. value := 'VALUE_STRING'.
  46. anAssociation := Association key: key value: value.
  47. self assert: key equals: anAssociation key.
  48. self assert: value equals: anAssociation value.
  49. !
  50. testEqualAssociations
  51. "Test if two equal Association objects compare to true"
  52. | anAssociation anotherAssociation |
  53. anAssociation := Association key: 'KEY' value: 'VALUE'.
  54. anotherAssociation := Association key: 'KEY' value: 'VALUE'.
  55. self assert: anAssociation equals: anotherAssociation.
  56. !
  57. testKeyAccess
  58. "Test getter and setter messages for Association keys"
  59. | anAssociation key |
  60. key := 'KEY_STRING'.
  61. anAssociation := Association new.
  62. anAssociation key: key.
  63. self assert: key equals: anAssociation key.
  64. self assert: nil equals: anAssociation value.
  65. !
  66. testNotEqualAssociations
  67. "Test if two unequal Association objects compare to false"
  68. | anAssociation anotherAssociation |
  69. anAssociation := Association key: 'KEY' value: 'VALUE'.
  70. anotherAssociation := Association key: 'KEY2' value: 'VALUE2'.
  71. self deny: (anAssociation = anotherAssociation).
  72. !
  73. testPrintString
  74. "Test if the output of the printString message is correct"
  75. | anAssociation returnString |
  76. anAssociation := Association key: 'KEY' value: 'VALUE'.
  77. returnString := anAssociation printString.
  78. self assert: '''KEY''->''VALUE''' equals: returnString.
  79. !
  80. testUninitializedObject
  81. "Test if uninitialized Associatin object contains correct values"
  82. | anAssociation |
  83. anAssociation := Association new.
  84. self assert: nil equals: anAssociation key.
  85. self assert: nil equals: anAssociation value.
  86. !
  87. testValueAccess
  88. "Test getter and setter messages for Association values"
  89. | anAssociation value |
  90. value := 'VALUE_STRING'.
  91. anAssociation := Association new.
  92. anAssociation value: value.
  93. self assert: nil equals: anAssociation key.
  94. self assert: value equals: anAssociation value.
  95. ! !
  96. TestCase subclass: #BlockClosureTest
  97. instanceVariableNames: ''
  98. package: 'Kernel-Tests'!
  99. !BlockClosureTest methodsFor: 'tests'!
  100. testCompiledSource
  101. self assert: ([1+1] compiledSource includesSubString: 'function')
  102. !
  103. testEnsure
  104. self assert: ([Error new] ensure: [true])
  105. !
  106. testNumArgs
  107. self assert: [] numArgs equals: 0.
  108. self assert: [:a :b | ] numArgs equals: 2
  109. !
  110. testOnDo
  111. self assert: ([Error new signal] on: Error do: [:ex | true])
  112. !
  113. testValue
  114. self assert: ([1+1] value) equals: 2.
  115. self assert: ([:x | x +1] value: 2) equals: 3.
  116. self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
  117. "Arguments are optional in Amber. This isn't ANSI compliant."
  118. self assert: ([:a :b :c | 1] value) equals: 1
  119. !
  120. testValueWithPossibleArguments
  121. self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
  122. self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
  123. self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
  124. !
  125. testWhileFalse
  126. | i |
  127. i := 0.
  128. [i > 5] whileFalse: [i := i + 1].
  129. self assert: i equals: 6.
  130. i := 0.
  131. [i := i + 1. i > 5] whileFalse.
  132. self assert: i equals: 6
  133. !
  134. testWhileTrue
  135. | i |
  136. i := 0.
  137. [i < 5] whileTrue: [i := i + 1].
  138. self assert: i equals: 5.
  139. i := 0.
  140. [i := i + 1. i < 5] whileTrue.
  141. self assert: i equals: 5
  142. ! !
  143. TestCase subclass: #BooleanTest
  144. instanceVariableNames: ''
  145. package: 'Kernel-Tests'!
  146. !BooleanTest methodsFor: 'tests'!
  147. testEquality
  148. "We're on top of JS...just be sure to check the basics!!"
  149. self deny: 0 = false.
  150. self deny: false = 0.
  151. self deny: '' = false.
  152. self deny: false = ''.
  153. self assert: true = true.
  154. self deny: false = true.
  155. self deny: true = false.
  156. self assert: false = false.
  157. "JS may do some type coercing after sending a message"
  158. self assert: true yourself = true.
  159. self assert: true yourself = true yourself
  160. !
  161. testIdentity
  162. "We're on top of JS...just be sure to check the basics!!"
  163. self deny: 0 == false.
  164. self deny: false == 0.
  165. self deny: '' == false.
  166. self deny: false == ''.
  167. self assert: true == true.
  168. self deny: false == true.
  169. self deny: true == false.
  170. self assert: false == false.
  171. "JS may do some type coercing after sending a message"
  172. self assert: true yourself == true.
  173. self assert: true yourself == true yourself
  174. !
  175. testIfTrueIfFalse
  176. self assert: (true ifTrue: ['alternative block']) = 'alternative block'.
  177. self assert: (true ifFalse: ['alternative block']) = nil.
  178. self assert: (false ifTrue: ['alternative block']) = nil.
  179. self assert: (false ifFalse: ['alternative block']) = 'alternative block'.
  180. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block2'.
  181. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block'.
  182. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block'.
  183. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block2'.
  184. !
  185. testLogic
  186. "Trivial logic table"
  187. self assert: (true & true); deny: (true & false); deny: (false & true); deny: (false & false).
  188. self assert: (true | true); assert: (true | false); assert: (false | true); deny: (false | false).
  189. "Checking that expressions work fine too"
  190. self assert: (true & (1 > 0)); deny: ((1 > 0) & false); deny: ((1 > 0) & (1 > 2)).
  191. self assert: (false | (1 > 0)); assert: ((1 > 0) | false); assert: ((1 > 0) | (1 > 2))
  192. !
  193. testLogicKeywords
  194. "Trivial logic table"
  195. self
  196. assert: (true and: [ true]);
  197. deny: (true and: [ false ]);
  198. deny: (false and: [ true ]);
  199. deny: (false and: [ false ]).
  200. self
  201. assert: (true or: [ true ]);
  202. assert: (true or: [ false ]);
  203. assert: (false or: [ true ]);
  204. deny: (false or: [ false ]).
  205. "Checking that expressions work fine too"
  206. self
  207. assert: (true and: [ 1 > 0 ]);
  208. deny: ((1 > 0) and: [ false ]);
  209. deny: ((1 > 0) and: [ 1 > 2 ]).
  210. self
  211. assert: (false or: [ 1 > 0 ]);
  212. assert: ((1 > 0) or: [ false ]);
  213. assert: ((1 > 0) or: [ 1 > 2 ])
  214. ! !
  215. TestCase subclass: #ClassBuilderTest
  216. instanceVariableNames: 'builder theClass'
  217. package: 'Kernel-Tests'!
  218. !ClassBuilderTest methodsFor: 'running'!
  219. setUp
  220. builder := ClassBuilder new
  221. !
  222. tearDown
  223. theClass ifNotNil: [Smalltalk current removeClass: theClass. theClass := nil]
  224. !
  225. testClassCopy
  226. theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
  227. self assert: theClass superclass == ObjectMock superclass.
  228. self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
  229. self assert: theClass name equals: 'ObjectMock2'.
  230. self assert: theClass package == ObjectMock package.
  231. self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
  232. !
  233. testInstanceVariableNames
  234. self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
  235. ! !
  236. TestCase subclass: #CollectionTest
  237. instanceVariableNames: ''
  238. package: 'Kernel-Tests'!
  239. !CollectionTest methodsFor: 'accessing'!
  240. collection
  241. ^ self collectionClass withAll: self defaultValues
  242. !
  243. collectionClass
  244. ^ self class collectionClass
  245. !
  246. defaultValues
  247. ^ #('a' 1 2 #e)
  248. ! !
  249. !CollectionTest methodsFor: 'convenience'!
  250. assertSameContents: aCollection as: anotherCollection
  251. self assert: aCollection size = anotherCollection size.
  252. aCollection do: [ :each |
  253. self assert: (aCollection at: each) = (anotherCollection at: each) ]
  254. ! !
  255. !CollectionTest methodsFor: 'testing'!
  256. testAsArray
  257. self
  258. assertSameContents: self collection
  259. as: self collection asArray
  260. !
  261. testAsOrderedCollection
  262. self
  263. assertSameContents: self collection
  264. as: self collection asOrderedCollection
  265. !
  266. testAsSet
  267. | c set |
  268. c := self collectionClass withAll: #('a' 'b' 'c' 1 2 1 'a').
  269. set := c asSet.
  270. self assert: set size = 5.
  271. c do: [ :each |
  272. self assert: (set includes: each) ]
  273. !
  274. testIsEmpty
  275. self assert: self collectionClass new isEmpty.
  276. self deny: self collection isEmpty
  277. !
  278. testSize
  279. self assert: self collectionClass new size = 0.
  280. self assert: self collection size = 4
  281. ! !
  282. !CollectionTest class methodsFor: 'accessing'!
  283. collectionClass
  284. ^ nil
  285. ! !
  286. !CollectionTest class methodsFor: 'testing'!
  287. isAbstract
  288. ^ self collectionClass notNil
  289. ! !
  290. TestCase subclass: #DictionaryTest
  291. instanceVariableNames: ''
  292. package: 'Kernel-Tests'!
  293. !DictionaryTest methodsFor: 'tests'!
  294. testAccessing
  295. | d |
  296. d := Dictionary new.
  297. d at: 'hello' put: 'world'.
  298. self assert: (d at: 'hello') = 'world'.
  299. self assert: (d at: 'hello' ifAbsent: [nil]) = 'world'.
  300. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  301. d at: 1 put: 2.
  302. self assert: (d at: 1) = 2.
  303. d at: 1@3 put: 3.
  304. self assert: (d at: 1@3) = 3
  305. !
  306. testDynamicDictionaries
  307. self assert: #{'hello' -> 1} asDictionary = (Dictionary with: 'hello' -> 1)
  308. !
  309. testEquality
  310. | d1 d2 |
  311. self assert: Dictionary new = Dictionary new.
  312. d1 := Dictionary new at: 1 put: 2; yourself.
  313. d2 := Dictionary new at: 1 put: 2; yourself.
  314. self assert: d1 = d2.
  315. d2 := Dictionary new at: 1 put: 3; yourself.
  316. self deny: d1 = d2.
  317. d2 := Dictionary new at: 2 put: 2; yourself.
  318. self deny: d1 = d2.
  319. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  320. self deny: d1 = d2.
  321. !
  322. testIfAbsent
  323. | d visited |
  324. visited := false.
  325. d := Dictionary new.
  326. d at: 'hello' ifAbsent: [ visited := true ].
  327. self assert: visited.
  328. !
  329. testIfPresent
  330. | d visited absent |
  331. visited := false.
  332. d := Dictionary new.
  333. d at: 'hello' put: 'world'.
  334. d at: 'hello' ifPresent: [ :value | visited := value ].
  335. self assert: visited = 'world'.
  336. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  337. self assert: absent isNil.
  338. !
  339. testIfPresentIfAbsent
  340. | d visited |
  341. visited := false.
  342. d := Dictionary new.
  343. d at: 'hello' put: 'world'.
  344. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  345. self assert: visited = 'world'.
  346. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  347. self assert: visited.
  348. !
  349. testKeys
  350. | d |
  351. d := Dictionary new.
  352. d at: 1 put: 2.
  353. d at: 2 put: 3.
  354. d at: 3 put: 4.
  355. self assert: d keys = #(1 2 3)
  356. !
  357. testPrintString
  358. self
  359. assert: 'a Dictionary(''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  360. equals: (Dictionary new
  361. at:'firstname' put: 'James';
  362. at:'lastname' put: 'Bond';
  363. printString)
  364. !
  365. testRemoveKey
  366. | d key |
  367. d := Dictionary new.
  368. d at: 1 put: 2.
  369. d at: 2 put: 3.
  370. d at: 3 put: 4.
  371. key := 2.
  372. self assert: d keys = #(1 2 3).
  373. d removeKey: key.
  374. self assert: d keys = #(1 3).
  375. self assert: d values = #(2 4).
  376. self deny: (d includesKey: 2)
  377. !
  378. testRemoveKeyIfAbsent
  379. | d key |
  380. d := Dictionary new.
  381. d at: 1 put: 2.
  382. d at: 2 put: 3.
  383. d at: 3 put: 4.
  384. key := 2.
  385. self assert: (d removeKey: key) = 3.
  386. key := 3.
  387. self assert: (d removeKey: key ifAbsent: [42]) = 4.
  388. key := 'why'.
  389. self assert: (d removeKey: key ifAbsent: [42] ) = 42.
  390. !
  391. testSize
  392. | d |
  393. d := Dictionary new.
  394. self assert: d size = 0.
  395. d at: 1 put: 2.
  396. self assert: d size = 1.
  397. d at: 2 put: 3.
  398. self assert: d size = 2.
  399. !
  400. testValues
  401. | d |
  402. d := Dictionary new.
  403. d at: 1 put: 2.
  404. d at: 2 put: 3.
  405. d at: 3 put: 4.
  406. self assert: d values = #(2 3 4)
  407. ! !
  408. TestCase subclass: #HashedCollectionTest
  409. instanceVariableNames: ''
  410. package: 'Kernel-Tests'!
  411. !HashedCollectionTest methodsFor: 'tests'!
  412. testPrintString
  413. "test if the printString message returns the correct string representation"
  414. | aHashedCollection |
  415. aHashedCollection := HashedCollection new.
  416. self assert: 'a HashedCollection()' equals: aHashedCollection printString.
  417. aHashedCollection at: 'key1' put: 1.
  418. self assert: 'a HashedCollection(''key1''->1)' equals: aHashedCollection printString.
  419. aHashedCollection at: 'key2' put: 2.
  420. self assert: 'a HashedCollection(''key1''->1 , ''key2''->2)' equals: aHashedCollection printString.
  421. ! !
  422. TestCase subclass: #JSObjectProxyTest
  423. instanceVariableNames: ''
  424. package: 'Kernel-Tests'!
  425. !JSObjectProxyTest methodsFor: 'accessing'!
  426. jsObject
  427. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}}>
  428. ! !
  429. !JSObjectProxyTest methodsFor: 'tests'!
  430. testDNU
  431. self should: [self jsObject foo] raise: MessageNotUnderstood
  432. !
  433. testMessageSend
  434. self assert: self jsObject a equals: 1.
  435. self assert: self jsObject b equals: 2.
  436. self assert: (self jsObject c: 3) equals: 3
  437. !
  438. testMethodWithArguments
  439. self deny: ('body' asJQuery hasClass: 'amber').
  440. 'body' asJQuery addClass: 'amber'.
  441. self assert: ('body' asJQuery hasClass: 'amber').
  442. 'body' asJQuery removeClass: 'amber'.
  443. self deny: ('body' asJQuery hasClass: 'amber').
  444. !
  445. testPrinting
  446. self assert: self jsObject printString = '[object Object]'
  447. !
  448. testPropertyThatReturnsEmptyString
  449. <document.location.hash = ''>.
  450. self assert: '' equals: document location hash.
  451. document location hash: 'test'.
  452. self assert: '#test' equals: document location hash.
  453. !
  454. testYourself
  455. |body|
  456. body := 'body' asJQuery
  457. addClass: 'amber';
  458. yourself.
  459. self assert: (body hasClass: 'amber').
  460. body removeClass: 'amber'.
  461. self deny: (body hasClass: 'amber').
  462. ! !
  463. TestCase subclass: #NumberTest
  464. instanceVariableNames: ''
  465. package: 'Kernel-Tests'!
  466. !NumberTest methodsFor: 'tests'!
  467. testArithmetic
  468. "We rely on JS here, so we won't test complex behavior, just check if
  469. message sends are corrects"
  470. self assert: 1.5 + 1 = 2.5.
  471. self assert: 2 - 1 = 1.
  472. self assert: -2 - 1 = -3.
  473. self assert: 12 / 2 = 6.
  474. self assert: 3 * 4 = 12.
  475. "Simple parenthesis and execution order"
  476. self assert: 1 + 2 * 3 = 9.
  477. self assert: 1 + (2 * 3) = 7
  478. !
  479. testComparison
  480. self assert: 3 > 2.
  481. self assert: 2 < 3.
  482. self deny: 3 < 2.
  483. self deny: 2 > 3.
  484. self assert: 3 >= 3.
  485. self assert: 3.1 >= 3.
  486. self assert: 3 <= 3.
  487. self assert: 3 <= 3.1
  488. !
  489. testCopying
  490. self assert: 1 copy == 1.
  491. self assert: 1 deepCopy == 1
  492. !
  493. testEquality
  494. self assert: 1 = 1.
  495. self assert: 0 = 0.
  496. self deny: 1 = 0.
  497. self assert: 1 yourself = 1.
  498. self assert: 1 = 1 yourself.
  499. self assert: 1 yourself = 1 yourself.
  500. self deny: 0 = false.
  501. self deny: false = 0.
  502. self deny: '' = 0.
  503. self deny: 0 = ''
  504. !
  505. testIdentity
  506. self assert: 1 == 1.
  507. self assert: 0 == 0.
  508. self deny: 1 == 0.
  509. self assert: 1 yourself == 1.
  510. self assert: 1 == 1 yourself.
  511. self assert: 1 yourself == 1 yourself.
  512. self deny: 1 == 2
  513. !
  514. testMinMax
  515. self assert: (2 max: 5) equals: 5.
  516. self assert: (2 min: 5) equals: 2
  517. !
  518. testNegated
  519. self assert: 3 negated = -3.
  520. self assert: -3 negated = 3
  521. !
  522. testPrintShowingDecimalPlaces
  523. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  524. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  525. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  526. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  527. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  528. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  529. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  530. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  531. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  532. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  533. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  534. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  535. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  536. !
  537. testRounded
  538. self assert: 3 rounded = 3.
  539. self assert: 3.212 rounded = 3.
  540. self assert: 3.51 rounded = 4
  541. !
  542. testSqrt
  543. self assert: 4 sqrt = 2.
  544. self assert: 16 sqrt = 4
  545. !
  546. testSquared
  547. self assert: 4 squared = 16
  548. !
  549. testTimesRepeat
  550. | i |
  551. i := 0.
  552. 0 timesRepeat: [i := i + 1].
  553. self assert: i equals: 0.
  554. 5 timesRepeat: [i := i + 1].
  555. self assert: i equals: 5
  556. !
  557. testTo
  558. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  559. !
  560. testToBy
  561. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  562. self should: [1 to: 4 by: 0] raise: Error
  563. !
  564. testTruncated
  565. self assert: 3 truncated = 3.
  566. self assert: 3.212 truncated = 3.
  567. self assert: 3.51 truncated = 3
  568. ! !
  569. Object subclass: #ObjectMock
  570. instanceVariableNames: 'foo bar'
  571. package: 'Kernel-Tests'!
  572. !ObjectMock methodsFor: 'not yet classified'!
  573. foo
  574. ^foo
  575. !
  576. foo: anObject
  577. foo := anObject
  578. ! !
  579. TestCase subclass: #ObjectTest
  580. instanceVariableNames: ''
  581. package: 'Kernel-Tests'!
  582. !ObjectTest methodsFor: 'tests'!
  583. testBasicAccess
  584. | o |
  585. o := Object new.
  586. o basicAt: 'a' put: 1.
  587. self assert: (o basicAt: 'a') equals: 1.
  588. self assert: (o basicAt: 'b') equals: nil
  589. !
  590. testBasicPerform
  591. | o |
  592. o := Object new.
  593. o basicAt: 'func' put: ['hello'].
  594. o basicAt: 'func2' put: [:a | a + 1].
  595. self assert: (o basicPerform: 'func') equals: 'hello'.
  596. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  597. !
  598. testDNU
  599. self should: [Object new foo] raise: MessageNotUnderstood
  600. !
  601. testEquality
  602. | o |
  603. o := Object new.
  604. self deny: o = Object new.
  605. self assert: o = o.
  606. self assert: o yourself = o.
  607. self assert: o = o yourself
  608. !
  609. testHalt
  610. self should: [Object new halt] raise: Error
  611. !
  612. testIdentity
  613. | o |
  614. o := Object new.
  615. self deny: o == Object new.
  616. self assert: o == o.
  617. self assert: o yourself == o.
  618. self assert: o == o yourself
  619. !
  620. testIfNil
  621. self deny: Object new isNil.
  622. self deny: (Object new ifNil: [true]) = true.
  623. self assert: (Object new ifNotNil: [true]) = true.
  624. self assert: (Object new ifNil: [false] ifNotNil: [true]) = true.
  625. self assert: (Object new ifNotNil: [true] ifNil: [false]) = true
  626. !
  627. testInstVars
  628. | o |
  629. o := ObjectMock new.
  630. self assert: (o instVarAt: #foo) equals: nil.
  631. o instVarAt: #foo put: 1.
  632. self assert: (o instVarAt: #foo) equals: 1.
  633. self assert: (o instVarAt: 'foo') equals: 1
  634. !
  635. testNilUndefined
  636. "nil in Smalltalk is the undefined object in JS"
  637. self assert: nil = undefined
  638. !
  639. testYourself
  640. | o |
  641. o := ObjectMock new.
  642. self assert: o yourself == o
  643. !
  644. testidentityHash
  645. | o1 o2 |
  646. o1 := Object new.
  647. o2 := Object new.
  648. self assert: o1 identityHash == o1 identityHash.
  649. self deny: o1 identityHash == o2 identityHash
  650. ! !
  651. TestCase subclass: #PackageTest
  652. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  653. package: 'Kernel-Tests'!
  654. !PackageTest methodsFor: 'running'!
  655. setUp
  656. backUpCommitPathJs := Package defaultCommitPathJs.
  657. backUpCommitPathSt := Package defaultCommitPathSt.
  658. Package resetCommitPaths.
  659. zorkPackage := Package new name: 'Zork'.
  660. grulPackage := Package new
  661. name: 'Grul';
  662. commitPathJs: 'server/grul/js';
  663. commitPathSt: 'grul/st';
  664. yourself
  665. !
  666. tearDown
  667. Package
  668. defaultCommitPathJs: backUpCommitPathJs;
  669. defaultCommitPathSt: backUpCommitPathSt
  670. ! !
  671. !PackageTest methodsFor: 'tests'!
  672. testGrulCommitPathJsShouldBeServerGrulJs
  673. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  674. !
  675. testGrulCommitPathStShouldBeGrulSt
  676. self assert: 'grul/st' equals: grulPackage commitPathSt
  677. !
  678. testZorkCommitPathJsShouldBeJs
  679. self assert: 'js' equals: zorkPackage commitPathJs
  680. !
  681. testZorkCommitPathStShouldBeSt
  682. self assert: 'st' equals: zorkPackage commitPathSt
  683. ! !
  684. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  685. instanceVariableNames: ''
  686. package: 'Kernel-Tests'!
  687. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  688. setUp
  689. super setUp.
  690. Package
  691. defaultCommitPathJs: 'javascripts/';
  692. defaultCommitPathSt: 'smalltalk/'.
  693. ! !
  694. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  695. testGrulCommitPathJsShouldBeServerGrulJs
  696. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  697. !
  698. testGrulCommitPathStShouldBeGrulSt
  699. self assert: 'grul/st' equals: grulPackage commitPathSt
  700. !
  701. testZorkCommitPathJsShouldBeJavascript
  702. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  703. !
  704. testZorkCommitPathStShouldBeSmalltalk
  705. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  706. ! !
  707. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  708. shouldInheritSelectors
  709. ^ false
  710. ! !
  711. TestCase subclass: #PointTest
  712. instanceVariableNames: ''
  713. package: 'Kernel-Tests'!
  714. !PointTest methodsFor: 'tests'!
  715. testAccessing
  716. self assert: (Point x: 3 y: 4) x equals: 3.
  717. self assert: (Point x: 3 y: 4) y equals: 4.
  718. self assert: (Point new x: 3) x equals: 3.
  719. self assert: (Point new y: 4) y equals: 4
  720. !
  721. testArithmetic
  722. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  723. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  724. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  725. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  726. !
  727. testAt
  728. self assert: 3@4 equals: (Point x: 3 y: 4)
  729. !
  730. testEgality
  731. self assert: 3@4 = (3@4).
  732. self deny: 3@5 = (3@6)
  733. !
  734. testTranslateBy
  735. self assert: 3@4 equals: (3@3 translateBy: 0@1).
  736. self assert: 3@2 equals: (3@3 translateBy: 0@1 negated).
  737. self assert: 5@6 equals: (3@3 translateBy: 2@3).
  738. self assert: 0@3 equals: (3@3 translateBy: 3 negated @0).
  739. ! !
  740. TestCase subclass: #RandomTest
  741. instanceVariableNames: ''
  742. package: 'Kernel-Tests'!
  743. !RandomTest methodsFor: 'tests'!
  744. textNext
  745. 10000 timesRepeat: [
  746. | current next |
  747. next := Random new next.
  748. self assert: (next >= 0).
  749. self assert: (next < 1).
  750. self deny: current = next.
  751. next = current]
  752. ! !
  753. TestCase subclass: #SetTest
  754. instanceVariableNames: ''
  755. package: 'Kernel-Tests'!
  756. !SetTest methodsFor: 'tests'!
  757. testAddRemove
  758. | set |
  759. set := Set new.
  760. self assert: set isEmpty.
  761. set add: 3.
  762. self assert: (set includes: 3).
  763. set add: 5.
  764. self assert: (set includes: 5).
  765. set remove: 3.
  766. self deny: (set includes: 3)
  767. !
  768. testAt
  769. self should: [Set new at: 1 put: 2] raise: Error
  770. !
  771. testPrintString
  772. | set |
  773. set := Set new.
  774. self assert: 'a Set ()' equals: ( set printString ).
  775. set add: 1; add: 3.
  776. self assert: 'a Set (1 3)' equals: ( set printString ).
  777. set add: 'foo'.
  778. self assert: 'a Set (1 3 ''foo'')' equals: ( set printString ).
  779. set remove: 1; remove: 3.
  780. self assert: 'a Set (''foo'')' equals: ( set printString ).
  781. set add: 3.
  782. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  783. set add: 3.
  784. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  785. !
  786. testSize
  787. self assert: Set new size equals: 0.
  788. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  789. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  790. !
  791. testUnicity
  792. | set |
  793. set := Set new.
  794. set add: 21.
  795. set add: 'hello'.
  796. set add: 21.
  797. self assert: set size = 2.
  798. set add: 'hello'.
  799. self assert: set size = 2.
  800. self assert: set asArray equals: #(21 'hello')
  801. ! !
  802. TestCase subclass: #StringTest
  803. instanceVariableNames: ''
  804. package: 'Kernel-Tests'!
  805. !StringTest methodsFor: 'tests'!
  806. testAddRemove
  807. self should: ['hello' add: 'a'] raise: Error.
  808. self should: ['hello' remove: 'h'] raise: Error
  809. !
  810. testAsArray
  811. self assert: 'hello' asArray = #('h' 'e' 'l' 'l' 'o').
  812. !
  813. testAt
  814. self assert: ('hello' at: 1) = 'h'.
  815. self assert: ('hello' at: 5) = 'o'.
  816. self assert: ('hello' at: 6 ifAbsent: [nil]) = nil
  817. !
  818. testAtPut
  819. "String instances are read-only"
  820. self should: ['hello' at: 1 put: 'a'] raise: Error
  821. !
  822. testCopyWithoutAll
  823. self
  824. assert: 'hello world'
  825. equals: ('*hello* *world*' copyWithoutAll: '*')
  826. !
  827. testEquality
  828. self assert: 'hello' = 'hello'.
  829. self deny: 'hello' = 'world'.
  830. self assert: 'hello' = 'hello' yourself.
  831. self assert: 'hello' yourself = 'hello'.
  832. "test JS falsy value"
  833. self deny: '' = 0
  834. !
  835. testIdentity
  836. self assert: 'hello' == 'hello'.
  837. self deny: 'hello' == 'world'.
  838. self assert: 'hello' == 'hello' yourself.
  839. self assert: 'hello' yourself == 'hello'.
  840. "test JS falsy value"
  841. self deny: '' == 0
  842. !
  843. testIncludesSubString
  844. self assert: ('amber' includesSubString: 'ber').
  845. self deny: ('amber' includesSubString: 'zork').
  846. !
  847. testJoin
  848. self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
  849. !
  850. testPrintString
  851. self assert: '''test_string''' equals: 'test_string' printString.
  852. !
  853. testSize
  854. self assert: 'smalltalk' size equals: 9.
  855. self assert: '' size equals: 0
  856. !
  857. testStreamContents
  858. self
  859. assert: 'hello world'
  860. equals: (String streamContents: [:aStream| aStream
  861. nextPutAll: 'hello'; space;
  862. nextPutAll: 'world'])
  863. ! !
  864. TestCase subclass: #SymbolTest
  865. instanceVariableNames: ''
  866. package: 'Kernel-Tests'!
  867. !SymbolTest methodsFor: 'tests'!
  868. testAsString
  869. self assert: #hello asString equals: 'hello'
  870. !
  871. testAsSymbol
  872. self assert: #hello == #hello asSymbol
  873. !
  874. testAt
  875. self assert: (#hello at: 1) = 'h'.
  876. self assert: (#hello at: 5) = 'o'.
  877. self assert: (#hello at: 6 ifAbsent: [nil]) = nil
  878. !
  879. testAtPut
  880. "Symbol instances are read-only"
  881. self should: ['hello' at: 1 put: 'a'] raise: Error
  882. !
  883. testComparing
  884. self assert: #ab > #aa.
  885. self deny: #ab > #ba.
  886. self assert: #ab < #ba.
  887. self deny: #bb < #ba.
  888. self assert: #ab >= #aa.
  889. self deny: #ab >= #ba.
  890. self assert: #ab <= #ba.
  891. self deny: #bb <= #ba
  892. !
  893. testCopying
  894. self assert: #hello copy == #hello.
  895. self assert: #hello deepCopy == #hello
  896. !
  897. testEquality
  898. self assert: #hello = #hello.
  899. self deny: #hello = #world.
  900. self assert: #hello = #hello yourself.
  901. self assert: #hello yourself = #hello.
  902. self deny: #hello = 'hello'.
  903. self deny: 'hello' = #hello.
  904. !
  905. testIdentity
  906. self assert: #hello == #hello.
  907. self deny: #hello == #world.
  908. self assert: #hello = #hello yourself.
  909. self assert: #hello yourself = #hello asString asSymbol
  910. !
  911. testIsSymbolIsString
  912. self assert: #hello isSymbol.
  913. self deny: 'hello' isSymbol.
  914. self deny: #hello isString.
  915. self assert: 'hello' isString
  916. !
  917. testPrintString
  918. self assert: '#symbol' equals: #symbol printString.
  919. !
  920. testSize
  921. self assert: #a size equals: 1.
  922. self assert: #aaaaa size equals: 5
  923. ! !
  924. TestCase subclass: #UndefinedTest
  925. instanceVariableNames: ''
  926. package: 'Kernel-Tests'!
  927. !UndefinedTest methodsFor: 'tests'!
  928. testCopying
  929. self assert: nil copy equals: nil
  930. !
  931. testDeepCopy
  932. self assert: nil deepCopy = nil
  933. !
  934. testIfNil
  935. self assert: (nil ifNil: [true]) equals: true.
  936. self deny: (nil ifNotNil: [true]) = true.
  937. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  938. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  939. !
  940. testIsNil
  941. self assert: nil isNil.
  942. self deny: nil notNil.
  943. ! !