Kernel-Tests.st 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. TestCase subclass: #StringTest
  2. instanceVariableNames: ''
  3. category: 'Kernel-Tests'!
  4. !StringTest methodsFor: 'tests'!
  5. testJoin
  6. self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
  7. !
  8. testStreamContents
  9. self
  10. assert: 'hello world'
  11. equals: (String streamContents: [:aStream| aStream
  12. nextPutAll: 'hello'; space;
  13. nextPutAll: 'world'])
  14. !
  15. testIncludesSubString
  16. self assert: ('amber' includesSubString: 'ber').
  17. self deny: ('amber' includesSubString: 'zork').
  18. !
  19. testEquality
  20. self assert: 'hello' = 'hello'.
  21. self deny: 'hello' = 'world'.
  22. self assert: 'hello' = 'hello' yourself.
  23. self assert: 'hello' yourself = 'hello'.
  24. "test JS falsy value"
  25. self deny: '' = 0
  26. !
  27. testCopyWithoutAll
  28. self
  29. assert: 'hello world'
  30. equals: ('*hello* *world*' copyWithoutAll: '*')
  31. ! !
  32. TestCase subclass: #DictionaryTest
  33. instanceVariableNames: ''
  34. category: 'Kernel-Tests'!
  35. !DictionaryTest methodsFor: 'tests'!
  36. testPrintString
  37. self
  38. assert: 'a Dictionary(''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  39. equals: (Dictionary new
  40. at:'firstname' put: 'James';
  41. at:'lastname' put: 'Bond';
  42. printString)
  43. !
  44. testEquality
  45. | d1 d2 |
  46. self assert: Dictionary new = Dictionary new.
  47. d1 := Dictionary new at: 1 put: 2; yourself.
  48. d2 := Dictionary new at: 1 put: 2; yourself.
  49. self assert: d1 = d2.
  50. d2 := Dictionary new at: 1 put: 3; yourself.
  51. self deny: d1 = d2.
  52. d2 := Dictionary new at: 2 put: 2; yourself.
  53. self deny: d1 = d2.
  54. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  55. self deny: d1 = d2.
  56. !
  57. testDynamicDictionaries
  58. self assert: #{1 -> 'hello'. 2 -> 'world'} = (Dictionary with: 1 -> 'hello' with: 2 -> 'world')
  59. ! !
  60. TestCase subclass: #BooleanTest
  61. instanceVariableNames: ''
  62. category: 'Kernel-Tests'!
  63. !BooleanTest methodsFor: 'not yet classified'!
  64. testLogic
  65. "Trivial logic table"
  66. self assert: (true & true); deny: (true & false); deny: (false & true); deny: (false & false).
  67. self assert: (true | true); assert: (true | false); assert: (false | true); deny: (false | false).
  68. "Checking that expressions work fine too"
  69. self assert: (true & (1 > 0)); deny: ((1 > 0) & false); deny: ((1 > 0) & (1 > 2)).
  70. self assert: (false | (1 > 0)); assert: ((1 > 0) | false); assert: ((1 > 0) | (1 > 2))
  71. !
  72. testEquality
  73. "We're on top of JS...just be sure to check the basics!!"
  74. self deny: 0 = false.
  75. self deny: false = 0.
  76. self deny: '' = false.
  77. self deny: false = ''.
  78. self assert: true = true.
  79. self deny: false = true.
  80. self deny: true = false.
  81. self assert: false = false.
  82. "JS may do some type coercing after sending a message"
  83. self assert: true yourself = true.
  84. self assert: true yourself = true yourself
  85. !
  86. testLogicKeywords
  87. "Trivial logic table"
  88. self
  89. assert: (true and: [ true]);
  90. deny: (true and: [ false ]);
  91. deny: (false and: [ true ]);
  92. deny: (false and: [ false ]).
  93. self
  94. assert: (true or: [ true ]);
  95. assert: (true or: [ false ]);
  96. assert: (false or: [ true ]);
  97. deny: (false or: [ false ]).
  98. "Checking that expressions work fine too"
  99. self
  100. assert: (true and: [ 1 > 0 ]);
  101. deny: ((1 > 0) and: [ false ]);
  102. deny: ((1 > 0) and: [ 1 > 2 ]).
  103. self
  104. assert: (false or: [ 1 > 0 ]);
  105. assert: ((1 > 0) or: [ false ]);
  106. assert: ((1 > 0) or: [ 1 > 2 ])
  107. !
  108. testIfTrueIfFalse
  109. self assert: (true ifTrue: ['alternative block']) = 'alternative block'.
  110. self assert: (true ifFalse: ['alternative block']) = nil.
  111. self assert: (false ifTrue: ['alternative block']) = nil.
  112. self assert: (false ifFalse: ['alternative block']) = 'alternative block'.
  113. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block2'.
  114. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block'.
  115. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block'.
  116. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block2'.
  117. ! !
  118. TestCase subclass: #NumberTest
  119. instanceVariableNames: ''
  120. category: 'Kernel-Tests'!
  121. !NumberTest methodsFor: 'not yet classified'!
  122. testPrintShowingDecimalPlaces
  123. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  124. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  125. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  126. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  127. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  128. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  129. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  130. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  131. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  132. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  133. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  134. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  135. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  136. ! !
  137. !NumberTest methodsFor: 'tests'!
  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. TestCase subclass: #JSObjectProxyTest
  187. instanceVariableNames: ''
  188. category: 'Kernel-Tests'!
  189. !JSObjectProxyTest methodsFor: 'tests'!
  190. testMethodWithArguments
  191. self deny: ('body' asJQuery hasClass: 'amber').
  192. 'body' asJQuery addClass: 'amber'.
  193. self assert: ('body' asJQuery hasClass: 'amber').
  194. 'body' asJQuery removeClass: 'amber'.
  195. self deny: ('body' asJQuery hasClass: 'amber').
  196. !
  197. testYourself
  198. |body|
  199. body := 'body' asJQuery
  200. addClass: 'amber';
  201. yourself.
  202. self assert: (body hasClass: 'amber').
  203. body removeClass: 'amber'.
  204. self deny: (body hasClass: 'amber').
  205. !
  206. testPropertyThatReturnsEmptyString
  207. <document.location.hash = ''>.
  208. self assert: '' equals: document location hash.
  209. document location hash: 'test'.
  210. self assert: '#test' equals: document location hash.
  211. ! !