diff options
Diffstat (limited to 'library/std/src/io/error/tests.rs')
| -rw-r--r-- | library/std/src/io/error/tests.rs | 82 |
1 files changed, 78 insertions, 4 deletions
diff --git a/library/std/src/io/error/tests.rs b/library/std/src/io/error/tests.rs index 5098a46313d..c2c51553b20 100644 --- a/library/std/src/io/error/tests.rs +++ b/library/std/src/io/error/tests.rs @@ -1,4 +1,5 @@ -use super::{Custom, Error, ErrorKind, Repr}; +use super::{const_io_error, Custom, Error, ErrorData, ErrorKind, Repr}; +use crate::assert_matches::assert_matches; use crate::error; use crate::fmt; use crate::mem::size_of; @@ -16,9 +17,9 @@ fn test_debug_error() { let msg = error_string(code); let kind = decode_error_kind(code); let err = Error { - repr: Repr::Custom(box Custom { + repr: Repr::new_custom(box Custom { kind: ErrorKind::InvalidInput, - error: box Error { repr: super::Repr::Os(code) }, + error: box Error { repr: super::Repr::new_os(code) }, }), }; let expected = format!( @@ -60,10 +61,83 @@ fn test_downcasting() { #[test] fn test_const() { - const E: Error = Error::new_const(ErrorKind::NotFound, &"hello"); + const E: Error = const_io_error!(ErrorKind::NotFound, "hello"); assert_eq!(E.kind(), ErrorKind::NotFound); assert_eq!(E.to_string(), "hello"); assert!(format!("{:?}", E).contains("\"hello\"")); assert!(format!("{:?}", E).contains("NotFound")); } + +#[test] +fn test_os_packing() { + for code in -20i32..20i32 { + let e = Error::from_raw_os_error(code); + assert_eq!(e.raw_os_error(), Some(code)); + assert_matches!( + e.repr.data(), + ErrorData::Os(c) if c == code, + ); + } +} + +#[test] +fn test_errorkind_packing() { + assert_eq!(Error::from(ErrorKind::NotFound).kind(), ErrorKind::NotFound); + assert_eq!(Error::from(ErrorKind::PermissionDenied).kind(), ErrorKind::PermissionDenied); + assert_eq!(Error::from(ErrorKind::Uncategorized).kind(), ErrorKind::Uncategorized); + // Check that the innards look like like what we want. + assert_matches!( + Error::from(ErrorKind::OutOfMemory).repr.data(), + ErrorData::Simple(ErrorKind::OutOfMemory), + ); +} + +#[test] +fn test_simple_message_packing() { + use super::{ErrorKind::*, SimpleMessage}; + macro_rules! check_simple_msg { + ($err:expr, $kind:ident, $msg:literal) => {{ + let e = &$err; + // Check that the public api is right. + assert_eq!(e.kind(), $kind); + assert!(format!("{:?}", e).contains($msg)); + // and we got what we expected + assert_matches!( + e.repr.data(), + ErrorData::SimpleMessage(SimpleMessage { kind: $kind, message: $msg }) + ); + }}; + } + + let not_static = const_io_error!(Uncategorized, "not a constant!"); + check_simple_msg!(not_static, Uncategorized, "not a constant!"); + + const CONST: Error = const_io_error!(NotFound, "definitely a constant!"); + check_simple_msg!(CONST, NotFound, "definitely a constant!"); + + static STATIC: Error = const_io_error!(BrokenPipe, "a constant, sort of!"); + check_simple_msg!(STATIC, BrokenPipe, "a constant, sort of!"); +} + +#[derive(Debug, PartialEq)] +struct Bojji(bool); +impl error::Error for Bojji {} +impl fmt::Display for Bojji { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "ah! {:?}", self) + } +} + +#[test] +fn test_custom_error_packing() { + use super::Custom; + let test = Error::new(ErrorKind::Uncategorized, Bojji(true)); + assert_matches!( + test.repr.data(), + ErrorData::Custom(Custom { + kind: ErrorKind::Uncategorized, + error, + }) if error.downcast_ref::<Bojji>().as_deref() == Some(&Bojji(true)), + ); +} |
