From 06157fdeddd4ad4cad785c681148c3dff6cc0d35 Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Wed, 26 Jul 2017 23:37:58 +0100 Subject: [PATCH] Add some tests for the time flags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently I forgot to give the --time flag an argument, and this wasn’t actually covered by any of the xtests! Well, it’s tested now. I’m not sure how to handle multiple --time arguments. --- src/options/flags.rs | 2 +- src/options/view.rs | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/options/flags.rs b/src/options/flags.rs index d0cf319..6e4790f 100644 --- a/src/options/flags.rs +++ b/src/options/flags.rs @@ -38,7 +38,7 @@ pub static INODE: Arg = Arg { short: Some(b'i'), long: "inode", takes_ pub static LINKS: Arg = Arg { short: Some(b'H'), long: "links", takes_value: TakesValue::Forbidden }; pub static MODIFIED: Arg = Arg { short: Some(b'm'), long: "modified", takes_value: TakesValue::Forbidden }; pub static BLOCKS: Arg = Arg { short: Some(b'S'), long: "blocks", takes_value: TakesValue::Forbidden }; -pub static TIME: Arg = Arg { short: Some(b't'), long: "time", takes_value: TakesValue::Forbidden }; +pub static TIME: Arg = Arg { short: Some(b't'), long: "time", takes_value: TakesValue::Necessary }; pub static ACCESSED: Arg = Arg { short: Some(b'u'), long: "accessed", takes_value: TakesValue::Forbidden }; pub static CREATED: Arg = Arg { short: Some(b'U'), long: "created", takes_value: TakesValue::Forbidden }; pub static TIME_STYLE: Arg = Arg { short: None, long: "time-style", takes_value: TakesValue::Necessary }; diff --git a/src/options/view.rs b/src/options/view.rs index 202cafd..3a6e138 100644 --- a/src/options/view.rs +++ b/src/options/view.rs @@ -253,6 +253,8 @@ impl TimeFormat { } +static TIMES: &[&str] = &["modified", "accessed", "created"]; + impl TimeTypes { /// Determine which of a file’s time fields should be displayed for it @@ -282,7 +284,6 @@ impl TimeTypes { return Err(Misfire::Useless(&flags::ACCESSED, true, &flags::TIME)); } - static TIMES: &[&str] = &["modified", "accessed", "created"]; if word == "mod" || word == "modified" { Ok(TimeTypes { accessed: false, modified: true, created: false }) } @@ -423,7 +424,8 @@ mod test { use options::parser::{parse, Args, Arg}; use std::ffi::OsString; - static TEST_ARGS: &[&Arg] = &[ &flags::BINARY, &flags::BYTES ]; + static TEST_ARGS: &[&Arg] = &[ &flags::BINARY, &flags::BYTES, + &flags::TIME, &flags::MODIFIED, &flags::CREATED, &flags::ACCESSED ]; let bits = $inputs.as_ref().into_iter().map(|&o| os(o)).collect::>(); let results = parse(&Args(TEST_ARGS), bits.iter()); @@ -432,6 +434,7 @@ mod test { }; } + mod size_formats { use super::*; @@ -440,4 +443,36 @@ mod test { test!(bytes: SizeFormat <- ["--bytes"] => Ok(SizeFormat::JustBytes)); test!(both: SizeFormat <- ["--binary", "--bytes"] => Err(Misfire::Conflict(&flags::BINARY, &flags::BYTES))); } + + + mod time_types { + use super::*; + + // Default behaviour + test!(empty: TimeTypes <- [] => Ok(TimeTypes::default())); + test!(modified: TimeTypes <- ["--modified"] => Ok(TimeTypes { accessed: false, modified: true, created: false })); + test!(m: TimeTypes <- ["-m"] => Ok(TimeTypes { accessed: false, modified: true, created: false })); + test!(time_mod: TimeTypes <- ["--time=modified"] => Ok(TimeTypes { accessed: false, modified: true, created: false })); + test!(time_m: TimeTypes <- ["-tmod"] => Ok(TimeTypes { accessed: false, modified: true, created: false })); + + test!(acc: TimeTypes <- ["--accessed"] => Ok(TimeTypes { accessed: true, modified: false, created: false })); + test!(a: TimeTypes <- ["-u"] => Ok(TimeTypes { accessed: true, modified: false, created: false })); + test!(time_acc: TimeTypes <- ["--time", "accessed"] => Ok(TimeTypes { accessed: true, modified: false, created: false })); + test!(time_a: TimeTypes <- ["-t", "acc"] => Ok(TimeTypes { accessed: true, modified: false, created: false })); + + test!(cr: TimeTypes <- ["--created"] => Ok(TimeTypes { accessed: false, modified: false, created: true })); + test!(c: TimeTypes <- ["-U"] => Ok(TimeTypes { accessed: false, modified: false, created: true })); + test!(time_cr: TimeTypes <- ["--time=created"] => Ok(TimeTypes { accessed: false, modified: false, created: true })); + test!(time_c: TimeTypes <- ["-tcr"] => Ok(TimeTypes { accessed: false, modified: false, created: true })); + + // Multiples + test!(time_uu: TimeTypes <- ["-uU"] => Ok(TimeTypes { accessed: true, modified: false, created: true })); + + // Overriding + test!(time_mc: TimeTypes <- ["-tcr", "-tmod"] => Ok(TimeTypes { accessed: false, modified: true, created: false })); + + // Errors + test!(time_tea: TimeTypes <- ["--time=tea"] => Err(Misfire::bad_argument(&flags::TIME, &os("tea"), super::TIMES))); + test!(time_ea: TimeTypes <- ["-tea"] => Err(Misfire::bad_argument(&flags::TIME, &os("ea"), super::TIMES))); + } } -- GitLab