blob: 907c444fa52f65ae6a24037fbc30fe5c615f305c [file] [log] [blame]
Herbie Ong800c9902018-12-06 15:28:53 -08001package textpb_test
2
3import (
4 "testing"
5
6 protoV1 "github.com/golang/protobuf/proto"
7 "github.com/golang/protobuf/v2/encoding/textpb"
8 "github.com/golang/protobuf/v2/encoding/textpb/testprotos/pb2"
Herbie Ong70651952018-12-13 14:19:50 -08009 "github.com/golang/protobuf/v2/proto"
Herbie Ong800c9902018-12-06 15:28:53 -080010
11 // The legacy package must be imported prior to use of any legacy messages.
12 // TODO: Remove this when protoV1 registers these hooks for you.
13 _ "github.com/golang/protobuf/v2/internal/legacy"
14
15 anypb "github.com/golang/protobuf/ptypes/any"
16 durpb "github.com/golang/protobuf/ptypes/duration"
17 emptypb "github.com/golang/protobuf/ptypes/empty"
18 stpb "github.com/golang/protobuf/ptypes/struct"
19 tspb "github.com/golang/protobuf/ptypes/timestamp"
20 wpb "github.com/golang/protobuf/ptypes/wrappers"
21)
22
23func TestRoundTrip(t *testing.T) {
24 tests := []struct {
25 desc string
Herbie Ong70651952018-12-13 14:19:50 -080026 message proto.Message
Herbie Ong800c9902018-12-06 15:28:53 -080027 }{{
28 desc: "well-known type fields set to empty messages",
29 message: &pb2.KnownTypes{
30 OptBool: &wpb.BoolValue{},
31 OptInt32: &wpb.Int32Value{},
32 OptInt64: &wpb.Int64Value{},
33 OptUint32: &wpb.UInt32Value{},
34 OptUint64: &wpb.UInt64Value{},
35 OptFloat: &wpb.FloatValue{},
36 OptDouble: &wpb.DoubleValue{},
37 OptString: &wpb.StringValue{},
38 OptBytes: &wpb.BytesValue{},
39 OptDuration: &durpb.Duration{},
40 OptTimestamp: &tspb.Timestamp{},
41 OptStruct: &stpb.Struct{},
42 OptList: &stpb.ListValue{},
43 OptValue: &stpb.Value{},
44 OptEmpty: &emptypb.Empty{},
45 OptAny: &anypb.Any{},
46 },
47 }, {
48 desc: "well-known type scalar fields",
49 message: &pb2.KnownTypes{
50 OptBool: &wpb.BoolValue{
51 Value: true,
52 },
53 OptInt32: &wpb.Int32Value{
54 Value: -42,
55 },
56 OptInt64: &wpb.Int64Value{
57 Value: -42,
58 },
59 OptUint32: &wpb.UInt32Value{
60 Value: 0xff,
61 },
62 OptUint64: &wpb.UInt64Value{
63 Value: 0xffff,
64 },
65 OptFloat: &wpb.FloatValue{
66 Value: 1.234,
67 },
68 OptDouble: &wpb.DoubleValue{
69 Value: 1.23e308,
70 },
71 OptString: &wpb.StringValue{
72 Value: "谷歌",
73 },
74 OptBytes: &wpb.BytesValue{
75 Value: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
76 },
77 },
78 }, {
79 desc: "well-known type time-related fields",
80 message: &pb2.KnownTypes{
81 OptDuration: &durpb.Duration{
82 Seconds: -3600,
83 Nanos: -123,
84 },
85 OptTimestamp: &tspb.Timestamp{
86 Seconds: 1257894000,
87 Nanos: 123,
88 },
89 },
90 }, {
91 desc: "well-known type struct field and different Value types",
92 message: &pb2.KnownTypes{
93 OptStruct: &stpb.Struct{
94 Fields: map[string]*stpb.Value{
95 "bool": &stpb.Value{
96 Kind: &stpb.Value_BoolValue{
97 BoolValue: true,
98 },
99 },
100 "double": &stpb.Value{
101 Kind: &stpb.Value_NumberValue{
102 NumberValue: 3.1415,
103 },
104 },
105 "null": &stpb.Value{
106 Kind: &stpb.Value_NullValue{
107 NullValue: stpb.NullValue_NULL_VALUE,
108 },
109 },
110 "string": &stpb.Value{
111 Kind: &stpb.Value_StringValue{
112 StringValue: "string",
113 },
114 },
115 "struct": &stpb.Value{
116 Kind: &stpb.Value_StructValue{
117 StructValue: &stpb.Struct{
118 Fields: map[string]*stpb.Value{
119 "bool": &stpb.Value{
120 Kind: &stpb.Value_BoolValue{
121 BoolValue: false,
122 },
123 },
124 },
125 },
126 },
127 },
128 "list": &stpb.Value{
129 Kind: &stpb.Value_ListValue{
130 ListValue: &stpb.ListValue{
131 Values: []*stpb.Value{
132 {
133 Kind: &stpb.Value_BoolValue{
134 BoolValue: false,
135 },
136 },
137 {
138 Kind: &stpb.Value_StringValue{
139 StringValue: "hello",
140 },
141 },
142 },
143 },
144 },
145 },
146 },
147 },
148 },
149 }}
150
151 for _, tt := range tests {
152 tt := tt
153 t.Run(tt.desc, func(t *testing.T) {
154 t.Parallel()
Herbie Ong70651952018-12-13 14:19:50 -0800155 b, err := textpb.Marshal(tt.message)
Herbie Ong800c9902018-12-06 15:28:53 -0800156 if err != nil {
157 t.Errorf("Marshal() returned error: %v\n\n", err)
158 }
Herbie Ong70651952018-12-13 14:19:50 -0800159 gotMessage := tt.message.ProtoReflect().Type().New()
160 err = textpb.Unmarshal(gotMessage, b)
Herbie Ong800c9902018-12-06 15:28:53 -0800161 if err != nil {
162 t.Errorf("Unmarshal() returned error: %v\n\n", err)
163 }
Herbie Ong70651952018-12-13 14:19:50 -0800164 if !protoV1.Equal(gotMessage.(protoV1.Message), tt.message.(protoV1.Message)) {
165 t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", gotMessage, tt.message)
166 }
Herbie Ong800c9902018-12-06 15:28:53 -0800167 })
168 }
169}