]>
Commit | Line | Data |
---|---|---|
1b1a35ee XL |
1 | use super::{Custom, Error, ErrorKind, Repr}; |
2 | use crate::error; | |
3 | use crate::fmt; | |
cdc7bbd5 | 4 | use crate::mem::size_of; |
1b1a35ee XL |
5 | use crate::sys::decode_error_kind; |
6 | use crate::sys::os::error_string; | |
7 | ||
cdc7bbd5 XL |
8 | #[test] |
9 | fn test_size() { | |
10 | assert!(size_of::<Error>() <= size_of::<[usize; 2]>()); | |
11 | } | |
12 | ||
1b1a35ee XL |
13 | #[test] |
14 | fn test_debug_error() { | |
15 | let code = 6; | |
16 | let msg = error_string(code); | |
17 | let kind = decode_error_kind(code); | |
18 | let err = Error { | |
19 | repr: Repr::Custom(box Custom { | |
20 | kind: ErrorKind::InvalidInput, | |
21 | error: box Error { repr: super::Repr::Os(code) }, | |
22 | }), | |
23 | }; | |
24 | let expected = format!( | |
25 | "Custom {{ \ | |
26 | kind: InvalidInput, \ | |
27 | error: Os {{ \ | |
28 | code: {:?}, \ | |
29 | kind: {:?}, \ | |
30 | message: {:?} \ | |
31 | }} \ | |
32 | }}", | |
33 | code, kind, msg | |
34 | ); | |
35 | assert_eq!(format!("{:?}", err), expected); | |
36 | } | |
37 | ||
38 | #[test] | |
39 | fn test_downcasting() { | |
40 | #[derive(Debug)] | |
41 | struct TestError; | |
42 | ||
43 | impl fmt::Display for TestError { | |
44 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
45 | f.write_str("asdf") | |
46 | } | |
47 | } | |
48 | ||
49 | impl error::Error for TestError {} | |
50 | ||
51 | // we have to call all of these UFCS style right now since method | |
52 | // resolution won't implicitly drop the Send+Sync bounds | |
53 | let mut err = Error::new(ErrorKind::Other, TestError); | |
54 | assert!(err.get_ref().unwrap().is::<TestError>()); | |
55 | assert_eq!("asdf", err.get_ref().unwrap().to_string()); | |
56 | assert!(err.get_mut().unwrap().is::<TestError>()); | |
57 | let extracted = err.into_inner().unwrap(); | |
58 | extracted.downcast::<TestError>().unwrap(); | |
59 | } | |
cdc7bbd5 XL |
60 | |
61 | #[test] | |
62 | fn test_const() { | |
63 | const E: Error = Error::new_const(ErrorKind::NotFound, &"hello"); | |
64 | ||
65 | assert_eq!(E.kind(), ErrorKind::NotFound); | |
66 | assert_eq!(E.to_string(), "hello"); | |
67 | assert!(format!("{:?}", E).contains("\"hello\"")); | |
68 | assert!(format!("{:?}", E).contains("NotFound")); | |
69 | } |