evaluator_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package evaluator
  2. import (
  3. "github/runnignwater/monkey/lexer"
  4. "github/runnignwater/monkey/object"
  5. "github/runnignwater/monkey/parser"
  6. "testing"
  7. )
  8. func TestEvalIntegerExpression(t *testing.T) {
  9. tests := []struct {
  10. input string
  11. expected int64
  12. }{
  13. {"5", 5},
  14. {"10", 10},
  15. {"-5", -5},
  16. {"-10", -10},
  17. {"5+5+5+5-10", 10},
  18. {"2*2*2*2*2", 32},
  19. {"-50+100+-50", 0},
  20. }
  21. for _, tt := range tests {
  22. evaluated := testEval(tt.input)
  23. testIntegerObject(t, evaluated, tt.expected)
  24. }
  25. }
  26. func TestEvalBooleanExpression(t *testing.T) {
  27. tests := []struct {
  28. input string
  29. expected bool
  30. }{
  31. {"true", true},
  32. {"false", false},
  33. {"1 < 2", true},
  34. {"1 > 2", false},
  35. {"1 == 1", true},
  36. {"1 != 1", false},
  37. {"1 == 2", false},
  38. {"1 != 2", true},
  39. {"true == true", true},
  40. {"false == false", true},
  41. {"(1 < 2) == true", true},
  42. }
  43. for _, tt := range tests {
  44. evaluated := testEval(tt.input)
  45. testBooleanObject(t, evaluated, tt.expected)
  46. }
  47. }
  48. func TestBangOperator(t *testing.T) {
  49. tests := []struct {
  50. input string
  51. expected bool
  52. }{
  53. {"!true", false},
  54. {"!false", true},
  55. {"!5", false},
  56. {"!!true", true},
  57. {"!!false", false},
  58. {"!!5", true},
  59. }
  60. for _, tt := range tests {
  61. evaluated := testEval(tt.input)
  62. testBooleanObject(t, evaluated, tt.expected)
  63. }
  64. }
  65. func TestIfElseExpression(t *testing.T) {
  66. tests := []struct {
  67. input string
  68. expected interface{}
  69. }{
  70. {"if (true) {10}", 10},
  71. {"if(false) {10}", nil},
  72. {"if (1) {10}", 10},
  73. {"if(1<2){10}", 10},
  74. {"if(1>2){10}", nil},
  75. {"if(1<2){10} else {20}", 10},
  76. {"if(1>2){10} else {20}", 20},
  77. }
  78. for _, tt := range tests {
  79. evaluated := testEval(tt.input)
  80. integer, ok := tt.expected.(int)
  81. if ok {
  82. testIntegerObject(t, evaluated, int64(integer))
  83. } else {
  84. testNullObject(t, evaluated)
  85. }
  86. }
  87. }
  88. func TestReturnStatements(t *testing.T) {
  89. tests := []struct {
  90. input string
  91. expected int64
  92. }{
  93. {"return 10;", 10},
  94. {"return 10;9", 10},
  95. {"return 2*5;9;", 10},
  96. {"9;return 3 *5; 9;", 15},
  97. {
  98. `if ( 10 > 1) {
  99. if( 10 > 1) {
  100. return 10;
  101. }
  102. return 1;
  103. }
  104. `,
  105. 10,
  106. },
  107. }
  108. for _, tt := range tests {
  109. evaluated := testEval(tt.input)
  110. testIntegerObject(t, evaluated, tt.expected)
  111. }
  112. }
  113. func TestErrorHandling(t *testing.T) {
  114. tests := []struct {
  115. input string
  116. expectedMsg string
  117. }{
  118. {
  119. "5 + true",
  120. "type mismatch: INTEGER + BOOLEAN",
  121. },
  122. {
  123. "5 + true; 5;",
  124. "type mismatch: INTEGER + BOOLEAN",
  125. },
  126. {
  127. "-true",
  128. "unknown operator: -BOOLEAN",
  129. },
  130. {
  131. "true + false",
  132. "unknown operator: BOOLEAN + BOOLEAN",
  133. },
  134. {
  135. "5;true + false;5",
  136. "unknown operator: BOOLEAN + BOOLEAN",
  137. },
  138. {
  139. "if ( 10 > 1) { true + false; }",
  140. "unknown operator: BOOLEAN + BOOLEAN",
  141. },
  142. {
  143. `
  144. if (10 > 1) {
  145. if (10 > 1) {
  146. return true + false;
  147. }
  148. return 1;
  149. }
  150. `,
  151. "unknown operator: BOOLEAN + BOOLEAN",
  152. },
  153. }
  154. for _, tt := range tests {
  155. evaluated := testEval(tt.input)
  156. errObj, ok := evaluated.(*object.Error)
  157. if !ok {
  158. t.Errorf("no error object returned. got=%T (%+v)", evaluated, evaluated)
  159. continue
  160. }
  161. if errObj.Msg != tt.expectedMsg {
  162. t.Errorf("wrong error message. expected=%q, got=%q", tt.expectedMsg, errObj.Msg)
  163. }
  164. }
  165. }
  166. func testNullObject(t *testing.T, obj object.Object) bool {
  167. if obj != NULL {
  168. t.Errorf("object is not NULL. got=%T (%+v)", obj, obj)
  169. return false
  170. }
  171. return true
  172. }
  173. func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool {
  174. result, ok := obj.(*object.Integer)
  175. if !ok {
  176. t.Errorf("object is not Integer. got=%T (%+v)", obj, obj)
  177. return false
  178. }
  179. if result.Value != expected {
  180. t.Errorf("object has wrong value. got=%d, want=%d", result.Value, expected)
  181. return false
  182. }
  183. return true
  184. }
  185. func testBooleanObject(t *testing.T, obj object.Object, expected bool) bool {
  186. result, ok := obj.(*object.Boolean)
  187. if !ok {
  188. t.Errorf("object is not Boolean. got=%T (%+v)", obj, obj)
  189. return false
  190. }
  191. if result.Value != expected {
  192. t.Errorf("object has wrong value. got=%t, want=%t", result.Value, expected)
  193. return false
  194. }
  195. return true
  196. }
  197. func testEval(input string) object.Object {
  198. l := lexer.New(input)
  199. p := parser.New(l)
  200. program := p.ParseProgram()
  201. return Eval(program)
  202. }