Skip to content

Commit

Permalink
[tests] Add more read_exact and write_all to BadFile #104 (#105)
Browse files Browse the repository at this point in the history
* Update bad_file.rs

* Update bad_file.rs
  • Loading branch information
michaelvlach authored Aug 27, 2022
1 parent ca1ed3d commit 439677b
Showing 1 changed file with 83 additions and 39 deletions.
122 changes: 83 additions & 39 deletions src/test_utilities/bad_file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
struct BadFile {
read_result: std::io::Result<usize>,
seek_result: std::io::Result<u64>,
write_result: std::io::Result<usize>,
flush_result: std::io::Result<()>,
pub(crate) struct BadFile {
pub(crate) read_result: std::io::Result<usize>,
pub(crate) read_exact_result: std::io::Result<()>,
pub(crate) seek_result: std::io::Result<u64>,
pub(crate) write_result: std::io::Result<usize>,
pub(crate) write_all_result: std::io::Result<()>,
pub(crate) flush_result: std::io::Result<()>,
}

impl std::io::Read for BadFile {
Expand All @@ -12,6 +14,13 @@ impl std::io::Read for BadFile {
Err(e) => Err(std::io::Error::from(e.kind())),
}
}

fn read_exact(&mut self, _buf: &mut [u8]) -> std::io::Result<()> {
match &self.read_exact_result {
Ok(_) => Ok(()),
Err(e) => Err(std::io::Error::from(e.kind())),
}
}
}

impl std::io::Seek for BadFile {
Expand All @@ -31,6 +40,13 @@ impl std::io::Write for BadFile {
}
}

fn write_all(&mut self, mut _buf: &[u8]) -> std::io::Result<()> {
match &self.write_all_result {
Ok(_) => Ok(()),
Err(e) => Err(std::io::Error::from(e.kind())),
}
}

fn flush(&mut self) -> std::io::Result<()> {
match &self.flush_result {
Ok(_) => Ok(()),
Expand All @@ -39,6 +55,19 @@ impl std::io::Write for BadFile {
}
}

impl Default for BadFile {
fn default() -> Self {
Self {
read_result: Ok(0),
read_exact_result: Ok(()),
seek_result: Ok(0),
write_result: Ok(0),
write_all_result: Ok(()),
flush_result: Ok(()),
}
}
}

#[cfg(test)]
mod tests {
use std::io::Read;
Expand All @@ -47,39 +76,51 @@ mod tests {

use super::*;

#[test]
fn default_constructed() {
let _file = BadFile::default();
}

#[test]
fn flush_ok() {
let mut file = BadFile {
read_result: Ok(0),
seek_result: Ok(0),
write_result: Ok(0),
flush_result: Ok(()),
};
let mut file = BadFile::default();

assert!(file.flush().is_ok());
}

#[test]
fn flush_err() {
let mut file = BadFile {
read_result: Ok(0),
seek_result: Ok(0),
write_result: Ok(0),
flush_result: Err(std::io::Error::from(std::io::ErrorKind::Other)),
..Default::default()
};

assert!(file.flush().is_err());
}

#[test]
fn read_ok() {
fn read_exact_ok() {
let mut file = BadFile::default();

let mut buf = vec![0_u8; 0];
assert!(file.read_exact(&mut buf).is_ok());
}

#[test]
fn read_exact_err() {
let mut file = BadFile {
read_result: Ok(0),
seek_result: Ok(0),
write_result: Ok(0),
flush_result: Ok(()),
read_exact_result: Err(std::io::Error::from(std::io::ErrorKind::Other)),
..Default::default()
};

let mut buf = vec![0_u8; 0];
assert!(file.read_exact(&mut buf).is_err());
}

#[test]
fn read_ok() {
let mut file = BadFile::default();

let mut buf = vec![0_u8; 0];
assert!(file.read(&mut buf).is_ok());
}
Expand All @@ -88,9 +129,7 @@ mod tests {
fn read_err() {
let mut file = BadFile {
read_result: Err(std::io::Error::from(std::io::ErrorKind::Other)),
seek_result: Ok(0),
write_result: Ok(0),
flush_result: Ok(()),
..Default::default()
};

let mut buf = vec![0_u8; 0];
Expand All @@ -99,48 +138,53 @@ mod tests {

#[test]
fn seek_ok() {
let mut file = BadFile {
read_result: Ok(0),
seek_result: Ok(0),
write_result: Ok(0),
flush_result: Ok(()),
};
let mut file = BadFile::default();

assert!(file.seek(std::io::SeekFrom::Current(0)).is_ok());
}

#[test]
fn seek_err() {
let mut file = BadFile {
read_result: Ok(0),
seek_result: Err(std::io::Error::from(std::io::ErrorKind::Other)),
write_result: Ok(0),
flush_result: Ok(()),
..Default::default()
};

assert!(file.seek(std::io::SeekFrom::Current(0)).is_err());
}

#[test]
fn write_ok() {
fn write_all_ok() {
let mut file = BadFile::default();

let buf = vec![0_u8; 0];
assert!(file.write_all(&buf).is_ok());
}

#[test]
fn write_all_err() {
let mut file = BadFile {
read_result: Ok(0),
seek_result: Ok(0),
write_result: Ok(0),
flush_result: Ok(()),
write_all_result: Err(std::io::Error::from(std::io::ErrorKind::Other)),
..Default::default()
};

let buf = vec![0_u8; 0];
assert!(file.write_all(&buf).is_err());
}

#[test]
fn write_ok() {
let mut file = BadFile::default();

let buf = vec![0_u8; 0];
assert!(file.write(&buf).is_ok());
}

#[test]
fn write_err() {
let mut file = BadFile {
read_result: Ok(0),
seek_result: Ok(0),
write_result: Err(std::io::Error::from(std::io::ErrorKind::Other)),
flush_result: Ok(()),
..Default::default()
};

let buf = vec![0_u8; 0];
Expand Down

0 comments on commit 439677b

Please sign in to comment.