Kernel-Tests.st 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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: #DictionaryTest
  237. instanceVariableNames: ''
  238. package: 'Kernel-Tests'!
  239. !DictionaryTest methodsFor: 'tests'!
  240. testAccessing
  241. | d |
  242. d := Dictionary new.
  243. d at: 'hello' put: 'world'.
  244. self assert: (d at: 'hello') = 'world'.
  245. self assert: (d at: 'hello' ifAbsent: [nil]) = 'world'.
  246. self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
  247. d at: 1 put: 2.
  248. self assert: (d at: 1) = 2.
  249. d at: 1@3 put: 3.
  250. self assert: (d at: 1@3) = 3
  251. !
  252. testDynamicDictionaries
  253. self assert: #{'hello' -> 1} asDictionary = (Dictionary with: 'hello' -> 1)
  254. !
  255. testEquality
  256. | d1 d2 |
  257. self assert: Dictionary new = Dictionary new.
  258. d1 := Dictionary new at: 1 put: 2; yourself.
  259. d2 := Dictionary new at: 1 put: 2; yourself.
  260. self assert: d1 = d2.
  261. d2 := Dictionary new at: 1 put: 3; yourself.
  262. self deny: d1 = d2.
  263. d2 := Dictionary new at: 2 put: 2; yourself.
  264. self deny: d1 = d2.
  265. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  266. self deny: d1 = d2.
  267. !
  268. testIfAbsent
  269. | d visited |
  270. visited := false.
  271. d := Dictionary new.
  272. d at: 'hello' ifAbsent: [ visited := true ].
  273. self assert: visited.
  274. !
  275. testIfPresent
  276. | d visited absent |
  277. visited := false.
  278. d := Dictionary new.
  279. d at: 'hello' put: 'world'.
  280. d at: 'hello' ifPresent: [ :value | visited := value ].
  281. self assert: visited = 'world'.
  282. absent := d at: 'bye' ifPresent: [ :value | visited := value ].
  283. self assert: absent isNil.
  284. !
  285. testIfPresentIfAbsent
  286. | d visited |
  287. visited := false.
  288. d := Dictionary new.
  289. d at: 'hello' put: 'world'.
  290. d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  291. self assert: visited = 'world'.
  292. d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
  293. self assert: visited.
  294. !
  295. testKeys
  296. | d |
  297. d := Dictionary new.
  298. d at: 1 put: 2.
  299. d at: 2 put: 3.
  300. d at: 3 put: 4.
  301. self assert: d keys = #(1 2 3)
  302. !
  303. testPrintString
  304. self
  305. assert: 'a Dictionary(''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  306. equals: (Dictionary new
  307. at:'firstname' put: 'James';
  308. at:'lastname' put: 'Bond';
  309. printString)
  310. !
  311. testRemoveKey
  312. | d key |
  313. d := Dictionary new.
  314. d at: 1 put: 2.
  315. d at: 2 put: 3.
  316. d at: 3 put: 4.
  317. key := 2.
  318. self assert: d keys = #(1 2 3).
  319. d removeKey: key.
  320. self assert: d keys = #(1 3).
  321. self assert: d values = #(2 4).
  322. self deny: (d includesKey: 2)
  323. !
  324. testRemoveKeyIfAbsent
  325. | d key |
  326. d := Dictionary new.
  327. d at: 1 put: 2.
  328. d at: 2 put: 3.
  329. d at: 3 put: 4.
  330. key := 2.
  331. self assert: (d removeKey: key) = 3.
  332. key := 3.
  333. self assert: (d removeKey: key ifAbsent: [42]) = 4.
  334. key := 'why'.
  335. self assert: (d removeKey: key ifAbsent: [42] ) = 42.
  336. !
  337. testSize
  338. | d |
  339. d := Dictionary new.
  340. self assert: d size = 0.
  341. d at: 1 put: 2.
  342. self assert: d size = 1.
  343. d at: 2 put: 3.
  344. self assert: d size = 2.
  345. !
  346. testValues
  347. | d |
  348. d := Dictionary new.
  349. d at: 1 put: 2.
  350. d at: 2 put: 3.
  351. d at: 3 put: 4.
  352. self assert: d values = #(2 3 4)
  353. ! !
  354. TestCase subclass: #JSObjectProxyTest
  355. instanceVariableNames: ''
  356. package: 'Kernel-Tests'!
  357. !JSObjectProxyTest methodsFor: 'accessing'!
  358. jsObject
  359. <return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}}>
  360. ! !
  361. !JSObjectProxyTest methodsFor: 'tests'!
  362. testDNU
  363. self should: [self jsObject foo] raise: MessageNotUnderstood
  364. !
  365. testMessageSend
  366. self assert: self jsObject a equals: 1.
  367. self assert: self jsObject b equals: 2.
  368. self assert: (self jsObject c: 3) equals: 3
  369. !
  370. testMethodWithArguments
  371. self deny: ('body' asJQuery hasClass: 'amber').
  372. 'body' asJQuery addClass: 'amber'.
  373. self assert: ('body' asJQuery hasClass: 'amber').
  374. 'body' asJQuery removeClass: 'amber'.
  375. self deny: ('body' asJQuery hasClass: 'amber').
  376. !
  377. testPrinting
  378. self assert: self jsObject printString = '[object Object]'
  379. !
  380. testPropertyThatReturnsEmptyString
  381. <document.location.hash = ''>.
  382. self assert: '' equals: document location hash.
  383. document location hash: 'test'.
  384. self assert: '#test' equals: document location hash.
  385. !
  386. testYourself
  387. |body|
  388. body := 'body' asJQuery
  389. addClass: 'amber';
  390. yourself.
  391. self assert: (body hasClass: 'amber').
  392. body removeClass: 'amber'.
  393. self deny: (body hasClass: 'amber').
  394. ! !
  395. TestCase subclass: #NumberTest
  396. instanceVariableNames: ''
  397. package: 'Kernel-Tests'!
  398. !NumberTest methodsFor: 'tests'!
  399. testArithmetic
  400. "We rely on JS here, so we won't test complex behavior, just check if
  401. message sends are corrects"
  402. self assert: 1.5 + 1 = 2.5.
  403. self assert: 2 - 1 = 1.
  404. self assert: -2 - 1 = -3.
  405. self assert: 12 / 2 = 6.
  406. self assert: 3 * 4 = 12.
  407. "Simple parenthesis and execution order"
  408. self assert: 1 + 2 * 3 = 9.
  409. self assert: 1 + (2 * 3) = 7
  410. !
  411. testComparison
  412. self assert: 3 > 2.
  413. self assert: 2 < 3.
  414. self deny: 3 < 2.
  415. self deny: 2 > 3.
  416. self assert: 3 >= 3.
  417. self assert: 3.1 >= 3.
  418. self assert: 3 <= 3.
  419. self assert: 3 <= 3.1
  420. !
  421. testCopying
  422. self assert: 1 copy == 1.
  423. self assert: 1 deepCopy == 1
  424. !
  425. testEquality
  426. self assert: 1 = 1.
  427. self assert: 0 = 0.
  428. self deny: 1 = 0.
  429. self assert: 1 yourself = 1.
  430. self assert: 1 = 1 yourself.
  431. self assert: 1 yourself = 1 yourself.
  432. self deny: 0 = false.
  433. self deny: false = 0.
  434. self deny: '' = 0.
  435. self deny: 0 = ''
  436. !
  437. testIdentity
  438. self assert: 1 == 1.
  439. self assert: 0 == 0.
  440. self deny: 1 == 0.
  441. self assert: 1 yourself == 1.
  442. self assert: 1 == 1 yourself.
  443. self assert: 1 yourself == 1 yourself.
  444. self deny: 1 == 2
  445. !
  446. testMinMax
  447. self assert: (2 max: 5) equals: 5.
  448. self assert: (2 min: 5) equals: 2
  449. !
  450. testNegated
  451. self assert: 3 negated = -3.
  452. self assert: -3 negated = 3
  453. !
  454. testPrintShowingDecimalPlaces
  455. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  456. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  457. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  458. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  459. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  460. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  461. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  462. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  463. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  464. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  465. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  466. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  467. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  468. !
  469. testRounded
  470. self assert: 3 rounded = 3.
  471. self assert: 3.212 rounded = 3.
  472. self assert: 3.51 rounded = 4
  473. !
  474. testSqrt
  475. self assert: 4 sqrt = 2.
  476. self assert: 16 sqrt = 4
  477. !
  478. testSquared
  479. self assert: 4 squared = 16
  480. !
  481. testTimesRepeat
  482. | i |
  483. i := 0.
  484. 0 timesRepeat: [i := i + 1].
  485. self assert: i equals: 0.
  486. 5 timesRepeat: [i := i + 1].
  487. self assert: i equals: 5
  488. !
  489. testTo
  490. self assert: (1 to: 5) equals: #(1 2 3 4 5)
  491. !
  492. testToBy
  493. self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
  494. self should: [1 to: 4 by: 0] raise: Error
  495. !
  496. testTruncated
  497. self assert: 3 truncated = 3.
  498. self assert: 3.212 truncated = 3.
  499. self assert: 3.51 truncated = 3
  500. ! !
  501. Object subclass: #ObjectMock
  502. instanceVariableNames: 'foo bar'
  503. package: 'Kernel-Tests'!
  504. !ObjectMock methodsFor: 'not yet classified'!
  505. foo
  506. ^foo
  507. !
  508. foo: anObject
  509. foo := anObject
  510. ! !
  511. TestCase subclass: #ObjectTest
  512. instanceVariableNames: ''
  513. package: 'Kernel-Tests'!
  514. !ObjectTest methodsFor: 'tests'!
  515. testBasicAccess
  516. | o |
  517. o := Object new.
  518. o basicAt: 'a' put: 1.
  519. self assert: (o basicAt: 'a') equals: 1.
  520. self assert: (o basicAt: 'b') equals: nil
  521. !
  522. testBasicPerform
  523. | o |
  524. o := Object new.
  525. o basicAt: 'func' put: ['hello'].
  526. o basicAt: 'func2' put: [:a | a + 1].
  527. self assert: (o basicPerform: 'func') equals: 'hello'.
  528. self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
  529. !
  530. testDNU
  531. self should: [Object new foo] raise: MessageNotUnderstood
  532. !
  533. testEquality
  534. | o |
  535. o := Object new.
  536. self deny: o = Object new.
  537. self assert: o = o.
  538. self assert: o yourself = o.
  539. self assert: o = o yourself
  540. !
  541. testHalt
  542. self should: [Object new halt] raise: Error
  543. !
  544. testIdentity
  545. | o |
  546. o := Object new.
  547. self deny: o == Object new.
  548. self assert: o == o.
  549. self assert: o yourself == o.
  550. self assert: o == o yourself
  551. !
  552. testIfNil
  553. self deny: Object new isNil.
  554. self deny: (Object new ifNil: [true]) = true.
  555. self assert: (Object new ifNotNil: [true]) = true.
  556. self assert: (Object new ifNil: [false] ifNotNil: [true]) = true.
  557. self assert: (Object new ifNotNil: [true] ifNil: [false]) = true
  558. !
  559. testInstVars
  560. | o |
  561. o := ObjectMock new.
  562. self assert: (o instVarAt: #foo) equals: nil.
  563. o instVarAt: #foo put: 1.
  564. self assert: (o instVarAt: #foo) equals: 1.
  565. self assert: (o instVarAt: 'foo') equals: 1
  566. !
  567. testNilUndefined
  568. "nil in Smalltalk is the undefined object in JS"
  569. self assert: nil = undefined
  570. !
  571. testYourself
  572. | o |
  573. o := ObjectMock new.
  574. self assert: o yourself == o
  575. !
  576. testidentityHash
  577. | o1 o2 |
  578. o1 := Object new.
  579. o2 := Object new.
  580. self assert: o1 identityHash == o1 identityHash.
  581. self deny: o1 identityHash == o2 identityHash
  582. ! !
  583. TestCase subclass: #PackageTest
  584. instanceVariableNames: 'zorkPackage grulPackage backUpCommitPathJs backUpCommitPathSt'
  585. package: 'Kernel-Tests'!
  586. !PackageTest methodsFor: 'running'!
  587. setUp
  588. backUpCommitPathJs := Package defaultCommitPathJs.
  589. backUpCommitPathSt := Package defaultCommitPathSt.
  590. Package resetCommitPaths.
  591. zorkPackage := Package new name: 'Zork'.
  592. grulPackage := Package new
  593. name: 'Grul';
  594. commitPathJs: 'server/grul/js';
  595. commitPathSt: 'grul/st';
  596. yourself
  597. !
  598. tearDown
  599. Package
  600. defaultCommitPathJs: backUpCommitPathJs;
  601. defaultCommitPathSt: backUpCommitPathSt
  602. ! !
  603. !PackageTest methodsFor: 'tests'!
  604. testGrulCommitPathJsShouldBeServerGrulJs
  605. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  606. !
  607. testGrulCommitPathStShouldBeGrulSt
  608. self assert: 'grul/st' equals: grulPackage commitPathSt
  609. !
  610. testZorkCommitPathJsShouldBeJs
  611. self assert: 'js' equals: zorkPackage commitPathJs
  612. !
  613. testZorkCommitPathStShouldBeSt
  614. self assert: 'st' equals: zorkPackage commitPathSt
  615. ! !
  616. PackageTest subclass: #PackageWithDefaultCommitPathChangedTest
  617. instanceVariableNames: ''
  618. package: 'Kernel-Tests'!
  619. !PackageWithDefaultCommitPathChangedTest methodsFor: 'running'!
  620. setUp
  621. super setUp.
  622. Package
  623. defaultCommitPathJs: 'javascripts/';
  624. defaultCommitPathSt: 'smalltalk/'.
  625. ! !
  626. !PackageWithDefaultCommitPathChangedTest methodsFor: 'tests'!
  627. testGrulCommitPathJsShouldBeServerGrulJs
  628. self assert: 'server/grul/js' equals: grulPackage commitPathJs
  629. !
  630. testGrulCommitPathStShouldBeGrulSt
  631. self assert: 'grul/st' equals: grulPackage commitPathSt
  632. !
  633. testZorkCommitPathJsShouldBeJavascript
  634. self assert: 'javascripts/' equals: zorkPackage commitPathJs
  635. !
  636. testZorkCommitPathStShouldBeSmalltalk
  637. self assert: 'smalltalk/' equals: zorkPackage commitPathSt
  638. ! !
  639. !PackageWithDefaultCommitPathChangedTest class methodsFor: 'accessing'!
  640. shouldInheritSelectors
  641. ^ false
  642. ! !
  643. TestCase subclass: #PointTest
  644. instanceVariableNames: ''
  645. package: 'Kernel-Tests'!
  646. !PointTest methodsFor: 'tests'!
  647. testAccessing
  648. self assert: (Point x: 3 y: 4) x equals: 3.
  649. self assert: (Point x: 3 y: 4) y equals: 4.
  650. self assert: (Point new x: 3) x equals: 3.
  651. self assert: (Point new y: 4) y equals: 4
  652. !
  653. testArithmetic
  654. self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
  655. self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
  656. self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
  657. self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
  658. !
  659. testAt
  660. self assert: 3@4 equals: (Point x: 3 y: 4)
  661. !
  662. testEgality
  663. self assert: 3@4 = (3@4).
  664. self deny: 3@5 = (3@6)
  665. !
  666. testTranslateBy
  667. self assert: 3@4 equals: (3@3 translateBy: 0@1).
  668. self assert: 3@2 equals: (3@3 translateBy: 0@1 negated).
  669. self assert: 5@6 equals: (3@3 translateBy: 2@3).
  670. self assert: 0@3 equals: (3@3 translateBy: 3 negated @0).
  671. ! !
  672. TestCase subclass: #RandomTest
  673. instanceVariableNames: ''
  674. package: 'Kernel-Tests'!
  675. !RandomTest methodsFor: 'tests'!
  676. textNext
  677. 10000 timesRepeat: [
  678. | current next |
  679. next := Random new next.
  680. self assert: (next >= 0).
  681. self assert: (next < 1).
  682. self deny: current = next.
  683. next = current]
  684. ! !
  685. TestCase subclass: #SetTest
  686. instanceVariableNames: ''
  687. package: 'Kernel-Tests'!
  688. !SetTest methodsFor: 'tests'!
  689. testAddRemove
  690. | set |
  691. set := Set new.
  692. self assert: set isEmpty.
  693. set add: 3.
  694. self assert: (set includes: 3).
  695. set add: 5.
  696. self assert: (set includes: 5).
  697. set remove: 3.
  698. self deny: (set includes: 3)
  699. !
  700. testAt
  701. self should: [Set new at: 1 put: 2] raise: Error
  702. !
  703. testPrintString
  704. | set |
  705. set := Set new.
  706. self assert: 'a Set ()' equals: ( set printString ).
  707. set add: 1; add: 3.
  708. self assert: 'a Set (1 3)' equals: ( set printString ).
  709. set add: 'foo'.
  710. self assert: 'a Set (1 3 ''foo'')' equals: ( set printString ).
  711. set remove: 1; remove: 3.
  712. self assert: 'a Set (''foo'')' equals: ( set printString ).
  713. set add: 3.
  714. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  715. set add: 3.
  716. self assert: 'a Set (''foo'' 3)' equals: ( set printString ).
  717. !
  718. testSize
  719. self assert: Set new size equals: 0.
  720. self assert: (Set withAll: #(1 2 3 4)) size equals: 4.
  721. self assert: (Set withAll: #(1 1 1 1)) size equals: 1
  722. !
  723. testUnicity
  724. | set |
  725. set := Set new.
  726. set add: 21.
  727. set add: 'hello'.
  728. set add: 21.
  729. self assert: set size = 2.
  730. set add: 'hello'.
  731. self assert: set size = 2.
  732. self assert: set asArray equals: #(21 'hello')
  733. ! !
  734. TestCase subclass: #StringTest
  735. instanceVariableNames: ''
  736. package: 'Kernel-Tests'!
  737. !StringTest methodsFor: 'tests'!
  738. testAddRemove
  739. self should: ['hello' add: 'a'] raise: Error.
  740. self should: ['hello' remove: 'h'] raise: Error
  741. !
  742. testAsArray
  743. self assert: 'hello' asArray = #('h' 'e' 'l' 'l' 'o').
  744. !
  745. testAt
  746. self assert: ('hello' at: 1) = 'h'.
  747. self assert: ('hello' at: 5) = 'o'.
  748. self assert: ('hello' at: 6 ifAbsent: [nil]) = nil
  749. !
  750. testAtPut
  751. "String instances are read-only"
  752. self should: ['hello' at: 1 put: 'a'] raise: Error
  753. !
  754. testCopyWithoutAll
  755. self
  756. assert: 'hello world'
  757. equals: ('*hello* *world*' copyWithoutAll: '*')
  758. !
  759. testEquality
  760. self assert: 'hello' = 'hello'.
  761. self deny: 'hello' = 'world'.
  762. self assert: 'hello' = 'hello' yourself.
  763. self assert: 'hello' yourself = 'hello'.
  764. "test JS falsy value"
  765. self deny: '' = 0
  766. !
  767. testIdentity
  768. self assert: 'hello' == 'hello'.
  769. self deny: 'hello' == 'world'.
  770. self assert: 'hello' == 'hello' yourself.
  771. self assert: 'hello' yourself == 'hello'.
  772. "test JS falsy value"
  773. self deny: '' == 0
  774. !
  775. testIncludesSubString
  776. self assert: ('amber' includesSubString: 'ber').
  777. self deny: ('amber' includesSubString: 'zork').
  778. !
  779. testJoin
  780. self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
  781. !
  782. testPrintString
  783. self assert: '''test_string''' equals: 'test_string' printString.
  784. !
  785. testSize
  786. self assert: 'smalltalk' size equals: 9.
  787. self assert: '' size equals: 0
  788. !
  789. testStreamContents
  790. self
  791. assert: 'hello world'
  792. equals: (String streamContents: [:aStream| aStream
  793. nextPutAll: 'hello'; space;
  794. nextPutAll: 'world'])
  795. ! !
  796. TestCase subclass: #SymbolTest
  797. instanceVariableNames: ''
  798. package: 'Kernel-Tests'!
  799. !SymbolTest methodsFor: 'tests'!
  800. testAsString
  801. self assert: #hello asString equals: 'hello'
  802. !
  803. testAsSymbol
  804. self assert: #hello == #hello asSymbol
  805. !
  806. testAt
  807. self assert: (#hello at: 1) = 'h'.
  808. self assert: (#hello at: 5) = 'o'.
  809. self assert: (#hello at: 6 ifAbsent: [nil]) = nil
  810. !
  811. testAtPut
  812. "Symbol instances are read-only"
  813. self should: ['hello' at: 1 put: 'a'] raise: Error
  814. !
  815. testComparing
  816. self assert: #ab > #aa.
  817. self deny: #ab > #ba.
  818. self assert: #ab < #ba.
  819. self deny: #bb < #ba.
  820. self assert: #ab >= #aa.
  821. self deny: #ab >= #ba.
  822. self assert: #ab <= #ba.
  823. self deny: #bb <= #ba
  824. !
  825. testCopying
  826. self assert: #hello copy == #hello.
  827. self assert: #hello deepCopy == #hello
  828. !
  829. testEquality
  830. self assert: #hello = #hello.
  831. self deny: #hello = #world.
  832. self assert: #hello = #hello yourself.
  833. self assert: #hello yourself = #hello.
  834. self deny: #hello = 'hello'.
  835. self deny: 'hello' = #hello.
  836. !
  837. testIdentity
  838. self assert: #hello == #hello.
  839. self deny: #hello == #world.
  840. self assert: #hello = #hello yourself.
  841. self assert: #hello yourself = #hello asString asSymbol
  842. !
  843. testIsSymbolIsString
  844. self assert: #hello isSymbol.
  845. self deny: 'hello' isSymbol.
  846. self deny: #hello isString.
  847. self assert: 'hello' isString
  848. !
  849. testSize
  850. self assert: #a size equals: 1.
  851. self assert: #aaaaa size equals: 5
  852. ! !
  853. TestCase subclass: #UndefinedTest
  854. instanceVariableNames: ''
  855. package: 'Kernel-Tests'!
  856. !UndefinedTest methodsFor: 'tests'!
  857. testCopying
  858. self assert: nil copy equals: nil
  859. !
  860. testDeepCopy
  861. self assert: nil deepCopy = nil
  862. !
  863. testIfNil
  864. self assert: (nil ifNil: [true]) equals: true.
  865. self deny: (nil ifNotNil: [true]) = true.
  866. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  867. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  868. !
  869. testIsNil
  870. self assert: nil isNil.
  871. self deny: nil notNil.
  872. ! !