blob: 4bf0258e6c562d848a87f03f476e7c5fd7c3ab3e [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 {
29 xt: testpb.E_OptionalInt32Extension,
30 value: int32(0),
31 },
32 {
33 xt: testpb.E_OptionalInt64Extension,
34 value: int64(0),
35 },
36 {
37 xt: testpb.E_OptionalUint32Extension,
38 value: uint32(0),
39 },
40 {
41 xt: testpb.E_OptionalUint64Extension,
42 value: uint64(0),
43 },
44 {
45 xt: testpb.E_OptionalFloatExtension,
46 value: float32(0),
47 },
48 {
49 xt: testpb.E_OptionalDoubleExtension,
50 value: float64(0),
51 },
52 {
53 xt: testpb.E_OptionalBoolExtension,
54 value: true,
55 },
56 {
57 xt: testpb.E_OptionalStringExtension,
58 value: "",
59 },
60 {
61 xt: testpb.E_OptionalBytesExtension,
62 value: []byte{},
63 },
64 {
65 xt: testpb.E_OptionalNestedMessageExtension,
Damien Neil212b05b2020-01-28 13:11:20 -080066 value: &testpb.TestAllExtensions_NestedMessage{},
Damien Neil835b2712019-08-29 14:08:28 -070067 },
68 {
69 xt: testpb.E_OptionalNestedEnumExtension,
70 value: testpb.TestAllTypes_FOO,
71 },
72 {
73 xt: testpb.E_RepeatedInt32Extension,
74 value: []int32{0},
75 },
76 {
77 xt: testpb.E_RepeatedInt64Extension,
78 value: []int64{0},
79 },
80 {
81 xt: testpb.E_RepeatedUint32Extension,
82 value: []uint32{0},
83 },
84 {
85 xt: testpb.E_RepeatedUint64Extension,
86 value: []uint64{0},
87 },
88 {
89 xt: testpb.E_RepeatedFloatExtension,
90 value: []float32{0},
91 },
92 {
93 xt: testpb.E_RepeatedDoubleExtension,
94 value: []float64{0},
95 },
96 {
97 xt: testpb.E_RepeatedBoolExtension,
98 value: []bool{true},
99 },
100 {
101 xt: testpb.E_RepeatedStringExtension,
102 value: []string{""},
103 },
104 {
105 xt: testpb.E_RepeatedBytesExtension,
106 value: [][]byte{nil},
107 },
108 {
109 xt: testpb.E_RepeatedNestedMessageExtension,
Damien Neil212b05b2020-01-28 13:11:20 -0800110 value: []*testpb.TestAllExtensions_NestedMessage{{}},
Damien Neil835b2712019-08-29 14:08:28 -0700111 },
112 {
113 xt: testpb.E_RepeatedNestedEnumExtension,
114 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}