Skip to content
Myr

test_boxed.rs

This file

Path

tests/test_boxed.rs

45 lines · 1.0 KiB · Rust

Last changed by

Not known.

History of this file

Take it

1#![allow(2    // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/74223    clippy::nonstandard_macro_braces,4)]56use anyhow::anyhow;7use std::error::Error as StdError;8use std::io;9use thiserror::Error;1011#[derive(Error, Debug)]12#[error("outer")]13struct MyError {14    source: io::Error,15}1617#[test]18fn test_boxed_str() {19    let error = Box::<dyn StdError + Send + Sync>::from("oh no!");20    let error = anyhow!(error);21    assert_eq!("oh no!", error.to_string());22    assert_eq!(23        "oh no!",24        error25            .downcast_ref::<Box<dyn StdError + Send + Sync>>()26            .unwrap()27            .to_string()28    );29}3031#[test]32fn test_boxed_thiserror() {33    let error = MyError {34        source: io::Error::new(io::ErrorKind::Other, "oh no!"),35    };36    let error = anyhow!(error);37    assert_eq!("oh no!", error.source().unwrap().to_string());38}3940#[test]41fn test_boxed_anyhow() {42    let error = anyhow!("oh no!").context("it failed");43    let error = anyhow!(error);44    assert_eq!("oh no!", error.source().unwrap().to_string());45}