]> git.lizzy.rs Git - rust.git/blob - tests/ui/write-fmt-errors.rs
add tests for 107090
[rust.git] / tests / ui / write-fmt-errors.rs
1 // run-pass
2
3 #![feature(io_error_uncategorized)]
4
5 use std::fmt;
6 use std::io::{self, Error, Write, sink};
7
8 struct ErrorDisplay;
9
10 impl fmt::Display for ErrorDisplay {
11     fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
12         Err(fmt::Error)
13     }
14 }
15
16 struct ErrorWriter;
17
18 const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Uncategorized;
19 const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
20
21 impl Write for ErrorWriter {
22     fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
23         Err(Error::new(WRITER_ERROR, "not connected"))
24     }
25
26     fn flush(&mut self) -> io::Result<()> { Ok(()) }
27 }
28
29 fn main() {
30     // Test that the error from the formatter is propagated.
31     let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar");
32     assert!(res.is_err(), "formatter error did not propagate");
33     assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
34
35     // Test that an underlying error is propagated
36     let res = write!(ErrorWriter, "abc");
37     assert!(res.is_err(), "writer error did not propagate");
38
39     // Writer error
40     let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
41     assert!(res.is_err(), "writer error did not propagate");
42     assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
43
44     // Formatter error
45     let res = write!(ErrorWriter, "{} abc", ErrorDisplay);
46     assert!(res.is_err(), "formatter error did not propagate");
47     assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
48 }