Skip to content
Myr

test_fmt.rs

This file

Path

tests/test_fmt.rs

93 lines · 1.9 KiB · Rust

Last changed by

Merge pull request #377 from dtolnay/debugtestDavid Tolnaybefore adoptiona3f7436

History of this file

Take it

1use anyhow::{bail, Context, Result};2use std::io;34fn f() -> Result<()> {5    bail!(io::Error::new(io::ErrorKind::PermissionDenied, "oh no!"));6}78fn g() -> Result<()> {9    f().context("f failed")10}1112fn h() -> Result<()> {13    g().context("g failed")14}1516const EXPECTED_ALTDISPLAY_F: &str = "oh no!";1718const EXPECTED_ALTDISPLAY_G: &str = "f failed: oh no!";1920const EXPECTED_ALTDISPLAY_H: &str = "g failed: f failed: oh no!";2122const EXPECTED_DEBUG_F: &str = "oh no!";2324const EXPECTED_DEBUG_G: &str = "\25f failed2627Caused by:28    oh no!\29";3031const EXPECTED_DEBUG_H: &str = "\32g failed3334Caused by:35    0: f failed36    1: oh no!\37";3839const EXPECTED_ALTDEBUG_F: &str = "\40Custom {41    kind: PermissionDenied,42    error: \"oh no!\",43}\44";4546const EXPECTED_ALTDEBUG_G: &str = "\47Error {48    context: \"f failed\",49    source: Custom {50        kind: PermissionDenied,51        error: \"oh no!\",52    },53}\54";5556const EXPECTED_ALTDEBUG_H: &str = "\57Error {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";6869#[test]70fn test_display() {71    assert_eq!("g failed", h().unwrap_err().to_string());72}7374#[test]75fn 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}8081#[test]82fn test_debug() {83    assert_eq!(EXPECTED_DEBUG_F, format!("{:?}", f().unwrap_err()));84    assert_eq!(EXPECTED_DEBUG_G, format!("{:?}", g().unwrap_err()));85    assert_eq!(EXPECTED_DEBUG_H, format!("{:?}", h().unwrap_err()));86}8788#[test]89fn test_altdebug() {90    assert_eq!(EXPECTED_ALTDEBUG_F, format!("{:#?}", f().unwrap_err()));91    assert_eq!(EXPECTED_ALTDEBUG_G, format!("{:#?}", g().unwrap_err()));92    assert_eq!(EXPECTED_ALTDEBUG_H, format!("{:#?}", h().unwrap_err()));93}