Kernel-Tests.st 31 KB

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