evaluator_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. "foobar",
  155. "identifier not found: foobar",
  156. },
  157. }
  158. for _, tt := range tests {
  159. evaluated := testEval(tt.input)
  160. errObj, ok := evaluated.(*object.Error)
  161. if !ok {
  162. t.Errorf("no error object returned. got=%T (%+v)", evaluated, evaluated)
  163. continue
  164. }
  165. if errObj.Msg != tt.expectedMsg {
  166. t.Errorf("wrong error message. expected=%q, got=%q", tt.expectedMsg, errObj.Msg)
  167. }
  168. }
  169. }
  170. func TestLetStatement(t *testing.T) {
  171. tests := []struct {
  172. input string
  173. expected int64
  174. }{
  175. {"let a = 5;a;", 5},
  176. {"let a = 5 * 5;a;", 25},
  177. {"let a = 5;let b = a;b;", 5},
  178. {"let a = 5; let b = a;let c = a + b + 5;c;", 15},
  179. }
  180. for _, tt := range tests {
  181. testIntegerObject(t, testEval(tt.input), tt.expected)
  182. }
  183. }
  184. func TestFunctionObject(t *testing.T) {
  185. input := "fn(x) {x + 2;};"
  186. evaluated := testEval(input)
  187. fn, ok := evaluated.(*object.Function)
  188. if !ok {
  189. t.Fatalf("object is not Function. got=%T (%+v)", evaluated, evaluated)
  190. }
  191. if len(fn.Parameters) != 1 {
  192. t.Fatalf("function has wrong parameters. Parameters=%+v", fn.Parameters)
  193. }
  194. if fn.Parameters[0].String() != "x" {
  195. t.Fatalf("parameter is not 'x'. got=%q", fn.Parameters[0])
  196. }
  197. expectedBody := "(x + 2)"
  198. if fn.Body.String() != expectedBody {
  199. t.Fatalf("body is not %q. got=%q", expectedBody, fn.Body.String())
  200. }
  201. }
  202. func TestFunctionApplication(t *testing.T) {
  203. tests := []struct {
  204. input string
  205. expected int64
  206. }{
  207. {"let identity = fn(x) {x;};identity(5);", 5},
  208. {"let identify = fn(x){return x;};identify(5);", 5},
  209. {"let double = fn(x) { x * 2; };double(5);", 10},
  210. {"let add = fn(x,y){x+y;};add(5,5);", 10},
  211. {"let add = fn(x,y){x+y;};add(5+5,add(5,5));", 20},
  212. {"fn(x){x;}(5)", 5},
  213. }
  214. for _, tt := range tests {
  215. testIntegerObject(t, testEval(tt.input), tt.expected)
  216. }
  217. }
  218. // --------------------------------------------------------------------------------------------------------------------
  219. func testNullObject(t *testing.T, obj object.Object) bool {
  220. if obj != NULL {
  221. t.Errorf("object is not NULL. got=%T (%+v)", obj, obj)
  222. return false
  223. }
  224. return true
  225. }
  226. func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool {
  227. result, ok := obj.(*object.Integer)
  228. if !ok {
  229. t.Errorf("object is not Integer. got=%T (%+v)", obj, obj)
  230. return false
  231. }
  232. if result.Value != expected {
  233. t.Errorf("object has wrong value. got=%d, want=%d", result.Value, expected)
  234. return false
  235. }
  236. return true
  237. }
  238. func testBooleanObject(t *testing.T, obj object.Object, expected bool) bool {
  239. result, ok := obj.(*object.Boolean)
  240. if !ok {
  241. t.Errorf("object is not Boolean. got=%T (%+v)", obj, obj)
  242. return false
  243. }
  244. if result.Value != expected {
  245. t.Errorf("object has wrong value. got=%t, want=%t", result.Value, expected)
  246. return false
  247. }
  248. return true
  249. }
  250. func testEval(input string) object.Object {
  251. l := lexer.New(input)
  252. p := parser.New(l)
  253. program := p.ParseProgram()
  254. env := object.NewEnvironment()
  255. return Eval(program, env)
  256. }
  257. func TestClosures(t *testing.T) {
  258. input := `
  259. let newAdder = fn(x) {
  260. fn (y) { x + y };
  261. };
  262. let addTwo = newAdder(2); addTwo(2);
  263. `
  264. testIntegerObject(t, testEval(input), 4)
  265. }