From fc6e1744bddeea81f51f3126ded2b81411350eb2 Mon Sep 17 00:00:00 2001 From: Elara6331 Date: Mon, 12 Feb 2024 10:50:53 -0800 Subject: [PATCH] Handle more function call error cases --- func_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/func_test.go b/func_test.go index 15771d0..ca8a5c9 100644 --- a/func_test.go +++ b/func_test.go @@ -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") + } +}