]> git.proxmox.com Git - rustc.git/blob - library/std/src/io/error/tests.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / io / error / tests.rs
1 use super::{Custom, Error, ErrorKind, Repr};
2 use crate::error;
3 use crate::fmt;
4 use crate::sys::decode_error_kind;
5 use crate::sys::os::error_string;
6
7 #[test]
8 fn test_debug_error() {
9 let code = 6;
10 let msg = error_string(code);
11 let kind = decode_error_kind(code);
12 let err = Error {
13 repr: Repr::Custom(box Custom {
14 kind: ErrorKind::InvalidInput,
15 error: box Error { repr: super::Repr::Os(code) },
16 }),
17 };
18 let expected = format!(
19 "Custom {{ \
20 kind: InvalidInput, \
21 error: Os {{ \
22 code: {:?}, \
23 kind: {:?}, \
24 message: {:?} \
25 }} \
26 }}",
27 code, kind, msg
28 );
29 assert_eq!(format!("{:?}", err), expected);
30 }
31
32 #[test]
33 fn test_downcasting() {
34 #[derive(Debug)]
35 struct TestError;
36
37 impl fmt::Display for TestError {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.write_str("asdf")
40 }
41 }
42
43 impl error::Error for TestError {}
44
45 // we have to call all of these UFCS style right now since method
46 // resolution won't implicitly drop the Send+Sync bounds
47 let mut err = Error::new(ErrorKind::Other, TestError);
48 assert!(err.get_ref().unwrap().is::<TestError>());
49 assert_eq!("asdf", err.get_ref().unwrap().to_string());
50 assert!(err.get_mut().unwrap().is::<TestError>());
51 let extracted = err.into_inner().unwrap();
52 extracted.downcast::<TestError>().unwrap();
53 }