If statements

This commit is contained in:
Chuck Smith
2024-01-19 15:46:10 -05:00
parent ea1ae5cfb0
commit e6d5567681
2 changed files with 68 additions and 0 deletions

View File

@@ -86,6 +86,31 @@ 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 }", 20},
{"if (1 < 2) { 10 } else { 20 }", 10},
}
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 testEval(input string) object.Object {
l := lexer.New(input)
p := parser.New(l)
@@ -121,3 +146,11 @@ func testBooleanObject(t *testing.T, obj object.Object, expected bool) bool {
return true
}
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
}