Compare commits
No commits in common. "fc6e1744bddeea81f51f3126ded2b81411350eb2" and "feb0f0db3122ee10dd75083af7589b74008024d7" have entirely different histories.
fc6e1744bd
...
feb0f0db31
77
func_test.go
77
func_test.go
@ -29,46 +29,6 @@ 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, ", ") }
|
||||
@ -162,40 +122,3 @@ 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")
|
||||
}
|
||||
}
|
||||
|
104
structs_test.go
104
structs_test.go
@ -3,7 +3,6 @@ package salix
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.elara.ws/salix/ast"
|
||||
)
|
||||
@ -50,23 +49,6 @@ 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}
|
||||
|
||||
@ -109,23 +91,6 @@ 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)
|
||||
|
||||
@ -159,25 +124,8 @@ 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 }
|
||||
@ -215,23 +163,7 @@ func TestGetField(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFieldNil(t *testing.T) {
|
||||
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": nil})
|
||||
if err == nil {
|
||||
t.Error("Expected error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFieldNoFields(t *testing.T) {
|
||||
func TestGetFieldNotFound(t *testing.T) {
|
||||
testStruct := struct{}{}
|
||||
tmpl := testTmpl(t)
|
||||
|
||||
@ -248,8 +180,7 @@ func TestGetFieldNoFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFieldNotFound(t *testing.T) {
|
||||
testStruct := struct{ X int }{0}
|
||||
func TestGetFieldNil(t *testing.T) {
|
||||
tmpl := testTmpl(t)
|
||||
|
||||
// test.Field
|
||||
@ -259,7 +190,7 @@ func TestGetFieldNotFound(t *testing.T) {
|
||||
Position: testPos(t),
|
||||
}
|
||||
|
||||
_, err := tmpl.getField(ast, map[string]any{"test": testStruct})
|
||||
_, err := tmpl.getField(ast, map[string]any{"test": nil})
|
||||
if err == nil {
|
||||
t.Error("Expected error, got nil")
|
||||
}
|
||||
@ -267,7 +198,7 @@ func TestGetFieldNotFound(t *testing.T) {
|
||||
|
||||
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
|
||||
@ -295,31 +226,6 @@ 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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user