File: oneof_get_test.go

package info (click to toggle)
golang-google-protobuf 1.36.7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 14,996 kB
  • sloc: sh: 94; makefile: 4
file content (273 lines) | stat: -rw-r--r-- 9,461 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright 2024 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 proto_test

import (
	"testing"

	testhybridpb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_hybrid"
	testopaquepb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_opaque"
)

func expectPanic(t *testing.T, f func(), fmt string, a ...any) {
	t.Helper()
	defer func() {
		t.Helper()
		if r := recover(); r == nil {
			t.Errorf(fmt, a...)
		}
	}()
	f()
}

func TestOpenGet(t *testing.T) {
	x := &testhybridpb.TestAllTypes{}

	tab := []struct {
		fName  string      // Field name (in proto)
		set    func()      // Set the field
		clear  func()      // Clear the field
		has    func() bool // Has for the field
		isZero func() bool // Get and return true if zero value
	}{
		{
			fName:  "oneof_uint32",
			set:    func() { x.SetOneofUint32(47) },
			clear:  func() { x.ClearOneofUint32() },
			has:    func() bool { return x.HasOneofUint32() },
			isZero: func() bool { return x.GetOneofUint32() == 0 },
		},
		{
			fName:  "oneof_nested_message",
			set:    func() { x.SetOneofNestedMessage(&testhybridpb.TestAllTypes_NestedMessage{}) },
			clear:  func() { x.ClearOneofNestedMessage() },
			has:    func() bool { return x.HasOneofNestedMessage() },
			isZero: func() bool { return x.GetOneofNestedMessage() == nil },
		},
		{
			fName:  "oneof_string",
			set:    func() { x.SetOneofString("test") },
			clear:  func() { x.ClearOneofString() },
			has:    func() bool { return x.HasOneofString() },
			isZero: func() bool { return x.GetOneofString() == "" },
		},
		{
			fName:  "oneof_bytes",
			set:    func() { x.SetOneofBytes([]byte("test")) },
			clear:  func() { x.ClearOneofBytes() },
			has:    func() bool { return x.HasOneofBytes() },
			isZero: func() bool { return len(x.GetOneofBytes()) == 0 },
		},
		{
			fName:  "oneof_bool",
			set:    func() { x.SetOneofBool(true) },
			clear:  func() { x.ClearOneofBool() },
			has:    func() bool { return x.HasOneofBool() },
			isZero: func() bool { return x.GetOneofBool() == false },
		},
		{
			fName:  "oneof_uint64",
			set:    func() { x.SetOneofUint64(7438109473104) },
			clear:  func() { x.ClearOneofUint64() },
			has:    func() bool { return x.HasOneofUint64() },
			isZero: func() bool { return x.GetOneofUint64() == 0 },
		},
		{
			fName:  "oneof_float",
			set:    func() { x.SetOneofFloat(3.1415) },
			clear:  func() { x.ClearOneofFloat() },
			has:    func() bool { return x.HasOneofFloat() },
			isZero: func() bool { return x.GetOneofFloat() == 0.0 },
		},
		{
			fName:  "oneof_double",
			set:    func() { x.SetOneofDouble(3e+8) },
			clear:  func() { x.ClearOneofDouble() },
			has:    func() bool { return x.HasOneofDouble() },
			isZero: func() bool { return x.GetOneofDouble() == 0.0 },
		},
		{
			fName:  "oneof_enum",
			set:    func() { x.SetOneofEnum(testhybridpb.TestAllTypes_BAZ) },
			clear:  func() { x.ClearOneofEnum() },
			has:    func() bool { return x.HasOneofEnum() },
			isZero: func() bool { return x.GetOneofEnum() == 0 },
		},
	}

	for i, mv := range tab {
		x.ClearOneofField()
		if got, want := x.HasOneofField(), false; got != want {
			t.Errorf("HasOneofField returned %v, expected %v", got, want)
		}
		if got, want := mv.isZero(), true; got != want {
			t.Errorf("Get on empty oneof member did not return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}
		mv.set()

		if got, want := x.HasOneofField(), true; got != want {
			t.Errorf("HasOneofField returned %v, expected %v (%s)", got, want, mv.fName)
		}
		if got, want := mv.isZero(), false; got != want {
			t.Errorf("Get on non-empty oneof member did return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}

		mv.clear()
		if got, want := x.HasOneofField(), false; got != want {
			t.Errorf("HasOneofField returned %v, expected %v (%s)", got, want, mv.fName)
		}
		if got, want := mv.isZero(), true; got != want {
			t.Errorf("Get on empty oneof member did not return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}
		other := tab[(i+1)%len(tab)]
		mv.set()
		other.set()

		if got, want := x.HasOneofField(), true; got != want {
			t.Errorf("HasOneofField returned %v, expected %v (%s)", got, want, mv.fName)
		}
		if got, want := mv.isZero(), true; got != want {
			t.Errorf("Get on wrong oneof member did not return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}
		other.clear()
		if got, want := x.HasOneofField(), false; got != want {
			t.Errorf("HasOneofField returned %v, expected %v (%s)", got, want, mv.fName)
		}
	}
	x = nil
	for _, mv := range tab {
		if got, want := mv.isZero(), true; got != want {
			t.Errorf("Get on nil receiver did not return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}
		if got, want := mv.has(), false; got != want {
			t.Errorf("Has on nil receiver failed, got %v, expected %v (%s)", got, want, mv.fName)
		}
	}
}

func TestOpaqueGet(t *testing.T) {
	x := &testopaquepb.TestAllTypes{}

	tab := []struct {
		fName  string      // Field name (in proto)
		set    func()      // Set the field
		clear  func()      // Clear the field
		has    func() bool // Has for the field
		isZero func() bool // Get and return true if zero value
	}{
		{
			fName:  "oneof_uint32",
			set:    func() { x.SetOneofUint32(47) },
			clear:  func() { x.ClearOneofUint32() },
			has:    func() bool { return x.HasOneofUint32() },
			isZero: func() bool { return x.GetOneofUint32() == 0 },
		},
		{
			fName:  "oneof_nested_message",
			set:    func() { x.SetOneofNestedMessage(&testopaquepb.TestAllTypes_NestedMessage{}) },
			clear:  func() { x.ClearOneofNestedMessage() },
			has:    func() bool { return x.HasOneofNestedMessage() },
			isZero: func() bool { return x.GetOneofNestedMessage() == nil },
		},
		{
			fName:  "oneof_string",
			set:    func() { x.SetOneofString("test") },
			clear:  func() { x.ClearOneofString() },
			has:    func() bool { return x.HasOneofString() },
			isZero: func() bool { return x.GetOneofString() == "" },
		},
		{
			fName:  "oneof_bytes",
			set:    func() { x.SetOneofBytes([]byte("test")) },
			clear:  func() { x.ClearOneofBytes() },
			has:    func() bool { return x.HasOneofBytes() },
			isZero: func() bool { return len(x.GetOneofBytes()) == 0 },
		},
		{
			fName:  "oneof_bool",
			set:    func() { x.SetOneofBool(true) },
			clear:  func() { x.ClearOneofBool() },
			has:    func() bool { return x.HasOneofBool() },
			isZero: func() bool { return x.GetOneofBool() == false },
		},
		{
			fName:  "oneof_uint64",
			set:    func() { x.SetOneofUint64(7438109473104) },
			clear:  func() { x.ClearOneofUint64() },
			has:    func() bool { return x.HasOneofUint64() },
			isZero: func() bool { return x.GetOneofUint64() == 0 },
		},
		{
			fName:  "oneof_float",
			set:    func() { x.SetOneofFloat(3.1415) },
			clear:  func() { x.ClearOneofFloat() },
			has:    func() bool { return x.HasOneofFloat() },
			isZero: func() bool { return x.GetOneofFloat() == 0.0 },
		},
		{
			fName:  "oneof_double",
			set:    func() { x.SetOneofDouble(3e+8) },
			clear:  func() { x.ClearOneofDouble() },
			has:    func() bool { return x.HasOneofDouble() },
			isZero: func() bool { return x.GetOneofDouble() == 0.0 },
		},
		{
			fName:  "oneof_enum",
			set:    func() { x.SetOneofEnum(testopaquepb.TestAllTypes_BAZ) },
			clear:  func() { x.ClearOneofEnum() },
			has:    func() bool { return x.HasOneofEnum() },
			isZero: func() bool { return x.GetOneofEnum() == 0 },
		},
	}

	for i, mv := range tab {
		x.ClearOneofField()
		if got, want := x.HasOneofField(), false; got != want {
			t.Errorf("HasOneofField returned %v, expected %v", got, want)
		}
		if got, want := mv.isZero(), true; got != want {
			t.Errorf("Get on empty oneof member did not return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}
		mv.set()

		if got, want := x.HasOneofField(), true; got != want {
			t.Errorf("HasOneofField returned %v, expected %v (%s)", got, want, mv.fName)
		}
		if got, want := mv.isZero(), false; got != want {
			t.Errorf("Get on non-empty oneof member did return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}

		mv.clear()
		if got, want := x.HasOneofField(), false; got != want {
			t.Errorf("HasOneofField returned %v, expected %v (%s)", got, want, mv.fName)
		}
		if got, want := mv.isZero(), true; got != want {
			t.Errorf("Get on empty oneof member did not return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}
		other := tab[(i+1)%len(tab)]
		mv.set()
		other.set()

		if got, want := x.HasOneofField(), true; got != want {
			t.Errorf("HasOneofField returned %v, expected %v (%s)", got, want, mv.fName)
		}
		if got, want := mv.isZero(), true; got != want {
			t.Errorf("Get on wrong oneof member did not return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}
		other.clear()
		if got, want := x.HasOneofField(), false; got != want {
			t.Errorf("HasOneofField returned %v, expected %v (%s)", got, want, mv.fName)
		}
	}
	x = nil
	for _, mv := range tab {
		if got, want := mv.isZero(), true; got != want {
			t.Errorf("Get on nil receiver did not return zero value, got %v, expected %v (%s)", got, want, mv.fName)
		}
		if got, want := mv.has(), false; got != want {
			t.Errorf("Has on nil receiver failed, got %v, expected %v (%s)", got, want, mv.fName)
		}
	}
}