diff options
| author | Lzu Tao <taolzu@gmail.com> | 2020-08-27 13:45:01 +0000 |
|---|---|---|
| committer | Lzu Tao <taolzu@gmail.com> | 2020-08-31 02:56:59 +0000 |
| commit | a4e926daeeaedc9178846711daf1f4cb6ce505fb (patch) | |
| tree | 0c830f716f6f5ad17736d459f5de9b9199006d54 /library/std/src/error | |
| parent | db6cbfc49ca655739ba8caae43ebd7c77c8a1179 (diff) | |
| download | rust-a4e926daeeaedc9178846711daf1f4cb6ce505fb.tar.gz rust-a4e926daeeaedc9178846711daf1f4cb6ce505fb.zip | |
std: move "mod tests/benches" to separate files
Also doing fmt inplace as requested.
Diffstat (limited to 'library/std/src/error')
| -rw-r--r-- | library/std/src/error/tests.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/library/std/src/error/tests.rs b/library/std/src/error/tests.rs new file mode 100644 index 00000000000..66d6924f34d --- /dev/null +++ b/library/std/src/error/tests.rs @@ -0,0 +1,37 @@ +use super::Error; +use crate::fmt; + +#[derive(Debug, PartialEq)] +struct A; +#[derive(Debug, PartialEq)] +struct B; + +impl fmt::Display for A { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "A") + } +} +impl fmt::Display for B { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "B") + } +} + +impl Error for A {} +impl Error for B {} + +#[test] +fn downcasting() { + let mut a = A; + let a = &mut a as &mut (dyn Error + 'static); + assert_eq!(a.downcast_ref::<A>(), Some(&A)); + assert_eq!(a.downcast_ref::<B>(), None); + assert_eq!(a.downcast_mut::<A>(), Some(&mut A)); + assert_eq!(a.downcast_mut::<B>(), None); + + let a: Box<dyn Error> = Box::new(A); + match a.downcast::<B>() { + Ok(..) => panic!("expected error"), + Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A), + } +} |
