simplify tests for failures with a varyadic macro

This commit is contained in:
Axel Kohlmeyer
2020-07-05 12:48:52 -04:00
parent cfaa3be898
commit ca24cd5006
2 changed files with 31 additions and 17 deletions

View File

@ -11,6 +11,7 @@
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "info.h"
#include "input.h"
#include "lammps.h"
#include "utils.h"
@ -30,6 +31,14 @@ using LAMMPS_NS::utils::split_words;
namespace LAMMPS_NS {
using ::testing::Eq;
using ::testing::MatchesRegex;
#define TEST_FAILURE(...) \
if (Info::has_exceptions()) { \
ASSERT_ANY_THROW({__VA_ARGS__}); \
} else { \
ASSERT_DEATH({__VA_ARGS__}, ""); \
}
class SimpleCommandsTest : public ::testing::Test {
protected:
@ -81,6 +90,16 @@ TEST_F(SimpleCommandsTest, Echo)
if (!verbose) ::testing::internal::GetCapturedStdout();
ASSERT_EQ(lmp->input->echo_screen, 0);
ASSERT_EQ(lmp->input->echo_log, 1);
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("echo"););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex("^ERROR: Illegal echo command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("echo xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex("^ERROR: Illegal echo command.*"));
}
TEST_F(SimpleCommandsTest, Log)

View File

@ -29,6 +29,13 @@ using utils::sfgets;
using utils::sfread;
using utils::split_words;
#define TEST_FAILURE(...) \
if (Info::has_exceptions()) { \
ASSERT_ANY_THROW({__VA_ARGS__}); \
} else { \
ASSERT_DEATH({__VA_ARGS__}, ""); \
}
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
bool verbose = false;
@ -93,15 +100,9 @@ TEST_F(FileOperationsTest, safe_fgets)
memset(buf, 0, MAX_BUF_SIZE);
::testing::internal::CaptureStdout();
if (Info::has_exceptions()) {
ASSERT_ANY_THROW(
utils::sfgets(FLERR, buf, MAX_BUF_SIZE, fp, "safe_file_read_test.txt", lmp->error));
} else {
ASSERT_DEATH(
{ utils::sfgets(FLERR, buf, MAX_BUF_SIZE, fp, "safe_file_read_test.txt", lmp->error); },
"");
}
std::string mesg = ::testing::internal::GetCapturedStdout();
TEST_FAILURE(
utils::sfgets(FLERR, buf, MAX_BUF_SIZE, fp, "safe_file_read_test.txt", lmp->error););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, StartsWith("ERROR on proc 0: Unexpected end of file while "
"reading file 'safe_file_read_test.txt'"));
@ -125,14 +126,8 @@ TEST_F(FileOperationsTest, safe_fread)
ASSERT_THAT(buf, StrEq("two_lines\n"));
::testing::internal::CaptureStdout();
if (Info::has_exceptions()) {
ASSERT_ANY_THROW(
utils::sfread(FLERR, buf, 1, 100, fp, "safe_file_read_test.txt", lmp->error));
} else {
ASSERT_DEATH(
{ utils::sfread(FLERR, buf, 1, 100, fp, "safe_file_read_test.txt", lmp->error); }, "");
}
std::string mesg = ::testing::internal::GetCapturedStdout();
TEST_FAILURE(utils::sfread(FLERR, buf, 1, 100, fp, "safe_file_read_test.txt", lmp->error););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, StartsWith("ERROR on proc 0: Unexpected end of file while "
"reading file 'safe_file_read_test.txt'"));