Skip to content
Myr

Test the alt format representations

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent 42d0286 · Browse files · 1 file+65 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+65 added−1 removed

  • tests:+65 added−1 removed

1 file+65 added−1 removed

modified:tests/test_fmt.rs+65 added-1 removed
1use anyhow::{bail, Context, Result};2use std::io;34fn f() -> Result<()> {4 bail!("oh no!");5 bail!(io::Error::new(io::ErrorKind::PermissionDenied, "oh no!"));6}78fn g() -> Result<()> {9 f().context("f failed")
2 unmodified lines
12fn h() -> Result<()> {13 g().context("g failed")14}1516const EXPECTED_ALTDISPLAY_F: &str = "\17oh no!\18";1920const EXPECTED_ALTDISPLAY_G: &str = "\21f failed2223Caused by:24 oh no!\25";2627const EXPECTED_ALTDISPLAY_H: &str = "\28g failed2930Caused by:31 0: f failed32 1: oh no!\33";3435const EXPECTED_DEBUG_F: &str = "\36oh no!3738Stack backtrace:
20 unmodified lines
59Stack backtrace:60 Run with RUST_LIB_BACKTRACE=1 env variable to display a backtrace61";6263const EXPECTED_ALTDEBUG_F: &str = "\64Custom {65 kind: PermissionDenied,66 error: \"oh no!\",67}\68";6970const EXPECTED_ALTDEBUG_G: &str = "\71Error {72 context: \"f failed\",73 source: Custom {74 kind: PermissionDenied,75 error: \"oh no!\",76 },77}\78";7980const EXPECTED_ALTDEBUG_H: &str = "\81Error {82 context: \"g failed\",83 source: Error {84 context: \"f failed\",85 source: Custom {86 kind: PermissionDenied,87 error: \"oh no!\",88 },89 },90}\91";9293#[test]94fn test_display() {95 assert_eq!("g failed", h().unwrap_err().to_string());96}9798#[test]99fn test_altdisplay() {100 assert_eq!(EXPECTED_ALTDISPLAY_F, format!("{:#}", f().unwrap_err()));101 assert_eq!(EXPECTED_ALTDISPLAY_G, format!("{:#}", g().unwrap_err()));102 assert_eq!(EXPECTED_ALTDISPLAY_H, format!("{:#}", h().unwrap_err()));103}104105#[test]106#[cfg_attr(not(backtrace), ignore)]107fn test_debug() {108 assert_eq!(EXPECTED_DEBUG_F, format!("{:?}", f().unwrap_err()));109 assert_eq!(EXPECTED_DEBUG_G, format!("{:?}", g().unwrap_err()));110 assert_eq!(EXPECTED_DEBUG_H, format!("{:?}", h().unwrap_err()));111}112113#[test]114fn test_altdebug() {115 assert_eq!(EXPECTED_ALTDEBUG_F, format!("{:#?}", f().unwrap_err()));116 assert_eq!(EXPECTED_ALTDEBUG_G, format!("{:#?}", g().unwrap_err()));117 assert_eq!(EXPECTED_ALTDEBUG_H, format!("{:#?}", h().unwrap_err()));118}