From 817c7d23187dda170e868d95b3e8d02ef4c3b01d Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Wed, 26 Jul 2017 23:05:45 +0100 Subject: [PATCH] Add tests for size format --- src/options/view.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/options/view.rs b/src/options/view.rs index e164c93..202cafd 100644 --- a/src/options/view.rs +++ b/src/options/view.rs @@ -401,3 +401,43 @@ lazy_static! { dimensions().map(|t| t.0) }; } + + + +#[cfg(test)] +mod test { + use super::*; + use std::ffi::OsString; + use options::flags; + + pub fn os(input: &'static str) -> OsString { + let mut os = OsString::new(); + os.push(input); + os + } + + macro_rules! test { + ($name:ident: $type:ident <- $inputs:expr => $result:expr) => { + #[test] + fn $name() { + use options::parser::{parse, Args, Arg}; + use std::ffi::OsString; + + static TEST_ARGS: &[&Arg] = &[ &flags::BINARY, &flags::BYTES ]; + + let bits = $inputs.as_ref().into_iter().map(|&o| os(o)).collect::>(); + let results = parse(&Args(TEST_ARGS), bits.iter()); + assert_eq!($type::deduce(results.as_ref().unwrap()), $result); + } + }; + } + + mod size_formats { + use super::*; + + test!(empty: SizeFormat <- [] => Ok(SizeFormat::DecimalBytes)); + test!(binary: SizeFormat <- ["--binary"] => Ok(SizeFormat::BinaryBytes)); + test!(bytes: SizeFormat <- ["--bytes"] => Ok(SizeFormat::JustBytes)); + test!(both: SizeFormat <- ["--binary", "--bytes"] => Err(Misfire::Conflict(&flags::BINARY, &flags::BYTES))); + } +}