Kernel-Tests.st 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. Smalltalk current createPackage: 'Kernel-Tests' properties: #{}!
  2. TestCase subclass: #StringTest
  3. instanceVariableNames: ''
  4. category: 'Kernel-Tests'!
  5. !StringTest methodsFor: 'tests'!
  6. testJoin
  7. self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
  8. !
  9. testStreamContents
  10. self
  11. assert: 'hello world'
  12. equals: (String streamContents: [:aStream| aStream
  13. nextPutAll: 'hello'; space;
  14. nextPutAll: 'world'])
  15. !
  16. testIncludesSubString
  17. self assert: ('amber' includesSubString: 'ber').
  18. self deny: ('amber' includesSubString: 'zork').
  19. !
  20. testEquality
  21. self assert: 'hello' = 'hello'.
  22. self deny: 'hello' = 'world'.
  23. self assert: 'hello' = 'hello' yourself.
  24. self assert: 'hello' yourself = 'hello'.
  25. "test JS falsy value"
  26. self deny: '' = 0
  27. !
  28. testCopyWithoutAll
  29. self
  30. assert: 'hello world'
  31. equals: ('*hello* *world*' copyWithoutAll: '*')
  32. ! !
  33. TestCase subclass: #DictionaryTest
  34. instanceVariableNames: ''
  35. category: 'Kernel-Tests'!
  36. !DictionaryTest methodsFor: 'tests'!
  37. testPrintString
  38. self
  39. assert: 'a Dictionary(''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  40. equals: (Dictionary new
  41. at:'firstname' put: 'James';
  42. at:'lastname' put: 'Bond';
  43. printString)
  44. !
  45. testEquality
  46. | d1 d2 |
  47. self assert: Dictionary new = Dictionary new.
  48. d1 := Dictionary new at: 1 put: 2; yourself.
  49. d2 := Dictionary new at: 1 put: 2; yourself.
  50. self assert: d1 = d2.
  51. d2 := Dictionary new at: 1 put: 3; yourself.
  52. self deny: d1 = d2.
  53. d2 := Dictionary new at: 2 put: 2; yourself.
  54. self deny: d1 = d2.
  55. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  56. self deny: d1 = d2.
  57. !
  58. testDynamicDictionaries
  59. self assert: #{1 -> 'hello'. 2 -> 'world'} = (Dictionary with: 1 -> 'hello' with: 2 -> 'world')
  60. ! !
  61. TestCase subclass: #BooleanTest
  62. instanceVariableNames: ''
  63. category: 'Kernel-Tests'!
  64. !BooleanTest methodsFor: 'not yet classified'!
  65. testLogic
  66. "Trivial logic table"
  67. self assert: (true & true); deny: (true & false); deny: (false & true); deny: (false & false).
  68. self assert: (true | true); assert: (true | false); assert: (false | true); deny: (false | false).
  69. "Checking that expressions work fine too"
  70. self assert: (true & (1 > 0)); deny: ((1 > 0) & false); deny: ((1 > 0) & (1 > 2)).
  71. self assert: (false | (1 > 0)); assert: ((1 > 0) | false); assert: ((1 > 0) | (1 > 2))
  72. !
  73. testEquality
  74. "We're on top of JS...just be sure to check the basics!!"
  75. self deny: 0 = false.
  76. self deny: false = 0.
  77. self deny: '' = false.
  78. self deny: false = ''.
  79. self assert: true = true.
  80. self deny: false = true.
  81. self deny: true = false.
  82. self assert: false = false.
  83. "JS may do some type coercing after sending a message"
  84. self assert: true yourself = true.
  85. self assert: true yourself = true yourself
  86. !
  87. testLogicKeywords
  88. "Trivial logic table"
  89. self
  90. assert: (true and: [ true]);
  91. deny: (true and: [ false ]);
  92. deny: (false and: [ true ]);
  93. deny: (false and: [ false ]).
  94. self
  95. assert: (true or: [ true ]);
  96. assert: (true or: [ false ]);
  97. assert: (false or: [ true ]);
  98. deny: (false or: [ false ]).
  99. "Checking that expressions work fine too"
  100. self
  101. assert: (true and: [ 1 > 0 ]);
  102. deny: ((1 > 0) and: [ false ]);
  103. deny: ((1 > 0) and: [ 1 > 2 ]).
  104. self
  105. assert: (false or: [ 1 > 0 ]);
  106. assert: ((1 > 0) or: [ false ]);
  107. assert: ((1 > 0) or: [ 1 > 2 ])
  108. !
  109. testIfTrueIfFalse
  110. self assert: (true ifTrue: ['alternative block']) = 'alternative block'.
  111. self assert: (true ifFalse: ['alternative block']) = nil.
  112. self assert: (false ifTrue: ['alternative block']) = nil.
  113. self assert: (false ifFalse: ['alternative block']) = 'alternative block'.
  114. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block2'.
  115. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block'.
  116. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block'.
  117. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block2'.
  118. ! !
  119. TestCase subclass: #NumberTest
  120. instanceVariableNames: ''
  121. category: 'Kernel-Tests'!
  122. !NumberTest methodsFor: 'tests'!
  123. testPrintShowingDecimalPlaces
  124. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  125. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  126. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  127. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  128. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  129. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  130. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  131. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  132. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  133. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  134. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  135. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  136. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  137. !
  138. testEquality
  139. self assert: 1 = 1.
  140. self assert: 0 = 0.
  141. self deny: 1 = 0.
  142. self assert: 1 yourself = 1.
  143. self assert: 1 = 1 yourself.
  144. self assert: 1 yourself = 1 yourself.
  145. self deny: 0 = false.
  146. self deny: false = 0.
  147. self deny: '' = 0.
  148. self deny: 0 = ''
  149. !
  150. testArithmetic
  151. "We rely on JS here, so we won't test complex behavior, just check if
  152. message sends are corrects"
  153. self assert: 1.5 + 1 = 2.5.
  154. self assert: 2 - 1 = 1.
  155. self assert: -2 - 1 = -3.
  156. self assert: 12 / 2 = 6.
  157. self assert: 3 * 4 = 12.
  158. "Simple parenthesis and execution order"
  159. self assert: 1 + 2 * 3 = 9.
  160. self assert: 1 + (2 * 3) = 7
  161. !
  162. testRounded
  163. self assert: 3 rounded = 3.
  164. self assert: 3.212 rounded = 3.
  165. self assert: 3.51 rounded = 4
  166. !
  167. testNegated
  168. self assert: 3 negated = -3.
  169. self assert: -3 negated = 3
  170. !
  171. testComparison
  172. self assert: 3 > 2.
  173. self assert: 2 < 3.
  174. self deny: 3 < 2.
  175. self deny: 2 > 3.
  176. self assert: 3 >= 3.
  177. self assert: 3.1 >= 3.
  178. self assert: 3 <= 3.
  179. self assert: 3 <= 3.1
  180. !
  181. testTruncated
  182. self assert: 3 truncated = 3.
  183. self assert: 3.212 truncated = 3.
  184. self assert: 3.51 truncated = 3
  185. !
  186. testCopying
  187. self assert: 1 copy = 1.
  188. self assert: 1 deepCopy = 1
  189. ! !
  190. TestCase subclass: #JSObjectProxyTest
  191. instanceVariableNames: ''
  192. category: 'Kernel-Tests'!
  193. !JSObjectProxyTest methodsFor: 'tests'!
  194. testMethodWithArguments
  195. self deny: ('body' asJQuery hasClass: 'amber').
  196. 'body' asJQuery addClass: 'amber'.
  197. self assert: ('body' asJQuery hasClass: 'amber').
  198. 'body' asJQuery removeClass: 'amber'.
  199. self deny: ('body' asJQuery hasClass: 'amber').
  200. !
  201. testYourself
  202. |body|
  203. body := 'body' asJQuery
  204. addClass: 'amber';
  205. yourself.
  206. self assert: (body hasClass: 'amber').
  207. body removeClass: 'amber'.
  208. self deny: (body hasClass: 'amber').
  209. !
  210. testPropertyThatReturnsEmptyString
  211. <document.location.hash = ''>.
  212. self assert: '' equals: document location hash.
  213. document location hash: 'test'.
  214. self assert: '#test' equals: document location hash.
  215. ! !