blob: 6f83d033bf592437d61c4e049965258196e9c1da [file] [log] [blame]
Damien Neil835b2712019-08-29 14:08:28 -07001// 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
5package impl_test
6
7import (
8 "fmt"
9 "testing"
10
Joe Tsai55f18252020-01-11 00:25:01 -080011 "github.com/google/go-cmp/cmp"
Damien Neil01fdc632019-10-04 10:34:01 -070012 "google.golang.org/protobuf/proto"
Damien Neil835b2712019-08-29 14:08:28 -070013 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsai55f18252020-01-11 00:25:01 -080014
15 testpb "google.golang.org/protobuf/internal/testprotos/test"
Damien Neil835b2712019-08-29 14:08:28 -070016)
17
18func TestExtensionType(t *testing.T) {
19 cmpOpts := cmp.Options{
20 cmp.Comparer(func(x, y proto.Message) bool {
21 return proto.Equal(x, y)
22 }),
23 }
24 for _, test := range []struct {
25 xt pref.ExtensionType
26 value interface{}
27 }{
28 {
Damien Neild025c952020-02-02 00:53:34 -080029 xt: testpb.E_OptionalInt32,
Damien Neil835b2712019-08-29 14:08:28 -070030 value: int32(0),
31 },
32 {
Damien Neild025c952020-02-02 00:53:34 -080033 xt: testpb.E_OptionalInt64,
Damien Neil835b2712019-08-29 14:08:28 -070034 value: int64(0),
35 },
36 {
Damien Neild025c952020-02-02 00:53:34 -080037 xt: testpb.E_OptionalUint32,
Damien Neil835b2712019-08-29 14:08:28 -070038 value: uint32(0),
39 },
40 {
Damien Neild025c952020-02-02 00:53:34 -080041 xt: testpb.E_OptionalUint64,
Damien Neil835b2712019-08-29 14:08:28 -070042 value: uint64(0),
43 },
44 {
Damien Neild025c952020-02-02 00:53:34 -080045 xt: testpb.E_OptionalFloat,
Damien Neil835b2712019-08-29 14:08:28 -070046 value: float32(0),
47 },
48 {
Damien Neild025c952020-02-02 00:53:34 -080049 xt: testpb.E_OptionalDouble,
Damien Neil835b2712019-08-29 14:08:28 -070050 value: float64(0),
51 },
52 {
Damien Neild025c952020-02-02 00:53:34 -080053 xt: testpb.E_OptionalBool,
Damien Neil835b2712019-08-29 14:08:28 -070054 value: true,
55 },
56 {
Damien Neild025c952020-02-02 00:53:34 -080057 xt: testpb.E_OptionalString,
Damien Neil835b2712019-08-29 14:08:28 -070058 value: "",
59 },
60 {
Damien Neild025c952020-02-02 00:53:34 -080061 xt: testpb.E_OptionalBytes,
Damien Neil835b2712019-08-29 14:08:28 -070062 value: []byte{},
63 },
64 {
Damien Neild025c952020-02-02 00:53:34 -080065 xt: testpb.E_OptionalNestedMessage,
Damien Neil212b05b2020-01-28 13:11:20 -080066 value: &testpb.TestAllExtensions_NestedMessage{},
Damien Neil835b2712019-08-29 14:08:28 -070067 },
68 {
Damien Neild025c952020-02-02 00:53:34 -080069 xt: testpb.E_OptionalNestedEnum,
Damien Neil835b2712019-08-29 14:08:28 -070070 value: testpb.TestAllTypes_FOO,
71 },
72 {
Damien Neild025c952020-02-02 00:53:34 -080073 xt: testpb.E_RepeatedInt32,
Damien Neil835b2712019-08-29 14:08:28 -070074 value: []int32{0},
75 },
76 {
Damien Neild025c952020-02-02 00:53:34 -080077 xt: testpb.E_RepeatedInt64,
Damien Neil835b2712019-08-29 14:08:28 -070078 value: []int64{0},
79 },
80 {
Damien Neild025c952020-02-02 00:53:34 -080081 xt: testpb.E_RepeatedUint32,
Damien Neil835b2712019-08-29 14:08:28 -070082 value: []uint32{0},
83 },
84 {
Damien Neild025c952020-02-02 00:53:34 -080085 xt: testpb.E_RepeatedUint64,
Damien Neil835b2712019-08-29 14:08:28 -070086 value: []uint64{0},
87 },
88 {
Damien Neild025c952020-02-02 00:53:34 -080089 xt: testpb.E_RepeatedFloat,
Damien Neil835b2712019-08-29 14:08:28 -070090 value: []float32{0},
91 },
92 {
Damien Neild025c952020-02-02 00:53:34 -080093 xt: testpb.E_RepeatedDouble,
Damien Neil835b2712019-08-29 14:08:28 -070094 value: []float64{0},
95 },
96 {
Damien Neild025c952020-02-02 00:53:34 -080097 xt: testpb.E_RepeatedBool,
Damien Neil835b2712019-08-29 14:08:28 -070098 value: []bool{true},
99 },
100 {
Damien Neild025c952020-02-02 00:53:34 -0800101 xt: testpb.E_RepeatedString,
Damien Neil835b2712019-08-29 14:08:28 -0700102 value: []string{""},
103 },
104 {
Damien Neild025c952020-02-02 00:53:34 -0800105 xt: testpb.E_RepeatedBytes,
Damien Neil835b2712019-08-29 14:08:28 -0700106 value: [][]byte{nil},
107 },
108 {
Damien Neild025c952020-02-02 00:53:34 -0800109 xt: testpb.E_RepeatedNestedMessage,
Damien Neil212b05b2020-01-28 13:11:20 -0800110 value: []*testpb.TestAllExtensions_NestedMessage{{}},
Damien Neil835b2712019-08-29 14:08:28 -0700111 },
112 {
Damien Neild025c952020-02-02 00:53:34 -0800113 xt: testpb.E_RepeatedNestedEnum,
Damien Neil835b2712019-08-29 14:08:28 -0700114 value: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO},
115 },
116 } {
117 name := test.xt.TypeDescriptor().FullName()
118 t.Run(fmt.Sprint(name), func(t *testing.T) {
119 if !test.xt.IsValidInterface(test.value) {
120 t.Fatalf("IsValidInterface(%[1]T(%[1]v)) = false, want true", test.value)
121 }
122 v := test.xt.ValueOf(test.value)
123 if !test.xt.IsValidValue(v) {
124 t.Fatalf("IsValidValue(%[1]T(%[1]v)) = false, want true", v)
125 }
126 if got, want := test.xt.InterfaceOf(v), test.value; !cmp.Equal(got, want, cmpOpts) {
127 t.Fatalf("round trip InterfaceOf(ValueOf(x)) = %v, want %v", got, want)
128 }
129 })
130 }
131}