Skip to content

Commit 6424bca

Browse files
honglookercopybara-github
authored andcommitted
Add custom matchers for ProxiedWithPresence.
We continue our foray into googletest-rust [1], this time with homegrown matchers. Our unit tests will now be able to check is_unset() and is_set() for all types that implement ProxiedWithPresence. In practice, this boils down to [u8] and ProtoStr. Note that we've broken out matchers_upb and matchers_cpp, similar to what was done with aliasing here [2]. [1] https://github.com/google/googletest-rust [2] 9a0bc39#diff-08e5182ff36ad340a3bfb628995524a2a36a89b59a514ba027b0f25e048dd5c3R90 PiperOrigin-RevId: 573895179
1 parent 9301479 commit 6424bca

File tree

3 files changed

+112
-25
lines changed

3 files changed

+112
-25
lines changed

rust/test/shared/BUILD

+29-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,31 @@
1414
# Once we have a couple of these tests we will investigate ways to remove boilerplate (for example
1515
# by introducing a build macro that registers 2 rust_test targets).
1616

17-
load("@rules_rust//rust:defs.bzl", "rust_test")
17+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
18+
19+
rust_library(
20+
name = "matchers_upb",
21+
srcs = ["matchers.rs"],
22+
aliases = {
23+
"//rust:protobuf_upb": "protobuf",
24+
},
25+
deps = [
26+
"//third_party/gtest_rust/googletest",
27+
"//rust:protobuf_upb",
28+
],
29+
)
30+
31+
rust_library(
32+
name = "matchers_cpp",
33+
srcs = ["matchers.rs"],
34+
aliases = {
35+
"//rust:protobuf_cpp": "protobuf",
36+
},
37+
deps = [
38+
"//third_party/gtest_rust/googletest",
39+
"//rust:protobuf_cpp",
40+
],
41+
)
1842

1943
rust_test(
2044
name = "child_parent_upb_test",
@@ -125,6 +149,7 @@ rust_test(
125149
srcs = ["accessors_test.rs"],
126150
aliases = {
127151
"//rust:protobuf_cpp": "protobuf",
152+
"//rust/test/shared:matchers_cpp": "matchers",
128153
},
129154
tags = [
130155
# TODO: Enable testing on arm once we support sanitizers for Rust on Arm.
@@ -134,6 +159,7 @@ rust_test(
134159
"//third_party/gtest_rust/googletest",
135160
"//rust:protobuf_cpp",
136161
"//rust/test:unittest_cc_rust_proto",
162+
"//rust/test/shared:matchers_cpp",
137163
],
138164
)
139165

@@ -142,6 +168,7 @@ rust_test(
142168
srcs = ["accessors_test.rs"],
143169
aliases = {
144170
"//rust:protobuf_upb": "protobuf",
171+
"//rust/test/shared:matchers_upb": "matchers",
145172
},
146173
tags = [
147174
# TODO: Enable testing on arm once we support sanitizers for Rust on Arm.
@@ -151,6 +178,7 @@ rust_test(
151178
"//third_party/gtest_rust/googletest",
152179
"//rust:protobuf_upb",
153180
"//rust/test:unittest_upb_rust_proto",
181+
"//rust/test/shared:matchers_upb",
154182
],
155183
)
156184

rust/test/shared/accessors_test.rs

+25-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! Tests covering accessors for singular bool, int32, int64, and bytes fields.
99
1010
use googletest::prelude::*;
11+
use matchers::{is_set, is_unset};
1112
use protobuf::Optional;
1213
use unittest_proto::proto2_unittest::{TestAllTypes, TestAllTypes_};
1314

@@ -199,45 +200,45 @@ fn test_optional_bool_accessors() {
199200
#[test]
200201
fn test_optional_bytes_accessors() {
201202
let mut msg = TestAllTypes::new();
202-
assert_that!(msg.optional_bytes(), eq(b""));
203+
assert_that!(*msg.optional_bytes(), empty());
203204
assert_that!(msg.optional_bytes_opt(), eq(Optional::Unset(&b""[..])));
204-
assert_that!(msg.optional_bytes_mut().get(), eq(b""));
205-
assert_that!(msg.optional_bytes_mut().is_unset(), eq(true));
205+
assert_that!(*msg.optional_bytes_mut().get(), empty());
206+
assert_that!(msg.optional_bytes_mut(), is_unset());
206207

207208
{
208209
let s = Vec::from(&b"hello world"[..]);
209210
msg.optional_bytes_mut().set(&s[..]);
210211
}
211212
assert_that!(msg.optional_bytes(), eq(b"hello world"));
212213
assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b"hello world"[..])));
213-
assert_that!(msg.optional_bytes_mut().is_set(), eq(true));
214+
assert_that!(msg.optional_bytes_mut(), is_set());
214215
assert_that!(msg.optional_bytes_mut().get(), eq(b"hello world"));
215216

216217
msg.optional_bytes_mut().or_default().set(b"accessors_test");
217218
assert_that!(msg.optional_bytes(), eq(b"accessors_test"));
218219
assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b"accessors_test"[..])));
219-
assert_that!(msg.optional_bytes_mut().is_set(), eq(true));
220+
assert_that!(msg.optional_bytes_mut(), is_set());
220221
assert_that!(msg.optional_bytes_mut().get(), eq(b"accessors_test"));
221222
assert_that!(msg.optional_bytes_mut().or_default().get(), eq(b"accessors_test"));
222223

223224
msg.optional_bytes_mut().clear();
224-
assert_that!(msg.optional_bytes(), eq(b""));
225+
assert_that!(*msg.optional_bytes(), empty());
225226
assert_that!(msg.optional_bytes_opt(), eq(Optional::Unset(&b""[..])));
226-
assert_that!(msg.optional_bytes_mut().is_unset(), eq(true));
227+
assert_that!(msg.optional_bytes_mut(), is_unset());
227228

228229
msg.optional_bytes_mut().set(b"");
229-
assert_that!(msg.optional_bytes(), eq(b""));
230+
assert_that!(*msg.optional_bytes(), empty());
230231
assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b""[..])));
231232

232233
msg.optional_bytes_mut().clear();
233234
msg.optional_bytes_mut().or_default();
234-
assert_that!(msg.optional_bytes(), eq(b""));
235+
assert_that!(*msg.optional_bytes(), empty());
235236
assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b""[..])));
236237

237238
msg.optional_bytes_mut().or_default().set(b"\xffbinary\x85non-utf8");
238239
assert_that!(msg.optional_bytes(), eq(b"\xffbinary\x85non-utf8"));
239240
assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b"\xffbinary\x85non-utf8"[..])));
240-
assert_that!(msg.optional_bytes_mut().is_set(), eq(true));
241+
assert_that!(msg.optional_bytes_mut(), is_set());
241242
assert_that!(msg.optional_bytes_mut().get(), eq(b"\xffbinary\x85non-utf8"));
242243
assert_that!(msg.optional_bytes_mut().or_default().get(), eq(b"\xffbinary\x85non-utf8"));
243244
}
@@ -248,31 +249,31 @@ fn test_nonempty_default_bytes_accessors() {
248249
assert_that!(msg.default_bytes(), eq(b"world"));
249250
assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
250251
assert_that!(msg.default_bytes_mut().get(), eq(b"world"));
251-
assert_that!(msg.default_bytes_mut().is_unset(), eq(true));
252+
assert_that!(msg.default_bytes_mut(), is_unset());
252253

253254
{
254255
let s = String::from("hello world");
255256
msg.default_bytes_mut().set(s.as_bytes());
256257
}
257258
assert_that!(msg.default_bytes(), eq(b"hello world"));
258259
assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"hello world"[..])));
259-
assert_that!(msg.default_bytes_mut().is_set(), eq(true));
260+
assert_that!(msg.default_bytes_mut(), is_set());
260261
assert_that!(msg.default_bytes_mut().get(), eq(b"hello world"));
261262

262263
msg.default_bytes_mut().or_default().set(b"accessors_test");
263264
assert_that!(msg.default_bytes(), eq(b"accessors_test"));
264265
assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"accessors_test"[..])));
265-
assert_that!(msg.default_bytes_mut().is_set(), eq(true));
266+
assert_that!(msg.default_bytes_mut(), is_set());
266267
assert_that!(msg.default_bytes_mut().get(), eq(b"accessors_test"));
267268
assert_that!(msg.default_bytes_mut().or_default().get(), eq(b"accessors_test"));
268269

269270
msg.default_bytes_mut().clear();
270271
assert_that!(msg.default_bytes(), eq(b"world"));
271272
assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
272-
assert_that!(msg.default_bytes_mut().is_unset(), eq(true));
273+
assert_that!(msg.default_bytes_mut(), is_unset());
273274

274275
msg.default_bytes_mut().set(b"");
275-
assert_that!(msg.default_bytes(), eq(b""));
276+
assert_that!(*msg.default_bytes(), empty());
276277
assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b""[..])));
277278

278279
msg.default_bytes_mut().clear();
@@ -283,7 +284,7 @@ fn test_nonempty_default_bytes_accessors() {
283284
msg.default_bytes_mut().or_default().set(b"\xffbinary\x85non-utf8");
284285
assert_that!(msg.default_bytes(), eq(b"\xffbinary\x85non-utf8"));
285286
assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"\xffbinary\x85non-utf8"[..])));
286-
assert_that!(msg.default_bytes_mut().is_set(), eq(true));
287+
assert_that!(msg.default_bytes_mut(), is_set());
287288
assert_that!(msg.default_bytes_mut().get(), eq(b"\xffbinary\x85non-utf8"));
288289
assert_that!(msg.default_bytes_mut().or_default().get(), eq(b"\xffbinary\x85non-utf8"));
289290
}
@@ -294,28 +295,28 @@ fn test_optional_string_accessors() {
294295
assert_that!(msg.optional_string(), eq(""));
295296
assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
296297
assert_that!(msg.optional_string_mut().get(), eq(""));
297-
assert_that!(msg.optional_string_mut().is_unset(), eq(true));
298+
assert_that!(msg.optional_string_mut(), is_unset());
298299

299300
{
300301
let s = String::from("hello world");
301302
msg.optional_string_mut().set(&s[..]);
302303
}
303304
assert_that!(msg.optional_string(), eq("hello world"));
304305
assert_that!(msg.optional_string_opt(), eq(Optional::Set("hello world".into())));
305-
assert_that!(msg.optional_string_mut().is_set(), eq(true));
306+
assert_that!(msg.optional_string_mut(), is_set());
306307
assert_that!(msg.optional_string_mut().get(), eq("hello world"));
307308

308309
msg.optional_string_mut().or_default().set("accessors_test");
309310
assert_that!(msg.optional_string(), eq("accessors_test"));
310311
assert_that!(msg.optional_string_opt(), eq(Optional::Set("accessors_test".into())));
311-
assert_that!(msg.optional_string_mut().is_set(), eq(true));
312+
assert_that!(msg.optional_string_mut(), is_set());
312313
assert_that!(msg.optional_string_mut().get(), eq("accessors_test"));
313314
assert_that!(msg.optional_string_mut().or_default().get(), eq("accessors_test"));
314315

315316
msg.optional_string_mut().clear();
316317
assert_that!(msg.optional_string(), eq(""));
317318
assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
318-
assert_that!(msg.optional_string_mut().is_unset(), eq(true));
319+
assert_that!(msg.optional_string_mut(), is_unset());
319320

320321
msg.optional_string_mut().set("");
321322
assert_that!(msg.optional_string(), eq(""));
@@ -333,28 +334,28 @@ fn test_nonempty_default_string_accessors() {
333334
assert_that!(msg.default_string(), eq("hello"));
334335
assert_that!(msg.default_string_opt(), eq(Optional::Unset("hello".into())));
335336
assert_that!(msg.default_string_mut().get(), eq("hello"));
336-
assert_that!(msg.default_string_mut().is_unset(), eq(true));
337+
assert_that!(msg.default_string_mut(), is_unset());
337338

338339
{
339340
let s = String::from("hello world");
340341
msg.default_string_mut().set(&s[..]);
341342
}
342343
assert_that!(msg.default_string(), eq("hello world"));
343344
assert_that!(msg.default_string_opt(), eq(Optional::Set("hello world".into())));
344-
assert_that!(msg.default_string_mut().is_set(), eq(true));
345+
assert_that!(msg.default_string_mut(), is_set());
345346
assert_that!(msg.default_string_mut().get(), eq("hello world"));
346347

347348
msg.default_string_mut().or_default().set("accessors_test");
348349
assert_that!(msg.default_string(), eq("accessors_test"));
349350
assert_that!(msg.default_string_opt(), eq(Optional::Set("accessors_test".into())));
350-
assert_that!(msg.default_string_mut().is_set(), eq(true));
351+
assert_that!(msg.default_string_mut(), is_set());
351352
assert_that!(msg.default_string_mut().get(), eq("accessors_test"));
352353
assert_that!(msg.default_string_mut().or_default().get(), eq("accessors_test"));
353354

354355
msg.default_string_mut().clear();
355356
assert_that!(msg.default_string(), eq("hello"));
356357
assert_that!(msg.default_string_opt(), eq(Optional::Unset("hello".into())));
357-
assert_that!(msg.default_string_mut().is_unset(), eq(true));
358+
assert_that!(msg.default_string_mut(), is_unset());
358359

359360
msg.default_string_mut().set("");
360361
assert_that!(msg.default_string(), eq(""));

rust/test/shared/matchers.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use googletest::matcher::MatcherResult;
2+
use googletest::prelude::*;
3+
use protobuf::{AbsentField, Optional, PresentField, ProxiedWithPresence};
4+
use std::marker::PhantomData;
5+
6+
/// ===============================
7+
/// IS_UNSET
8+
/// ===============================
9+
pub fn is_unset<'a, T: std::fmt::Debug + ProxiedWithPresence + ?Sized + 'a>()
10+
-> impl Matcher<ActualT = Optional<PresentField<'a, T>, AbsentField<'a, T>>> {
11+
UnsetMatcher::<T> { _phantom: PhantomData }
12+
}
13+
14+
struct UnsetMatcher<'a, T: ProxiedWithPresence + ?Sized> {
15+
_phantom: PhantomData<PresentField<'a, T>>,
16+
}
17+
18+
impl<'a, T: std::fmt::Debug + ProxiedWithPresence + ?Sized> Matcher for UnsetMatcher<'a, T> {
19+
type ActualT = Optional<PresentField<'a, T>, AbsentField<'a, T>>;
20+
21+
fn matches(&self, actual: &Self::ActualT) -> MatcherResult {
22+
actual.is_unset().into()
23+
}
24+
25+
fn describe(&self, matcher_result: MatcherResult) -> String {
26+
match matcher_result {
27+
MatcherResult::Match => "is not set".to_string(),
28+
MatcherResult::NoMatch => "is set".to_string(),
29+
}
30+
}
31+
}
32+
33+
/// ===============================
34+
/// IS_SET
35+
/// ===============================
36+
pub fn is_set<'a, T: std::fmt::Debug + ProxiedWithPresence + ?Sized + 'a>()
37+
-> impl Matcher<ActualT = Optional<PresentField<'a, T>, AbsentField<'a, T>>> {
38+
SetMatcher::<T> { _phantom: PhantomData }
39+
}
40+
41+
struct SetMatcher<'a, T: ProxiedWithPresence + ?Sized> {
42+
_phantom: PhantomData<PresentField<'a, T>>,
43+
}
44+
45+
impl<'a, T: std::fmt::Debug + ProxiedWithPresence + ?Sized> Matcher for SetMatcher<'a, T> {
46+
type ActualT = Optional<PresentField<'a, T>, AbsentField<'a, T>>;
47+
48+
fn matches(&self, actual: &Self::ActualT) -> MatcherResult {
49+
actual.is_set().into()
50+
}
51+
52+
fn describe(&self, matcher_result: MatcherResult) -> String {
53+
match matcher_result {
54+
MatcherResult::Match => "is set".to_string(),
55+
MatcherResult::NoMatch => "is not set".to_string(),
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)