/
reporting_test.go
272 lines (259 loc) · 7.95 KB
/
reporting_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package protoparse
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/jhump/protoreflect/internal/testutil"
)
func TestErrorReporting(t *testing.T) {
tooManyErrors := errors.New("too many errors")
limitedErrReporter := func(limit int, count *int) ErrorReporter {
return func(err ErrorWithPos) error {
*count++
if *count > limit {
return tooManyErrors
}
return nil
}
}
trackingReporter := func(errs *[]ErrorWithPos, count *int) ErrorReporter {
return func(err ErrorWithPos) error {
*count++
*errs = append(*errs, err)
return nil
}
}
fail := errors.New("failure!")
failFastReporter := func(count *int) ErrorReporter {
return func(err ErrorWithPos) error {
*count++
return fail
}
}
testCases := []struct {
fileNames []string
files map[string]string
expectedErrs []string
}{
{
// multiple syntax errors
fileNames: []string{"test.proto"},
files: map[string]string{
"test.proto": `
syntax = "proto";
package foo
enum State { A = 0; B = 1; C; D }
message Foo {
foo = 1;
}
`,
},
expectedErrs: []string{
"test.proto:5:41: syntax error: unexpected \"enum\", expecting ';'",
"test.proto:5:69: syntax error: unexpected ';', expecting '='",
"test.proto:7:53: syntax error: unexpected '='",
`test.proto:2:50: syntax value must be "proto2" or "proto3"`,
},
},
{
// multiple validation errors
fileNames: []string{"test.proto"},
files: map[string]string{
"test.proto": `
syntax = "proto3";
message Foo {
string foo = 0;
}
enum State { C }
enum Bar {
BAZ = 1;
BUZZ = 1;
}
`,
},
expectedErrs: []string{
"test.proto:6:56: syntax error: unexpected '}', expecting '='",
"test.proto:4:62: tag number 0 must be greater than zero",
"test.proto:8:55: enum Bar: proto3 requires that first value in enum have numeric value of 0",
"test.proto:9:56: enum Bar: values BAZ and BUZZ both have the same numeric value 1; use allow_alias option if intentional",
},
},
{
// multiple link errors
fileNames: []string{"test.proto"},
files: map[string]string{
"test.proto": `
syntax = "proto3";
message Foo {
string foo = 1;
}
enum Bar {
BAZ = 0;
BAZ = 2;
}
service Bar {
rpc Foo (Foo) returns (Foo);
rpc Foo (Frob) returns (Nitz);
}
`,
},
expectedErrs: []string{
"test.proto:8:49: duplicate symbol BAZ: already defined as enum value; protobuf uses C++ scoping rules for enum values, so they exist in the scope enclosing the enum",
"test.proto:10:41: duplicate symbol Bar: already defined as enum",
"test.proto:12:49: duplicate symbol Bar.Foo: already defined as method",
"test.proto:12:58: method Bar.Foo: unknown request type Frob",
"test.proto:12:73: method Bar.Foo: unknown response type Nitz",
},
},
{
// syntax errors across multiple files
fileNames: []string{"test1.proto", "test2.proto"},
files: map[string]string{
"test1.proto": `
syntax = "proto3";
import "test2.proto";
message Foo {
string foo = -1;
}
service Bar {
rpc Foo (Foo);
}
`,
"test2.proto": `
syntax = "proto3";
message Baz {
required string foo = 1;
}
service Service {
Foo; Bar; Baz;
}
`,
},
expectedErrs: []string{
"test1.proto:5:62: syntax error: unexpected '-', expecting int literal",
"test1.proto:8:62: syntax error: unexpected ';', expecting \"returns\"",
"test2.proto:7:49: syntax error: unexpected identifier, expecting \"option\" or \"rpc\" or ';' or '}'",
"test2.proto:4:49: field Baz.foo: label 'required' is not allowed in proto3",
},
},
{
// link errors across multiple files
fileNames: []string{"test1.proto", "test2.proto"},
files: map[string]string{
"test1.proto": `
syntax = "proto3";
import "test2.proto";
message Foo {
string foo = 1;
}
service Bar {
rpc Frob (Empty) returns (Nitz);
}
`,
"test2.proto": `
syntax = "proto3";
message Empty {}
enum Bar {
BAZ = 0;
}
service Foo {
rpc DoSomething (Empty) returns (Foo);
}
`,
},
expectedErrs: []string{
"test2.proto:4:41: duplicate symbol Bar: already defined as service in \"test1.proto\"",
"test2.proto:7:41: duplicate symbol Foo: already defined as message in \"test1.proto\"",
"test1.proto:8:75: method Bar.Frob: unknown response type Nitz",
"test2.proto:8:82: method Foo.DoSomething: invalid response type: Foo is a service, not a message",
},
},
}
for i, tc := range testCases {
var p Parser
p.Accessor = FileContentsFromMap(tc.files)
var reported []ErrorWithPos
count := 0
p.ErrorReporter = trackingReporter(&reported, &count)
_, err := p.ParseFiles(tc.fileNames...)
reportedMsgs := make([]string, len(reported))
for j := range reported {
reportedMsgs[j] = reported[j].Error()
}
t.Logf("case #%d: got %d errors:\n\t%s", i+1, len(reported), strings.Join(reportedMsgs, "\n\t"))
// returns sentinel, but all actual errors in reported
testutil.Eq(t, ErrInvalidSource, err, "case #%d: parse should have failed with invalid source error", i+1)
testutil.Eq(t, len(tc.expectedErrs), count, "case #%d: parse should have called reporter %d times", i+1, len(tc.expectedErrs))
testutil.Eq(t, len(tc.expectedErrs), len(reported), "case #%d: wrong number of errors reported", i+1)
for j := range tc.expectedErrs {
testutil.Eq(t, tc.expectedErrs[j], reported[j].Error(), "case #%d: parse error[%d] have %q; instead got %q", i+1, j, tc.expectedErrs[j], reported[j].Error())
split := strings.SplitN(tc.expectedErrs[j], ":", 4)
testutil.Eq(t, 4, len(split), "case #%d: expected %q [%d] to contain at least 4 elements split by :", i+1, tc.expectedErrs[j], j)
testutil.Eq(t, split[3], " "+reported[j].Unwrap().Error(), "case #%d: parse error underlying[%d] have %q; instead got %q", i+1, j, split[3], reported[j].Unwrap().Error())
}
count = 0
p.ErrorReporter = failFastReporter(&count)
_, err = p.ParseFiles(tc.fileNames...)
testutil.Eq(t, fail, err, "case #%d: parse should have failed fast", i+1)
testutil.Eq(t, 1, count, "case #%d: parse should have called reporter only once", i+1)
count = 0
p.ErrorReporter = limitedErrReporter(3, &count)
_, err = p.ParseFiles(tc.fileNames...)
if len(tc.expectedErrs) > 3 {
testutil.Eq(t, tooManyErrors, err, "case #%d: parse should have failed with too many errors", i+1)
testutil.Eq(t, 4, count, "case #%d: parse should have called reporter 4 times", i+1)
} else {
// less than threshold means reporter always returned nil,
// so parse returns ErrInvalidSource sentinel
testutil.Eq(t, ErrInvalidSource, err, "case #%d: parse should have failed with invalid source error", i+1)
testutil.Eq(t, len(tc.expectedErrs), count, "case #%d: parse should have called reporter %d times", i+1, len(tc.expectedErrs))
}
}
}
func TestWarningReporting(t *testing.T) {
type msg struct {
pos SourcePos
text string
}
var msgs []msg
rep := func(warn ErrorWithPos) {
msgs = append(msgs, msg{
pos: warn.GetPosition(), text: warn.Unwrap().Error(),
})
}
testCases := []struct {
source string
expectedNotices []string
}{
{
source: `syntax = "proto2"; message Foo {}`,
},
{
source: `syntax = "proto3"; message Foo {}`,
},
{
source: `message Foo {}`,
expectedNotices: []string{
"test.proto:1:1: no syntax specified; defaulting to proto2 syntax",
},
},
}
for _, testCase := range testCases {
accessor := FileContentsFromMap(map[string]string{
"test.proto": testCase.source,
})
p := Parser{
Accessor: accessor,
WarningReporter: rep,
}
msgs = nil
_, err := p.ParseFiles("test.proto")
testutil.Ok(t, err)
actualNotices := make([]string, len(msgs))
for j, msg := range msgs {
actualNotices[j] = fmt.Sprintf("%s: %s", msg.pos, msg.text)
}
testutil.Eq(t, testCase.expectedNotices, actualNotices)
}
}