Test more invalid cases for indexing

This commit is contained in:
Elara 2024-02-11 22:16:58 -08:00
parent feb0f0db31
commit 778071c8c5
1 changed files with 51 additions and 0 deletions

View File

@ -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