simplify tests for failures with a varyadic macro
This commit is contained in:
@ -11,6 +11,7 @@
|
|||||||
See the README file in the top-level LAMMPS directory.
|
See the README file in the top-level LAMMPS directory.
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
#include "info.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "lammps.h"
|
#include "lammps.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
@ -30,6 +31,14 @@ using LAMMPS_NS::utils::split_words;
|
|||||||
|
|
||||||
namespace LAMMPS_NS {
|
namespace LAMMPS_NS {
|
||||||
using ::testing::Eq;
|
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 {
|
class SimpleCommandsTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
@ -81,6 +90,16 @@ TEST_F(SimpleCommandsTest, Echo)
|
|||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
ASSERT_EQ(lmp->input->echo_screen, 0);
|
ASSERT_EQ(lmp->input->echo_screen, 0);
|
||||||
ASSERT_EQ(lmp->input->echo_log, 1);
|
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)
|
TEST_F(SimpleCommandsTest, Log)
|
||||||
|
|||||||
@ -29,6 +29,13 @@ using utils::sfgets;
|
|||||||
using utils::sfread;
|
using utils::sfread;
|
||||||
using utils::split_words;
|
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).
|
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
|
||||||
bool verbose = false;
|
bool verbose = false;
|
||||||
|
|
||||||
@ -93,15 +100,9 @@ TEST_F(FileOperationsTest, safe_fgets)
|
|||||||
|
|
||||||
memset(buf, 0, MAX_BUF_SIZE);
|
memset(buf, 0, MAX_BUF_SIZE);
|
||||||
::testing::internal::CaptureStdout();
|
::testing::internal::CaptureStdout();
|
||||||
if (Info::has_exceptions()) {
|
TEST_FAILURE(
|
||||||
ASSERT_ANY_THROW(
|
utils::sfgets(FLERR, buf, MAX_BUF_SIZE, fp, "safe_file_read_test.txt", lmp->error););
|
||||||
utils::sfgets(FLERR, buf, MAX_BUF_SIZE, fp, "safe_file_read_test.txt", lmp->error));
|
auto mesg = ::testing::internal::GetCapturedStdout();
|
||||||
} else {
|
|
||||||
ASSERT_DEATH(
|
|
||||||
{ utils::sfgets(FLERR, buf, MAX_BUF_SIZE, fp, "safe_file_read_test.txt", lmp->error); },
|
|
||||||
"");
|
|
||||||
}
|
|
||||||
std::string mesg = ::testing::internal::GetCapturedStdout();
|
|
||||||
ASSERT_THAT(mesg, StartsWith("ERROR on proc 0: Unexpected end of file while "
|
ASSERT_THAT(mesg, StartsWith("ERROR on proc 0: Unexpected end of file while "
|
||||||
"reading file 'safe_file_read_test.txt'"));
|
"reading file 'safe_file_read_test.txt'"));
|
||||||
|
|
||||||
@ -125,14 +126,8 @@ TEST_F(FileOperationsTest, safe_fread)
|
|||||||
ASSERT_THAT(buf, StrEq("two_lines\n"));
|
ASSERT_THAT(buf, StrEq("two_lines\n"));
|
||||||
|
|
||||||
::testing::internal::CaptureStdout();
|
::testing::internal::CaptureStdout();
|
||||||
if (Info::has_exceptions()) {
|
TEST_FAILURE(utils::sfread(FLERR, buf, 1, 100, fp, "safe_file_read_test.txt", lmp->error););
|
||||||
ASSERT_ANY_THROW(
|
auto mesg = ::testing::internal::GetCapturedStdout();
|
||||||
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();
|
|
||||||
ASSERT_THAT(mesg, StartsWith("ERROR on proc 0: Unexpected end of file while "
|
ASSERT_THAT(mesg, StartsWith("ERROR on proc 0: Unexpected end of file while "
|
||||||
"reading file 'safe_file_read_test.txt'"));
|
"reading file 'safe_file_read_test.txt'"));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user