Added sanity checks for test utils

This commit is contained in:
Gilad Naaman 2021-04-10 21:18:38 +03:00
parent cd3dba2481
commit 4695667c7c

View file

@ -64,7 +64,7 @@ fn read_scenario_fixture<S: AsRef<OsStr>>(tmpd: &Option<Rc<TempDir>>, file_rel_p
/// A command result is the outputs of a command (streams and status code)
/// within a struct which has convenience assertion functions about those outputs
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CmdResult {
//tmpd is used for convenience functions for asserts against fixtures
tmpd: Option<Rc<TempDir>>,
@ -304,7 +304,7 @@ impl CmdResult {
self
}
pub fn stderr_contains<T: AsRef<str>>(&self, cmp: &T) -> &CmdResult {
pub fn stderr_contains<T: AsRef<str>>(&self, cmp: T) -> &CmdResult {
assert!(self.stderr_str().contains(cmp.as_ref()));
self
}
@ -314,7 +314,7 @@ impl CmdResult {
self
}
pub fn stderr_does_not_contain<T: AsRef<str>>(&self, cmp: &T) -> &CmdResult {
pub fn stderr_does_not_contain<T: AsRef<str>>(&self, cmp: T) -> &CmdResult {
assert!(!self.stderr_str().contains(cmp.as_ref()));
self
}
@ -870,3 +870,211 @@ pub fn vec_of_size(n: usize) -> Vec<u8> {
assert_eq!(result.len(), n);
result
}
/// Sanity checks for test utils
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_code_is() {
let res = CmdResult {
tmpd: None,
code: Some(32),
success: false,
stdout: "".into(),
stderr: "".into(),
};
res.code_is(32);
}
#[test]
#[should_panic]
fn test_code_is_fail() {
let res = CmdResult {
tmpd: None,
code: Some(32),
success: false,
stdout: "".into(),
stderr: "".into(),
};
res.code_is(1);
}
#[test]
fn test_failure() {
let res = CmdResult {
tmpd: None,
code: None,
success: false,
stdout: "".into(),
stderr: "".into(),
};
res.failure();
}
#[test]
#[should_panic]
fn test_failure_fail() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "".into(),
stderr: "".into(),
};
res.failure();
}
#[test]
fn test_success() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "".into(),
stderr: "".into(),
};
res.success();
}
#[test]
#[should_panic]
fn test_success_fail() {
let res = CmdResult {
tmpd: None,
code: None,
success: false,
stdout: "".into(),
stderr: "".into(),
};
res.success();
}
#[test]
fn test_no_std_errout() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "".into(),
stderr: "".into(),
};
res.no_stderr();
res.no_stdout();
}
#[test]
#[should_panic]
fn test_no_stderr_fail() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "".into(),
stderr: "asdfsadfa".into(),
};
res.no_stderr();
}
#[test]
#[should_panic]
fn test_no_stdout_fail() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "asdfsadfa".into(),
stderr: "".into(),
};
res.no_stdout();
}
#[test]
fn test_std_does_not_contain() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "This is a likely error message\n".into(),
stderr: "This is a likely error message\n".into(),
};
res.stdout_does_not_contain("unlikely");
res.stderr_does_not_contain("unlikely");
}
#[test]
#[should_panic]
fn test_stdout_does_not_contain_fail() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "This is a likely error message\n".into(),
stderr: "".into(),
};
res.stdout_does_not_contain("likely");
}
#[test]
#[should_panic]
fn test_stderr_does_not_contain_fail() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "".into(),
stderr: "This is a likely error message\n".into(),
};
res.stderr_does_not_contain("likely");
}
#[test]
fn test_stdout_matches() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "This is a likely error message\n".into(),
stderr: "This is a likely error message\n".into(),
};
let positive = regex::Regex::new(".*likely.*").unwrap();
let negative = regex::Regex::new(".*unlikely.*").unwrap();
res.stdout_matches(&positive);
res.stdout_does_not_match(&negative);
}
#[test]
#[should_panic]
fn test_stdout_matches_fail() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "This is a likely error message\n".into(),
stderr: "This is a likely error message\n".into(),
};
let negative = regex::Regex::new(".*unlikely.*").unwrap();
res.stdout_matches(&negative);
}
#[test]
#[should_panic]
fn test_stdout_not_matches_fail() {
let res = CmdResult {
tmpd: None,
code: None,
success: true,
stdout: "This is a likely error message\n".into(),
stderr: "This is a likely error message\n".into(),
};
let positive = regex::Regex::new(".*likely.*").unwrap();
res.stdout_does_not_match(&positive);
}
}