Kernel-Tests.st 25 KB

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