diff options
author | Ryan Geary <rtgnj42@gmail.com> | 2021-07-21 16:30:37 -0400 |
---|---|---|
committer | Ryan Geary <rtgnj42@gmail.com> | 2021-07-23 16:27:02 -0400 |
commit | 1970d0cc74bf25afdb71f433b0392208098f4607 (patch) | |
tree | 6b167f58430a3feacab6eb86ce10402b76350367 /src/choice/test/mod.rs | |
parent | 03e5ca4c1d235e79f497fe91af2b33c0be013781 (diff) |
Improve error handling38-unwrap-on-empty-choice
Create choose::Error and choose::Result
Improve error propogation
Improve error printing
Add tests for negative indices
Add get_negative_start_end tests
Fixup negative choice indexing
Remove most uses of unwrap
Fixes #38
Diffstat (limited to 'src/choice/test/mod.rs')
-rw-r--r-- | src/choice/test/mod.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/choice/test/mod.rs b/src/choice/test/mod.rs new file mode 100644 index 0000000..762804c --- /dev/null +++ b/src/choice/test/mod.rs @@ -0,0 +1,55 @@ +use crate::config::Config; +use crate::opt::Opt; +use std::ffi::OsString; +use std::io::{self, BufWriter, Write}; +use structopt::StructOpt; + +mod get_negative_start_end; +mod is_reverse_range; +mod print_choice; + +impl Config { + pub fn from_iter<I>(iter: I) -> Self + where + I: IntoIterator, + I::Item: Into<OsString> + Clone, + { + Config::new(Opt::from_iter(iter)) + } +} + +struct MockStdout { + pub buffer: String, +} + +impl MockStdout { + fn new() -> Self { + MockStdout { + buffer: String::new(), + } + } + + fn str_from_buf_writer(b: BufWriter<MockStdout>) -> String { + match b.into_inner() { + Ok(b) => b.buffer, + Err(_) => panic!("Failed to access BufWriter inner writer"), + } + .trim_end() + .to_string() + } +} + +impl Write for MockStdout { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + let mut bytes_written = 0; + for i in buf { + self.buffer.push(*i as char); + bytes_written += 1; + } + Ok(bytes_written) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} |