Kernel-Tests.st 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. testSize
  783. self assert: 'smalltalk' size equals: 9.
  784. self assert: '' size equals: 0
  785. !
  786. testStreamContents
  787. self
  788. assert: 'hello world'
  789. equals: (String streamContents: [:aStream| aStream
  790. nextPutAll: 'hello'; space;
  791. nextPutAll: 'world'])
  792. ! !
  793. TestCase subclass: #SymbolTest
  794. instanceVariableNames: ''
  795. package: 'Kernel-Tests'!
  796. !SymbolTest methodsFor: 'tests'!
  797. testAsString
  798. self assert: #hello asString equals: 'hello'
  799. !
  800. testAsSymbol
  801. self assert: #hello == #hello asSymbol
  802. !
  803. testAt
  804. self assert: (#hello at: 1) = 'h'.
  805. self assert: (#hello at: 5) = 'o'.
  806. self assert: (#hello at: 6 ifAbsent: [nil]) = nil
  807. !
  808. testAtPut
  809. "Symbol instances are read-only"
  810. self should: ['hello' at: 1 put: 'a'] raise: Error
  811. !
  812. testComparing
  813. self assert: #ab > #aa.
  814. self deny: #ab > #ba.
  815. self assert: #ab < #ba.
  816. self deny: #bb < #ba.
  817. self assert: #ab >= #aa.
  818. self deny: #ab >= #ba.
  819. self assert: #ab <= #ba.
  820. self deny: #bb <= #ba
  821. !
  822. testCopying
  823. self assert: #hello copy == #hello.
  824. self assert: #hello deepCopy == #hello
  825. !
  826. testEquality
  827. self assert: #hello = #hello.
  828. self deny: #hello = #world.
  829. self assert: #hello = #hello yourself.
  830. self assert: #hello yourself = #hello.
  831. self deny: #hello = 'hello'.
  832. self deny: 'hello' = #hello.
  833. !
  834. testIdentity
  835. self assert: #hello == #hello.
  836. self deny: #hello == #world.
  837. self assert: #hello = #hello yourself.
  838. self assert: #hello yourself = #hello asString asSymbol
  839. !
  840. testIsSymbolIsString
  841. self assert: #hello isSymbol.
  842. self deny: 'hello' isSymbol.
  843. self deny: #hello isString.
  844. self assert: 'hello' isString
  845. !
  846. testSize
  847. self assert: #a size equals: 1.
  848. self assert: #aaaaa size equals: 5
  849. ! !
  850. TestCase subclass: #UndefinedTest
  851. instanceVariableNames: ''
  852. package: 'Kernel-Tests'!
  853. !UndefinedTest methodsFor: 'tests'!
  854. testCopying
  855. self assert: nil copy equals: nil
  856. !
  857. testDeepCopy
  858. self assert: nil deepCopy = nil
  859. !
  860. testIfNil
  861. self assert: (nil ifNil: [true]) equals: true.
  862. self deny: (nil ifNotNil: [true]) = true.
  863. self assert: (nil ifNil: [true] ifNotNil: [false]) equals: true.
  864. self deny: (nil ifNotNil: [true] ifNil: [false]) = true
  865. !
  866. testIsNil
  867. self assert: nil isNil.
  868. self deny: nil notNil.
  869. ! !