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,6 +259,9 @@ where
}
#[cfg(test)]
mod tests {
use super::*;
fn oss(v: &str) -> OsString {
OsString::from(v)
}
@ -337,7 +340,6 @@ fn temp_check_that_exec_context_observed() {
}
/// Parsing and size conversion tests
#[cfg(test)]
mod size_parsing {
use super::*;
@ -431,7 +433,6 @@ mod size_parsing {
}
/// Invalid parse testing
#[cfg(test)]
macro_rules! gen_size_filter_failure {
($($name:ident: $value:expr,)*) => {
$(
@ -445,7 +446,6 @@ macro_rules! gen_size_filter_failure {
}
/// Invalid parse data
#[cfg(test)]
gen_size_filter_failure! {
ensure_missing_symbol_returns_none: "10M",
ensure_missing_number_returns_none: "+g",
@ -483,3 +483,4 @@ fn is_within_greater_than_equal() {
let f = SizeFilter::from_string("+1K").unwrap();
assert!(f.is_within(1000));
}
}

View file

@ -167,6 +167,10 @@ impl LsColors {
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parse_simple() {
assert_eq!(Some(Colour::Red.normal()), LsColors::parse_style("31"));
@ -229,3 +233,4 @@ fn test_from_string() {
result.filenames.get("README")
);
}
}