Skip to content

Commit 90cf8be

Browse files
committed
add FieldName test case
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent ec40bd7 commit 90cf8be

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

pkg/analysis/utils/utils_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package utils_test
2+
3+
import (
4+
"go/ast"
5+
"testing"
6+
7+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
8+
)
9+
10+
func TestFieldName(t *testing.T) {
11+
t.Parallel()
12+
13+
cases := map[string]struct {
14+
field *ast.Field
15+
want string
16+
}{
17+
"field has Names": {
18+
field: &ast.Field{
19+
Names: []*ast.Ident{
20+
{
21+
Name: "foo",
22+
},
23+
},
24+
},
25+
want: "foo",
26+
},
27+
"filed has no Names, but is an Ident": {
28+
field: &ast.Field{
29+
Type: &ast.Ident{
30+
Name: "foo",
31+
},
32+
},
33+
want: "foo",
34+
},
35+
"field has no Names, but is a StarExpr with an Ident": {
36+
field: &ast.Field{
37+
Type: &ast.StarExpr{
38+
X: &ast.Ident{
39+
Name: "foo",
40+
},
41+
},
42+
},
43+
want: "foo",
44+
},
45+
"field has no Names, and is not an Ident or StarExpr": {
46+
field: &ast.Field{
47+
Type: &ast.ArrayType{
48+
Elt: &ast.Ident{
49+
Name: "foo",
50+
},
51+
},
52+
},
53+
want: "",
54+
},
55+
}
56+
57+
for name, tc := range cases {
58+
t.Run(name, func(t *testing.T) {
59+
t.Parallel()
60+
61+
got := utils.FieldName(tc.field)
62+
if got != tc.want {
63+
t.Errorf("got %q, want %q", got, tc.want)
64+
}
65+
})
66+
}
67+
}

0 commit comments

Comments
 (0)