comparisons and booleans

This commit is contained in:
Chuck Smith
2024-02-05 16:58:59 -05:00
parent b4cc771baa
commit dcc869a6e2
5 changed files with 233 additions and 6 deletions

View File

@@ -48,6 +48,12 @@ func testExpectedObject(t *testing.T, expected interface{}, actual object.Object
if err != nil {
t.Errorf("testIntegerObject failed: %s", err)
}
case bool:
err := testBooleanObject(bool(expected), actual)
if err != nil {
t.Errorf("testBooleanObject failed: %s", err)
}
}
}
@@ -70,6 +76,19 @@ func testIntegerObject(expected int64, actual object.Object) interface{} {
return nil
}
func testBooleanObject(expected bool, actual object.Object) interface{} {
result, ok := actual.(*object.Boolean)
if !ok {
return fmt.Errorf("object is not Boolean. got=%T (%+v", actual, actual)
}
if result.Value != expected {
return fmt.Errorf("object has wrong value. got=%t, want=%t", result.Value, expected)
}
return nil
}
func TestIntegerArithmetic(t *testing.T) {
tests := []vmTestCase{
{"1", 1},
@@ -83,6 +102,32 @@ func TestIntegerArithmetic(t *testing.T) {
{"5 * 2 + 10", 20},
{"5 + 2 * 10", 25},
{"5 * (2 + 10)", 60},
{"1 < 2", true},
{"1 > 2", false},
{"1 < 1", false},
{"1 > 1", false},
{"1 == 1", true},
{"1 != 1", false},
{"1 == 2", false},
{"1 != 2", true},
{"true == true", true},
{"false == false", true},
{"true == false", false},
{"true != false", true},
{"false != true", true},
{"(1 < 2) == true", true},
{"(1 < 2) == false", false},
{"(1 > 2) == true", false},
{"(1 > 2) == false", true},
}
runVmTests(t, tests)
}
func TestBooleanExpressions(t *testing.T) {
tests := []vmTestCase{
{"true", true},
{"false", false},
}
runVmTests(t, tests)