| 1 | extern crate yaml; |
| 2 | |
| 3 | use yaml::error::YamlError; |
| 4 | use yaml::emitter::YamlEmitter; |
| 5 | use yaml::ffi::{YamlEncoding, YamlScalarStyle}; |
| 6 | |
| 7 | use std::error::Error; |
| 8 | use std::io; |
| 9 | use std::io::{Read, Write}; |
| 10 | |
| 11 | struct MockRW { |
| 12 | _data: () |
| 13 | } |
| 14 | |
| 15 | impl MockRW { |
| 16 | pub fn new() -> MockRW { |
| 17 | MockRW { _data: () } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl Read for MockRW { |
| 22 | fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { |
| 23 | Err(io::Error::new(io::ErrorKind::Other, "")) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl Write for MockRW { |
| 28 | fn write(&mut self, _buf: &[u8]) -> io::Result<usize> { |
| 29 | Err(io::Error::new(io::ErrorKind::Other, "")) |
| 30 | } |
| 31 | |
| 32 | fn flush(&mut self) -> io::Result<()> { |
| 33 | Err(io::Error::new(io::ErrorKind::Other, "")) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn error_cause_test_read() { |
| 39 | let mut mock_reader = MockRW::new(); |
| 40 | match yaml::parse_io_utf8(&mut mock_reader) { |
| 41 | Ok(_) => panic!("Should return an error"), |
| 42 | Err(e) => assert_eq!(e.cause().map(|ioe| format!("{}", ioe)), Some("".to_string())) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn write_to_bad_stream() -> Result<(), YamlError> { |
| 47 | let mut mock_writer = MockRW::new(); |
| 48 | let mut emitter = YamlEmitter::init(&mut mock_writer); |
| 49 | try!(emitter.emit_stream(YamlEncoding::YamlUtf8Encoding, |stream| |
| 50 | stream.emit_document(None, &[], true, |doc| { |
| 51 | doc.emit_scalar_event(None, None, "a", true, false, YamlScalarStyle::YamlPlainScalarStyle) |
| 52 | }) |
| 53 | )); |
| 54 | emitter.flush() |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn error_cause_test_write() { |
| 59 | match write_to_bad_stream() { |
| 60 | Ok(_) => panic!("Should return an error"), |
| 61 | Err(e) => assert_eq!(e.cause().map(|ioe| format!("{}", ioe)), Some("".to_string())) |
| 62 | } |
| 63 | } |