Compare commits

...

3 Commits

2 changed files with 190 additions and 19 deletions

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")
}
}

View File

@ -3,6 +3,7 @@ package salix
import (
"fmt"
"testing"
"time"
"go.elara.ws/salix/ast"
)
@ -49,6 +50,23 @@ func TestSliceGetIndexOutOfRange(t *testing.T) {
}
}
func TestSliceGetIndexInvalidType(t *testing.T) {
testSlice := []any{}
tmpl := testTmpl(t)
// test[0.0]
ast := ast.Index{
Value: ast.Ident{Value: "test", Position: testPos(t)},
Index: ast.String{Value: "key", Position: testPos(t)},
Position: testPos(t),
}
_, err := tmpl.getIndex(ast, map[string]any{"test": testSlice})
if err == nil {
t.Errorf("Expected error, got nil")
}
}
func TestMapGetIndex(t *testing.T) {
testMap := map[any]any{1: "2", 3.0: 4, "5": 6.0}
@ -91,6 +109,23 @@ func TestMapGetIndexNotFound(t *testing.T) {
}
}
func TestMapGetIndexInvalidType(t *testing.T) {
testMap := map[int]any{}
tmpl := testTmpl(t)
// test["key"]
ast := ast.Index{
Value: ast.Ident{Value: "test", Position: testPos(t)},
Index: ast.String{Value: "key", Position: testPos(t)},
Position: testPos(t),
}
_, err := tmpl.getIndex(ast, map[string]any{"test": testMap})
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestGetIndexNil(t *testing.T) {
tmpl := testTmpl(t)
@ -124,8 +159,25 @@ func TestGetIndexNilIndex(t *testing.T) {
}
}
func TestGetIndexInvalidContainer(t *testing.T) {
testStruct := struct{}{}
tmpl := testTmpl(t)
// test["key"]
ast := ast.Index{
Value: ast.Ident{Value: "test", Position: testPos(t)},
Index: ast.String{Value: "key", Position: testPos(t)},
Position: testPos(t),
}
_, err := tmpl.getIndex(ast, map[string]any{"test": testStruct})
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestGetField(t *testing.T) {
testStruct := struct {
testStruct := &struct {
X int
Y string
Z struct{ A int }
@ -163,23 +215,6 @@ func TestGetField(t *testing.T) {
}
}
func TestGetFieldNotFound(t *testing.T) {
testStruct := struct{}{}
tmpl := testTmpl(t)
// test.Field
ast := ast.FieldAccess{
Value: ast.Ident{Value: "test", Position: testPos(t)},
Name: ast.Ident{Value: "Field", Position: testPos(t)},
Position: testPos(t),
}
_, err := tmpl.getField(ast, map[string]any{"test": testStruct})
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestGetFieldNil(t *testing.T) {
tmpl := testTmpl(t)
@ -196,9 +231,43 @@ func TestGetFieldNil(t *testing.T) {
}
}
func TestGetFieldNoFields(t *testing.T) {
testStruct := struct{}{}
tmpl := testTmpl(t)
// test.Field
ast := ast.FieldAccess{
Value: ast.Ident{Value: "test", Position: testPos(t)},
Name: ast.Ident{Value: "Field", Position: testPos(t)},
Position: testPos(t),
}
_, err := tmpl.getField(ast, map[string]any{"test": testStruct})
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestGetFieldNotFound(t *testing.T) {
testStruct := struct{ X int }{0}
tmpl := testTmpl(t)
// test.Field
ast := ast.FieldAccess{
Value: ast.Ident{Value: "test", Position: testPos(t)},
Name: ast.Ident{Value: "Field", Position: testPos(t)},
Position: testPos(t),
}
_, err := tmpl.getField(ast, map[string]any{"test": testStruct})
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestMethodCall(t *testing.T) {
executed := false
testStruct := struct{ Method func(bool) bool }{
testStruct := &struct{ Method func(bool) bool }{
Method: func(b bool) bool {
executed = b
return executed
@ -226,6 +295,31 @@ func TestMethodCall(t *testing.T) {
}
}
func TestMethodCallTime(t *testing.T) {
const expected = "2023-04-26T00:00:00Z"
testStruct := time.Date(2023, time.April, 26, 0, 0, 0, 0, time.UTC)
tmpl := testTmpl(t)
// t.Format("2006-01-02T15:04:05Z07:00)
ast := ast.MethodCall{
Value: ast.Ident{Value: "t", Position: testPos(t)},
Name: ast.Ident{Value: "Format", Position: testPos(t)},
Params: []ast.Node{
ast.String{Value: time.RFC3339, Position: testPos(t)},
},
Position: testPos(t),
}
val, err := tmpl.execMethodCall(ast, map[string]any{"t": testStruct})
if err != nil {
t.Fatalf("execMethodCall error: %s", err)
}
if val != expected {
t.Errorf("Expected %q, got %q", expected, val)
}
}
func TestMethodCallNotFound(t *testing.T) {
tmpl := testTmpl(t)