other_test.go 4.1 KB

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