Skip to content

Commit f840421

Browse files
committed
Improve the test assertion failure message when the wrong enum variant is used in matches_pattern!.
Previously, the match explanation when the wrong enum variant was used would just state that there was no such field: ``` Value of: actual Expected: is AnEnum :: B which has field `0`, which is equal to 123 Actual: A(123), which has no field `0` ``` This is confusing, since the actual value _does_ have such a field. It's just of the wrong enum variant. This fixes the error message to indicate that the wrong variant was supplied: ``` 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` ``` To output the supplied enum variant, it is necessary to construct the debug output and strip it of any fields it may have. This is inefficient, but unfortunately there is no obvious way to directly debug-output just the enum variant with no fields.
1 parent 30cfca0 commit f840421

File tree

3 files changed

+90
-25
lines changed

3 files changed

+90
-25
lines changed

googletest/src/matchers/field_matcher.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ pub mod internal {
183183
self.inner.explain_match(actual)
184184
)
185185
} else {
186-
// TODO(hovinen): This message could be misinterpreted to mean that there were a
187-
// typo in the field, when it actually means that the actual value uses the
188-
// wrong enum variant. Reword this appropriately.
189-
format!("which has no field `{}`", self.field_path)
186+
let formatted_actual_value = format!("{actual:?}");
187+
let without_fields = formatted_actual_value.split('(').next().unwrap_or("");
188+
let without_fields = without_fields.split('{').next().unwrap_or("").trim_end();
189+
format!("which has the wrong enum variant `{without_fields}`")
190190
}
191191
}
192192

googletest/tests/field_matcher_test.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,49 @@ fn shows_correct_failure_message_for_wrong_enum_value() -> Result<()> {
121121

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

124-
verify_that!(result, err(displays_as(contains_substring("which has no field `a`"))))
124+
verify_that!(
125+
result,
126+
err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
127+
)
128+
}
129+
130+
#[test]
131+
fn shows_correct_failure_message_for_wrong_enum_value_with_tuple_field() -> Result<()> {
132+
#[derive(Debug)]
133+
enum AnEnum {
134+
#[allow(dead_code)] // This variant is intentionally unused.
135+
AValue(u32),
136+
AnotherValue(u32),
137+
}
138+
let value = AnEnum::AnotherValue(123);
139+
140+
let result = verify_that!(value, field!(AnEnum::AValue.0, eq(123)));
141+
142+
verify_that!(
143+
result,
144+
err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
145+
)
146+
}
147+
148+
#[test]
149+
fn shows_correct_failure_message_for_wrong_enum_value_with_named_field() -> Result<()> {
150+
#[derive(Debug)]
151+
enum AnEnum {
152+
#[allow(dead_code)] // This variant is intentionally unused.
153+
AValue(u32),
154+
AnotherValue {
155+
#[allow(unused)]
156+
a: u32,
157+
},
158+
}
159+
let value = AnEnum::AnotherValue { a: 123 };
160+
161+
let result = verify_that!(value, field!(AnEnum::AValue.0, eq(123)));
162+
163+
verify_that!(
164+
result,
165+
err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
166+
)
125167
}
126168

127169
#[test]

googletest/tests/matches_pattern_test.rs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,29 @@ fn has_correct_assertion_failure_message_for_field_and_property() -> Result<()>
188188
)
189189
}
190190

191+
#[test]
192+
fn has_meaningful_assertion_failure_message_when_wrong_enum_variant_is_used() -> Result<()> {
193+
#[derive(Debug)]
194+
enum AnEnum {
195+
A(u32),
196+
#[allow(unused)]
197+
B(u32),
198+
}
199+
let actual = AnEnum::A(123);
200+
let result = verify_that!(actual, matches_pattern!(AnEnum::B(eq(123))));
201+
202+
verify_that!(
203+
result,
204+
err(displays_as(contains_substring(indoc! {"
205+
Value of: actual
206+
Expected: is AnEnum :: B which has field `0`, which is equal to 123
207+
Actual: A(123),
208+
which has the wrong enum variant `A`
209+
"
210+
})))
211+
)
212+
}
213+
191214
#[test]
192215
fn supports_qualified_struct_names() -> Result<()> {
193216
mod a_module {
@@ -972,8 +995,8 @@ fn matches_struct_with_a_method_returning_reference_followed_by_a_field() -> Res
972995
}
973996

974997
#[test]
975-
fn matches_struct_with_a_method_returning_reference_followed_by_a_field_with_trailing_comma()
976-
-> Result<()> {
998+
fn matches_struct_with_a_method_returning_reference_followed_by_a_field_with_trailing_comma(
999+
) -> Result<()> {
9771000
#[derive(Debug)]
9781001
struct AStruct {
9791002
a_field: u32,
@@ -1043,8 +1066,8 @@ fn matches_struct_with_a_method_taking_enum_value_param_ret_ref_followed_by_fiel
10431066
}
10441067

10451068
#[test]
1046-
fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma_ret_ref_and_field()
1047-
-> Result<()> {
1069+
fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma_ret_ref_and_field(
1070+
) -> Result<()> {
10481071
#[derive(Debug)]
10491072
struct AStruct {
10501073
a_field: u32,
@@ -1270,8 +1293,8 @@ fn matches_struct_with_field_followed_by_method_taking_enum_value_param_ret_ref(
12701293
}
12711294

12721295
#[test]
1273-
fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_comma_ret_ref()
1274-
-> Result<()> {
1296+
fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_comma_ret_ref(
1297+
) -> Result<()> {
12751298
#[derive(Debug)]
12761299
struct AStruct {
12771300
a_field: u32,
@@ -1320,8 +1343,8 @@ fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field() -> Res
13201343
}
13211344

13221345
#[test]
1323-
fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field_with_trailing_comma()
1324-
-> Result<()> {
1346+
fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field_with_trailing_comma(
1347+
) -> Result<()> {
13251348
#[derive(Debug)]
13261349
struct AStruct {
13271350
a_field: u32,
@@ -1376,8 +1399,8 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_followed_by_a_fi
13761399
}
13771400

13781401
#[test]
1379-
fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_comma_followed_by_a_field()
1380-
-> Result<()> {
1402+
fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_comma_followed_by_a_field(
1403+
) -> Result<()> {
13811404
#[derive(Debug)]
13821405
struct AStruct {
13831406
a_field: u32,
@@ -1404,8 +1427,8 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_com
14041427
}
14051428

14061429
#[test]
1407-
fn matches_struct_with_field_followed_by_method_taking_enum_value_param_followed_by_field()
1408-
-> Result<()> {
1430+
fn matches_struct_with_field_followed_by_method_taking_enum_value_param_followed_by_field(
1431+
) -> Result<()> {
14091432
enum AnEnum {
14101433
AVariant,
14111434
}
@@ -1463,8 +1486,8 @@ fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field(
14631486
}
14641487

14651488
#[test]
1466-
fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field_with_trailing_comma()
1467-
-> Result<()> {
1489+
fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field_with_trailing_comma(
1490+
) -> Result<()> {
14681491
#[derive(Debug)]
14691492
struct AStruct {
14701493
a_field: u32,
@@ -1491,8 +1514,8 @@ fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field_
14911514
}
14921515

14931516
#[test]
1494-
fn matches_struct_with_a_field_followed_by_a_method_with_params_ret_ref_followed_by_a_field()
1495-
-> Result<()> {
1517+
fn matches_struct_with_a_field_followed_by_a_method_with_params_ret_ref_followed_by_a_field(
1518+
) -> Result<()> {
14961519
#[derive(Debug)]
14971520
struct AStruct {
14981521
a_field: u32,
@@ -1519,8 +1542,8 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_ret_ref_followed
15191542
}
15201543

15211544
#[test]
1522-
fn matches_struct_with_field_followed_by_method_taking_enum_value_param_ret_ref_followed_by_field()
1523-
-> Result<()> {
1545+
fn matches_struct_with_field_followed_by_method_taking_enum_value_param_ret_ref_followed_by_field(
1546+
) -> Result<()> {
15241547
enum AnEnum {
15251548
AVariant,
15261549
}
@@ -1551,8 +1574,8 @@ fn matches_struct_with_field_followed_by_method_taking_enum_value_param_ret_ref_
15511574
}
15521575

15531576
#[test]
1554-
fn matches_struct_with_a_field_followed_by_a_method_with_params_trailing_comma_ret_ref_followed_by_a_field()
1555-
-> Result<()> {
1577+
fn matches_struct_with_a_field_followed_by_a_method_with_params_trailing_comma_ret_ref_followed_by_a_field(
1578+
) -> Result<()> {
15561579
#[derive(Debug)]
15571580
struct AStruct {
15581581
a_field: u32,

0 commit comments

Comments
 (0)