|
|
@@ -70,6 +70,39 @@ func TestBangOperator(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestIfElseExpression(t *testing.T) {
|
|
|
+ tests := []struct {
|
|
|
+ input string
|
|
|
+ expected interface{}
|
|
|
+ }{
|
|
|
+ {"if (true) {10}", 10},
|
|
|
+ {"if(false) {10}", nil},
|
|
|
+ {"if (1) {10}", 10},
|
|
|
+ {"if(1<2){10}", 10},
|
|
|
+ {"if(1>2){10}", nil},
|
|
|
+ {"if(1<2){10} else {20}", 10},
|
|
|
+ {"if(1>2){10} else {20}", 20},
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, tt := range tests {
|
|
|
+ evaluated := testEval(tt.input)
|
|
|
+ integer, ok := tt.expected.(int)
|
|
|
+ if ok {
|
|
|
+ testIntegerObject(t, evaluated, int64(integer))
|
|
|
+ } else {
|
|
|
+ testNullObject(t, evaluated)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func testNullObject(t *testing.T, obj object.Object) bool {
|
|
|
+ if obj != NULL {
|
|
|
+ t.Errorf("object is not NULL. got=%T (%+v)", obj, obj)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool {
|
|
|
result, ok := obj.(*object.Integer)
|
|
|
if !ok {
|