Add exec tests and cfg(test) compiler guards

Some tests throughout the codebase were just functions marked with the
`#[test]` macro. It is convention to have these test functions in a
`test` module with a compiler guard `cfg(test)`. This PR updates the
tests that aren't already setup like this to be in the `test` module.

Additionally, this PR also adds tests to the `exec` module to check the
remaining `Token` enum variations.
This commit is contained in:
Josh Leeb-du Toit 2018-10-03 18:36:15 +10:00 committed by David Peter
parent 56b6389dac
commit adc467b2b2
4 changed files with 314 additions and 265 deletions

View file

@ -72,7 +72,7 @@ pub fn dirname(path: &str) -> &str {
#[cfg(test)]
mod tests {
use super::{basename, dirname, remove_extension, MAIN_SEPARATOR};
use super::*;
fn correct(input: &str) -> String {
input.replace('/', &MAIN_SEPARATOR.to_string())

View file

@ -150,20 +150,24 @@ impl ArgumentTemplate {
#[cfg(test)]
mod tests {
use super::{ArgumentTemplate, CommandTemplate, Token};
use super::*;
#[test]
fn tokens() {
let expected = CommandTemplate {
fn tokens_with_placeholder() {
assert_eq!(
CommandTemplate::new(&[&"echo", &"${SHELL}:"]),
CommandTemplate {
args: vec![
ArgumentTemplate::Text("echo".into()),
ArgumentTemplate::Text("${SHELL}:".into()),
ArgumentTemplate::Tokens(vec![Token::Placeholder]),
],
};
assert_eq!(CommandTemplate::new(&[&"echo", &"${SHELL}:"]), expected);
}
);
}
#[test]
fn tokens_with_no_extension() {
assert_eq!(
CommandTemplate::new(&["echo", "{.}"]),
CommandTemplate {
@ -174,4 +178,43 @@ mod tests {
}
);
}
#[test]
fn tokens_with_basename() {
assert_eq!(
CommandTemplate::new(&["echo", "{/}"]),
CommandTemplate {
args: vec![
ArgumentTemplate::Text("echo".into()),
ArgumentTemplate::Tokens(vec![Token::Basename]),
],
}
);
}
#[test]
fn tokens_with_parent() {
assert_eq!(
CommandTemplate::new(&["echo", "{//}"]),
CommandTemplate {
args: vec![
ArgumentTemplate::Text("echo".into()),
ArgumentTemplate::Tokens(vec![Token::Parent]),
],
}
);
}
#[test]
fn tokens_with_basename_no_extension() {
assert_eq!(
CommandTemplate::new(&["echo", "{/.}"]),
CommandTemplate {
args: vec![
ArgumentTemplate::Text("echo".into()),
ArgumentTemplate::Tokens(vec![Token::BasenameNoExt]),
],
}
);
}
}

View file

@ -259,32 +259,35 @@ where
}
#[cfg(test)]
fn oss(v: &str) -> OsString {
OsString::from(v)
}
mod tests {
use super::*;
/// Ensure that -exec gets transformed into --exec
#[test]
fn normal_exec_substitution() {
fn oss(v: &str) -> OsString {
OsString::from(v)
}
/// Ensure that -exec gets transformed into --exec
#[test]
fn normal_exec_substitution() {
let original = vec![oss("fd"), oss("foo"), oss("-exec"), oss("cmd")];
let expected = vec![oss("fd"), oss("foo"), oss("--exec"), oss("cmd")];
let actual = transform_args_with_exec(original.into_iter());
assert_eq!(expected, actual);
}
}
/// Ensure that --exec is not touched
#[test]
fn passthru_of_original_exec() {
/// Ensure that --exec is not touched
#[test]
fn passthru_of_original_exec() {
let original = vec![oss("fd"), oss("foo"), oss("--exec"), oss("cmd")];
let expected = vec![oss("fd"), oss("foo"), oss("--exec"), oss("cmd")];
let actual = transform_args_with_exec(original.into_iter());
assert_eq!(expected, actual);
}
}
#[test]
fn temp_check_that_exec_context_observed() {
#[test]
fn temp_check_that_exec_context_observed() {
let original = vec![
oss("fd"),
oss("foo"),
@ -334,11 +337,10 @@ fn temp_check_that_exec_context_observed() {
let actual = transform_args_with_exec(original.into_iter());
assert_eq!(expected, actual);
}
}
/// Parsing and size conversion tests
#[cfg(test)]
mod size_parsing {
/// Parsing and size conversion tests
mod size_parsing {
use super::*;
macro_rules! gen_size_filter_parse_test {
@ -428,11 +430,10 @@ mod size_parsing {
tebi_minus_upper: ("-1Ti", SizeFilter::Max(1099511627776)),
tebi_minus_suffix_upper: ("-1TIB", SizeFilter::Max(1099511627776)),
}
}
}
/// Invalid parse testing
#[cfg(test)]
macro_rules! gen_size_filter_failure {
/// Invalid parse testing
macro_rules! gen_size_filter_failure {
($($name:ident: $value:expr,)*) => {
$(
#[test]
@ -444,9 +445,8 @@ macro_rules! gen_size_filter_failure {
};
}
/// Invalid parse data
#[cfg(test)]
gen_size_filter_failure! {
/// Invalid parse data
gen_size_filter_failure! {
ensure_missing_symbol_returns_none: "10M",
ensure_missing_number_returns_none: "+g",
ensure_missing_unit_returns_none: "+18",
@ -458,28 +458,29 @@ gen_size_filter_failure! {
ensure_invalid_unit_returns_none_3: "+1Mv",
ensure_bib_format_returns_none: "+1bib",
ensure_bb_format_returns_none: "+1bb",
}
}
#[test]
fn is_within_less_than() {
#[test]
fn is_within_less_than() {
let f = SizeFilter::from_string("-1k").unwrap();
assert!(f.is_within(999));
}
}
#[test]
fn is_within_less_than_equal() {
#[test]
fn is_within_less_than_equal() {
let f = SizeFilter::from_string("-1k").unwrap();
assert!(f.is_within(1000));
}
}
#[test]
fn is_within_greater_than() {
#[test]
fn is_within_greater_than() {
let f = SizeFilter::from_string("+1k").unwrap();
assert!(f.is_within(1001));
}
}
#[test]
fn is_within_greater_than_equal() {
#[test]
fn is_within_greater_than_equal() {
let f = SizeFilter::from_string("+1K").unwrap();
assert!(f.is_within(1000));
}
}

View file

@ -167,31 +167,35 @@ impl LsColors {
}
}
#[test]
fn test_parse_simple() {
assert_eq!(Some(Colour::Red.normal()), LsColors::parse_style("31"));
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parse_decoration() {
#[test]
fn test_parse_simple() {
assert_eq!(Some(Colour::Red.normal()), LsColors::parse_style("31"));
}
#[test]
fn test_parse_decoration() {
assert_eq!(Some(Colour::Red.normal()), LsColors::parse_style("00;31"));
assert_eq!(Some(Colour::Blue.italic()), LsColors::parse_style("03;34"));
assert_eq!(Some(Colour::Cyan.bold()), LsColors::parse_style("01;36"));
}
}
#[test]
fn test_parse_decoration_backwards() {
#[test]
fn test_parse_decoration_backwards() {
assert_eq!(Some(Colour::Blue.italic()), LsColors::parse_style("34;03"));
assert_eq!(Some(Colour::Cyan.bold()), LsColors::parse_style("36;01"));
assert_eq!(Some(Colour::Red.normal()), LsColors::parse_style("31;00"));
}
}
#[test]
fn test_parse_256() {
#[test]
fn test_parse_256() {
assert_eq!(
Some(Colour::Fixed(115).normal()),
LsColors::parse_style("38;5;115")
@ -211,10 +215,10 @@ fn test_parse_256() {
Some(Colour::Fixed(119).bold()),
LsColors::parse_style("38;5;119;01")
);
}
}
#[test]
fn test_from_string() {
#[test]
fn test_from_string() {
assert_eq!(LsColors::default(), LsColors::from_string(&String::new()));
let result = LsColors::from_string(&String::from(
@ -228,4 +232,5 @@ fn test_from_string() {
Some(&Colour::Yellow.normal()),
result.filenames.get("README")
);
}
}