2018-08-15 21:59:51 +00:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-06-06 20:01:53 +00:00
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2022-05-23 20:12:23 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2019-06-06 20:01:53 +00:00
|
|
|
|
|
|
|
"google.golang.org/protobuf/types/descriptorpb"
|
2018-08-15 21:59:51 +00:00
|
|
|
)
|
|
|
|
|
2019-06-06 20:01:53 +00:00
|
|
|
func mustMakeMessage(s string) *descriptorpb.DescriptorProto {
|
|
|
|
s = fmt.Sprintf(`name:"test.proto" syntax:"proto2" message_type:[{%s}]`, s)
|
|
|
|
pb := new(descriptorpb.FileDescriptorProto)
|
|
|
|
if err := prototext.Unmarshal([]byte(s), pb); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pb.MessageType[0]
|
|
|
|
}
|
|
|
|
|
2018-08-15 21:59:51 +00:00
|
|
|
func TestFields(t *testing.T) {
|
|
|
|
type fieldsKind struct {
|
2022-05-23 20:12:23 +00:00
|
|
|
kind protoreflect.Kind
|
2018-08-15 21:59:51 +00:00
|
|
|
fields string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
inFields []fieldsKind
|
2019-06-06 20:01:53 +00:00
|
|
|
wantMsg *descriptorpb.DescriptorProto
|
2018-08-15 21:59:51 +00:00
|
|
|
wantErr string
|
|
|
|
}{{
|
2022-05-23 20:12:23 +00:00
|
|
|
inFields: []fieldsKind{{protoreflect.MessageKind, ""}},
|
2019-07-02 22:19:08 +00:00
|
|
|
wantMsg: mustMakeMessage(`name:"X"`),
|
2018-08-15 21:59:51 +00:00
|
|
|
}, {
|
2022-05-23 20:12:23 +00:00
|
|
|
inFields: []fieldsKind{{protoreflect.MessageKind, "987654321"}},
|
2018-08-15 21:59:51 +00:00
|
|
|
wantErr: "invalid field: 987654321",
|
|
|
|
}, {
|
2022-05-23 20:12:23 +00:00
|
|
|
inFields: []fieldsKind{{protoreflect.MessageKind, "-1"}},
|
2018-08-15 21:59:51 +00:00
|
|
|
wantErr: "invalid field: -1",
|
|
|
|
}, {
|
2022-05-23 20:12:23 +00:00
|
|
|
inFields: []fieldsKind{{protoreflect.MessageKind, "k"}},
|
2018-08-15 21:59:51 +00:00
|
|
|
wantErr: "invalid field: k",
|
|
|
|
}, {
|
2022-05-23 20:12:23 +00:00
|
|
|
inFields: []fieldsKind{{protoreflect.MessageKind, "1.2"}, {protoreflect.Int32Kind, "1"}},
|
2018-08-15 21:59:51 +00:00
|
|
|
wantErr: "field 1 of int32 type cannot have sub-fields",
|
|
|
|
}, {
|
2022-05-23 20:12:23 +00:00
|
|
|
inFields: []fieldsKind{{protoreflect.Int32Kind, "1"}, {protoreflect.MessageKind, "1.2"}},
|
2018-08-15 21:59:51 +00:00
|
|
|
wantErr: "field 1 of int32 type cannot have sub-fields",
|
|
|
|
}, {
|
2022-05-23 20:12:23 +00:00
|
|
|
inFields: []fieldsKind{{protoreflect.Int32Kind, "30"}, {protoreflect.Int32Kind, "30"}},
|
2018-08-15 21:59:51 +00:00
|
|
|
wantErr: "field 30 already set as int32 type",
|
|
|
|
}, {
|
|
|
|
inFields: []fieldsKind{
|
2022-05-23 20:12:23 +00:00
|
|
|
{protoreflect.Int32Kind, "10.20.31"},
|
|
|
|
{protoreflect.MessageKind, " 10.20.30, 10.21 "},
|
|
|
|
{protoreflect.GroupKind, "10"},
|
2018-08-15 21:59:51 +00:00
|
|
|
},
|
2019-06-06 20:01:53 +00:00
|
|
|
wantMsg: mustMakeMessage(`
|
2019-07-02 22:19:08 +00:00
|
|
|
name: "X"
|
2019-06-06 20:01:53 +00:00
|
|
|
field: [
|
2019-07-02 22:19:08 +00:00
|
|
|
{name:"x10" number:10 label:LABEL_OPTIONAL type:TYPE_GROUP type_name:".X.X10"}
|
2019-06-06 20:01:53 +00:00
|
|
|
]
|
|
|
|
nested_type: [{
|
2019-07-02 22:19:08 +00:00
|
|
|
name: "X10"
|
2019-06-06 20:01:53 +00:00
|
|
|
field: [
|
2019-07-02 22:19:08 +00:00
|
|
|
{name:"x20" number:20 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".X.X10.X20"},
|
|
|
|
{name:"x21" number:21 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".X.X10.X21"}
|
2019-06-06 20:01:53 +00:00
|
|
|
]
|
|
|
|
nested_type: [{
|
2019-07-02 22:19:08 +00:00
|
|
|
name: "X20"
|
2019-06-06 20:01:53 +00:00
|
|
|
field:[
|
2019-07-02 22:19:08 +00:00
|
|
|
{name:"x30" number:30 label:LABEL_OPTIONAL type:TYPE_MESSAGE, type_name:".X.X10.X20.X30"},
|
|
|
|
{name:"x31" number:31 label:LABEL_REPEATED type:TYPE_INT32 options:{packed:true}}
|
2019-06-06 20:01:53 +00:00
|
|
|
]
|
|
|
|
nested_type: [{
|
2019-07-02 22:19:08 +00:00
|
|
|
name: "X30"
|
2019-06-06 20:01:53 +00:00
|
|
|
}]
|
2018-08-15 21:59:51 +00:00
|
|
|
}, {
|
2019-07-02 22:19:08 +00:00
|
|
|
name: "X21"
|
2019-06-06 20:01:53 +00:00
|
|
|
}]
|
|
|
|
}]
|
|
|
|
`),
|
2018-08-15 21:59:51 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
var fields fields
|
|
|
|
for i, tc := range tt.inFields {
|
|
|
|
gotErr := fields.Set(tc.fields, tc.kind)
|
|
|
|
if gotErr != nil {
|
|
|
|
if tt.wantErr == "" || !strings.Contains(fmt.Sprint(gotErr), tt.wantErr) {
|
|
|
|
t.Fatalf("fields %d, Set(%q, %v) = %v, want %v", i, tc.fields, tc.kind, gotErr, tt.wantErr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tt.wantErr != "" {
|
|
|
|
t.Errorf("all Set calls succeeded, want %v error", tt.wantErr)
|
|
|
|
}
|
2019-07-02 22:19:08 +00:00
|
|
|
gotMsg := fields.messageDescriptor("X")
|
2019-06-06 20:01:53 +00:00
|
|
|
if !proto.Equal(gotMsg, tt.wantMsg) {
|
|
|
|
t.Errorf("messageDescriptor() mismatch:\ngot %v\nwant %v", gotMsg, tt.wantMsg)
|
2018-08-15 21:59:51 +00:00
|
|
|
}
|
|
|
|
if _, err := fields.Descriptor(); err != nil {
|
|
|
|
t.Errorf("Descriptor() = %v, want nil error", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|