1
0

Kernel-Tests.st 6.3 KB

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