Skip to content
Myr

Add test of anyhow macro tagged dispatch

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 38c8d78 · Browse files · 1 file+51 added−0 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+51 added−0 removed

  • tests:+51 added−0 removed

1 file+51 added−0 removed

added:tests/test_source.rs+51 added
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());38}3940#[test]41fn test_fmt_source() {42 let error = anyhow!("{} {}!", "oh", "no");43 assert!(error.source().is_none());44}4546#[test]47fn test_io_source() {48 let io = io::Error::new(io::ErrorKind::Other, "oh no!");49 let error = anyhow!(TestError::Io(io));50 assert_eq!("oh no!", error.source().unwrap().to_string());51}