Handle more function call error cases

This commit is contained in:
Elara 2024-02-12 10:51:28 -08:00
parent 3bafd0ef11
commit 82001061da

View File

@ -29,6 +29,44 @@ func TestFuncCall(t *testing.T) {
} }
} }
func TestFuncCallInvalidParamCount(t *testing.T) {
fn := func() int { return 0 }
// test("string")
ast := ast.FuncCall{
Name: ast.Ident{Value: "test", Position: testPos(t)},
Params: []ast.Node{
ast.String{Value: "string", Position: testPos(t)},
},
Position: testPos(t),
}
tmpl := testTmpl(t)
_, err := tmpl.execFuncCall(ast, map[string]any{"test": fn})
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestFuncCallInvalidParamType(t *testing.T) {
fn := func(i int) int { return i }
// test("string")
ast := ast.FuncCall{
Name: ast.Ident{Value: "test", Position: testPos(t)},
Params: []ast.Node{
ast.String{Value: "string", Position: testPos(t)},
},
Position: testPos(t),
}
tmpl := testTmpl(t)
_, err := tmpl.execFuncCall(ast, map[string]any{"test": fn})
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestFuncCallVariadic(t *testing.T) { func TestFuncCallVariadic(t *testing.T) {
const expected = "Hello, World" const expected = "Hello, World"
concat := func(params ...string) string { return strings.Join(params, ", ") } concat := func(params ...string) string { return strings.Join(params, ", ") }
@ -122,3 +160,40 @@ func TestFuncCallNil(t *testing.T) {
t.Error("Expected error, got nil") t.Error("Expected error, got nil")
} }
} }
func TestFuncCallNotFound(t *testing.T) {
// test()
ast := ast.FuncCall{
Name: ast.Ident{Value: "test", Position: testPos(t)},
Position: testPos(t),
}
tmpl := testTmpl(t)
_, err := tmpl.execFuncCall(ast, map[string]any{})
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestFuncCallAssignment(t *testing.T) {
fn := func(i int) int { return i }
// test(x = 1)
ast := ast.FuncCall{
Name: ast.Ident{Value: "test", Position: testPos(t)},
Params: []ast.Node{
ast.Assignment{
Name: ast.Ident{Value: "x", Position: testPos(t)},
Value: ast.Integer{Value: 1, Position: testPos(t)},
Position: testPos(t),
},
},
Position: testPos(t),
}
tmpl := testTmpl(t)
_, err := tmpl.execFuncCall(ast, map[string]any{"test": fn})
if err == nil {
t.Error("Expected error, got nil")
}
}