Skip to content

Commit 3c9831d

Browse files
committed
some tests for new function
1 parent 3c2de63 commit 3c9831d

File tree

1 file changed

+99
-0
lines changed
  • staging/src/k8s.io/code-generator/cmd/go-to-protobuf/protobuf

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package protobuf
18+
19+
import (
20+
"go/ast"
21+
"testing"
22+
)
23+
24+
func TestProtoParser(t *testing.T) {
25+
ident := ast.NewIdent("FieldName")
26+
tests := []struct {
27+
expr ast.Expr
28+
err bool
29+
}{
30+
{
31+
expr: ident,
32+
err: false,
33+
},
34+
{
35+
expr: &ast.SelectorExpr{
36+
Sel: ident,
37+
},
38+
err: false,
39+
},
40+
{
41+
expr: &ast.StarExpr{
42+
X: ident,
43+
},
44+
err: false,
45+
},
46+
{
47+
expr: &ast.StarExpr{
48+
X: &ast.StarExpr{
49+
X: ident,
50+
},
51+
},
52+
err: false,
53+
},
54+
{
55+
expr: &ast.StarExpr{
56+
X: &ast.SelectorExpr{
57+
Sel: ident,
58+
},
59+
},
60+
err: false,
61+
},
62+
63+
{
64+
expr: &ast.KeyValueExpr{
65+
Key: ident,
66+
Colon: 0,
67+
Value: ident,
68+
},
69+
err: true,
70+
},
71+
{
72+
expr: &ast.StarExpr{
73+
X: &ast.KeyValueExpr{
74+
Key: ident,
75+
Colon: 0,
76+
Value: ident,
77+
},
78+
},
79+
err: true,
80+
},
81+
}
82+
83+
for _, test := range tests {
84+
actual, err := getFieldName(test.expr, "Struct")
85+
if !test.err {
86+
if err != nil {
87+
t.Errorf("%s: unexpected error %s", test.expr, err)
88+
} else {
89+
if actual != ident.Name {
90+
t.Errorf("%s: expected %s, got %s", test.expr, ident.Name, actual)
91+
}
92+
}
93+
} else {
94+
if err == nil {
95+
t.Errorf("%s: expected error did not occur, got %s instead", test.expr, actual)
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)