Debian Codegraph @

tests/test_macros.rs

raw symbols SCIP
1 #![allow(
2 clippy::assertions_on_result_states,
3 clippy::eq_op,
4 clippy::incompatible_msrv, // https://github.com/rust-lang/rust-clippy/issues/12257
5 clippy::items_after_statements,
6 clippy::match_single_binding,
7 clippy::needless_pass_by_value,
8 clippy::shadow_unrelated,
9 clippy::uninlined_format_args,
10 clippy::wildcard_imports
11 )]
12
13 mod common;
14
15 use self::common::*;
16 use anyhow::{anyhow, ensure, Result};
17 use std::cell::Cell;
18 use std::future;
19
20 #[test]
21 fn test_messages() {
22 assert_eq!("oh no!", bail_literal().unwrap_err().to_string());
23 assert_eq!("oh no!", bail_fmt().unwrap_err().to_string());
24 assert_eq!("oh no!", bail_error().unwrap_err().to_string());
25 }
26
27 #[test]
28 fn test_ensure() {
29 let f = || {
30 ensure!(1 + 1 == 2, "This is correct");
31 Ok(())
32 };
33 assert!(f().is_ok());
34
35 let v = 1;
36 let f = || {
37 ensure!(v + v == 2, "This is correct, v: {}", v);
38 Ok(())
39 };
40 assert!(f().is_ok());
41
42 let f = || {
43 ensure!(v + v == 1, "This is not correct, v: {}", v);
44 Ok(())
45 };
46 assert!(f().is_err());
47
48 let f = || {
49 ensure!(v + v == 1);
50 Ok(())
51 };
52 assert_eq!(
53 f().unwrap_err().to_string(),
54 "Condition failed: `v + v == 1` (2 vs 1)",
55 );
56 }
57
58 #[test]
59 fn test_ensure_nonbool() -> Result<()> {
60 struct Struct {
61 condition: bool,
62 }
63
64 let s = Struct { condition: true };
65 match &s {
66 Struct { condition } => ensure!(condition), // &bool
67 }
68
69 Ok(())
70 }
71
72 #[test]
73 fn test_temporaries() {
74 fn require_send_sync(_: impl Send + Sync) {}
75
76 require_send_sync(async {
77 // If anyhow hasn't dropped any temporary format_args it creates by the
78 // time it's done evaluating, those will stick around until the
79 // semicolon, which is on the other side of the await point, making the
80 // enclosing future non-Send.
81 future::ready(anyhow!("...")).await;
82 });
83
84 fn message(cell: Cell<&str>) -> &str {
85 cell.get()
86 }
87
88 require_send_sync(async {
89 future::ready(anyhow!(message(Cell::new("...")))).await;
90 });
91 }
92
93 #[test]
94 fn test_brace_escape() {
95 let err = anyhow!("unterminated ${{..}} expression");
96 assert_eq!("unterminated ${..} expression", err.to_string());
97 }