Skip to content

Commit

Permalink
Merge pull request #245 from google:show-wrong-enum-variant-in-matche…
Browse files Browse the repository at this point in the history
…s-pattern

PiperOrigin-RevId: 544655417
  • Loading branch information
copybara-github committed Jun 30, 2023
2 parents 933c8bc + f840421 commit e8eebff
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
8 changes: 4 additions & 4 deletions googletest/src/matchers/field_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ pub mod internal {
self.inner.explain_match(actual)
)
} else {
// TODO(hovinen): This message could be misinterpreted to mean that there were a
// typo in the field, when it actually means that the actual value uses the
// wrong enum variant. Reword this appropriately.
format!("which has no field `{}`", self.field_path)
let formatted_actual_value = format!("{actual:?}");
let without_fields = formatted_actual_value.split('(').next().unwrap_or("");
let without_fields = without_fields.split('{').next().unwrap_or("").trim_end();
format!("which has the wrong enum variant `{without_fields}`")
}
}

Expand Down
44 changes: 43 additions & 1 deletion googletest/tests/field_matcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,49 @@ fn shows_correct_failure_message_for_wrong_enum_value() -> Result<()> {

let result = verify_that!(value, field!(AnEnum::AValue.a, eq(123)));

verify_that!(result, err(displays_as(contains_substring("which has no field `a`"))))
verify_that!(
result,
err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
)
}

#[test]
fn shows_correct_failure_message_for_wrong_enum_value_with_tuple_field() -> Result<()> {
#[derive(Debug)]
enum AnEnum {
#[allow(dead_code)] // This variant is intentionally unused.
AValue(u32),
AnotherValue(u32),
}
let value = AnEnum::AnotherValue(123);

let result = verify_that!(value, field!(AnEnum::AValue.0, eq(123)));

verify_that!(
result,
err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
)
}

#[test]
fn shows_correct_failure_message_for_wrong_enum_value_with_named_field() -> Result<()> {
#[derive(Debug)]
enum AnEnum {
#[allow(dead_code)] // This variant is intentionally unused.
AValue(u32),
AnotherValue {
#[allow(unused)]
a: u32,
},
}
let value = AnEnum::AnotherValue { a: 123 };

let result = verify_that!(value, field!(AnEnum::AValue.0, eq(123)));

verify_that!(
result,
err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
)
}

#[test]
Expand Down
23 changes: 23 additions & 0 deletions googletest/tests/matches_pattern_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,29 @@ fn has_correct_assertion_failure_message_for_field_and_property() -> Result<()>
)
}

#[test]
fn has_meaningful_assertion_failure_message_when_wrong_enum_variant_is_used() -> Result<()> {
#[derive(Debug)]
enum AnEnum {
A(u32),
#[allow(unused)]
B(u32),
}
let actual = AnEnum::A(123);
let result = verify_that!(actual, matches_pattern!(AnEnum::B(eq(123))));

verify_that!(
result,
err(displays_as(contains_substring(indoc! {"
Value of: actual
Expected: is AnEnum :: B which has field `0`, which is equal to 123
Actual: A(123),
which has the wrong enum variant `A`
"
})))
)
}

#[test]
fn supports_qualified_struct_names() -> Result<()> {
mod a_module {
Expand Down

0 comments on commit e8eebff

Please sign in to comment.