Kernel-Tests.st 26 KB

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