Skip to content
Myr

test_source.rs

This file

Path

tests/test_source.rs

62 lines · 1.3 KiB · Rust

Last changed by

Not known.

History of this file

Take it

1use anyhow::anyhow;2use std::error::Error as StdError;3use std::fmt::{self, Display};4use std::io;56#[derive(Debug)]7enum TestError {8    Io(io::Error),9}1011impl Display for TestError {12    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {13        match self {14            TestError::Io(e) => Display::fmt(e, formatter),15        }16    }17}1819impl StdError for TestError {20    fn source(&self) -> Option<&(dyn StdError + 'static)> {21        match self {22            TestError::Io(io) => Some(io),23        }24    }25}2627#[test]28fn test_literal_source() {29    let error = anyhow!("oh no!");30    assert!(error.source().is_none());31}3233#[test]34fn test_variable_source() {35    let msg = "oh no!";36    let error = anyhow!(msg);37    assert!(error.source().is_none());3839    let msg = msg.to_owned();40    let error = anyhow!(msg);41    assert!(error.source().is_none());42}4344#[test]45fn test_fmt_source() {46    let error = anyhow!("{} {}!", "oh", "no");47    assert!(error.source().is_none());48}4950#[test]51fn test_io_source() {52    let io = io::Error::new(io::ErrorKind::Other, "oh no!");53    let error = anyhow!(TestError::Io(io));54    assert_eq!("oh no!", error.source().unwrap().to_string());55}5657#[test]58fn test_anyhow_from_anyhow() {59    let error = anyhow!("oh no!").context("context");60    let error = anyhow!(error);61    assert_eq!("oh no!", error.source().unwrap().to_string());62}