File: extension_test.go

package info (click to toggle)
golang-google-protobuf 1.36.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 16,816 kB
  • sloc: sh: 94; makefile: 4
file content (132 lines) | stat: -rw-r--r-- 2,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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"

	"github.com/google/go-cmp/cmp"

	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protoreflect"

	testpb "google.golang.org/protobuf/internal/testprotos/test"
)

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 {
		xt    protoreflect.ExtensionType
		value any
	}{
		{
			xt:    testpb.E_OptionalInt32,
			value: int32(0),
		},
		{
			xt:    testpb.E_OptionalInt64,
			value: int64(0),
		},
		{
			xt:    testpb.E_OptionalUint32,
			value: uint32(0),
		},
		{
			xt:    testpb.E_OptionalUint64,
			value: uint64(0),
		},
		{
			xt:    testpb.E_OptionalFloat,
			value: float32(0),
		},
		{
			xt:    testpb.E_OptionalDouble,
			value: float64(0),
		},
		{
			xt:    testpb.E_OptionalBool,
			value: true,
		},
		{
			xt:    testpb.E_OptionalString,
			value: "",
		},
		{
			xt:    testpb.E_OptionalBytes,
			value: []byte{},
		},
		{
			xt:    testpb.E_OptionalNestedMessage,
			value: &testpb.TestAllExtensions_NestedMessage{},
		},
		{
			xt:    testpb.E_OptionalNestedEnum,
			value: testpb.TestAllTypes_FOO,
		},
		{
			xt:    testpb.E_RepeatedInt32,
			value: []int32{0},
		},
		{
			xt:    testpb.E_RepeatedInt64,
			value: []int64{0},
		},
		{
			xt:    testpb.E_RepeatedUint32,
			value: []uint32{0},
		},
		{
			xt:    testpb.E_RepeatedUint64,
			value: []uint64{0},
		},
		{
			xt:    testpb.E_RepeatedFloat,
			value: []float32{0},
		},
		{
			xt:    testpb.E_RepeatedDouble,
			value: []float64{0},
		},
		{
			xt:    testpb.E_RepeatedBool,
			value: []bool{true},
		},
		{
			xt:    testpb.E_RepeatedString,
			value: []string{""},
		},
		{
			xt:    testpb.E_RepeatedBytes,
			value: [][]byte{nil},
		},
		{
			xt:    testpb.E_RepeatedNestedMessage,
			value: []*testpb.TestAllExtensions_NestedMessage{{}},
		},
		{
			xt:    testpb.E_RepeatedNestedEnum,
			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)
			}
		})
	}
}