From bebcfab52c00b3fa2d25b413129ee2f28b4fd39f Mon Sep 17 00:00:00 2001 From: Matt McCoy Date: Tue, 21 Jun 2016 22:02:24 -0400 Subject: [PATCH] Adding test for readline failing on err --- src/client.rs | 9 +++++++++ src/mock_stream.rs | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index e8fc85d..0367536 100644 --- a/src/client.rs +++ b/src/client.rs @@ -338,6 +338,15 @@ mod tests { client.read_greeting().unwrap(); } + #[test] + #[should_panic] + fn readline_err() { + // TODO Check the error test + let mock_stream = MockStream::new_err(); + let mut client = create_client_with_mock_stream(mock_stream); + client.readline().unwrap(); + } + #[test] fn create_command() { let base_command = "CHECK"; diff --git a/src/mock_stream.rs b/src/mock_stream.rs index da2af04..94c28e2 100644 --- a/src/mock_stream.rs +++ b/src/mock_stream.rs @@ -3,7 +3,8 @@ use std::io::{Read, Result, Write, Error, ErrorKind}; pub struct MockStream { read_buf: Vec, read_pos: usize, - pub written_buf: Vec + pub written_buf: Vec, + err_on_read: bool } impl MockStream { @@ -11,13 +12,26 @@ impl MockStream { MockStream{ read_buf: read_buf, read_pos: 0, - written_buf: Vec::new() + written_buf: Vec::new(), + err_on_read: false + } + } + + pub fn new_err() -> MockStream { + MockStream{ + read_buf: Vec::new(), + read_pos: 0, + written_buf: Vec::new(), + err_on_read: true } } } impl Read for MockStream { fn read(&mut self, buf: &mut[u8]) -> Result { + if self.err_on_read { + return Err(Error::new(ErrorKind::Other, "MockStream Error")) + } if self.read_pos >= self.read_buf.len() { return Err(Error::new(ErrorKind::UnexpectedEof, "EOF")) }