1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 //! Tests covering accessors for singular bool, int32, int64, and bytes fields.
9
10 use googletest::prelude::*;
11 use protobuf::prelude::*;
12
13 use protobuf::{Optional, ProtoBytes, ProtoStr, ProtoString};
14 use std::borrow::Cow;
15 use std::ffi::OsString;
16 use std::rc::Rc;
17 use std::sync::Arc;
18 use unittest_rust_proto::{test_all_types, TestAllTypes};
19
20 #[gtest]
test_default_accessors()21 fn test_default_accessors() {
22 let msg: TestAllTypes = Default::default();
23 assert_that!(
24 msg,
25 matches_pattern!(&TestAllTypes{
26 default_int32(): eq(41),
27 default_int64(): eq(42),
28 default_uint32(): eq(43),
29 default_uint64(): eq(44),
30 default_sint32(): eq(-45),
31 default_sint64(): eq(46),
32 default_fixed32(): eq(47),
33 default_fixed64(): eq(48),
34 default_sfixed32(): eq(49),
35 default_sfixed64(): eq(-50),
36 default_float(): eq(51.5),
37 default_double(): eq(52000.0),
38 default_bool(): eq(true),
39 })
40 );
41 assert_that!(msg.default_string(), eq("hello"));
42 assert_that!(msg.default_bytes(), eq("world".as_bytes()));
43 }
44
45 #[gtest]
test_optional_fixed32_accessors()46 fn test_optional_fixed32_accessors() {
47 let mut msg = TestAllTypes::new();
48 assert_that!(msg.has_optional_fixed32(), eq(false));
49 assert_that!(msg.optional_fixed32_opt(), eq(Optional::Unset(0)));
50 assert_that!(msg.optional_fixed32(), eq(0));
51
52 msg.set_optional_fixed32(7);
53 assert_that!(msg.has_optional_fixed32(), eq(true));
54 assert_that!(msg.optional_fixed32_opt(), eq(Optional::Set(7)));
55 assert_that!(msg.optional_fixed32(), eq(7));
56
57 msg.clear_optional_fixed32();
58 assert_that!(msg.has_optional_fixed32(), eq(false));
59 assert_that!(msg.optional_fixed32_opt(), eq(Optional::Unset(0)));
60 assert_that!(msg.optional_fixed32(), eq(0));
61 }
62
63 #[gtest]
test_default_fixed32_accessors()64 fn test_default_fixed32_accessors() {
65 let mut msg = TestAllTypes::new();
66 assert_that!(msg.default_fixed32(), eq(47));
67 assert_that!(msg.has_default_fixed32(), eq(false));
68 assert_that!(msg.default_fixed32_opt(), eq(Optional::Unset(47)));
69
70 msg.set_default_fixed32(7);
71 assert_that!(msg.default_fixed32(), eq(7));
72 assert_that!(msg.has_default_fixed32(), eq(true));
73 assert_that!(msg.default_fixed32_opt(), eq(Optional::Set(7)));
74
75 msg.clear_default_fixed32();
76 assert_that!(msg.default_fixed32(), eq(47));
77 assert_that!(msg.has_default_fixed32(), eq(false));
78 assert_that!(msg.default_fixed32_opt(), eq(Optional::Unset(47)));
79 }
80
81 #[gtest]
test_optional_fixed64_accessors()82 fn test_optional_fixed64_accessors() {
83 let mut msg = TestAllTypes::new();
84 assert_that!(msg.optional_fixed64_opt(), eq(Optional::Unset(0)));
85 assert_that!(msg.optional_fixed64(), eq(0));
86
87 msg.set_optional_fixed64(99);
88 assert_that!(msg.optional_fixed64_opt(), eq(Optional::Set(99)));
89 assert_that!(msg.optional_fixed64(), eq(99));
90
91 msg.set_optional_fixed64(2000);
92 assert_that!(msg.optional_fixed64_opt(), eq(Optional::Set(2000)));
93 assert_that!(msg.optional_fixed64(), eq(2000));
94
95 msg.clear_optional_fixed64();
96 assert_that!(msg.optional_fixed64_opt(), eq(Optional::Unset(0)));
97 assert_that!(msg.optional_fixed64(), eq(0));
98 }
99
100 #[gtest]
test_default_fixed64_accessors()101 fn test_default_fixed64_accessors() {
102 let mut msg = TestAllTypes::new();
103 assert_that!(msg.default_fixed64(), eq(48));
104 assert_that!(msg.default_fixed64_opt(), eq(Optional::Unset(48)));
105
106 msg.set_default_fixed64(4);
107 assert_that!(msg.default_fixed64(), eq(4));
108 assert_that!(msg.default_fixed64_opt(), eq(Optional::Set(4)));
109
110 msg.set_default_fixed64(999);
111 assert_that!(msg.default_fixed64(), eq(999));
112 assert_that!(msg.default_fixed64_opt(), eq(Optional::Set(999)));
113
114 msg.clear_default_fixed64();
115 assert_that!(msg.default_fixed64(), eq(48));
116 assert_that!(msg.default_fixed64_opt(), eq(Optional::Unset(48)));
117 }
118
119 #[gtest]
test_optional_int32_accessors()120 fn test_optional_int32_accessors() {
121 let mut msg = TestAllTypes::new();
122 assert_that!(msg.optional_int32_opt(), eq(Optional::Unset(0)));
123 assert_that!(msg.optional_int32(), eq(0));
124
125 msg.set_optional_int32(0);
126 assert_that!(msg.optional_int32_opt(), eq(Optional::Set(0)));
127 assert_that!(msg.optional_int32(), eq(0));
128
129 msg.set_optional_int32(1);
130 assert_that!(msg.optional_int32_opt(), eq(Optional::Set(1)));
131 assert_that!(msg.optional_int32(), eq(1));
132
133 msg.clear_optional_int32();
134 assert_that!(msg.optional_int32_opt(), eq(Optional::Unset(0)));
135 assert_that!(msg.optional_int32(), eq(0));
136 }
137
138 #[gtest]
test_default_int32_accessors()139 fn test_default_int32_accessors() {
140 let mut msg = TestAllTypes::new();
141 assert_that!(msg.default_int32(), eq(41));
142 assert_that!(msg.default_int32_opt(), eq(Optional::Unset(41)));
143
144 msg.set_default_int32(41);
145 assert_that!(msg.default_int32(), eq(41));
146 assert_that!(msg.default_int32_opt(), eq(Optional::Set(41)));
147
148 msg.clear_default_int32();
149 assert_that!(msg.default_int32(), eq(41));
150 assert_that!(msg.default_int32_opt(), eq(Optional::Unset(41)));
151
152 msg.set_default_int32(999);
153 assert_that!(msg.default_int32(), eq(999));
154 assert_that!(msg.default_int32_opt(), eq(Optional::Set(999)));
155
156 msg.clear_default_int32();
157 assert_that!(msg.default_int32(), eq(41));
158 assert_that!(msg.default_int32_opt(), eq(Optional::Unset(41)));
159 }
160
161 #[gtest]
test_optional_int64_accessors()162 fn test_optional_int64_accessors() {
163 let mut msg = TestAllTypes::new();
164 assert_that!(msg.optional_int64_opt(), eq(Optional::Unset(0)));
165 assert_that!(msg.optional_int64(), eq(0));
166
167 msg.set_optional_int64(42);
168 assert_that!(msg.optional_int64_opt(), eq(Optional::Set(42)));
169 assert_that!(msg.optional_int64(), eq(42));
170
171 msg.clear_optional_int64();
172 assert_that!(msg.optional_int64_opt(), eq(Optional::Unset(0)));
173 assert_that!(msg.optional_int64(), eq(0));
174 }
175
176 #[gtest]
test_default_int64_accessors()177 fn test_default_int64_accessors() {
178 let mut msg = TestAllTypes::new();
179 assert_that!(msg.default_int64(), eq(42));
180 assert_that!(msg.default_int64_opt(), eq(Optional::Unset(42)));
181
182 msg.set_default_int64(999);
183 assert_that!(msg.default_int64(), eq(999));
184 assert_that!(msg.default_int64_opt(), eq(Optional::Set(999)));
185
186 msg.clear_default_int64();
187 assert_that!(msg.default_int64(), eq(42));
188 assert_that!(msg.default_int64_opt(), eq(Optional::Unset(42)));
189 }
190
191 #[gtest]
test_optional_sint32_accessors()192 fn test_optional_sint32_accessors() {
193 let mut msg = TestAllTypes::new();
194 assert_that!(msg.optional_sint32_opt(), eq(Optional::Unset(0)));
195 assert_that!(msg.optional_sint32(), eq(0));
196
197 msg.set_optional_sint32(-22);
198 assert_that!(msg.optional_sint32_opt(), eq(Optional::Set(-22)));
199 assert_that!(msg.optional_sint32(), eq(-22));
200
201 msg.clear_optional_sint32();
202 assert_that!(msg.optional_sint32_opt(), eq(Optional::Unset(0)));
203 assert_that!(msg.optional_sint32(), eq(0));
204 }
205
206 #[gtest]
test_default_sint32_accessors()207 fn test_default_sint32_accessors() {
208 let mut msg = TestAllTypes::new();
209 assert_that!(msg.default_sint32(), eq(-45));
210 assert_that!(msg.default_sint32_opt(), eq(Optional::Unset(-45)));
211
212 msg.set_default_sint32(999);
213 assert_that!(msg.default_sint32(), eq(999));
214 assert_that!(msg.default_sint32_opt(), eq(Optional::Set(999)));
215
216 msg.clear_default_sint32();
217 assert_that!(msg.default_sint32(), eq(-45));
218 assert_that!(msg.default_sint32_opt(), eq(Optional::Unset(-45)));
219 }
220
221 #[gtest]
test_optional_sint64_accessors()222 fn test_optional_sint64_accessors() {
223 let mut msg = TestAllTypes::new();
224 assert_that!(msg.optional_sint64_opt(), eq(Optional::Unset(0)));
225 assert_that!(msg.optional_sint64(), eq(0));
226
227 msg.set_optional_sint64(7000);
228 assert_that!(msg.optional_sint64_opt(), eq(Optional::Set(7000)));
229 assert_that!(msg.optional_sint64(), eq(7000));
230
231 msg.clear_optional_sint64();
232 assert_that!(msg.optional_sint64_opt(), eq(Optional::Unset(0)));
233 assert_that!(msg.optional_sint64(), eq(0));
234 }
235
236 #[gtest]
test_default_sint64_accessors()237 fn test_default_sint64_accessors() {
238 let mut msg = TestAllTypes::new();
239 assert_that!(msg.default_sint64(), eq(46));
240 assert_that!(msg.default_sint64_opt(), eq(Optional::Unset(46)));
241
242 msg.set_default_sint64(999);
243 assert_that!(msg.default_sint64(), eq(999));
244 assert_that!(msg.default_sint64_opt(), eq(Optional::Set(999)));
245
246 msg.clear_default_sint64();
247 assert_that!(msg.default_sint64(), eq(46));
248 assert_that!(msg.default_sint64_opt(), eq(Optional::Unset(46)));
249 }
250
251 #[gtest]
test_optional_uint32_accessors()252 fn test_optional_uint32_accessors() {
253 let mut msg = TestAllTypes::new();
254 assert_that!(msg.optional_uint32_opt(), eq(Optional::Unset(0)));
255 assert_that!(msg.optional_uint32(), eq(0));
256
257 msg.set_optional_uint32(9001);
258 assert_that!(msg.optional_uint32_opt(), eq(Optional::Set(9001)));
259 assert_that!(msg.optional_uint32(), eq(9001));
260
261 msg.clear_optional_uint32();
262 assert_that!(msg.optional_uint32_opt(), eq(Optional::Unset(0)));
263 assert_that!(msg.optional_uint32(), eq(0));
264 }
265
266 #[gtest]
test_default_uint32_accessors()267 fn test_default_uint32_accessors() {
268 let mut msg = TestAllTypes::new();
269 assert_that!(msg.default_uint32(), eq(43));
270 assert_that!(msg.default_uint32_opt(), eq(Optional::Unset(43)));
271
272 msg.set_default_uint32(999);
273 assert_that!(msg.default_uint32(), eq(999));
274 assert_that!(msg.default_uint32_opt(), eq(Optional::Set(999)));
275
276 msg.clear_default_uint32();
277 assert_that!(msg.default_uint32(), eq(43));
278 assert_that!(msg.default_uint32_opt(), eq(Optional::Unset(43)));
279 }
280
281 #[gtest]
test_optional_uint64_accessors()282 fn test_optional_uint64_accessors() {
283 let mut msg = TestAllTypes::new();
284 assert_that!(msg.optional_uint64_opt(), eq(Optional::Unset(0)));
285 assert_that!(msg.optional_uint64(), eq(0));
286
287 msg.set_optional_uint64(42);
288 assert_that!(msg.optional_uint64_opt(), eq(Optional::Set(42)));
289 assert_that!(msg.optional_uint64(), eq(42));
290
291 msg.clear_optional_uint64();
292 assert_that!(msg.optional_uint64_opt(), eq(Optional::Unset(0)));
293 assert_that!(msg.optional_uint64(), eq(0));
294 }
295
296 #[gtest]
test_default_uint64_accessors()297 fn test_default_uint64_accessors() {
298 let mut msg = TestAllTypes::new();
299 assert_that!(msg.default_uint64(), eq(44));
300 assert_that!(msg.default_uint64_opt(), eq(Optional::Unset(44)));
301
302 msg.set_default_uint64(999);
303 assert_that!(msg.default_uint64(), eq(999));
304 assert_that!(msg.default_uint64_opt(), eq(Optional::Set(999)));
305
306 msg.clear_default_uint64();
307 assert_that!(msg.default_uint64(), eq(44));
308 assert_that!(msg.default_uint64_opt(), eq(Optional::Unset(44)));
309 }
310
311 #[gtest]
test_optional_float_accessors()312 fn test_optional_float_accessors() {
313 let mut msg = TestAllTypes::new();
314 assert_that!(msg.optional_float_opt(), eq(Optional::Unset(0.0)));
315 assert_that!(msg.optional_float(), eq(0.0));
316
317 msg.set_optional_float(std::f32::consts::PI);
318 assert_that!(msg.optional_float_opt(), eq(Optional::Set(std::f32::consts::PI)));
319 assert_that!(msg.optional_float(), eq(std::f32::consts::PI));
320
321 msg.clear_optional_float();
322 assert_that!(msg.optional_float_opt(), eq(Optional::Unset(0.0)));
323 assert_that!(msg.optional_float(), eq(0.0));
324 }
325
326 #[gtest]
test_default_float_accessors()327 fn test_default_float_accessors() {
328 let mut msg = TestAllTypes::new();
329 assert_that!(msg.default_float(), eq(51.5));
330 assert_that!(msg.default_float_opt(), eq(Optional::Unset(51.5)));
331
332 msg.set_default_float(999.9);
333 assert_that!(msg.default_float(), eq(999.9));
334 assert_that!(msg.default_float_opt(), eq(Optional::Set(999.9)));
335
336 msg.clear_default_float();
337 assert_that!(msg.default_float(), eq(51.5));
338 assert_that!(msg.default_float_opt(), eq(Optional::Unset(51.5)));
339 }
340
341 #[gtest]
test_optional_double_accessors()342 fn test_optional_double_accessors() {
343 let mut msg = TestAllTypes::new();
344 assert_that!(msg.optional_double_opt(), eq(Optional::Unset(0.0)));
345 assert_that!(msg.optional_double(), eq(0.0));
346
347 msg.set_optional_double(-10.99);
348 assert_that!(msg.optional_double_opt(), eq(Optional::Set(-10.99)));
349 assert_that!(msg.optional_double(), eq(-10.99));
350
351 msg.clear_optional_double();
352 assert_that!(msg.optional_double_opt(), eq(Optional::Unset(0.0)));
353 assert_that!(msg.optional_double(), eq(0.0));
354 }
355
356 #[gtest]
test_default_double_accessors()357 fn test_default_double_accessors() {
358 let mut msg = TestAllTypes::new();
359 assert_that!(msg.default_double(), eq(52e3));
360 assert_that!(msg.default_double_opt(), eq(Optional::Unset(52e3)));
361
362 msg.set_default_double(999.9);
363 assert_that!(msg.default_double(), eq(999.9));
364 assert_that!(msg.default_double_opt(), eq(Optional::Set(999.9)));
365
366 msg.clear_default_double();
367 assert_that!(msg.default_double(), eq(52e3));
368 assert_that!(msg.default_double_opt(), eq(Optional::Unset(52e3)));
369 }
370
371 #[gtest]
test_optional_bool_accessors()372 fn test_optional_bool_accessors() {
373 let mut msg = TestAllTypes::new();
374 assert_that!(msg.optional_bool_opt(), eq(Optional::Unset(false)));
375
376 msg.set_optional_bool(true);
377 assert_that!(msg.optional_bool_opt(), eq(Optional::Set(true)));
378
379 msg.clear_optional_bool();
380 assert_that!(msg.optional_bool_opt(), eq(Optional::Unset(false)));
381 }
382
383 #[gtest]
test_default_bool_accessors()384 fn test_default_bool_accessors() {
385 let mut msg = TestAllTypes::new();
386 assert_that!(msg.default_bool(), eq(true));
387 assert_that!(msg.default_bool_opt(), eq(Optional::Unset(true)));
388
389 msg.set_default_bool(false);
390 assert_that!(msg.default_bool(), eq(false));
391 assert_that!(msg.default_bool_opt(), eq(Optional::Set(false)));
392
393 msg.clear_default_bool();
394 assert_that!(msg.default_bool(), eq(true));
395 assert_that!(msg.default_bool_opt(), eq(Optional::Unset(true)));
396 }
397
398 #[gtest]
test_optional_bytes_accessors()399 fn test_optional_bytes_accessors() {
400 let mut msg = TestAllTypes::new();
401 assert_that!(*msg.optional_bytes(), empty());
402 assert_that!(msg.has_optional_bytes(), eq(false));
403 assert_that!(msg.optional_bytes_opt(), eq(Optional::Unset(&b""[..])));
404
405 {
406 let s = Vec::from(&b"hello world"[..]);
407 msg.set_optional_bytes(&s[..]);
408 }
409 assert_that!(msg.optional_bytes(), eq(b"hello world"));
410 assert_that!(msg.has_optional_bytes(), eq(true));
411 assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b"hello world"[..])));
412
413 msg.clear_optional_bytes();
414 assert_that!(*msg.optional_bytes(), empty());
415 assert_that!(msg.has_optional_bytes(), eq(false));
416 assert_that!(msg.optional_bytes_opt(), eq(Optional::Unset(&b""[..])));
417
418 msg.set_optional_bytes(b"");
419 assert_that!(*msg.optional_bytes(), empty());
420 assert_that!(msg.has_optional_bytes(), eq(true));
421 assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b""[..])));
422 }
423
424 #[gtest]
test_into_proxied_for_bytes()425 fn test_into_proxied_for_bytes() {
426 let mut msg = TestAllTypes::new();
427
428 // &[u8]
429 let bytes: &[u8] = b"first";
430 msg.set_optional_bytes(bytes);
431 assert_that!(msg.optional_bytes(), eq(bytes));
432
433 // &[u8; N]
434 msg.set_optional_bytes(b"second");
435 assert_that!(msg.optional_bytes(), eq(b"second"));
436
437 // Vec<u8>
438 msg.set_optional_bytes(Vec::from(b"third"));
439 assert_that!(msg.optional_bytes(), eq(b"third"));
440
441 // ProtoBytes
442 msg.set_optional_bytes(ProtoBytes::from(b"fourth"));
443 assert_that!(msg.optional_bytes(), eq(b"fourth"));
444
445 // Box<[u8]>
446 msg.set_optional_bytes(Box::from(b"fifth".to_owned()));
447 assert_that!(msg.optional_bytes(), eq(b"fifth"));
448
449 // Cow<[u8]>
450 msg.set_optional_bytes(Cow::from(b"sixth"));
451 assert_that!(msg.optional_bytes(), eq(b"sixth"));
452
453 // Rc<[u8]>
454 msg.set_optional_bytes(Rc::from(b"seventh".to_owned()));
455 assert_that!(msg.optional_bytes(), eq(b"seventh"));
456
457 // Arc<[u8]>
458 msg.set_optional_bytes(Arc::from(b"eighth".to_owned()));
459 assert_that!(msg.optional_bytes(), eq(b"eighth"));
460
461 // &Vec<u8>
462 msg.set_optional_bytes(&Vec::from(b"ninth"));
463 assert_that!(msg.optional_bytes(), eq(b"ninth"));
464 }
465
466 #[gtest]
test_nonempty_default_bytes_accessors()467 fn test_nonempty_default_bytes_accessors() {
468 let mut msg = TestAllTypes::new();
469 assert_that!(msg.default_bytes(), eq(b"world"));
470 assert_that!(msg.has_default_bytes(), eq(false));
471 assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
472
473 {
474 let s = String::from("hello world");
475 msg.set_default_bytes(s.as_bytes());
476 }
477 assert_that!(msg.default_bytes(), eq(b"hello world"));
478 assert_that!(msg.has_default_bytes(), eq(true));
479 assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"hello world"[..])));
480
481 msg.clear_default_bytes();
482 assert_that!(msg.default_bytes(), eq(b"world"));
483 assert_that!(msg.has_default_bytes(), eq(false));
484 assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
485
486 msg.set_default_bytes(b"");
487 assert_that!(*msg.default_bytes(), empty());
488 assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b""[..])));
489
490 msg.clear_default_bytes();
491 assert_that!(msg.default_bytes(), eq(b"world"));
492 assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
493 }
494
495 #[gtest]
test_optional_string_accessors()496 fn test_optional_string_accessors() {
497 let mut msg = TestAllTypes::new();
498 assert_that!(msg.optional_string(), eq(""));
499 assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
500
501 {
502 let s = String::from("hello world");
503 msg.set_optional_string(&s[..]);
504 }
505 assert_that!(msg.optional_string(), eq("hello world"));
506 assert_that!(msg.optional_string_opt(), eq(Optional::Set("hello world".into())));
507
508 msg.clear_optional_string();
509 assert_that!(msg.optional_string(), eq(""));
510 assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
511
512 msg.set_optional_string("");
513 assert_that!(msg.optional_string(), eq(""));
514 assert_that!(msg.optional_string_opt(), eq(Optional::Set("".into())));
515
516 msg.clear_optional_string();
517 assert_that!(msg.optional_string(), eq(""));
518 assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
519 }
520
521 #[gtest]
test_into_proxied_for_string()522 fn test_into_proxied_for_string() {
523 let mut msg = TestAllTypes::new();
524
525 // &str
526 msg.set_optional_string("first");
527 assert_that!(msg.optional_string(), eq("first"));
528
529 // String
530 msg.set_optional_string("second".to_string());
531 assert_that!(msg.optional_string(), eq("second"));
532
533 // ProtoStr
534 msg.set_optional_string(ProtoStr::from_str("third"));
535 assert_that!(msg.optional_string(), eq("third"));
536
537 // ProtoString
538 msg.set_optional_string(ProtoString::from("fourth"));
539 assert_that!(msg.optional_string(), eq("fourth"));
540
541 // OsString
542 msg.set_optional_string(OsString::from("fifth"));
543 assert_that!(msg.optional_string(), eq("fifth"));
544
545 // OsStr
546 msg.set_optional_string(OsString::from("sixth").as_os_str());
547 assert_that!(msg.optional_string(), eq("sixth"));
548
549 // Box<str>
550 msg.set_optional_string(Box::from("seventh"));
551 assert_that!(msg.optional_string(), eq("seventh"));
552
553 // Cow<str>
554 msg.set_optional_string(Cow::from("eighth"));
555 assert_that!(msg.optional_string(), eq("eighth"));
556
557 // Rc<str>
558 msg.set_optional_string(Rc::from("ninth"));
559 assert_that!(msg.optional_string(), eq("ninth"));
560
561 // Arc<str>
562 msg.set_optional_string(Arc::from("tenth"));
563 assert_that!(msg.optional_string(), eq("tenth"));
564
565 // &String
566 msg.set_optional_string(&"eleventh".to_string());
567 assert_that!(msg.optional_string(), eq("eleventh"));
568 }
569
570 #[gtest]
test_nonempty_default_string_accessors()571 fn test_nonempty_default_string_accessors() {
572 let mut msg = TestAllTypes::new();
573 assert_that!(msg.default_string(), eq("hello"));
574 assert_that!(msg.default_string_opt(), eq(Optional::Unset("hello".into())));
575
576 {
577 let s = String::from("hello world");
578 msg.set_default_string(&s[..]);
579 }
580 assert_that!(msg.default_string(), eq("hello world"));
581 assert_that!(msg.default_string_opt(), eq(Optional::Set("hello world".into())));
582
583 msg.clear_default_string();
584 assert_that!(msg.default_string(), eq("hello"));
585 assert_that!(msg.default_string_opt(), eq(Optional::Unset("hello".into())));
586
587 msg.set_default_string("");
588 assert_that!(msg.default_string(), eq(""));
589 assert_that!(msg.default_string_opt(), eq(Optional::Set("".into())));
590
591 msg.clear_default_string();
592 assert_that!(msg.default_string(), eq("hello"));
593 assert_that!(msg.default_string_opt(), eq(Optional::Unset("hello".into())));
594 }
595
596 #[gtest]
test_singular_msg_field()597 fn test_singular_msg_field() {
598 let mut msg = TestAllTypes::new();
599 let msg_view = msg.optional_nested_message();
600 // testing reading an int inside a view
601 assert_that!(msg_view.bb(), eq(0));
602
603 assert_that!(msg.has_optional_nested_message(), eq(false));
604 let nested_msg_mut = msg.optional_nested_message_mut();
605 // test reading an int inside a mut
606 assert_that!(nested_msg_mut.bb(), eq(0));
607
608 assert_that!(msg.has_optional_nested_message(), eq(true));
609 }
610
611 #[gtest]
test_message_opt()612 fn test_message_opt() {
613 let msg = TestAllTypes::new();
614 let opt: Optional<unittest_rust_proto::test_all_types::NestedMessageView<'_>> =
615 msg.optional_nested_message_opt();
616 assert_that!(opt.is_set(), eq(false));
617 assert_that!(opt.into_inner().bb(), eq(0));
618 }
619
620 #[gtest]
test_message_opt_set()621 fn test_message_opt_set() {
622 let mut msg = TestAllTypes::new();
623 let submsg = test_all_types::NestedMessage::new();
624 msg.set_optional_nested_message(submsg);
625 msg.clear_optional_nested_message();
626 assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
627 }
628
629 #[gtest]
test_setting_submsg()630 fn test_setting_submsg() {
631 let mut msg = TestAllTypes::new();
632 let submsg = test_all_types::NestedMessage::new();
633
634 assert_that!(msg.has_optional_nested_message(), eq(false));
635 assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
636
637 msg.set_optional_nested_message(submsg);
638 // confirm that invoking .set on a submsg indeed flips the set bit
639 assert_that!(msg.has_optional_nested_message(), eq(true));
640 assert_that!(msg.optional_nested_message_opt().is_set(), eq(true));
641
642 msg.clear_optional_nested_message();
643 assert_that!(msg.has_optional_nested_message(), eq(false));
644 assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
645 }
646
647 #[gtest]
test_msg_mut_initializes()648 fn test_msg_mut_initializes() {
649 let mut msg = TestAllTypes::new();
650 assert_that!(msg.has_optional_nested_message(), eq(false));
651 assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
652
653 let _ = msg.optional_nested_message_mut();
654 // confirm that that optional_nested_message_mut makes the field Present
655 assert_that!(msg.has_optional_nested_message(), eq(true));
656 assert_that!(msg.optional_nested_message_opt().is_set(), eq(true));
657
658 msg.clear_optional_nested_message();
659 assert_that!(msg.has_optional_nested_message(), eq(false));
660 assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
661 }
662
663 #[gtest]
test_optional_nested_enum_accessors()664 fn test_optional_nested_enum_accessors() {
665 use test_all_types::NestedEnum;
666
667 let mut msg = TestAllTypes::new();
668 assert_that!(msg.has_optional_nested_enum(), eq(false));
669 assert_that!(msg.optional_nested_enum_opt(), eq(Optional::Unset(NestedEnum::Foo)));
670 assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Foo));
671
672 msg.set_optional_nested_enum(NestedEnum::Neg);
673 assert_that!(msg.has_optional_nested_enum(), eq(true));
674 assert_that!(msg.optional_nested_enum_opt(), eq(Optional::Set(NestedEnum::Neg)));
675 assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Neg));
676
677 msg.clear_optional_nested_enum();
678 assert_that!(msg.has_optional_nested_enum(), eq(false));
679 assert_that!(msg.optional_nested_enum_opt(), eq(Optional::Unset(NestedEnum::Foo)));
680 assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Foo));
681 }
682
683 #[gtest]
test_default_nested_enum_accessors()684 fn test_default_nested_enum_accessors() {
685 use test_all_types::NestedEnum;
686
687 let mut msg = TestAllTypes::new();
688 assert_that!(msg.default_nested_enum(), eq(NestedEnum::Bar));
689 assert_that!(msg.default_nested_enum_opt(), eq(Optional::Unset(NestedEnum::Bar)));
690
691 msg.set_default_nested_enum(NestedEnum::Baz);
692 assert_that!(msg.default_nested_enum(), eq(NestedEnum::Baz));
693 assert_that!(msg.default_nested_enum_opt(), eq(Optional::Set(NestedEnum::Baz)));
694
695 msg.clear_default_nested_enum();
696 assert_that!(msg.default_nested_enum(), eq(NestedEnum::Bar));
697 assert_that!(msg.default_nested_enum_opt(), eq(Optional::Unset(NestedEnum::Bar)));
698 }
699
700 #[gtest]
test_optional_foreign_enum_accessors()701 fn test_optional_foreign_enum_accessors() {
702 use unittest_rust_proto::ForeignEnum;
703
704 let mut msg = TestAllTypes::new();
705 assert_that!(msg.optional_foreign_enum_opt(), eq(Optional::Unset(ForeignEnum::ForeignFoo)));
706 assert_that!(msg.optional_foreign_enum(), eq(ForeignEnum::ForeignFoo));
707
708 msg.set_optional_foreign_enum(ForeignEnum::ForeignBax);
709 assert_that!(msg.optional_foreign_enum_opt(), eq(Optional::Set(ForeignEnum::ForeignBax)));
710 assert_that!(msg.optional_foreign_enum(), eq(ForeignEnum::ForeignBax));
711
712 msg.clear_optional_foreign_enum();
713 assert_that!(msg.optional_foreign_enum_opt(), eq(Optional::Unset(ForeignEnum::ForeignFoo)));
714 assert_that!(msg.optional_foreign_enum(), eq(ForeignEnum::ForeignFoo));
715 }
716
717 #[gtest]
test_default_foreign_enum_accessors()718 fn test_default_foreign_enum_accessors() {
719 use unittest_rust_proto::ForeignEnum;
720
721 let mut msg = TestAllTypes::new();
722 assert_that!(msg.default_foreign_enum(), eq(ForeignEnum::ForeignBar));
723 assert_that!(msg.default_foreign_enum_opt(), eq(Optional::Unset(ForeignEnum::ForeignBar)));
724
725 msg.set_default_foreign_enum(ForeignEnum::ForeignBaz);
726 assert_that!(msg.default_foreign_enum(), eq(ForeignEnum::ForeignBaz));
727 assert_that!(msg.default_foreign_enum_opt(), eq(Optional::Set(ForeignEnum::ForeignBaz)));
728
729 msg.clear_default_foreign_enum();
730 assert_that!(msg.default_foreign_enum(), eq(ForeignEnum::ForeignBar));
731 assert_that!(msg.default_foreign_enum_opt(), eq(Optional::Unset(ForeignEnum::ForeignBar)));
732 }
733
734 #[gtest]
test_optional_import_enum_accessors()735 fn test_optional_import_enum_accessors() {
736 use unittest_rust_proto::ImportEnum;
737
738 let mut msg = TestAllTypes::new();
739 assert_that!(msg.optional_import_enum_opt(), eq(Optional::Unset(ImportEnum::ImportFoo)));
740 assert_that!(msg.optional_import_enum(), eq(ImportEnum::ImportFoo));
741
742 msg.set_optional_import_enum(ImportEnum::ImportBar);
743 assert_that!(msg.optional_import_enum_opt(), eq(Optional::Set(ImportEnum::ImportBar)));
744 assert_that!(msg.optional_import_enum(), eq(ImportEnum::ImportBar));
745
746 msg.clear_optional_import_enum();
747 assert_that!(msg.optional_import_enum_opt(), eq(Optional::Unset(ImportEnum::ImportFoo)));
748 assert_that!(msg.optional_import_enum(), eq(ImportEnum::ImportFoo));
749 }
750
751 #[gtest]
test_default_import_enum_accessors()752 fn test_default_import_enum_accessors() {
753 use unittest_rust_proto::ImportEnum;
754
755 let mut msg = TestAllTypes::new();
756 assert_that!(msg.default_import_enum(), eq(ImportEnum::ImportBar));
757 assert_that!(msg.default_import_enum_opt(), eq(Optional::Unset(ImportEnum::ImportBar)));
758
759 msg.set_default_import_enum(ImportEnum::ImportBaz);
760 assert_that!(msg.default_import_enum(), eq(ImportEnum::ImportBaz));
761 assert_that!(msg.default_import_enum_opt(), eq(Optional::Set(ImportEnum::ImportBaz)));
762
763 msg.clear_default_import_enum();
764 assert_that!(msg.default_import_enum(), eq(ImportEnum::ImportBar));
765 assert_that!(msg.default_import_enum_opt(), eq(Optional::Unset(ImportEnum::ImportBar)));
766 }
767
768 #[gtest]
test_oneof_accessors()769 fn test_oneof_accessors() {
770 use unittest_rust_proto::test_oneof2::{Foo::*, FooCase, NestedEnum};
771 use unittest_rust_proto::TestOneof2;
772
773 let mut msg = TestOneof2::new();
774 assert_that!(msg.foo(), matches_pattern!(not_set(_)));
775 assert_that!(msg.foo_case(), eq(FooCase::not_set));
776
777 msg.set_foo_int(7);
778 assert_that!(msg.has_foo_int(), eq(true));
779 assert_that!(msg.foo_int_opt(), eq(Optional::Set(7)));
780 assert_that!(msg.foo(), matches_pattern!(FooInt(eq(7))));
781 assert_that!(msg.foo_case(), eq(FooCase::FooInt));
782
783 msg.clear_foo_int();
784 assert_that!(msg.has_foo_int(), eq(false));
785 assert_that!(msg.foo_int_opt(), eq(Optional::Unset(0)));
786 assert_that!(msg.foo(), matches_pattern!(not_set(_)));
787 assert_that!(msg.foo_case(), eq(FooCase::not_set));
788
789 msg.set_foo_int(7);
790 msg.set_foo_bytes(b"123");
791 assert_that!(msg.has_foo_int(), eq(false));
792 assert_that!(msg.foo_int_opt(), eq(Optional::Unset(0)));
793
794 assert_that!(msg.foo(), matches_pattern!(FooBytes(eq(b"123"))));
795 assert_that!(msg.foo_case(), eq(FooCase::FooBytes));
796
797 msg.set_foo_enum(NestedEnum::Foo);
798 assert_that!(msg.foo(), matches_pattern!(FooEnum(eq(NestedEnum::Foo))));
799 assert_that!(msg.foo_case(), eq(FooCase::FooEnum));
800
801 // Test the accessors or $Msg$Mut
802 let mut msg_mut = msg.as_mut();
803 assert_that!(msg_mut.foo(), matches_pattern!(FooEnum(eq(NestedEnum::Foo))));
804 msg_mut.set_foo_int(7);
805 msg_mut.set_foo_bytes(b"123");
806 assert_that!(msg_mut.foo(), matches_pattern!(FooBytes(eq(b"123"))));
807 assert_that!(msg_mut.foo_case(), eq(FooCase::FooBytes));
808 assert_that!(msg_mut.foo_int_opt(), eq(Optional::Unset(0)));
809
810 // Test the accessors on $Msg$View
811 let msg_view = msg.as_view();
812 assert_that!(msg_view.foo(), matches_pattern!(FooBytes(eq(b"123"))));
813 assert_that!(msg_view.foo_case(), eq(FooCase::FooBytes));
814 assert_that!(msg_view.foo_int_opt(), eq(Optional::Unset(0)));
815
816 // TODO: Add tests covering a message-type field in a oneof.
817 }
818
819 #[gtest]
test_msg_oneof_default_accessors()820 fn test_msg_oneof_default_accessors() {
821 use unittest_rust_proto::test_oneof2::{Bar::*, BarCase, NestedEnum};
822
823 let mut msg = unittest_rust_proto::TestOneof2::new();
824 assert_that!(msg.bar(), matches_pattern!(not_set(_)));
825
826 msg.set_bar_int(7);
827 assert_that!(msg.bar_int_opt(), eq(Optional::Set(7)));
828 assert_that!(msg.bar(), matches_pattern!(BarInt(eq(7))));
829 assert_that!(msg.bar_case(), eq(BarCase::BarInt));
830
831 msg.clear_bar_int();
832 assert_that!(msg.bar_int_opt(), eq(Optional::Unset(5)));
833 assert_that!(msg.bar(), matches_pattern!(not_set(_)));
834 assert_that!(msg.bar_case(), eq(BarCase::not_set));
835
836 msg.set_bar_int(7);
837 msg.set_bar_bytes(b"123");
838 assert_that!(msg.bar_int_opt(), eq(Optional::Unset(5)));
839 assert_that!(msg.bar_enum_opt(), eq(Optional::Unset(NestedEnum::Bar)));
840 assert_that!(msg.bar(), matches_pattern!(BarBytes(eq(b"123"))));
841 assert_that!(msg.bar_case(), eq(BarCase::BarBytes));
842
843 msg.set_bar_enum(NestedEnum::Baz);
844 assert_that!(msg.bar(), matches_pattern!(BarEnum(eq(NestedEnum::Baz))));
845 assert_that!(msg.bar_case(), eq(BarCase::BarEnum));
846 assert_that!(msg.bar_int_opt(), eq(Optional::Unset(5)));
847
848 // TODO: Add tests covering a message-type field in a oneof.
849 }
850
851 #[gtest]
test_group()852 fn test_group() {
853 let mut m = TestAllTypes::new();
854
855 // Groups are exposed the same as nested message types.
856 assert_that!(m.optionalgroup_opt().is_set(), eq(false));
857 assert_that!(m.optionalgroup().a(), eq(0));
858
859 m.optionalgroup_mut().set_a(7);
860 assert_that!(m.optionalgroup_opt().is_set(), eq(true));
861 assert_that!(m.optionalgroup().a(), eq(7));
862 }
863
864 #[gtest]
test_submsg_setter()865 fn test_submsg_setter() {
866 use test_all_types::*;
867
868 let mut nested = NestedMessage::new();
869 nested.set_bb(7);
870
871 let mut parent = TestAllTypes::new();
872 parent.set_optional_nested_message(nested);
873
874 assert_that!(parent.optional_nested_message().bb(), eq(7));
875 }
876
877 #[gtest]
test_clone()878 fn test_clone() {
879 let mut m = TestAllTypes::new();
880 m.set_optional_int32(42);
881 let clone = m.clone();
882 assert_that!(clone.optional_int32(), eq(42));
883 m.clear_optional_int32();
884 assert_that!(m.has_optional_int32(), eq(false));
885 assert_that!(clone.has_optional_int32(), eq(true));
886 assert_that!(clone.optional_int32(), eq(42));
887 }
888
889 #[gtest]
test_to_owned()890 fn test_to_owned() {
891 let mut m = TestAllTypes::new();
892 m.set_optional_int32(42);
893 let clone = m.as_view().to_owned();
894 assert_that!(clone.optional_int32(), eq(42));
895
896 // to_owned should create a new message (modifying the original shouldn't affect
897 // the to_owned).
898 m.clear_optional_int32();
899 assert_that!(m.has_optional_int32(), eq(false));
900 assert_that!(clone.has_optional_int32(), eq(true));
901 assert_that!(clone.optional_int32(), eq(42));
902
903 let mut submsg_mut = m.optional_nested_message_mut();
904 submsg_mut.set_bb(7);
905 let submsg_clone = submsg_mut.to_owned();
906 assert_that!(submsg_clone.bb(), eq(7));
907 assert_that!(submsg_mut.bb(), eq(7));
908 submsg_mut.set_bb(8);
909 assert_that!(submsg_clone.bb(), eq(7));
910 assert_that!(submsg_mut.bb(), eq(8));
911 }
912
913 #[gtest]
test_ctype_stringpiece()914 fn test_ctype_stringpiece() {
915 let mut msg = TestAllTypes::new();
916 assert_that!(msg.optional_string_piece(), eq(""));
917 assert_that!(msg.has_optional_string_piece(), eq(false));
918 msg.set_optional_string_piece("hello");
919 assert_that!(msg.optional_string_piece(), eq("hello"));
920 assert_that!(msg.has_optional_string_piece(), eq(true));
921 }
922
923 #[gtest]
test_msg_clear()924 fn test_msg_clear() {
925 let mut m = TestAllTypes::new();
926 m.set_optional_int32(42);
927 assert_that!(m.has_optional_int32(), eq(true));
928 m.clear();
929 assert_that!(m.has_optional_int32(), eq(false));
930 }
931
932 #[gtest]
test_submsg_clear()933 fn test_submsg_clear() {
934 let mut m = TestAllTypes::new();
935 let mut sub = m.optional_nested_message_mut();
936 sub.set_bb(7);
937
938 assert_that!(m.has_optional_nested_message(), eq(true));
939 assert_that!(m.optional_nested_message().bb(), eq(7));
940
941 m.optional_nested_message_mut().clear();
942
943 // .clear() on the submsg doesn't affect its presence on the parent:
944 assert_that!(m.has_optional_nested_message(), eq(true));
945 // ...but it does clear the submsg's value:
946 assert_that!(m.optional_nested_message().bb(), eq(0));
947 }
948