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