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::proto;
12 use unittest_rust_proto::{
13 test_all_types::{self, NestedMessage},
14 NestedTestAllTypes, TestAllTypes,
15 };
16
17 struct TestValue {
18 val: i64,
19 }
20
21 #[gtest]
test_setting_literals()22 fn test_setting_literals() {
23 let fixed64 = || 108;
24 let test_ref = |x: &i64| *x;
25 let test_ref_b = |x: &u32| *x;
26 let one_oh_seven = [107_u32];
27 let value = TestValue { val: 106 };
28
29 let msg = proto!(TestAllTypes {
30 optional_int32: 101,
31 optional_int64: 102,
32 optional_uint32: 103,
33 optional_uint64: if true { 104 } else { 99 },
34 optional_sint32: -105,
35 optional_sint64: (test_ref(&value.val)),
36 optional_fixed32: { test_ref_b(&one_oh_seven[0]) },
37 optional_fixed64: fixed64(), //108
38 optional_sfixed32: 100 + 9,
39 optional_sfixed64: {
40 let x = 10;
41 100 + x
42 },
43 optional_nested_message: NestedMessage { bb: 42 },
44 optional_float: 111.5,
45 optional_double: 112000.5,
46 optional_bool: true,
47 optional_string: "foo",
48 optional_bytes: b"bar",
49 optional_nested_enum: test_all_types::NestedEnum::Baz,
50 });
51
52 assert_that!(msg.optional_int32(), eq(101));
53 assert_that!(msg.optional_int64(), eq(102));
54 assert_that!(msg.optional_uint32(), eq(103));
55 assert_that!(msg.optional_uint64(), eq(104));
56 assert_that!(msg.optional_sint32(), eq(-105));
57 assert_that!(msg.optional_sint64(), eq(106));
58 assert_that!(msg.optional_fixed32(), eq(107));
59 assert_that!(msg.optional_fixed64(), eq(108));
60 assert_that!(msg.optional_sfixed32(), eq(109));
61 assert_that!(msg.optional_sfixed64(), eq(110));
62 assert_that!(msg.optional_float(), eq(111.5));
63 assert_that!(msg.optional_double(), eq(112000.5));
64 assert_that!(msg.optional_bool(), eq(true));
65 assert_that!(msg.optional_string(), eq("foo"));
66 assert_that!(msg.optional_bytes(), eq(b"bar"));
67 assert_that!(msg.optional_nested_enum(), eq(test_all_types::NestedEnum::Baz));
68 }
69
70 #[gtest]
single_nested_message()71 fn single_nested_message() {
72 let msg = proto!(TestAllTypes { optional_nested_message: NestedMessage { bb: 42 } });
73 assert_that!(msg.optional_nested_message().bb(), eq(42));
74
75 // field above it
76 let msg = proto!(TestAllTypes {
77 optional_int32: 1,
78 optional_nested_message: NestedMessage { bb: 42 }
79 });
80 assert_that!(msg.optional_nested_message().bb(), eq(42));
81
82 // field below it
83 let msg = proto!(TestAllTypes {
84 optional_nested_message: NestedMessage { bb: 42 },
85 optional_int32: 1
86 });
87 assert_that!(msg.optional_nested_message().bb(), eq(42));
88
89 // field above and below it
90 let msg = proto!(TestAllTypes {
91 optional_int32: 1,
92 optional_nested_message: NestedMessage { bb: 42 },
93 optional_int64: 2
94 });
95 assert_that!(msg.optional_nested_message().bb(), eq(42));
96
97 // field above and below it
98 let msg = proto!(TestAllTypes {
99 optional_int32: 1,
100 optional_nested_message: __ { bb: 42 },
101 optional_int64: 2
102 });
103 assert_that!(msg.optional_nested_message().bb(), eq(42));
104
105 // test empty initializer
106 let msg = proto!(TestAllTypes {});
107 assert_that!(msg.has_optional_nested_message(), eq(false));
108
109 // empty nested message should be present
110 // make sure that qualified path names work
111 let msg = proto!(::unittest_rust_proto::TestAllTypes {
112 optional_nested_message: unittest_rust_proto::test_all_types::NestedMessage {}
113 });
114 assert_that!(msg.has_optional_nested_message(), eq(true));
115
116 let msg = proto!(::unittest_rust_proto::TestAllTypes {
117 optional_nested_message: ::unittest_rust_proto::test_all_types::NestedMessage {}
118 });
119 assert_that!(msg.has_optional_nested_message(), eq(true));
120
121 let msg = proto!(::unittest_rust_proto::TestAllTypes { optional_nested_message: __ {} });
122 assert_that!(msg.has_optional_nested_message(), eq(true));
123 }
124
125 #[gtest]
test_recursive_msg()126 fn test_recursive_msg() {
127 let msg = proto!(NestedTestAllTypes {
128 child: NestedTestAllTypes {
129 payload: TestAllTypes { optional_int32: 41 },
130 child: NestedTestAllTypes {
131 child: NestedTestAllTypes { payload: TestAllTypes { optional_int32: 43 } },
132 payload: TestAllTypes { optional_int32: 42 }
133 }
134 }
135 });
136
137 assert_that!(msg.child().payload().optional_int32(), eq(41));
138 assert_that!(msg.child().child().payload().optional_int32(), eq(42));
139 assert_that!(msg.child().child().child().payload().optional_int32(), eq(43));
140 }
141
142 #[gtest]
test_spread_msg()143 fn test_spread_msg() {
144 let msg = proto!(TestAllTypes { optional_nested_message: NestedMessage { bb: 42 } });
145 let msg2 = proto!(TestAllTypes { ..msg.as_view() });
146 assert_that!(msg2.optional_nested_message().bb(), eq(42));
147 let msg3 = proto!(TestAllTypes { optional_int32: 1, ..msg.as_view() });
148 assert_that!(msg3.optional_nested_message().bb(), eq(42));
149 assert_that!(msg3.optional_int32(), eq(1));
150 }
151
152 #[gtest]
test_spread_nested_msg()153 fn test_spread_nested_msg() {
154 let msg = proto!(NestedTestAllTypes {
155 child: NestedTestAllTypes {
156 payload: TestAllTypes { optional_int32: 41 },
157 child: NestedTestAllTypes {
158 child: NestedTestAllTypes { payload: TestAllTypes { optional_int32: 43 } },
159 payload: TestAllTypes { optional_int32: 42 }
160 }
161 }
162 });
163 let msg2 = proto!(NestedTestAllTypes {
164 child: NestedTestAllTypes { payload: TestAllTypes { optional_int32: 100 }, ..msg.child() }
165 });
166 assert_that!(msg2.child().payload().optional_int32(), eq(100));
167 assert_that!(msg2.child().child().payload().optional_int32(), eq(42));
168 assert_that!(msg2.child().child().child().payload().optional_int32(), eq(43));
169 }
170
171 #[gtest]
test_repeated_i32()172 fn test_repeated_i32() {
173 let msg = proto!(TestAllTypes { repeated_int32: [1, 1 + 1, 3] });
174 assert_that!(msg.repeated_int32().len(), eq(3));
175 assert_that!(msg.repeated_int32().get(0).unwrap(), eq(1));
176 assert_that!(msg.repeated_int32().get(1).unwrap(), eq(2));
177 assert_that!(msg.repeated_int32().get(2).unwrap(), eq(3));
178 }
179
180 #[gtest]
test_repeated_msg()181 fn test_repeated_msg() {
182 let msg2 = proto!(NestedTestAllTypes { payload: TestAllTypes { optional_int32: 1 } });
183 let msg = proto!(NestedTestAllTypes {
184 child: NestedTestAllTypes {
185 repeated_child: [
186 NestedTestAllTypes { payload: TestAllTypes { optional_int32: 0 } },
187 msg2,
188 __ { payload: TestAllTypes { optional_int32: 2 } }
189 ]
190 },
191 repeated_child: [
192 __ { payload: __ { optional_int32: 1 } },
193 NestedTestAllTypes { payload: TestAllTypes { optional_int32: 2 } }
194 ]
195 });
196 assert_that!(msg.child().repeated_child().len(), eq(3));
197 assert_that!(msg.child().repeated_child().get(0).unwrap().payload().optional_int32(), eq(0));
198 assert_that!(msg.child().repeated_child().get(1).unwrap().payload().optional_int32(), eq(1));
199 assert_that!(msg.child().repeated_child().get(2).unwrap().payload().optional_int32(), eq(2));
200
201 assert_that!(msg.repeated_child().len(), eq(2));
202 assert_that!(msg.repeated_child().get(0).unwrap().payload().optional_int32(), eq(1));
203 assert_that!(msg.repeated_child().get(1).unwrap().payload().optional_int32(), eq(2));
204 }
205