Kernel-Tests.st 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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: ('jtalk' includesSubString: 'alk').
  17. self deny: ('jtalk' 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. TestCase subclass: #DictionaryTest
  28. instanceVariableNames: ''
  29. category: 'Kernel-Tests'!
  30. !DictionaryTest methodsFor: 'tests'!
  31. testPrintString
  32. self
  33. assert: 'a Dictionary(''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
  34. equals: (Dictionary new
  35. at:'firstname' put: 'James';
  36. at:'lastname' put: 'Bond';
  37. printString)
  38. !
  39. testEquality
  40. | d1 d2 |
  41. self assert: Dictionary new = Dictionary new.
  42. d1 := Dictionary new at: 1 put: 2; yourself.
  43. d2 := Dictionary new at: 1 put: 2; yourself.
  44. self assert: d1 = d2.
  45. d2 := Dictionary new at: 1 put: 3; yourself.
  46. self deny: d1 = d2.
  47. d2 := Dictionary new at: 2 put: 2; yourself.
  48. self deny: d1 = d2.
  49. d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
  50. self deny: d1 = d2.
  51. !
  52. testDynamicDictionaries
  53. self assert: #{1 -> 'hello'. 2 -> 'world'} = (Dictionary with: 1 -> 'hello' with: 2 -> 'world')
  54. ! !
  55. TestCase subclass: #BooleanTest
  56. instanceVariableNames: ''
  57. category: 'Kernel-Tests'!
  58. !BooleanTest methodsFor: 'not yet classified'!
  59. testLogic
  60. "Trivial logic table"
  61. self assert: (true & true); deny: (true & false); deny: (false & true); deny: (false & false).
  62. self assert: (true | true); assert: (true | false); assert: (false | true); deny: (false | false).
  63. "Checking that expressions work fine too"
  64. self assert: (true & (1 > 0)); deny: ((1 > 0) & false); deny: ((1 > 0) & (1 > 2)).
  65. self assert: (false | (1 > 0)); assert: ((1 > 0) | false); assert: ((1 > 0) | (1 > 2))
  66. !
  67. testEquality
  68. "We're on top of JS...just be sure to check the basics!!"
  69. self deny: 0 = false.
  70. self deny: false = 0.
  71. self deny: '' = false.
  72. self deny: false = ''.
  73. self assert: true = true.
  74. self deny: false = true.
  75. self deny: true = false.
  76. self assert: false = false.
  77. "JS may do some type coercing after sending a message"
  78. self assert: true yourself = true.
  79. self assert: true yourself = true yourself
  80. !
  81. testLogicKeywords
  82. "Trivial logic table"
  83. self
  84. assert: (true and: [ true]);
  85. deny: (true and: [ false ]);
  86. deny: (false and: [ true ]);
  87. deny: (false and: [ false ]).
  88. self
  89. assert: (true or: [ true ]);
  90. assert: (true or: [ false ]);
  91. assert: (false or: [ true ]);
  92. deny: (false or: [ false ]).
  93. "Checking that expressions work fine too"
  94. self
  95. assert: (true and: [ 1 > 0 ]);
  96. deny: ((1 > 0) and: [ false ]);
  97. deny: ((1 > 0) and: [ 1 > 2 ]).
  98. self
  99. assert: (false or: [ 1 > 0 ]);
  100. assert: ((1 > 0) or: [ false ]);
  101. assert: ((1 > 0) or: [ 1 > 2 ])
  102. !
  103. testIfTrueIfFalse
  104. self assert: (true ifTrue: ['alternative block']) = 'alternative block'.
  105. self assert: (true ifFalse: ['alternative block']) = nil.
  106. self assert: (false ifTrue: ['alternative block']) = nil.
  107. self assert: (false ifFalse: ['alternative block']) = 'alternative block'.
  108. self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block2'.
  109. self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block'.
  110. self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) = 'alternative block'.
  111. self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) = 'alternative block2'.
  112. ! !
  113. TestCase subclass: #NumberTest
  114. instanceVariableNames: ''
  115. category: 'Kernel-Tests'!
  116. !NumberTest methodsFor: 'tests'!
  117. testEquality
  118. self assert: 1 = 1.
  119. self assert: 0 = 0.
  120. self deny: 1 = 0.
  121. self assert: 1 yourself = 1.
  122. self assert: 1 = 1 yourself.
  123. self assert: 1 yourself = 1 yourself.
  124. self deny: 0 = false.
  125. self deny: false = 0.
  126. self deny: '' = 0.
  127. self deny: 0 = ''
  128. !
  129. testArithmetic
  130. "We rely on JS here, so we won't test complex behavior, just check if
  131. message sends are corrects"
  132. self assert: 1.5 + 1 = 2.5.
  133. self assert: 2 - 1 = 1.
  134. self assert: -2 - 1 = -3.
  135. self assert: 12 / 2 = 6.
  136. self assert: 3 * 4 = 12.
  137. "Simple parenthesis and execution order"
  138. self assert: 1 + 2 * 3 = 9.
  139. self assert: 1 + (2 * 3) = 7
  140. !
  141. testRounded
  142. self assert: 3 rounded = 3.
  143. self assert: 3.212 rounded = 3.
  144. self assert: 3.51 rounded = 4
  145. !
  146. testNegated
  147. self assert: 3 negated = -3.
  148. self assert: -3 negated = 3
  149. !
  150. testComparison
  151. self assert: 3 > 2.
  152. self assert: 2 < 3.
  153. self deny: 3 < 2.
  154. self deny: 2 > 3.
  155. self assert: 3 >= 3.
  156. self assert: 3.1 >= 3.
  157. self assert: 3 <= 3.
  158. self assert: 3 <= 3.1
  159. !
  160. testTruncated
  161. self assert: 3 truncated = 3.
  162. self assert: 3.212 truncated = 3.
  163. self assert: 3.51 truncated = 3
  164. ! !
  165. TestCase subclass: #NumberTest
  166. instanceVariableNames: ''
  167. category: 'Kernel-Tests'!
  168. !NumberTest methodsFor: 'not yet classified'!
  169. testPrintShowingDecimalPlaces
  170. self assert: '23.00' equals: (23 printShowingDecimalPlaces: 2).
  171. self assert: '23.57' equals: (23.5698 printShowingDecimalPlaces: 2).
  172. self assert: '-234.56700' equals:( 234.567 negated printShowingDecimalPlaces: 5).
  173. self assert: '23' equals: (23.4567 printShowingDecimalPlaces: 0).
  174. self assert: '24' equals: (23.5567 printShowingDecimalPlaces: 0).
  175. self assert: '-23' equals: (23.4567 negated printShowingDecimalPlaces: 0).
  176. self assert: '-24' equals: (23.5567 negated printShowingDecimalPlaces: 0).
  177. self assert: '100000000.0' equals: (100000000 printShowingDecimalPlaces: 1).
  178. self assert: '0.98000' equals: (0.98 printShowingDecimalPlaces: 5).
  179. self assert: '-0.98' equals: (0.98 negated printShowingDecimalPlaces: 2).
  180. self assert: '2.57' equals: (2.567 printShowingDecimalPlaces: 2).
  181. self assert: '-2.57' equals: (-2.567 printShowingDecimalPlaces: 2).
  182. self assert: '0.00' equals: (0 printShowingDecimalPlaces: 2).
  183. ! !