From 3bafd0ef11d331ef54a9ad2231b097084446b65a Mon Sep 17 00:00:00 2001 From: Elara6331 Date: Sun, 11 Feb 2024 22:29:31 -0800 Subject: [PATCH] Handle more test cases for field access and method calls --- structs_test.go | 81 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 19 deletions(-) diff --git a/structs_test.go b/structs_test.go index 2cc267d..787a00b 100644 --- a/structs_test.go +++ b/structs_test.go @@ -3,6 +3,7 @@ package salix import ( "fmt" "testing" + "time" "go.elara.ws/salix/ast" ) @@ -176,7 +177,7 @@ func TestGetIndexInvalidContainer(t *testing.T) { } func TestGetField(t *testing.T) { - testStruct := struct { + testStruct := &struct { X int Y string Z struct{ A int } @@ -214,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) @@ -247,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 @@ -277,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)