Handle more function call error cases

This commit is contained in:
Elara 2024-02-12 10:50:53 -08:00
parent 3bafd0ef11
commit fc6e1744bd

View File

@ -29,6 +29,46 @@ func TestFuncCall(t *testing.T) {
}
}
//TODO: COMMIT THIS!!!
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) {
const expected = "Hello, World"
concat := func(params ...string) string { return strings.Join(params, ", ") }
@ -122,3 +162,40 @@ func TestFuncCallNil(t *testing.T) {
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")
}
}