Decouple assert_eq! and assert_parses

The assert_parses function was problematic because it insisted on using assert_eq! to check its contents. This won’t work for any type we want to test that doesn’t implement PartialEq, such as TimeFormat, which holds references to years and date strings and other such.

To go about fixing this, the first step is to change that function so it only does the initial processing, rather than the assertion, which is now done outside of it in the test macros instead.
This commit is contained in:
Benjamin Sago 2017-08-09 13:36:06 +01:00
parent ff497b52e5
commit 0b87392fd4
4 changed files with 29 additions and 13 deletions

View File

@ -63,11 +63,13 @@ mod test {
#[test]
fn $name() {
use options::parser::Arg;
use options::test::assert_parses;
use options::test::parse_for_test;
use options::test::Strictnesses::*;
static TEST_ARGS: &[&Arg] = &[&flags::RECURSE, &flags::LIST_DIRS, &flags::TREE, &flags::LEVEL ];
assert_parses($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf), $result)
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
assert_eq!(result, $result);
}
}
};
}

View File

@ -150,11 +150,13 @@ mod test {
#[test]
fn $name() {
use options::parser::Arg;
use options::test::assert_parses;
use options::test::parse_for_test;
use options::test::Strictnesses::*;
static TEST_ARGS: &[&Arg] = &[ &flags::SORT, &flags::ALL, &flags::TREE, &flags::IGNORE_GLOB ];
assert_parses($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf), $result)
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
assert_eq!(result, $result);
}
}
};
}

View File

@ -160,7 +160,6 @@ pub mod test {
use options::parser::{Arg, MatchedFlags};
use std::ffi::OsString;
use fs::filter::{SortField, SortCase};
use std::fmt::Debug;
#[derive(PartialEq, Debug)]
@ -173,24 +172,30 @@ pub mod test {
/// This function gets used by the other testing modules.
/// It can run with one or both strictness values: if told to run with
/// both, then both should resolve to the same result.
pub fn assert_parses<T, F>(inputs: &[&str], args: &'static [&'static Arg], strictnesses: Strictnesses, get: F, result: T)
where T: PartialEq + Debug, F: Fn(&MatchedFlags) -> T
///
/// It returns a vector with one or two elements in.
/// These elements can then be tested with assert_eq or what have you.
pub fn parse_for_test<T, F>(inputs: &[&str], args: &'static [&'static Arg], strictnesses: Strictnesses, get: F) -> Vec<T>
where F: Fn(&MatchedFlags) -> T
{
use self::Strictnesses::*;
use options::parser::{Args, Strictness};
use std::ffi::OsString;
let bits = inputs.into_iter().map(|&o| os(o)).collect::<Vec<OsString>>();
let mut rezzies = Vec::new();
if strictnesses == Last || strictnesses == Both {
let results = Args(args).parse(bits.iter(), Strictness::UseLastArguments);
assert_eq!(get(&results.unwrap().flags), result);
rezzies.push(get(&results.unwrap().flags));
}
if strictnesses == Complain || strictnesses == Both {
let results = Args(args).parse(bits.iter(), Strictness::ComplainAboutRedundantArguments);
assert_eq!(get(&results.unwrap().flags), result);
rezzies.push(get(&results.unwrap().flags));
}
rezzies
}
/// Creates an `OSStr` (used in tests)

View File

@ -219,12 +219,13 @@ impl SizeFormat {
}
const TIME_STYLES: &[&str] = &["default", "long-iso", "full-iso", "iso"];
impl TimeFormat {
/// Determine how time should be formatted in timestamp columns.
fn deduce(matches: &MatchedFlags) -> Result<TimeFormat, Misfire> {
pub use output::time::{DefaultFormat, ISOFormat};
const STYLES: &[&str] = &["default", "long-iso", "full-iso", "iso"];
let word = match matches.get(&flags::TIME_STYLE)? {
Some(w) => w,
@ -244,7 +245,7 @@ impl TimeFormat {
Ok(TimeFormat::FullISO)
}
else {
Err(Misfire::bad_argument(&flags::TIME_STYLE, word, STYLES))
Err(Misfire::bad_argument(&flags::TIME_STYLE, word, TIME_STYLES))
}
}
}
@ -425,14 +426,16 @@ mod test {
#[test]
fn $name() {
use options::parser::Arg;
use options::test::assert_parses;
use options::test::parse_for_test;
use options::test::Strictnesses::*;
static TEST_ARGS: &[&Arg] = &[ &flags::BINARY, &flags::BYTES,
&flags::TIME, &flags::MODIFIED, &flags::CREATED, &flags::ACCESSED,
&flags::COLOR, &flags::COLOUR ];
assert_parses($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf), $result)
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
assert_eq!(result, $result);
}
}
};
}
@ -453,16 +456,20 @@ mod test {
// Default behaviour
test!(empty: TimeTypes <- []; Both => Ok(TimeTypes::default()));
// Modified
test!(modified: TimeTypes <- ["--modified"]; Both => Ok(TimeTypes { accessed: false, modified: true, created: false }));
test!(m: TimeTypes <- ["-m"]; Both => Ok(TimeTypes { accessed: false, modified: true, created: false }));
test!(time_mod: TimeTypes <- ["--time=modified"]; Both => Ok(TimeTypes { accessed: false, modified: true, created: false }));
test!(time_m: TimeTypes <- ["-tmod"]; Both => Ok(TimeTypes { accessed: false, modified: true, created: false }));
// Accessed
test!(acc: TimeTypes <- ["--accessed"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: false }));
test!(a: TimeTypes <- ["-u"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: false }));
test!(time_acc: TimeTypes <- ["--time", "accessed"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: false }));
test!(time_a: TimeTypes <- ["-t", "acc"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: false }));
// Created
test!(cr: TimeTypes <- ["--created"]; Both => Ok(TimeTypes { accessed: false, modified: false, created: true }));
test!(c: TimeTypes <- ["-U"]; Both => Ok(TimeTypes { accessed: false, modified: false, created: true }));
test!(time_cr: TimeTypes <- ["--time=created"]; Both => Ok(TimeTypes { accessed: false, modified: false, created: true }));