1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package proto
25
26 import (
27 "testing"
28 )
29
30 func TestField(t *testing.T) {
31 proto := `repeated foo.bar lots =1 [option1=a, option2=b, option3="happy"];`
32 p := newParserOn(proto)
33 f := newNormalField()
34 err := f.parse(p)
35 if err != nil {
36 t.Fatal(err)
37 }
38 if got, want := f.Repeated, true; got != want {
39 t.Errorf("got [%v] want [%v]", got, want)
40 }
41 if got, want := f.Type, "foo.bar"; got != want {
42 t.Errorf("got [%v] want [%v]", got, want)
43 }
44 if got, want := f.Name, "lots"; got != want {
45 t.Errorf("got [%v] want [%v]", got, want)
46 }
47 if got, want := len(f.Options), 3; got != want {
48 t.Fatalf("got [%v] want [%v]", got, want)
49 }
50 if got, want := f.Options[0].Name, "option1"; got != want {
51 t.Errorf("got [%v] want [%v]", got, want)
52 }
53 if got, want := f.Options[0].Constant.Source, "a"; got != want {
54 t.Errorf("got [%v] want [%v]", got, want)
55 }
56 if got, want := f.Options[1].Name, "option2"; got != want {
57 t.Errorf("got [%v] want [%v]", got, want)
58 }
59 if got, want := f.Options[1].Constant.Source, "b"; got != want {
60 t.Errorf("got [%v] want [%v]", got, want)
61 }
62 if got, want := f.Options[2].Constant.Source, "happy"; got != want {
63 t.Errorf("got [%v] want [%v]", got, want)
64 }
65 checkParent(f.Options[0], t)
66 }
67
68 func TestFieldSimple(t *testing.T) {
69 proto := `string optional_string_piece = 24 [ctype=STRING_PIECE];`
70 p := newParserOn(proto)
71 f := newNormalField()
72 err := f.parse(p)
73 if err != nil {
74 t.Fatal(err)
75 }
76 if got, want := f.Type, "string"; got != want {
77 t.Errorf("got [%v] want [%v]", got, want)
78 }
79 if got, want := f.Name, "optional_string_piece"; got != want {
80 t.Errorf("got [%v] want [%v]", got, want)
81 }
82 if got, want := f.Sequence, 24; got != want {
83 t.Errorf("got [%v] want [%v]", got, want)
84 }
85 if got, want := len(f.Options), 1; got != want {
86 t.Fatalf("got [%v] want [%v]", got, want)
87 }
88 if got, want := f.Options[0].Name, "ctype"; got != want {
89 t.Errorf("got [%v] want [%v]", got, want)
90 }
91 if got, want := f.Options[0].Constant.Source, "STRING_PIECE"; got != want {
92 t.Errorf("got [%v] want [%v]", got, want)
93 }
94 }
95
96 func TestFieldSyntaxErrors(t *testing.T) {
97 for i, each := range []string{
98 `repeatet foo.bar lots = 1;`,
99 `string lots === 1;`,
100 } {
101 f := newNormalField()
102 if f.parse(newParserOn(each)) == nil {
103 t.Errorf("uncaught syntax error in test case %d, %#v", i, f)
104 }
105 }
106 }
107
108 func TestMapField(t *testing.T) {
109 proto := ` <string, Project> projects = 3 [foo=bar];`
110 p := newParserOn(proto)
111 f := newMapField()
112 err := f.parse(p)
113 if err != nil {
114 t.Fatal(err)
115 }
116 if got, want := f.KeyType, "string"; got != want {
117 t.Errorf("got [%v] want [%v]", got, want)
118 }
119 if got, want := f.Type, "Project"; got != want {
120 t.Errorf("got [%v] want [%v]", got, want)
121 }
122 if got, want := f.Name, "projects"; got != want {
123 t.Errorf("got [%v] want [%v]", got, want)
124 }
125 if got, want := f.Sequence, 3; got != want {
126 t.Errorf("got [%v] want [%v]", got, want)
127 }
128 checkParent(f.Options[0], t)
129 }
130
131 func TestMapFieldWithDotTypes(t *testing.T) {
132 proto := ` <.Some.How, .Such.Project> projects = 3;`
133 p := newParserOn(proto)
134 f := newMapField()
135 err := f.parse(p)
136 if err != nil {
137 t.Fatal(err)
138 }
139 if got, want := f.KeyType, ".Some.How"; got != want {
140 t.Errorf("got [%v] want [%v]", got, want)
141 }
142 if got, want := f.Type, ".Such.Project"; got != want {
143 t.Errorf("got [%v] want [%v]", got, want)
144 }
145 }
146
147 func TestOptionalWithOption(t *testing.T) {
148 proto := `optional int32 default_int32 = 61 [default = 41 ];`
149 p := newParserOn(proto)
150 f := newNormalField()
151 err := f.parse(p)
152 if err != nil {
153 t.Fatal(err)
154 }
155 if got, want := f.Sequence, 61; got != want {
156 t.Errorf("got [%v] want [%v]", got, want)
157 }
158 o := f.Options[0]
159 if got, want := o.Name, "default"; got != want {
160 t.Errorf("got [%v] want [%v]", got, want)
161 }
162 if got, want := o.Constant.Source, "41"; got != want {
163 t.Errorf("got [%v] want [%v]", got, want)
164 }
165 }
166
167 func TestFieldInlineComment(t *testing.T) {
168 proto := `message Hello {
169 // comment
170 bool foo = 1; // inline comment
171 }`
172 p := newParserOn(proto)
173 def := new(Proto)
174 err := def.parse(p)
175 if err != nil {
176 t.Fatal(err)
177 }
178 m := def.Elements[0].(*Message)
179 if len(m.Elements) != 1 {
180 t.Error("expected one element", m.Elements)
181 }
182 f := m.Elements[0].(*NormalField)
183 if f.InlineComment == nil {
184 t.Error("expected inline comment")
185 }
186 }
187
188 func TestFieldTypeStartsWithDot(t *testing.T) {
189 proto := `.game.Resource foo = 1;`
190 p := newParserOn(proto)
191 f := newNormalField()
192 err := f.parse(p)
193 if err != nil {
194 t.Fatal(err)
195 }
196 foot := f.Field.Type
197 if got, want := foot, ".game.Resource"; got != want {
198 t.Errorf("got [%v] want [%v]", got, want)
199 }
200 }
201
202 func TestMultiLineFieldType(t *testing.T) {
203 src := `google.ads.googleads.v1.enums.ConversionAdjustmentTypeEnum
204 .ConversionAdjustmentType adjustment_type = 5;`
205 p := newParserOn(src)
206 f := newNormalField()
207 err := f.parse(p)
208 if err != nil {
209 t.Fatal(err)
210 }
211 }
212
View as plain text