• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 use googletest::prelude::*;
9 use nested_rust_proto::outer::inner::InnerEnum;
10 use nested_rust_proto::outer::InnerView;
11 use nested_rust_proto::*;
12 
13 #[gtest]
test_deeply_nested_message()14 fn test_deeply_nested_message() {
15     let deep =
16         outer::inner::super_inner::duper_inner::even_more_inner::CantBelieveItsSoInner::new();
17     assert_that!(deep.num(), eq(0));
18 
19     let outermsg = Outer::new();
20     assert_that!(outermsg.deep().num(), eq(0));
21 }
22 
23 #[gtest]
test_deeply_nested_enum()24 fn test_deeply_nested_enum() {
25     use outer::inner::super_inner::duper_inner::even_more_inner::JustWayTooInner;
26     let deep = JustWayTooInner::default();
27     assert_that!(i32::from(deep), eq(0));
28 
29     let outermsg = Outer::new();
30     assert_that!(outermsg.deep_enum(), eq(JustWayTooInner::Unspecified));
31 }
32 
33 #[gtest]
test_nested_views()34 fn test_nested_views() {
35     let outermsg = Outer::new();
36     let inner_msg: InnerView<'_> = outermsg.inner();
37     assert_that!(inner_msg.double(), eq(0.0));
38     assert_that!(inner_msg.float(), eq(0.0));
39     assert_that!(inner_msg.int32(), eq(0));
40     assert_that!(inner_msg.int64(), eq(0));
41     assert_that!(inner_msg.uint32(), eq(0));
42     assert_that!(inner_msg.uint64(), eq(0));
43     assert_that!(inner_msg.sint32(), eq(0));
44     assert_that!(inner_msg.sint64(), eq(0));
45     assert_that!(inner_msg.fixed32(), eq(0));
46     assert_that!(inner_msg.fixed64(), eq(0));
47     assert_that!(inner_msg.sfixed32(), eq(0));
48     assert_that!(inner_msg.sfixed64(), eq(0));
49     assert_that!(inner_msg.bool(), eq(false));
50     assert_that!(*inner_msg.string().as_bytes(), empty());
51     assert_that!(*inner_msg.bytes(), empty());
52     assert_that!(inner_msg.inner_submsg().flag(), eq(false));
53     assert_that!(inner_msg.inner_enum(), eq(InnerEnum::Unspecified));
54 }
55 
56 #[gtest]
test_nested_view_lifetimes()57 fn test_nested_view_lifetimes() {
58     // Ensure that views have the lifetime of the first layer of borrow, and don't
59     // create intermediate borrows through nested accessors.
60 
61     let outermsg = Outer::new();
62 
63     let string = outermsg.inner().string();
64     assert_that!(string, eq(""));
65 
66     let bytes = outermsg.inner().bytes();
67     assert_that!(bytes, eq(b""));
68 
69     let inner_submsg = outermsg.inner().inner_submsg();
70     assert_that!(inner_submsg.flag(), eq(false));
71 
72     let repeated_int32 = outermsg.inner().repeated_int32();
73     assert_that!(repeated_int32, empty());
74 
75     let repeated_inner_submsg = outermsg.inner().repeated_inner_submsg();
76     assert_that!(repeated_inner_submsg, empty());
77 
78     let string_map = outermsg.inner().string_map();
79     assert_that!(string_map.len(), eq(0));
80 }
81 
82 #[gtest]
test_msg_from_outside()83 fn test_msg_from_outside() {
84     // let's make sure that we're not just working for messages nested inside
85     // messages, messages from without and within should work
86     let outer = Outer::new();
87     assert_that!(outer.notinside().num(), eq(0));
88 }
89 
90 #[gtest]
test_recursive_view()91 fn test_recursive_view() {
92     let rec = nested_rust_proto::Recursive::new();
93     assert_that!(rec.num(), eq(0));
94     assert_that!(rec.rec().num(), eq(0));
95     assert_that!(rec.rec().rec().num(), eq(0)); // turtles all the way down...
96     assert_that!(rec.rec().rec().rec().num(), eq(0)); // ... ad infinitum
97 
98     // Test that intermediate borrows are not created.
99     let nested = rec.rec().rec().rec();
100     assert_that!(nested.num(), eq(0));
101 }
102 
103 #[gtest]
test_recursive_mut()104 fn test_recursive_mut() {
105     let mut rec = nested_rust_proto::Recursive::new();
106     let mut one = rec.rec_mut();
107     let mut two = one.rec_mut();
108     let mut three = two.rec_mut();
109     let mut four = three.rec_mut();
110 
111     four.set_num(1);
112     assert_that!(four.num(), eq(1));
113 
114     assert_that!(rec.num(), eq(0));
115     assert_that!(rec.rec().rec().num(), eq(0));
116     assert_that!(rec.rec().rec().rec().rec().num(), eq(1));
117 
118     // This fails since `RecursiveMut` has `&mut self` methods.
119     // See b/314989133.
120     // let nested = rec.rec_mut().rec_mut().rec_mut();
121     // assert_that!(nested.num(), eq(0));
122 }
123