default_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package defval_test
  5. import (
  6. "math"
  7. "reflect"
  8. "testing"
  9. "google.golang.org/protobuf/internal/encoding/defval"
  10. "google.golang.org/protobuf/internal/filedesc"
  11. "google.golang.org/protobuf/reflect/protoreflect"
  12. )
  13. func Test(t *testing.T) {
  14. evs := filedesc.EnumValues{List: []filedesc.EnumValue{{}}}
  15. evs.List[0].L0.ParentFile = filedesc.SurrogateProto2
  16. evs.List[0].L0.FullName = "ALPHA"
  17. evs.List[0].L1.Number = 1
  18. V := protoreflect.ValueOf
  19. tests := []struct {
  20. val protoreflect.Value
  21. enum protoreflect.EnumValueDescriptor
  22. enums protoreflect.EnumValueDescriptors
  23. kind protoreflect.Kind
  24. strPB string
  25. strGo string
  26. }{{
  27. val: V(bool(true)),
  28. enum: nil,
  29. enums: nil,
  30. kind: protoreflect.BoolKind,
  31. strPB: "true",
  32. strGo: "1",
  33. }, {
  34. val: V(int32(-0x1234)),
  35. enum: nil,
  36. enums: nil,
  37. kind: protoreflect.Int32Kind,
  38. strPB: "-4660",
  39. strGo: "-4660",
  40. }, {
  41. val: V(float32(math.Pi)),
  42. enum: nil,
  43. enums: nil,
  44. kind: protoreflect.FloatKind,
  45. strPB: "3.1415927",
  46. strGo: "3.1415927",
  47. }, {
  48. val: V(float64(math.Pi)),
  49. enum: nil,
  50. enums: nil,
  51. kind: protoreflect.DoubleKind,
  52. strPB: "3.141592653589793",
  53. strGo: "3.141592653589793",
  54. }, {
  55. val: V(string("hello, \xde\xad\xbe\xef\n")),
  56. enum: nil,
  57. enums: nil,
  58. kind: protoreflect.StringKind,
  59. strPB: "hello, \xde\xad\xbe\xef\n",
  60. strGo: "hello, \xde\xad\xbe\xef\n",
  61. }, {
  62. val: V([]byte("hello, \xde\xad\xbe\xef\n")),
  63. enum: nil,
  64. enums: nil,
  65. kind: protoreflect.BytesKind,
  66. strPB: "hello, \\336\\255\\276\\357\\n",
  67. strGo: "hello, \\336\\255\\276\\357\\n",
  68. }, {
  69. val: V(protoreflect.EnumNumber(1)),
  70. enum: &evs.List[0],
  71. enums: &evs,
  72. kind: protoreflect.EnumKind,
  73. strPB: "ALPHA",
  74. strGo: "1",
  75. }}
  76. for _, tt := range tests {
  77. t.Run("", func(t *testing.T) {
  78. gotStr, _ := defval.Marshal(tt.val, tt.enum, tt.kind, defval.Descriptor)
  79. if gotStr != tt.strPB {
  80. t.Errorf("Marshal(%v, %v, Descriptor) = %q, want %q", tt.val, tt.kind, gotStr, tt.strPB)
  81. }
  82. gotStr, _ = defval.Marshal(tt.val, tt.enum, tt.kind, defval.GoTag)
  83. if gotStr != tt.strGo {
  84. t.Errorf("Marshal(%v, %v, GoTag) = %q, want %q", tt.val, tt.kind, gotStr, tt.strGo)
  85. }
  86. gotVal, gotEnum, _ := defval.Unmarshal(tt.strPB, tt.kind, tt.enums, defval.Descriptor)
  87. if !reflect.DeepEqual(gotVal.Interface(), tt.val.Interface()) || gotEnum != tt.enum {
  88. t.Errorf("Unmarshal(%v, %v, Descriptor) = (%q, %v), want (%q, %v)", tt.strPB, tt.kind, gotVal, gotEnum, tt.val, tt.enum)
  89. }
  90. gotVal, gotEnum, _ = defval.Unmarshal(tt.strGo, tt.kind, tt.enums, defval.GoTag)
  91. if !reflect.DeepEqual(gotVal.Interface(), tt.val.Interface()) || gotEnum != tt.enum {
  92. t.Errorf("Unmarshal(%v, %v, GoTag) = (%q, %v), want (%q, %v)", tt.strGo, tt.kind, gotVal, gotEnum, tt.val, tt.enum)
  93. }
  94. })
  95. }
  96. }