Skip to content
Myr

test_downcast.rs

This file

Path

tests/test_downcast.rs

123 lines · 3.0 KiB · Rust

Last changed by

Not known.

History of this file

Take it

1#![allow(clippy::assertions_on_result_states, clippy::wildcard_imports)]23mod common;4mod drop;56use self::common::*;7use self::drop::{DetectDrop, Flag};8use anyhow::Error;9use std::error::Error as StdError;10use std::fmt::{self, Display};11use std::io;1213#[test]14fn test_downcast() {15    assert_eq!(16        "oh no!",17        bail_literal().unwrap_err().downcast::<&str>().unwrap(),18    );19    assert_eq!(20        "oh no!",21        bail_fmt().unwrap_err().downcast::<String>().unwrap(),22    );23    assert_eq!(24        "oh no!",25        bail_error()26            .unwrap_err()27            .downcast::<io::Error>()28            .unwrap()29            .to_string(),30    );31}3233#[test]34fn test_downcast_ref() {35    assert_eq!(36        "oh no!",37        *bail_literal().unwrap_err().downcast_ref::<&str>().unwrap(),38    );39    assert_eq!(40        "oh no!",41        bail_fmt().unwrap_err().downcast_ref::<String>().unwrap(),42    );43    assert_eq!(44        "oh no!",45        bail_error()46            .unwrap_err()47            .downcast_ref::<io::Error>()48            .unwrap()49            .to_string(),50    );51}5253#[test]54fn test_downcast_mut() {55    assert_eq!(56        "oh no!",57        *bail_literal().unwrap_err().downcast_mut::<&str>().unwrap(),58    );59    assert_eq!(60        "oh no!",61        bail_fmt().unwrap_err().downcast_mut::<String>().unwrap(),62    );63    assert_eq!(64        "oh no!",65        bail_error()66            .unwrap_err()67            .downcast_mut::<io::Error>()68            .unwrap()69            .to_string(),70    );7172    let mut bailed = bail_fmt().unwrap_err();73    *bailed.downcast_mut::<String>().unwrap() = "clobber".to_string();74    assert_eq!(bailed.downcast_ref::<String>().unwrap(), "clobber");75    assert_eq!(bailed.downcast_mut::<String>().unwrap(), "clobber");76    assert_eq!(bailed.downcast::<String>().unwrap(), "clobber");77}7879#[test]80fn test_drop() {81    let has_dropped = Flag::new();82    let error = Error::new(DetectDrop::new(&has_dropped));83    drop(error.downcast::<DetectDrop>().unwrap());84    assert!(has_dropped.get());85}8687#[test]88fn test_as_ref() {89    let error = bail_error().unwrap_err();90    let ref_dyn: &dyn StdError = error.as_ref();91    assert_eq!("oh no!", ref_dyn.to_string());92    let ref_dyn_send_sync: &(dyn StdError + Send + Sync) = error.as_ref();93    assert_eq!("oh no!", ref_dyn_send_sync.to_string());94}9596#[test]97fn test_large_alignment() {98    #[repr(align(64))]99    #[derive(Debug)]100    struct LargeAlignedError(&'static str);101102    impl Display for LargeAlignedError {103        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {104            f.write_str(self.0)105        }106    }107108    impl StdError for LargeAlignedError {}109110    let error = Error::new(LargeAlignedError("oh no!"));111    assert_eq!(112        "oh no!",113        error.downcast_ref::<LargeAlignedError>().unwrap().0114    );115}116117#[test]118fn test_unsuccessful_downcast() {119    let mut error = bail_error().unwrap_err();120    assert!(error.downcast_ref::<&str>().is_none());121    assert!(error.downcast_mut::<&str>().is_none());122    assert!(error.downcast::<&str>().is_err());123}