Skip to content

Commit 5f524cf

Browse files
committed
Remove the __result_of_internal and __result_of_ref_internal
1 parent 01c0279 commit 5f524cf

File tree

1 file changed

+179
-77
lines changed

1 file changed

+179
-77
lines changed

googletest/src/matchers/result_of_matcher.rs

Lines changed: 179 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,21 @@
1717
///
1818
/// The `callable` will be called twice, so make sure it is pure.
1919
/// ```
20-
/// # use googletest::prelude::*;
21-
/// # fn should_pass() -> googletest::Result<()> {
22-
/// # verify_that!(100, result_of!(|value| value + 1, eq(101)))?; // Passes
23-
/// # Ok(())
24-
/// # }
20+
/// use googletest::prelude::*;
21+
/// fn should_pass() -> googletest::Result<()> {
22+
/// verify_that!(100, result_of!(|value| value + 1, eq(101)))?; // Passes
23+
/// Ok(())
24+
/// }
2525
///
26-
/// # fn should_fail() -> googletest::Result<()> {
27-
/// # verify_that!(100, result_of!(|value| value * 2, eq(100)))?; // Fails
28-
/// # Ok(())
29-
/// # }
30-
/// # should_pass().unwrap();
31-
/// # should_fail().unwrap_err();
26+
/// fn should_fail() -> googletest::Result<()> {
27+
/// verify_that!(100, result_of!(|value| value * 2, eq(100)))?; // Fails
28+
/// Ok(())
29+
/// }
30+
/// should_pass().unwrap();
31+
/// should_fail().unwrap_err();
3232
/// ```
3333
#[macro_export]
34-
#[doc(hidden)]
3534
macro_rules! __result_of {
36-
($($t: tt)*) => { $crate::result_of_internal!($($t)*) };
37-
}
38-
39-
#[macro_export]
40-
macro_rules! result_of_internal {
4135
($function: expr, $matcher: expr) => {{
4236
$crate::matchers::__internal_unstable_do_not_depend_on_these::result_of(
4337
$function,
@@ -52,33 +46,27 @@ macro_rules! result_of_internal {
5246
///
5347
/// The `callable` will be called twice, so make sure it is pure.
5448
/// ```
55-
/// # use googletest::prelude::*;
56-
/// # fn should_pass_1() -> googletest::Result<()> {
57-
/// # verify_that!("hello", result_of_ref!(|s: &str| s.to_uppercase(), eq("HELLO")))?; // Passes
58-
/// # Ok(())
59-
/// # }
49+
/// use googletest::prelude::*;
50+
/// fn should_pass_1() -> googletest::Result<()> {
51+
/// verify_that!("hello", result_of_ref!(|s: &str| s.to_uppercase(), eq("HELLO")))?; // Passes
52+
/// Ok(())
53+
/// }
6054
///
61-
/// # fn should_pass_2() -> googletest::Result<()> {
62-
/// # verify_that!(100, result_of_ref!(|value| value + 1, eq(&101)))?; // Passes
63-
/// # Ok(())
64-
/// # }
55+
/// fn should_pass_2() -> googletest::Result<()> {
56+
/// verify_that!(100, result_of_ref!(|value| value + 1, eq(&101)))?; // Passes
57+
/// Ok(())
58+
/// }
6559
///
66-
/// # fn should_fail() -> googletest::Result<()> {
67-
/// # verify_that!("world", result_of_ref!(|s: &str| s.to_uppercase(), eq("HELLO")))?; // Passes
68-
/// # Ok(())
69-
/// # }
70-
/// # should_pass_1().unwrap();
71-
/// # should_pass_2().unwrap();
72-
/// # should_fail().unwrap_err();
60+
/// fn should_fail() -> googletest::Result<()> {
61+
/// verify_that!("world", result_of_ref!(|s: &str| s.to_uppercase(), eq("HELLO")))?; // Passes
62+
/// Ok(())
63+
/// }
64+
/// should_pass_1().unwrap();
65+
/// should_pass_2().unwrap();
66+
/// should_fail().unwrap_err();
7367
/// ```
7468
#[macro_export]
75-
#[doc(hidden)]
7669
macro_rules! __result_of_ref {
77-
($($t: tt)*) => { $crate::result_of_ref_internal!($($t)*)};
78-
}
79-
80-
#[macro_export]
81-
macro_rules! result_of_ref_internal {
8270
($function: expr, $matcher: expr) => {{
8371
$crate::matchers::__internal_unstable_do_not_depend_on_these::result_of_ref(
8472
$function,
@@ -120,17 +108,16 @@ pub mod internal {
120108
}
121109

122110
fn describe(&self, matcher_result: MatcherResult) -> Description {
123-
self.inner_matcher.describe(matcher_result)
111+
Description::new()
112+
.text(format!("by applying {},", self.callable_description))
113+
.nested(self.inner_matcher.describe(matcher_result))
124114
}
125115

126116
fn explain_match(&self, actual: I) -> Description {
127117
let actual_result = (self.callable)(actual);
128-
format!(
129-
"where the result of applying {actual:?} to the callable `{}` is {actual_result:?} which {}",
130-
self.callable_description,
131-
self.describe(self.inner_matcher.matches(actual_result))
132-
)
133-
.into()
118+
Description::new()
119+
.text(format!("which, results into {actual_result:?}",))
120+
.nested(self.describe(self.matches(actual)))
134121
}
135122
}
136123

@@ -160,19 +147,24 @@ pub mod internal {
160147
}
161148

162149
fn describe(&self, matcher_result: MatcherResult) -> Description {
163-
self.inner_matcher.describe(matcher_result)
150+
Description::new()
151+
.text(format!("by applying {},", self.callable_description))
152+
.nested(self.inner_matcher.describe(matcher_result))
164153
}
165154

166155
fn explain_match(&self, actual: I) -> Description {
167156
let actual_result = (self.callable)(actual);
168-
Description::new().text(format!("where the result of applying {actual:?} to the callable `{}` is {actual_result:?}", self.callable_description)).nested(self.inner_matcher.explain_match(&actual_result))
157+
Description::new()
158+
.text(format!("which, results into {actual_result:?}",))
159+
.nested(self.describe(self.matches(actual)))
169160
}
170161
}
171162
}
172163

173164
#[cfg(test)]
174165
mod tests {
175166
use crate::prelude::*;
167+
use indoc::indoc;
176168

177169
#[test]
178170
fn result_of_match_with_value() -> Result<()> {
@@ -192,9 +184,17 @@ mod tests {
192184
let result = verify_that!(0, result_of!(|value| value - 1, eq(2)));
193185
verify_that!(
194186
result,
195-
err(displays_as(contains_substring(
196-
"where the result of applying 0 to the callable `|value| value - 1` is -1 which isn't equal to 2"
197-
)))
187+
err(displays_as(contains_substring(indoc!(
188+
"
189+
Value of: 0
190+
Expected: by applying |value| value - 1,
191+
is equal to 2
192+
Actual: 0,
193+
which, results into -1
194+
by applying |value| value - 1,
195+
isn't equal to 2
196+
"
197+
))))
198198
)
199199
}
200200

@@ -203,9 +203,17 @@ mod tests {
203203
let result = verify_that!(0, result_of!(|value| { value - 1 }, eq(2)));
204204
verify_that!(
205205
result,
206-
err(displays_as(contains_substring(
207-
"where the result of applying 0 to the callable `|value| { value - 1 }` is -1 which isn't equal to 2"
208-
)))
206+
err(displays_as(contains_substring(indoc!(
207+
"
208+
Value of: 0
209+
Expected: by applying |value| { value - 1 },
210+
is equal to 2
211+
Actual: 0,
212+
which, results into -1
213+
by applying |value| { value - 1 },
214+
isn't equal to 2
215+
"
216+
))))
209217
)
210218
}
211219

@@ -224,9 +232,17 @@ mod tests {
224232
);
225233
verify_that!(
226234
result,
227-
err(displays_as(contains_substring(
228-
"where the result of applying 0 to the callable `|value| { let dec = value - 1; let inc = dec + 1; inc - 2 }` is -2 which isn't equal to 2"
229-
)))
235+
err(displays_as(contains_substring(indoc!(
236+
"
237+
Value of: 0
238+
Expected: by applying |value| { let dec = value - 1; let inc = dec + 1; inc - 2 },
239+
is equal to 2
240+
Actual: 0,
241+
which, results into -2
242+
by applying |value| { let dec = value - 1; let inc = dec + 1; inc - 2 },
243+
isn't equal to 2
244+
"
245+
))))
230246
)
231247
}
232248

@@ -238,9 +254,17 @@ mod tests {
238254
let result = verify_that!(0, result_of!(dec_by_one, eq(2)));
239255
verify_that!(
240256
result,
241-
err(displays_as(contains_substring(
242-
"where the result of applying 0 to the callable `dec_by_one` is -1 which isn't equal to 2"
243-
)))
257+
err(displays_as(contains_substring(indoc!(
258+
"
259+
Value of: 0
260+
Expected: by applying dec_by_one,
261+
is equal to 2
262+
Actual: 0,
263+
which, results into -1
264+
by applying dec_by_one,
265+
isn't equal to 2
266+
"
267+
))))
244268
)
245269
}
246270

@@ -267,9 +291,16 @@ mod tests {
267291
let result = verify_that!("world", result_of_ref!(|s: &str| s.to_uppercase(), eq("HELLO")));
268292
verify_that!(
269293
result,
270-
err(displays_as(contains_substring(
271-
"where the result of applying \"world\" to the callable `|s: &str| s.to_uppercase()` is \"WORLD\"\n which isn't equal to \"HELLO\""
272-
)))
294+
err(displays_as(contains_substring(indoc!(
295+
r#"
296+
Value of: "world"
297+
Expected: by applying |s: &str| s.to_uppercase(),
298+
is equal to "HELLO"
299+
Actual: "world",
300+
which, results into "WORLD"
301+
by applying |s: &str| s.to_uppercase(),
302+
isn't equal to "HELLO""#
303+
))))
273304
)
274305
}
275306

@@ -279,9 +310,17 @@ mod tests {
279310
verify_that!("world", result_of_ref!(|s: &str| { s.to_uppercase() }, eq("HELLO")));
280311
verify_that!(
281312
result,
282-
err(displays_as(contains_substring(
283-
"where the result of applying \"world\" to the callable `|s: &str| { s.to_uppercase() }` is \"WORLD\"\n which isn't equal to \"HELLO\""
284-
)))
313+
err(displays_as(contains_substring(indoc!(
314+
r#"
315+
Value of: "world"
316+
Expected: by applying |s: &str| { s.to_uppercase() },
317+
is equal to "HELLO"
318+
Actual: "world",
319+
which, results into "WORLD"
320+
by applying |s: &str| { s.to_uppercase() },
321+
isn't equal to "HELLO"
322+
"#
323+
))))
285324
)
286325
}
287326

@@ -293,9 +332,17 @@ mod tests {
293332
let result = verify_that!("world", result_of_ref!(to_upper_case, eq("HELLO")));
294333
verify_that!(
295334
result,
296-
err(displays_as(contains_substring(
297-
"where the result of applying \"world\" to the callable `to_upper_case` is \"WORLD\"\n which isn't equal to \"HELLO\""
298-
)))
335+
err(displays_as(contains_substring(indoc!(
336+
r#"
337+
Value of: "world"
338+
Expected: by applying to_upper_case,
339+
is equal to "HELLO"
340+
Actual: "world",
341+
which, results into "WORLD"
342+
by applying to_upper_case,
343+
isn't equal to "HELLO"
344+
"#
345+
))))
299346
)
300347
}
301348

@@ -305,9 +352,17 @@ mod tests {
305352
let result = verify_that!("world", result_of_ref!(to_upper_case, eq("HELLO")));
306353
verify_that!(
307354
result,
308-
err(displays_as(contains_substring(
309-
"where the result of applying \"world\" to the callable `to_upper_case` is \"WORLD\"\n which isn't equal to \"HELLO\""
310-
)))
355+
err(displays_as(contains_substring(indoc!(
356+
r#"
357+
Value of: "world"
358+
Expected: by applying to_upper_case,
359+
is equal to "HELLO"
360+
Actual: "world",
361+
which, results into "WORLD"
362+
by applying to_upper_case,
363+
isn't equal to "HELLO"
364+
"#
365+
))))
311366
)
312367
}
313368

@@ -316,9 +371,17 @@ mod tests {
316371
let result = verify_that!("world", result_of_ref!(str::to_uppercase, eq("HELLO")));
317372
verify_that!(
318373
result,
319-
err(displays_as(contains_substring(
320-
"where the result of applying \"world\" to the callable `str::to_uppercase` is \"WORLD\"\n which isn't equal to \"HELLO\""
321-
)))
374+
err(displays_as(contains_substring(indoc!(
375+
r#"
376+
Value of: "world"
377+
Expected: by applying str::to_uppercase,
378+
is equal to "HELLO"
379+
Actual: "world",
380+
which, results into "WORLD"
381+
by applying str::to_uppercase,
382+
isn't equal to "HELLO"
383+
"#
384+
))))
322385
)
323386
}
324387

@@ -330,8 +393,47 @@ mod tests {
330393
let result = verify_that!("world", result_of_ref!(upper_case(), eq("HELLO")));
331394
verify_that!(
332395
result,
333-
err(displays_as(contains_substring(
334-
"where the result of applying \"world\" to the callable `upper_case()` is \"WORLD\"\n which isn't equal to \"HELLO\""
396+
err(displays_as(contains_substring(indoc!(
397+
r#"
398+
Value of: "world"
399+
Expected: by applying upper_case(),
400+
is equal to "HELLO"
401+
Actual: "world",
402+
which, results into "WORLD"
403+
by applying upper_case(),
404+
isn't equal to "HELLO"
405+
"#
406+
))))
407+
)
408+
}
409+
410+
#[test]
411+
fn test_describe_simple() -> Result<()> {
412+
let matcher = result_of!(|x| x + 1, eq(2));
413+
let description = matcher.describe(matcher.matches(0));
414+
verify_that!(
415+
description,
416+
displays_as(eq(indoc!(
417+
r#"
418+
by applying |x| x + 1,
419+
isn't equal to 2"#
420+
)))
421+
)
422+
}
423+
424+
#[test]
425+
fn test_describe_complicated() -> Result<()> {
426+
let matcher = result_of_ref!(
427+
|s: &str| s.chars().collect::<Vec<_>>(),
428+
each(predicate(char::is_ascii_alphabetic))
429+
);
430+
let description = matcher.describe(matcher.matches("A quick brown fox"));
431+
verify_that!(
432+
description,
433+
displays_as(eq(indoc!(
434+
r#"
435+
by applying |s: &str| s.chars().collect::<Vec<_>>(),
436+
contains no element that matches"#
335437
)))
336438
)
337439
}

0 commit comments

Comments
 (0)