Strings
Some checks failed
Build / build (push) Failing after 1m35s
Test / build (push) Failing after 1m31s

This commit is contained in:
Chuck Smith
2024-02-21 16:29:53 -05:00
parent 8caeaca559
commit e4bca02235
5 changed files with 189 additions and 5 deletions

View File

@@ -55,6 +55,12 @@ func testExpectedObject(t *testing.T, expected interface{}, actual object.Object
t.Errorf("testBooleanObject failed: %s", err)
}
case string:
err := testStringObject(expected, actual)
if err != nil {
t.Errorf("testStringObject failed: %s", err)
}
case *object.Null:
if actual != Null {
t.Errorf("object is not Null: %T (%+v)", actual, actual)
@@ -68,7 +74,7 @@ func parse(input string) *ast.Program {
return p.ParseProgram()
}
func testIntegerObject(expected int64, actual object.Object) interface{} {
func testIntegerObject(expected int64, actual object.Object) error {
result, ok := actual.(*object.Integer)
if !ok {
return fmt.Errorf("object is not Integer. got=%T (%+v", actual, actual)
@@ -81,7 +87,7 @@ func testIntegerObject(expected int64, actual object.Object) interface{} {
return nil
}
func testBooleanObject(expected bool, actual object.Object) interface{} {
func testBooleanObject(expected bool, actual object.Object) error {
result, ok := actual.(*object.Boolean)
if !ok {
return fmt.Errorf("object is not Boolean. got=%T (%+v", actual, actual)
@@ -94,6 +100,19 @@ func testBooleanObject(expected bool, actual object.Object) interface{} {
return nil
}
func testStringObject(expected string, actual object.Object) error {
result, ok := actual.(*object.String)
if !ok {
return fmt.Errorf("object is not String. got=%T (%+v", actual, actual)
}
if result.Value != expected {
return fmt.Errorf("object has wrong value. got=%q, want=%q", result.Value, expected)
}
return nil
}
func TestIntegerArithmetic(t *testing.T) {
tests := []vmTestCase{
{"1", 1},
@@ -175,3 +194,13 @@ func TestGlobalLetStatements(t *testing.T) {
runVmTests(t, tests)
}
func TestStringExpressions(t *testing.T) {
tests := []vmTestCase{
{`"monkey"`, "monkey"},
{`"mon" + "key"`, "monkey"},
{`"mon" + "key" + "banana"`, "monkeybanana"},
}
runVmTests(t, tests)
}