Skip to content

Commit 4c086f8

Browse files
hovinenbcopybara-github
authored andcommitted
Do not repeat the actual value inside Option::Some, Result::Ok, or Result::Err in test assertion failure messages.
Previously, the test assertion failure message for the matchers some, ok, and err would contain the actual value. This is redundant since the value was already printed right above. This changes the implementation of explain_match in each of those matchers to just make reference to the explanation of the inner matcher along with a really brief description of the variant found ("has a value", "is a success with value", "is an error with value"). This makes the test assertion failure messages shorter and less jarring. PiperOrigin-RevId: 505970192
1 parent a7d7cc9 commit 4c086f8

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

googletest/src/matchers/err_matcher.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<T: Debug, E: Debug, InnerMatcherT: Matcher<E>> Matcher<Result<T, E>>
4242
fn explain_match(&self, actual: &Result<T, E>) -> MatchExplanation {
4343
match actual {
4444
Err(e) => MatchExplanation::create(format!(
45-
"which is an error containing {e:#?}, {}",
45+
"which is an error {}",
4646
self.inner.explain_match(e)
4747
)),
4848
Ok(_) => MatchExplanation::create("which is a success".to_string()),
@@ -52,14 +52,11 @@ impl<T: Debug, E: Debug, InnerMatcherT: Matcher<E>> Matcher<Result<T, E>>
5252
fn describe(&self, matcher_result: MatcherResult) -> String {
5353
match matcher_result {
5454
MatcherResult::Matches => {
55-
format!(
56-
"is an error containing a value, which {}",
57-
self.inner.describe(MatcherResult::Matches)
58-
)
55+
format!("is an error which {}", self.inner.describe(MatcherResult::Matches))
5956
}
6057
MatcherResult::DoesNotMatch => {
6158
format!(
62-
"is a success or is an error containing a value, which {}",
59+
"is a success or is an error containing a value which {}",
6360
self.inner.describe(MatcherResult::DoesNotMatch)
6461
)
6562
}
@@ -116,10 +113,10 @@ mod tests {
116113
err(displays_as(contains_substring(
117114
"\
118115
Value of: Err::<i32, i32>(1)
119-
Expected: is an error containing a value, which is equal to 2
116+
Expected: is an error which is equal to 2
120117
Actual: Err(
121118
1,
122-
), which is an error containing 1, which isn't equal to 2
119+
), which is an error which isn't equal to 2
123120
"
124121
)))
125122
)
@@ -129,7 +126,7 @@ Actual: Err(
129126
fn err_describe_matches() -> Result<()> {
130127
verify_that!(
131128
err::<i32, i32>(eq(1)).describe(MatcherResult::Matches),
132-
eq("is an error containing a value, which is equal to 1")
129+
eq("is an error which is equal to 1")
133130
)
134131
}
135132
}

googletest/src/matchers/ok_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<T: Debug, E: Debug, InnerMatcherT: Matcher<T>> Matcher<Result<T, E>>
4242
fn explain_match(&self, actual: &Result<T, E>) -> MatchExplanation {
4343
match actual {
4444
Ok(o) => MatchExplanation::create(format!(
45-
"which is a success containing {o:#?}, {}",
45+
"which is a success {}",
4646
self.inner.explain_match(o)
4747
)),
4848
Err(_) => MatchExplanation::create("which is an error".to_string()),
@@ -119,7 +119,7 @@ Value of: Ok::<i32, i32>(1)
119119
Expected: is a success containing a value, which is equal to 2
120120
Actual: Ok(
121121
1,
122-
), which is a success containing 1, which isn't equal to 2
122+
), which is a success which isn't equal to 2
123123
"
124124
)))
125125
)

googletest/src/matchers/some_matcher.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ impl<T: Debug, InnerMatcherT: Matcher<T>> Matcher<Option<T>> for SomeMatcher<Inn
4040
fn explain_match(&self, actual: &Option<T>) -> MatchExplanation {
4141
match (self.matches(actual), actual) {
4242
(_, Some(t)) => MatchExplanation::create(format!(
43-
"which contains {t:#?}, {}",
43+
"which has a value {}",
4444
self.inner.explain_match(t)
4545
)),
46-
(_, None) => MatchExplanation::create("which is none".to_string()),
46+
(_, None) => MatchExplanation::create("which is None".to_string()),
4747
}
4848
}
4949

5050
fn describe(&self, matcher_result: MatcherResult) -> String {
5151
match matcher_result {
5252
MatcherResult::Matches => {
53-
format!("is some(x), where x {}", self.inner.describe(MatcherResult::Matches))
53+
format!("has a value which {}", self.inner.describe(MatcherResult::Matches))
5454
}
5555
MatcherResult::DoesNotMatch => {
5656
format!(
57-
"is none or some(x), where x {}",
57+
"is None or has a value which {}",
5858
self.inner.describe(MatcherResult::DoesNotMatch)
5959
)
6060
}
@@ -107,10 +107,10 @@ mod tests {
107107
err(displays_as(contains_substring(
108108
"\
109109
Value of: Some(2)
110-
Expected: is some(x), where x is equal to 1
110+
Expected: has a value which is equal to 1
111111
Actual: Some(
112112
2,
113-
), which contains 2, which isn't equal to 1
113+
), which has a value which isn't equal to 1
114114
"
115115
)))
116116
)
@@ -120,36 +120,36 @@ Actual: Some(
120120
fn some_describe_matches() -> Result<()> {
121121
verify_that!(
122122
some(eq(1)).describe(MatcherResult::Matches),
123-
eq("is some(x), where x is equal to 1")
123+
eq("has a value which is equal to 1")
124124
)
125125
}
126126

127127
#[google_test]
128128
fn some_describe_does_not_match() -> Result<()> {
129129
verify_that!(
130130
some(eq(1)).describe(MatcherResult::DoesNotMatch),
131-
eq("is none or some(x), where x isn't equal to 1")
131+
eq("is None or has a value which isn't equal to 1")
132132
)
133133
}
134134

135135
#[google_test]
136136
fn some_explain_match_with_none() -> Result<()> {
137-
verify_that!(some(eq(1)).explain_match(&None), displays_as(eq("which is none")))
137+
verify_that!(some(eq(1)).explain_match(&None), displays_as(eq("which is None")))
138138
}
139139

140140
#[google_test]
141141
fn some_explain_match_with_some_success() -> Result<()> {
142142
verify_that!(
143143
some(eq(1)).explain_match(&Some(1)),
144-
displays_as(eq("which contains 1, which is equal to 1"))
144+
displays_as(eq("which has a value which is equal to 1"))
145145
)
146146
}
147147

148148
#[google_test]
149149
fn some_explain_match_with_some_fail() -> Result<()> {
150150
verify_that!(
151151
some(eq(1)).explain_match(&Some(2)),
152-
displays_as(eq("which contains 2, which isn't equal to 1"))
152+
displays_as(eq("which has a value which isn't equal to 1"))
153153
)
154154
}
155155
}

0 commit comments

Comments
 (0)