Kernel-Tests.st 31 KB

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