Macro-ify the matching tests

This commit is contained in:
Benjamin Sago 2017-07-13 11:51:58 +01:00
parent 53dc370a41
commit 5227f09f5b

View File

@ -7,7 +7,7 @@ pub type ShortArg = u8;
pub type LongArg = &'static str; pub type LongArg = &'static str;
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug, Clone)]
pub enum Flag { pub enum Flag {
Short(ShortArg), Short(ShortArg),
Long(LongArg), Long(LongArg),
@ -331,52 +331,37 @@ mod parse_test {
mod matches_test { mod matches_test {
use super::*; use super::*;
static LONG: Arg = Arg { short: Some(b'l'), long: "long", takes_value: TakesValue::Forbidden }; macro_rules! test {
($name:ident: $input:expr, has $param:expr => $result:expr) => {
#[test]
fn $name() {
let frees = Vec::new();
let flags = $input.to_vec();
assert_eq!(Matches { frees, flags }.has(&$param), $result);
}
};
}
static VERBOSE: Arg = Arg { short: Some(b'v'), long: "verbose", takes_value: TakesValue::Forbidden }; static VERBOSE: Arg = Arg { short: Some(b'v'), long: "verbose", takes_value: TakesValue::Forbidden };
static COUNT: Arg = Arg { short: Some(b'c'), long: "count", takes_value: TakesValue::Necessary }; static COUNT: Arg = Arg { short: Some(b'c'), long: "count", takes_value: TakesValue::Necessary };
static TEST_ARGS: &[&Arg] = &[ &LONG, &VERBOSE, &COUNT ]; static TEST_ARGS: &[&Arg] = &[ &VERBOSE, &COUNT ];
#[test]
fn has_long() {
let matches = Matches {
frees: Vec::new(),
flags: vec![ (Flag::Short(b'l'), None) ],
};
assert!(matches.has(&LONG)); test!(short_never: [], has VERBOSE => false);
} test!(short_once: [(Flag::Short(b'v'), None)], has VERBOSE => true);
test!(short_twice: [(Flag::Short(b'v'), None), (Flag::Short(b'v'), None)], has VERBOSE => true);
#[test] test!(long_once: [(Flag::Long("verbose"), None)], has VERBOSE => true);
fn has_long_twice() { test!(long_twice: [(Flag::Long("verbose"), None), (Flag::Long("verbose"), None)], has VERBOSE => true);
let matches = Matches { test!(long_mixed: [(Flag::Long("verbose"), None), (Flag::Short(b'v'), None)], has VERBOSE => true);
frees: Vec::new(),
flags: vec![ (Flag::Short(b'l'), None),
(Flag::Short(b'l'), None) ],
};
assert!(matches.has(&LONG));
}
#[test]
fn no_long() {
let matches = Matches {
frees: Vec::new(),
flags: Vec::new(),
};
assert!(!matches.has(&LONG));
}
#[test] #[test]
fn only_count() { fn only_count() {
let everything = os("everything"); let everything = os("everything");
let frees = Vec::new();
let matches = Matches { let flags = vec![ (Flag::Short(b'c'), Some(&*everything)) ];
frees: Vec::new(), assert_eq!(Matches { frees, flags }.get(&COUNT), Some(&*everything));
flags: vec![ (Flag::Short(b'c'), Some(&*everything)) ],
};
assert_eq!(matches.get(&COUNT), Some(&*everything));
} }
#[test] #[test]
@ -384,22 +369,18 @@ mod matches_test {
let everything = os("everything"); let everything = os("everything");
let nothing = os("nothing"); let nothing = os("nothing");
let matches = Matches { let frees = Vec::new();
frees: Vec::new(), let flags = vec![ (Flag::Short(b'c'), Some(&*everything)),
flags: vec![ (Flag::Short(b'c'), Some(&*everything)), (Flag::Short(b'c'), Some(&*nothing)) ];
(Flag::Short(b'c'), Some(&*nothing)) ],
};
assert_eq!(matches.get(&COUNT), Some(&*nothing)); assert_eq!(Matches { frees, flags }.get(&COUNT), Some(&*nothing));
} }
#[test] #[test]
fn no_count() { fn no_count() {
let matches = Matches { let frees = Vec::new();
frees: Vec::new(), let flags = Vec::new();
flags: Vec::new(),
};
assert!(!matches.has(&COUNT)); assert!(!Matches { frees, flags }.has(&COUNT));
} }
} }