diff --git a/structs_test.go b/structs_test.go index 6ce6573..2cc267d 100644 --- a/structs_test.go +++ b/structs_test.go @@ -49,6 +49,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 +108,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,6 +158,23 @@ 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 { X int