blob: 171f31222fc1120ddc9e1c81080c354bf8660346 [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"
Joe Tsaie0daf312020-02-25 12:51:10 -080012
Damien Neil01fdc632019-10-04 10:34:01 -070013 "google.golang.org/protobuf/proto"
Damien Neil835b2712019-08-29 14:08:28 -070014 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsai55f18252020-01-11 00:25:01 -080015
16 testpb "google.golang.org/protobuf/internal/testprotos/test"
Damien Neil835b2712019-08-29 14:08:28 -070017)
18
19func TestExtensionType(t *testing.T) {
20 cmpOpts := cmp.Options{
21 cmp.Comparer(func(x, y proto.Message) bool {
22 return proto.Equal(x, y)
23 }),
24 }
25 for _, test := range []struct {
26 xt pref.ExtensionType
27 value interface{}
28 }{
29 {
Damien Neild025c952020-02-02 00:53:34 -080030 xt: testpb.E_OptionalInt32,
Damien Neil835b2712019-08-29 14:08:28 -070031 value: int32(0),
32 },
33 {
Damien Neild025c952020-02-02 00:53:34 -080034 xt: testpb.E_OptionalInt64,
Damien Neil835b2712019-08-29 14:08:28 -070035 value: int64(0),
36 },
37 {
Damien Neild025c952020-02-02 00:53:34 -080038 xt: testpb.E_OptionalUint32,
Damien Neil835b2712019-08-29 14:08:28 -070039 value: uint32(0),
40 },
41 {
Damien Neild025c952020-02-02 00:53:34 -080042 xt: testpb.E_OptionalUint64,
Damien Neil835b2712019-08-29 14:08:28 -070043 value: uint64(0),
44 },
45 {
Damien Neild025c952020-02-02 00:53:34 -080046 xt: testpb.E_OptionalFloat,
Damien Neil835b2712019-08-29 14:08:28 -070047 value: float32(0),
48 },
49 {
Damien Neild025c952020-02-02 00:53:34 -080050 xt: testpb.E_OptionalDouble,
Damien Neil835b2712019-08-29 14:08:28 -070051 value: float64(0),
52 },
53 {
Damien Neild025c952020-02-02 00:53:34 -080054 xt: testpb.E_OptionalBool,
Damien Neil835b2712019-08-29 14:08:28 -070055 value: true,
56 },
57 {
Damien Neild025c952020-02-02 00:53:34 -080058 xt: testpb.E_OptionalString,
Damien Neil835b2712019-08-29 14:08:28 -070059 value: "",
60 },
61 {
Damien Neild025c952020-02-02 00:53:34 -080062 xt: testpb.E_OptionalBytes,
Damien Neil835b2712019-08-29 14:08:28 -070063 value: []byte{},
64 },
65 {
Damien Neild025c952020-02-02 00:53:34 -080066 xt: testpb.E_OptionalNestedMessage,
Damien Neil212b05b2020-01-28 13:11:20 -080067 value: &testpb.TestAllExtensions_NestedMessage{},
Damien Neil835b2712019-08-29 14:08:28 -070068 },
69 {
Damien Neild025c952020-02-02 00:53:34 -080070 xt: testpb.E_OptionalNestedEnum,
Damien Neil835b2712019-08-29 14:08:28 -070071 value: testpb.TestAllTypes_FOO,
72 },
73 {
Damien Neild025c952020-02-02 00:53:34 -080074 xt: testpb.E_RepeatedInt32,
Damien Neil835b2712019-08-29 14:08:28 -070075 value: []int32{0},
76 },
77 {
Damien Neild025c952020-02-02 00:53:34 -080078 xt: testpb.E_RepeatedInt64,
Damien Neil835b2712019-08-29 14:08:28 -070079 value: []int64{0},
80 },
81 {
Damien Neild025c952020-02-02 00:53:34 -080082 xt: testpb.E_RepeatedUint32,
Damien Neil835b2712019-08-29 14:08:28 -070083 value: []uint32{0},
84 },
85 {
Damien Neild025c952020-02-02 00:53:34 -080086 xt: testpb.E_RepeatedUint64,
Damien Neil835b2712019-08-29 14:08:28 -070087 value: []uint64{0},
88 },
89 {
Damien Neild025c952020-02-02 00:53:34 -080090 xt: testpb.E_RepeatedFloat,
Damien Neil835b2712019-08-29 14:08:28 -070091 value: []float32{0},
92 },
93 {
Damien Neild025c952020-02-02 00:53:34 -080094 xt: testpb.E_RepeatedDouble,
Damien Neil835b2712019-08-29 14:08:28 -070095 value: []float64{0},
96 },
97 {
Damien Neild025c952020-02-02 00:53:34 -080098 xt: testpb.E_RepeatedBool,
Damien Neil835b2712019-08-29 14:08:28 -070099 value: []bool{true},
100 },
101 {
Damien Neild025c952020-02-02 00:53:34 -0800102 xt: testpb.E_RepeatedString,
Damien Neil835b2712019-08-29 14:08:28 -0700103 value: []string{""},
104 },
105 {
Damien Neild025c952020-02-02 00:53:34 -0800106 xt: testpb.E_RepeatedBytes,
Damien Neil835b2712019-08-29 14:08:28 -0700107 value: [][]byte{nil},
108 },
109 {
Damien Neild025c952020-02-02 00:53:34 -0800110 xt: testpb.E_RepeatedNestedMessage,
Damien Neil212b05b2020-01-28 13:11:20 -0800111 value: []*testpb.TestAllExtensions_NestedMessage{{}},
Damien Neil835b2712019-08-29 14:08:28 -0700112 },
113 {
Damien Neild025c952020-02-02 00:53:34 -0800114 xt: testpb.E_RepeatedNestedEnum,
Damien Neil835b2712019-08-29 14:08:28 -0700115 value: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO},
116 },
117 } {
118 name := test.xt.TypeDescriptor().FullName()
119 t.Run(fmt.Sprint(name), func(t *testing.T) {
120 if !test.xt.IsValidInterface(test.value) {
121 t.Fatalf("IsValidInterface(%[1]T(%[1]v)) = false, want true", test.value)
122 }
123 v := test.xt.ValueOf(test.value)
124 if !test.xt.IsValidValue(v) {
125 t.Fatalf("IsValidValue(%[1]T(%[1]v)) = false, want true", v)
126 }
127 if got, want := test.xt.InterfaceOf(v), test.value; !cmp.Equal(got, want, cmpOpts) {
128 t.Fatalf("round trip InterfaceOf(ValueOf(x)) = %v, want %v", got, want)
129 }
130 })
131 }
132}