encoding/textpb: switch tests to use V2 generated messages

Change-Id: I817568aec5fbf053c3566b311e92b79aff1caf7e
Reviewed-on: https://go-review.googlesource.com/c/154177
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
This commit is contained in:
Herbie Ong 2018-12-13 14:19:50 -08:00
parent 20a1d31685
commit 70651959ff
6 changed files with 1927 additions and 256 deletions

View File

@ -11,6 +11,7 @@ import (
protoV1 "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/v2/encoding/textpb"
"github.com/golang/protobuf/v2/internal/scalar"
"github.com/golang/protobuf/v2/proto"
// The legacy package must be imported prior to use of any legacy messages.
// TODO: Remove this when protoV1 registers these hooks for you.
@ -21,13 +22,11 @@ import (
)
func TestUnmarshal(t *testing.T) {
// TODO: Switch to using proto.Message for inputMessage and wantMessage fields when v2
// proto.Equal is implemented.
tests := []struct {
desc string
inputMessage protoV1.Message
inputMessage proto.Message
inputText string
wantMessage protoV1.Message
wantMessage proto.Message
wantErr bool
}{{
desc: "proto2 empty message",
@ -970,14 +969,14 @@ str_to_nested: {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
err := textpb.Unmarshal(M(tt.inputMessage), []byte(tt.inputText))
err := textpb.Unmarshal(tt.inputMessage, []byte(tt.inputText))
if err != nil && !tt.wantErr {
t.Errorf("Unmarshal() returned error: %v\n\n", err)
}
if err == nil && tt.wantErr {
t.Error("Unmarshal() got nil error, want error\n\n")
}
if tt.wantMessage != nil && !protoV1.Equal(tt.inputMessage, tt.wantMessage) {
if tt.wantMessage != nil && !protoV1.Equal(tt.inputMessage.(protoV1.Message), tt.wantMessage.(protoV1.Message)) {
t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", tt.inputMessage, tt.wantMessage)
}
})

View File

@ -9,11 +9,9 @@ import (
"strings"
"testing"
protoV1 "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/v2/encoding/textpb"
"github.com/golang/protobuf/v2/internal/detrand"
"github.com/golang/protobuf/v2/internal/encoding/pack"
"github.com/golang/protobuf/v2/internal/impl"
"github.com/golang/protobuf/v2/internal/scalar"
"github.com/golang/protobuf/v2/proto"
"github.com/google/go-cmp/cmp"
@ -32,10 +30,6 @@ func init() {
detrand.Disable()
}
func M(m interface{}) proto.Message {
return impl.Export{}.MessageOf(m).Interface()
}
// splitLines is a cmpopts.Option for comparing strings with line breaks.
var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
return strings.Split(s, "\n")
@ -56,7 +50,7 @@ func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
func TestMarshal(t *testing.T) {
tests := []struct {
desc string
input protoV1.Message
input proto.Message
want string
wantErr bool
}{{
@ -806,7 +800,7 @@ req_nested: {}
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
b, err := textpb.Marshal(M(tt.input))
b, err := textpb.Marshal(tt.input)
if err != nil && !tt.wantErr {
t.Errorf("Marshal() returned error: %v\n\n", err)
}

View File

@ -6,6 +6,7 @@ import (
protoV1 "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/v2/encoding/textpb"
"github.com/golang/protobuf/v2/encoding/textpb/testprotos/pb2"
"github.com/golang/protobuf/v2/proto"
// The legacy package must be imported prior to use of any legacy messages.
// TODO: Remove this when protoV1 registers these hooks for you.
@ -22,7 +23,7 @@ import (
func TestRoundTrip(t *testing.T) {
tests := []struct {
desc string
message protoV1.Message
message proto.Message
}{{
desc: "well-known type fields set to empty messages",
message: &pb2.KnownTypes{
@ -151,16 +152,18 @@ func TestRoundTrip(t *testing.T) {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
b, err := textpb.Marshal(M(tt.message))
b, err := textpb.Marshal(tt.message)
if err != nil {
t.Errorf("Marshal() returned error: %v\n\n", err)
}
want := protoV1.Clone(tt.message)
want.Reset()
err = textpb.Unmarshal(M(want), b)
gotMessage := tt.message.ProtoReflect().Type().New()
err = textpb.Unmarshal(gotMessage, b)
if err != nil {
t.Errorf("Unmarshal() returned error: %v\n\n", err)
}
if !protoV1.Equal(gotMessage.(protoV1.Message), tt.message.(protoV1.Message)) {
t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", gotMessage, tt.message)
}
})
}
}

File diff suppressed because it is too large Load Diff

View File

@ -4,21 +4,17 @@
package pb3
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
prototype "github.com/golang/protobuf/v2/reflect/prototype"
protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type Enum int32
@ -29,6 +25,18 @@ const (
Enum_TEN Enum = 10
)
type xxx_Enum Enum
func (e Enum) ProtoReflect() protoreflect.Enum {
return (xxx_Enum)(e)
}
func (e xxx_Enum) Type() protoreflect.EnumType {
return xxx_Test_ProtoFile_EnumTypes[0]
}
func (e xxx_Enum) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(e)
}
var Enum_name = map[int32]string{
0: "ZERO",
1: "ONE",
@ -60,6 +68,18 @@ const (
Enums_DIEZ Enums_NestedEnum = 10
)
type xxx_Enums_NestedEnum Enums_NestedEnum
func (e Enums_NestedEnum) ProtoReflect() protoreflect.Enum {
return (xxx_Enums_NestedEnum)(e)
}
func (e xxx_Enums_NestedEnum) Type() protoreflect.EnumType {
return xxx_Test_ProtoFile_EnumTypes[1]
}
func (e xxx_Enums_NestedEnum) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(e)
}
var Enums_NestedEnum_name = map[int32]string{
0: "CERO",
1: "UNO",
@ -104,6 +124,24 @@ type Scalars struct {
XXX_sizecache int32 `json:"-"`
}
type xxx_Scalars struct{ m *Scalars }
func (m *Scalars) ProtoReflect() protoreflect.Message {
return xxx_Scalars{m}
}
func (m xxx_Scalars) Type() protoreflect.MessageType {
return xxx_Test_ProtoFile_MessageTypes[0].Type
}
func (m xxx_Scalars) KnownFields() protoreflect.KnownFields {
return xxx_Test_ProtoFile_MessageTypes[0].KnownFieldsOf(m.m)
}
func (m xxx_Scalars) UnknownFields() protoreflect.UnknownFields {
return xxx_Test_ProtoFile_MessageTypes[0].UnknownFieldsOf(m.m)
}
func (m xxx_Scalars) Interface() protoreflect.ProtoMessage {
return m.m
}
func (m *Scalars) Reset() { *m = Scalars{} }
func (m *Scalars) String() string { return proto.CompactTextString(m) }
func (*Scalars) ProtoMessage() {}
@ -243,6 +281,24 @@ type Enums struct {
XXX_sizecache int32 `json:"-"`
}
type xxx_Enums struct{ m *Enums }
func (m *Enums) ProtoReflect() protoreflect.Message {
return xxx_Enums{m}
}
func (m xxx_Enums) Type() protoreflect.MessageType {
return xxx_Test_ProtoFile_MessageTypes[1].Type
}
func (m xxx_Enums) KnownFields() protoreflect.KnownFields {
return xxx_Test_ProtoFile_MessageTypes[1].KnownFieldsOf(m.m)
}
func (m xxx_Enums) UnknownFields() protoreflect.UnknownFields {
return xxx_Test_ProtoFile_MessageTypes[1].UnknownFieldsOf(m.m)
}
func (m xxx_Enums) Interface() protoreflect.ProtoMessage {
return m.m
}
func (m *Enums) Reset() { *m = Enums{} }
func (m *Enums) String() string { return proto.CompactTextString(m) }
func (*Enums) ProtoMessage() {}
@ -290,6 +346,24 @@ type Nests struct {
XXX_sizecache int32 `json:"-"`
}
type xxx_Nests struct{ m *Nests }
func (m *Nests) ProtoReflect() protoreflect.Message {
return xxx_Nests{m}
}
func (m xxx_Nests) Type() protoreflect.MessageType {
return xxx_Test_ProtoFile_MessageTypes[2].Type
}
func (m xxx_Nests) KnownFields() protoreflect.KnownFields {
return xxx_Test_ProtoFile_MessageTypes[2].KnownFieldsOf(m.m)
}
func (m xxx_Nests) UnknownFields() protoreflect.UnknownFields {
return xxx_Test_ProtoFile_MessageTypes[2].UnknownFieldsOf(m.m)
}
func (m xxx_Nests) Interface() protoreflect.ProtoMessage {
return m.m
}
func (m *Nests) Reset() { *m = Nests{} }
func (m *Nests) String() string { return proto.CompactTextString(m) }
func (*Nests) ProtoMessage() {}
@ -331,6 +405,24 @@ type Nested struct {
XXX_sizecache int32 `json:"-"`
}
type xxx_Nested struct{ m *Nested }
func (m *Nested) ProtoReflect() protoreflect.Message {
return xxx_Nested{m}
}
func (m xxx_Nested) Type() protoreflect.MessageType {
return xxx_Test_ProtoFile_MessageTypes[3].Type
}
func (m xxx_Nested) KnownFields() protoreflect.KnownFields {
return xxx_Test_ProtoFile_MessageTypes[3].KnownFieldsOf(m.m)
}
func (m xxx_Nested) UnknownFields() protoreflect.UnknownFields {
return xxx_Test_ProtoFile_MessageTypes[3].UnknownFieldsOf(m.m)
}
func (m xxx_Nested) Interface() protoreflect.ProtoMessage {
return m.m
}
func (m *Nested) Reset() { *m = Nested{} }
func (m *Nested) String() string { return proto.CompactTextString(m) }
func (*Nested) ProtoMessage() {}
@ -371,6 +463,7 @@ func (m *Nested) GetSNested() *Nested {
}
func init() {
proto.RegisterFile("encoding/textpb/testprotos/pb3/test.proto", fileDescriptor_0854715c5b41c422)
proto.RegisterEnum("pb3.Enum", Enum_name, Enum_value)
proto.RegisterEnum("pb3.Enums_NestedEnum", Enums_NestedEnum_name, Enums_NestedEnum_value)
proto.RegisterType((*Scalars)(nil), "pb3.Scalars")
@ -379,10 +472,6 @@ func init() {
proto.RegisterType((*Nested)(nil), "pb3.Nested")
}
func init() {
proto.RegisterFile("encoding/textpb/testprotos/pb3/test.proto", fileDescriptor_0854715c5b41c422)
}
var fileDescriptor_0854715c5b41c422 = []byte{
// 503 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x93, 0xdf, 0xab, 0xd3, 0x30,
@ -418,3 +507,270 @@ var fileDescriptor_0854715c5b41c422 = []byte{
0x19, 0x85, 0xff, 0x7f, 0x36, 0xb9, 0x67, 0xea, 0xf8, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xd9,
0x7d, 0x94, 0x3a, 0x5f, 0x03, 0x00, 0x00,
}
func init() {
xxx_Test_ProtoFile_FileDesc.Enums = xxx_Test_ProtoFile_EnumDescs[0:1]
xxx_Test_ProtoFile_FileDesc.Messages = xxx_Test_ProtoFile_MessageDescs[0:4]
xxx_Test_ProtoFile_MessageDescs[1].Enums = xxx_Test_ProtoFile_EnumDescs[1:2]
xxx_Test_ProtoFile_MessageDescs[1].Fields[0].EnumType = xxx_Test_ProtoFile_EnumTypes[0]
xxx_Test_ProtoFile_MessageDescs[1].Fields[1].EnumType = xxx_Test_ProtoFile_EnumTypes[1]
xxx_Test_ProtoFile_MessageDescs[2].Fields[0].MessageType = xxx_Test_ProtoFile_MessageTypes[3].Type
xxx_Test_ProtoFile_MessageDescs[3].Fields[1].MessageType = xxx_Test_ProtoFile_MessageTypes[3].Type
var err error
Test_ProtoFile, err = prototype.NewFile(&xxx_Test_ProtoFile_FileDesc)
if err != nil {
panic(err)
}
}
const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
var Test_ProtoFile protoreflect.FileDescriptor
var xxx_Test_ProtoFile_FileDesc = prototype.File{
Syntax: protoreflect.Proto3,
Path: "encoding/textpb/testprotos/pb3/test.proto",
Package: "pb3",
}
var xxx_Test_ProtoFile_EnumTypes = [2]protoreflect.EnumType{
prototype.GoEnum(
xxx_Test_ProtoFile_EnumDescs[0].Reference(),
func(_ protoreflect.EnumType, n protoreflect.EnumNumber) protoreflect.ProtoEnum {
return Enum(n)
},
),
prototype.GoEnum(
xxx_Test_ProtoFile_EnumDescs[1].Reference(),
func(_ protoreflect.EnumType, n protoreflect.EnumNumber) protoreflect.ProtoEnum {
return Enums_NestedEnum(n)
},
),
}
var xxx_Test_ProtoFile_EnumDescs = [2]prototype.Enum{
{
Name: "Enum",
Values: []prototype.EnumValue{
{Name: "ZERO", Number: 0},
{Name: "ONE", Number: 1},
{Name: "TWO", Number: 2},
{Name: "TEN", Number: 10},
},
},
{
Name: "NestedEnum",
Values: []prototype.EnumValue{
{Name: "CERO", Number: 0},
{Name: "UNO", Number: 1},
{Name: "DOS", Number: 2},
{Name: "DIEZ", Number: 10},
},
},
}
var xxx_Test_ProtoFile_MessageTypes = [4]protoimpl.MessageType{
{Type: prototype.GoMessage(
xxx_Test_ProtoFile_MessageDescs[0].Reference(),
func(protoreflect.MessageType) protoreflect.ProtoMessage {
return new(Scalars)
},
)},
{Type: prototype.GoMessage(
xxx_Test_ProtoFile_MessageDescs[1].Reference(),
func(protoreflect.MessageType) protoreflect.ProtoMessage {
return new(Enums)
},
)},
{Type: prototype.GoMessage(
xxx_Test_ProtoFile_MessageDescs[2].Reference(),
func(protoreflect.MessageType) protoreflect.ProtoMessage {
return new(Nests)
},
)},
{Type: prototype.GoMessage(
xxx_Test_ProtoFile_MessageDescs[3].Reference(),
func(protoreflect.MessageType) protoreflect.ProtoMessage {
return new(Nested)
},
)},
}
var xxx_Test_ProtoFile_MessageDescs = [4]prototype.Message{
{
Name: "Scalars",
Fields: []prototype.Field{
{
Name: "s_bool",
Number: 1,
Cardinality: protoreflect.Optional,
Kind: protoreflect.BoolKind,
JSONName: "sBool",
IsPacked: prototype.False,
},
{
Name: "s_int32",
Number: 2,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Int32Kind,
JSONName: "sInt32",
IsPacked: prototype.False,
},
{
Name: "s_int64",
Number: 3,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Int64Kind,
JSONName: "sInt64",
IsPacked: prototype.False,
},
{
Name: "s_uint32",
Number: 4,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Uint32Kind,
JSONName: "sUint32",
IsPacked: prototype.False,
},
{
Name: "s_uint64",
Number: 5,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Uint64Kind,
JSONName: "sUint64",
IsPacked: prototype.False,
},
{
Name: "s_sint32",
Number: 6,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Sint32Kind,
JSONName: "sSint32",
IsPacked: prototype.False,
},
{
Name: "s_sint64",
Number: 7,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Sint64Kind,
JSONName: "sSint64",
IsPacked: prototype.False,
},
{
Name: "s_fixed32",
Number: 8,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Fixed32Kind,
JSONName: "sFixed32",
IsPacked: prototype.False,
},
{
Name: "s_fixed64",
Number: 9,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Fixed64Kind,
JSONName: "sFixed64",
IsPacked: prototype.False,
},
{
Name: "s_sfixed32",
Number: 10,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Sfixed32Kind,
JSONName: "sSfixed32",
IsPacked: prototype.False,
},
{
Name: "s_sfixed64",
Number: 11,
Cardinality: protoreflect.Optional,
Kind: protoreflect.Sfixed64Kind,
JSONName: "sSfixed64",
IsPacked: prototype.False,
},
{
Name: "s_float",
Number: 20,
Cardinality: protoreflect.Optional,
Kind: protoreflect.FloatKind,
JSONName: "sFloat",
IsPacked: prototype.False,
},
{
Name: "s_double",
Number: 21,
Cardinality: protoreflect.Optional,
Kind: protoreflect.DoubleKind,
JSONName: "sDouble",
IsPacked: prototype.False,
},
{
Name: "s_bytes",
Number: 14,
Cardinality: protoreflect.Optional,
Kind: protoreflect.BytesKind,
JSONName: "sBytes",
IsPacked: prototype.False,
},
{
Name: "s_string",
Number: 13,
Cardinality: protoreflect.Optional,
Kind: protoreflect.StringKind,
JSONName: "sString",
IsPacked: prototype.False,
},
},
},
{
Name: "Enums",
Fields: []prototype.Field{
{
Name: "s_enum",
Number: 1,
Cardinality: protoreflect.Optional,
Kind: protoreflect.EnumKind,
JSONName: "sEnum",
IsPacked: prototype.False,
},
{
Name: "s_nested_enum",
Number: 3,
Cardinality: protoreflect.Optional,
Kind: protoreflect.EnumKind,
JSONName: "sNestedEnum",
IsPacked: prototype.False,
},
},
},
{
Name: "Nests",
Fields: []prototype.Field{
{
Name: "s_nested",
Number: 1,
Cardinality: protoreflect.Optional,
Kind: protoreflect.MessageKind,
JSONName: "sNested",
IsPacked: prototype.False,
},
},
},
{
Name: "Nested",
Fields: []prototype.Field{
{
Name: "s_string",
Number: 1,
Cardinality: protoreflect.Optional,
Kind: protoreflect.StringKind,
JSONName: "sString",
IsPacked: prototype.False,
},
{
Name: "s_nested",
Number: 2,
Cardinality: protoreflect.Optional,
Kind: protoreflect.MessageKind,
JSONName: "sNested",
IsPacked: prototype.False,
},
},
},
}

View File

@ -47,7 +47,6 @@ PROTOC_GEN_GO_ENABLE_REFLECT=1 protoc -I$tmpdir/src \
$tmpdir/src/google/protobuf/compiler/plugin.proto
cp $tmpdir/src/google/protobuf/compiler/plugin.pb.go ./types/plugin/plugin.pb.go
# TODO: Uncomment when test protos are ready.
# echo "# encoding/textpb/testprotos/pb?/test.proto"
# PROTOC_GEN_GO_ENABLE_REFLECT=1 protoc --go_out=paths=source_relative:. \
# encoding/textpb/testprotos/pb?/test.proto
echo "# encoding/textpb/testprotos/pb?/test.proto"
PROTOC_GEN_GO_ENABLE_REFLECT=1 protoc --go_out=paths=source_relative:. \
encoding/textpb/testprotos/pb?/test.proto