Skip to content

Commit 0a13b0a

Browse files
committed
Add support for trailing commas on assert_that! and expect_that!.
Previously, putting a trailing comma on `assert_that!` or `expect_that!` would produce a compilation error when no custom error message is present: ``` assert_that!( value, eq(2), // Error! ); ``` This change adds support for trailing commas, making the behaviour of these macros more consistent with the rest of the library and Rust idioms. Fixes #340
1 parent 1fb0652 commit 0a13b0a

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

googletest/src/assertions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ macro_rules! verify_that {
136136
$crate::internal::source_location::SourceLocation::new(file!(), line!(), column!()),
137137
)
138138
};
139-
($actual:expr, $expected:expr) => {
139+
($actual:expr, $expected:expr $(,)?) => {
140140
$crate::assertions::internal::check_matcher(
141141
&$actual,
142142
$expected,
@@ -351,7 +351,7 @@ macro_rules! fail {
351351
/// equivalent to `ASSERT_THAT`, use [`verify_that!`] with the `?` operator.
352352
#[macro_export]
353353
macro_rules! assert_that {
354-
($actual:expr, $expected:expr) => {
354+
($actual:expr, $expected:expr $(,)?) => {
355355
match $crate::verify_that!($actual, $expected) {
356356
Ok(_) => {}
357357
Err(e) => {
@@ -442,7 +442,7 @@ macro_rules! assert_pred {
442442
/// ```
443443
#[macro_export]
444444
macro_rules! expect_that {
445-
($actual:expr, $expected:expr) => {{
445+
($actual:expr, $expected:expr $(,)?) => {{
446446
use $crate::GoogleTestSupport;
447447
$crate::verify_that!($actual, $expected).and_log_failure();
448448
}};

integration_tests/src/integration_tests.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ mod tests {
2626
verify_that!(value, eq(2))
2727
}
2828

29+
#[test]
30+
fn verify_that_supports_trailing_comma() -> Result<()> {
31+
let value = 2;
32+
verify_that!(value, eq(2),)
33+
}
34+
2935
#[test]
3036
fn should_pass_with_omitted_elements_are() -> Result<()> {
3137
verify_that!(vec![1, 2], [eq(1), eq(2)])
@@ -48,10 +54,21 @@ mod tests {
4854
}
4955

5056
#[test]
51-
fn should_pass_with_assert_that() -> Result<()> {
57+
fn should_pass_with_assert_that() {
5258
let value = 2;
5359
assert_that!(value, eq(2));
54-
Ok(())
60+
}
61+
62+
#[test]
63+
fn assert_that_supports_trailing_comma() {
64+
let value = 2;
65+
assert_that!(value, eq(2),);
66+
}
67+
68+
#[test]
69+
fn assert_that_with_custom_failure_message_supports_trailing_comma() {
70+
let value = 2;
71+
assert_that!(value, eq(2), "A custom error message",);
5572
}
5673

5774
#[googletest::test]
@@ -67,6 +84,18 @@ mod tests {
6784
expect_that!(value, eq(2));
6885
}
6986

87+
#[googletest::test]
88+
fn expect_that_supports_trailing_comma() {
89+
let value = 2;
90+
expect_that!(value, eq(2),);
91+
}
92+
93+
#[googletest::test]
94+
fn expect_that_with_custom_failure_message_supports_trailing_comma() {
95+
let value = 2;
96+
expect_that!(value, eq(2), "A custom error message",);
97+
}
98+
7099
#[test]
71100
fn should_fail_on_assertion_failure() -> Result<()> {
72101
let status = run_external_process("simple_assertion_failure").status()?;

0 commit comments

Comments
 (0)