This commit is contained in:
Chuck Smith
2024-01-21 11:41:17 -05:00
parent 13c9062fed
commit 6bb06370bb
4 changed files with 87 additions and 11 deletions

View File

@@ -326,6 +326,37 @@ func TestStringConcatenation(t *testing.T) {
}
}
func TestBuiltinFunction(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{`len("")`, 0},
{`len("four")`, 4},
{`len("hello world")`, 11},
{`len(1)`, "argument to `len` not supported, got INTEGER"},
{`len("one", "two")`, "wrong number of arguments. got=2, want=1"},
}
for _, tt := range tests {
evaluated := testEval(tt.input)
switch expected := tt.expected.(type) {
case int:
testIntegerObject(t, evaluated, int64(expected))
case string:
errObj, ok := evaluated.(*object.Error)
if !ok {
t.Errorf("object is not Error. got=%T (%+v)", evaluated, evaluated)
continue
}
if errObj.Message != expected {
t.Errorf("wrong error message. expected=%q, got=%q", expected, errObj.Message)
}
}
}
}
func testEval(input string) object.Object {
l := lexer.New(input)
p := parser.New(l)