extension_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 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 impl_test
  5. import (
  6. "fmt"
  7. "testing"
  8. cmp "github.com/google/go-cmp/cmp"
  9. testpb "google.golang.org/protobuf/internal/testprotos/test"
  10. "google.golang.org/protobuf/proto"
  11. pref "google.golang.org/protobuf/reflect/protoreflect"
  12. )
  13. func TestExtensionType(t *testing.T) {
  14. cmpOpts := cmp.Options{
  15. cmp.Comparer(func(x, y proto.Message) bool {
  16. return proto.Equal(x, y)
  17. }),
  18. }
  19. for _, test := range []struct {
  20. xt pref.ExtensionType
  21. value interface{}
  22. }{
  23. {
  24. xt: testpb.E_OptionalInt32Extension,
  25. value: int32(0),
  26. },
  27. {
  28. xt: testpb.E_OptionalInt64Extension,
  29. value: int64(0),
  30. },
  31. {
  32. xt: testpb.E_OptionalUint32Extension,
  33. value: uint32(0),
  34. },
  35. {
  36. xt: testpb.E_OptionalUint64Extension,
  37. value: uint64(0),
  38. },
  39. {
  40. xt: testpb.E_OptionalFloatExtension,
  41. value: float32(0),
  42. },
  43. {
  44. xt: testpb.E_OptionalDoubleExtension,
  45. value: float64(0),
  46. },
  47. {
  48. xt: testpb.E_OptionalBoolExtension,
  49. value: true,
  50. },
  51. {
  52. xt: testpb.E_OptionalStringExtension,
  53. value: "",
  54. },
  55. {
  56. xt: testpb.E_OptionalBytesExtension,
  57. value: []byte{},
  58. },
  59. {
  60. xt: testpb.E_OptionalNestedMessageExtension,
  61. value: &testpb.TestAllTypes_NestedMessage{},
  62. },
  63. {
  64. xt: testpb.E_OptionalNestedEnumExtension,
  65. value: testpb.TestAllTypes_FOO,
  66. },
  67. {
  68. xt: testpb.E_RepeatedInt32Extension,
  69. value: []int32{0},
  70. },
  71. {
  72. xt: testpb.E_RepeatedInt64Extension,
  73. value: []int64{0},
  74. },
  75. {
  76. xt: testpb.E_RepeatedUint32Extension,
  77. value: []uint32{0},
  78. },
  79. {
  80. xt: testpb.E_RepeatedUint64Extension,
  81. value: []uint64{0},
  82. },
  83. {
  84. xt: testpb.E_RepeatedFloatExtension,
  85. value: []float32{0},
  86. },
  87. {
  88. xt: testpb.E_RepeatedDoubleExtension,
  89. value: []float64{0},
  90. },
  91. {
  92. xt: testpb.E_RepeatedBoolExtension,
  93. value: []bool{true},
  94. },
  95. {
  96. xt: testpb.E_RepeatedStringExtension,
  97. value: []string{""},
  98. },
  99. {
  100. xt: testpb.E_RepeatedBytesExtension,
  101. value: [][]byte{nil},
  102. },
  103. {
  104. xt: testpb.E_RepeatedNestedMessageExtension,
  105. value: []*testpb.TestAllTypes_NestedMessage{{}},
  106. },
  107. {
  108. xt: testpb.E_RepeatedNestedEnumExtension,
  109. value: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO},
  110. },
  111. } {
  112. name := test.xt.TypeDescriptor().FullName()
  113. t.Run(fmt.Sprint(name), func(t *testing.T) {
  114. if !test.xt.IsValidInterface(test.value) {
  115. t.Fatalf("IsValidInterface(%[1]T(%[1]v)) = false, want true", test.value)
  116. }
  117. v := test.xt.ValueOf(test.value)
  118. if !test.xt.IsValidValue(v) {
  119. t.Fatalf("IsValidValue(%[1]T(%[1]v)) = false, want true", v)
  120. }
  121. if got, want := test.xt.InterfaceOf(v), test.value; !cmp.Equal(got, want, cmpOpts) {
  122. t.Fatalf("round trip InterfaceOf(ValueOf(x)) = %v, want %v", got, want)
  123. }
  124. })
  125. }
  126. }