Kernel-Tests.st 24 KB

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