optimizations
Some checks failed
Build / build (push) Successful in 9m47s
Publish Image / publish (push) Failing after 49s
Test / build (push) Failing after 6m19s

This commit is contained in:
Chuck Smith
2024-04-02 14:32:03 -04:00
parent 07fd82b261
commit 88e3330856
18 changed files with 510 additions and 31 deletions

View File

@@ -57,7 +57,7 @@ func assertEvaluated(t *testing.T, expected interface{}, actual object.Object) {
func TestEvalExpressions(t *testing.T) {
tests := []struct {
input string
expected interface{}
expected any
}{
{"5", 5},
{"10", 10},
@@ -84,12 +84,15 @@ func TestEvalExpressions(t *testing.T) {
{`4 * " "`, " "},
{"1 << 2", 4},
{"4 >> 2", 1},
{"5.0 / 2.0", 2.5},
}
for _, tt := range tests {
evaluated := testEval(tt.input)
if expected, ok := tt.expected.(int64); ok {
testIntegerObject(t, evaluated, expected)
} else if expected, ok := tt.expected.(float64); ok {
testFloatObject(t, evaluated, expected)
} else if expected, ok := tt.expected.(bool); ok {
testBooleanObject(t, evaluated, expected)
}
@@ -405,17 +408,45 @@ func TestClosures(t *testing.T) {
testIntegerObject(t, testEval(input), 4)
}
func TestIntegerLiteral(t *testing.T) {
input := `2`
evaluated := testEval(input)
obj, ok := evaluated.(object.Integer)
if !ok {
t.Fatalf("object is not Integer. got=%T (%+v)", evaluated, evaluated)
}
if obj.Value != 2 {
t.Errorf("Integer has wrong value. got=%q", obj.Value)
}
}
func TestFloatLiteral(t *testing.T) {
input := `2.5`
evaluated := testEval(input)
obj, ok := evaluated.(object.Float)
if !ok {
t.Fatalf("object is not Float. got=%T (%+v)", evaluated, evaluated)
}
if obj.Value != 2.5 {
t.Errorf("Float has wrong value. got=%f", obj.Value)
}
}
func TestStringLiteral(t *testing.T) {
input := `"Hello World!"`
evaluated := testEval(input)
str, ok := evaluated.(object.String)
obj, ok := evaluated.(object.String)
if !ok {
t.Fatalf("object is not String. got=%T (+%v)", evaluated, evaluated)
}
if str.Value != "Hello World!" {
t.Errorf("String has wrong value. got=%q", str.Value)
if obj.Value != "Hello World!" {
t.Errorf("String has wrong value. got=%q", obj.Value)
}
}
@@ -803,6 +834,20 @@ func testEval(input string) object.Object {
return Eval(context.New(), program, env)
}
func testFloatObject(t *testing.T, obj object.Object, expected float64) bool {
result, ok := obj.(object.Float)
if !ok {
t.Errorf("object is not Float. got=%T (%+v)", obj, obj)
return false
}
if result.Value != expected {
t.Errorf("object has wrong value. got=%f, want=%f",
result.Value, expected)
return false
}
return true
}
func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool {
result, ok := obj.(object.Integer)
if !ok {