From cd3dba24816a6b01a6af85716ecb31e7b2114fa4 Mon Sep 17 00:00:00 2001 From: Gilad Naaman Date: Sat, 10 Apr 2021 21:24:30 +0300 Subject: [PATCH] Added some tests utils for future refactoring --- tests/common/macros.rs | 37 -------------------------- tests/common/util.rs | 59 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 41 deletions(-) diff --git a/tests/common/macros.rs b/tests/common/macros.rs index e8b9c9d5d..81878bf1b 100644 --- a/tests/common/macros.rs +++ b/tests/common/macros.rs @@ -1,40 +1,3 @@ -/// Assertion helper macro for [`CmdResult`] types -/// -/// [`CmdResult`]: crate::tests::common::util::CmdResult -#[macro_export] -macro_rules! assert_empty_stderr( - ($cond:expr) => ( - if $cond.stderr.len() > 0 { - panic!("stderr: {}", $cond.stderr_str()) - } - ); -); - -/// Assertion helper macro for [`CmdResult`] types -/// -/// [`CmdResult`]: crate::tests::common::util::CmdResult -#[macro_export] -macro_rules! assert_empty_stdout( - ($cond:expr) => ( - if $cond.stdout.len() > 0 { - panic!("stdout: {}", $cond.stdout_str()) - } - ); -); - -/// Assertion helper macro for [`CmdResult`] types -/// -/// [`CmdResult`]: crate::tests::common::util::CmdResult -#[macro_export] -macro_rules! assert_no_error( - ($cond:expr) => ( - assert!($cond.success); - if $cond.stderr.len() > 0 { - panic!("stderr: {}", $cond.stderr_str()) - } - ); -); - /// Platform-independent helper for constructing a PathBuf from individual elements #[macro_export] macro_rules! path_concat { diff --git a/tests/common/util.rs b/tests/common/util.rs index d5663e9ed..18830919c 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -130,6 +130,11 @@ impl CmdResult { self.code.expect("Program must be run first") } + pub fn code_is(&self, expected_code: i32) -> &CmdResult { + assert_eq!(self.code(), expected_code); + self + } + /// Returns the program's TempDir /// Panics if not present pub fn tmpd(&self) -> Rc { @@ -146,13 +151,25 @@ impl CmdResult { /// asserts that the command resulted in a success (zero) status code pub fn success(&self) -> &CmdResult { - assert!(self.success); + if !self.success { + panic!( + "Command was expected to succeed.\nstdout = {}\n stderr = {}", + self.stdout_str(), + self.stderr_str() + ); + } self } /// asserts that the command resulted in a failure (non-zero) status code pub fn failure(&self) -> &CmdResult { - assert!(!self.success); + if self.success { + panic!( + "Command was expected to fail.\nstdout = {}\n stderr = {}", + self.stdout_str(), + self.stderr_str() + ); + } self } @@ -168,7 +185,12 @@ impl CmdResult { /// 1. you can not know exactly what stdout will be or /// 2. you know that stdout will also be empty pub fn no_stderr(&self) -> &CmdResult { - assert!(self.stderr.is_empty()); + if !self.stderr.is_empty() { + panic!( + "Expected stderr to be empty, but it's:\n{}", + self.stderr_str() + ); + } self } @@ -179,7 +201,12 @@ impl CmdResult { /// 1. you can not know exactly what stderr will be or /// 2. you know that stderr will also be empty pub fn no_stdout(&self) -> &CmdResult { - assert!(self.stdout.is_empty()); + if !self.stdout.is_empty() { + panic!( + "Expected stdout to be empty, but it's:\n{}", + self.stderr_str() + ); + } self } @@ -281,6 +308,30 @@ impl CmdResult { assert!(self.stderr_str().contains(cmp.as_ref())); self } + + pub fn stdout_does_not_contain>(&self, cmp: T) -> &CmdResult { + assert!(!self.stdout_str().contains(cmp.as_ref())); + self + } + + pub fn stderr_does_not_contain>(&self, cmp: &T) -> &CmdResult { + assert!(!self.stderr_str().contains(cmp.as_ref())); + self + } + + pub fn stdout_matches(&self, regex: ®ex::Regex) -> &CmdResult { + if !regex.is_match(self.stdout_str()) { + panic!("Stdout does not match regex:\n{}", self.stdout_str()) + } + self + } + + pub fn stdout_does_not_match(&self, regex: ®ex::Regex) -> &CmdResult { + if regex.is_match(self.stdout_str()) { + panic!("Stdout matches regex:\n{}", self.stdout_str()) + } + self + } } pub fn log_info, U: AsRef>(msg: T, par: U) {