]> git.proxmox.com Git - rustc.git/blob - vendor/anyhow/tests/test_fmt.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / vendor / anyhow / tests / test_fmt.rs
1 use anyhow::{bail, Context, Result};
2 use std::io;
3
4 fn f() -> Result<()> {
5 bail!(io::Error::new(io::ErrorKind::PermissionDenied, "oh no!"));
6 }
7
8 fn g() -> Result<()> {
9 f().context("f failed")
10 }
11
12 fn h() -> Result<()> {
13 g().context("g failed")
14 }
15
16 const EXPECTED_ALTDISPLAY_F: &str = "oh no!";
17
18 const EXPECTED_ALTDISPLAY_G: &str = "f failed: oh no!";
19
20 const EXPECTED_ALTDISPLAY_H: &str = "g failed: f failed: oh no!";
21
22 const EXPECTED_DEBUG_F: &str = "oh no!";
23
24 const EXPECTED_DEBUG_G: &str = "\
25 f failed
26
27 Caused by:
28 oh no!\
29 ";
30
31 const EXPECTED_DEBUG_H: &str = "\
32 g failed
33
34 Caused by:
35 0: f failed
36 1: oh no!\
37 ";
38
39 const EXPECTED_ALTDEBUG_F: &str = "\
40 Custom {
41 kind: PermissionDenied,
42 error: \"oh no!\",
43 }\
44 ";
45
46 const EXPECTED_ALTDEBUG_G: &str = "\
47 Error {
48 context: \"f failed\",
49 source: Custom {
50 kind: PermissionDenied,
51 error: \"oh no!\",
52 },
53 }\
54 ";
55
56 const EXPECTED_ALTDEBUG_H: &str = "\
57 Error {
58 context: \"g failed\",
59 source: Error {
60 context: \"f failed\",
61 source: Custom {
62 kind: PermissionDenied,
63 error: \"oh no!\",
64 },
65 },
66 }\
67 ";
68
69 #[test]
70 fn test_display() {
71 assert_eq!("g failed", h().unwrap_err().to_string());
72 }
73
74 #[test]
75 fn test_altdisplay() {
76 assert_eq!(EXPECTED_ALTDISPLAY_F, format!("{:#}", f().unwrap_err()));
77 assert_eq!(EXPECTED_ALTDISPLAY_G, format!("{:#}", g().unwrap_err()));
78 assert_eq!(EXPECTED_ALTDISPLAY_H, format!("{:#}", h().unwrap_err()));
79 }
80
81 #[test]
82 #[cfg_attr(not(backtrace), ignore)]
83 fn test_debug() {
84 assert_eq!(EXPECTED_DEBUG_F, format!("{:?}", f().unwrap_err()));
85 assert_eq!(EXPECTED_DEBUG_G, format!("{:?}", g().unwrap_err()));
86 assert_eq!(EXPECTED_DEBUG_H, format!("{:?}", h().unwrap_err()));
87 }
88
89 #[test]
90 fn test_altdebug() {
91 assert_eq!(EXPECTED_ALTDEBUG_F, format!("{:#?}", f().unwrap_err()));
92 assert_eq!(EXPECTED_ALTDEBUG_G, format!("{:#?}", g().unwrap_err()));
93 assert_eq!(EXPECTED_ALTDEBUG_H, format!("{:#?}", h().unwrap_err()));
94 }