2019-08-29 21:08:28 +00:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package impl_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-11 08:25:01 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-02-25 20:51:10 +00:00
|
|
|
|
2019-10-04 17:34:01 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2022-05-23 20:12:23 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2020-01-11 08:25:01 +00:00
|
|
|
|
|
|
|
testpb "google.golang.org/protobuf/internal/testprotos/test"
|
2019-08-29 21:08:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExtensionType(t *testing.T) {
|
|
|
|
cmpOpts := cmp.Options{
|
|
|
|
cmp.Comparer(func(x, y proto.Message) bool {
|
|
|
|
return proto.Equal(x, y)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
for _, test := range []struct {
|
2022-05-23 20:12:23 +00:00
|
|
|
xt protoreflect.ExtensionType
|
2024-05-15 11:11:52 +00:00
|
|
|
value any
|
2019-08-29 21:08:28 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalInt32,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: int32(0),
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalInt64,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: int64(0),
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalUint32,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: uint32(0),
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalUint64,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: uint64(0),
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalFloat,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: float32(0),
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalDouble,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: float64(0),
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalBool,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: true,
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalString,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: "",
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalBytes,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []byte{},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalNestedMessage,
|
2020-01-28 21:11:20 +00:00
|
|
|
value: &testpb.TestAllExtensions_NestedMessage{},
|
2019-08-29 21:08:28 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_OptionalNestedEnum,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: testpb.TestAllTypes_FOO,
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedInt32,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []int32{0},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedInt64,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []int64{0},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedUint32,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []uint32{0},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedUint64,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []uint64{0},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedFloat,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []float32{0},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedDouble,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []float64{0},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedBool,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []bool{true},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedString,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []string{""},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedBytes,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: [][]byte{nil},
|
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedNestedMessage,
|
2020-01-28 21:11:20 +00:00
|
|
|
value: []*testpb.TestAllExtensions_NestedMessage{{}},
|
2019-08-29 21:08:28 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-02 08:53:34 +00:00
|
|
|
xt: testpb.E_RepeatedNestedEnum,
|
2019-08-29 21:08:28 +00:00
|
|
|
value: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
name := test.xt.TypeDescriptor().FullName()
|
|
|
|
t.Run(fmt.Sprint(name), func(t *testing.T) {
|
|
|
|
if !test.xt.IsValidInterface(test.value) {
|
|
|
|
t.Fatalf("IsValidInterface(%[1]T(%[1]v)) = false, want true", test.value)
|
|
|
|
}
|
|
|
|
v := test.xt.ValueOf(test.value)
|
|
|
|
if !test.xt.IsValidValue(v) {
|
|
|
|
t.Fatalf("IsValidValue(%[1]T(%[1]v)) = false, want true", v)
|
|
|
|
}
|
|
|
|
if got, want := test.xt.InterfaceOf(v), test.value; !cmp.Equal(got, want, cmpOpts) {
|
|
|
|
t.Fatalf("round trip InterfaceOf(ValueOf(x)) = %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|