Simplify PythonPackage tests

This commit is contained in:
Richard Berger
2021-03-23 19:55:08 -04:00
parent 403a7a6f84
commit 180e816886

View File

@ -40,6 +40,8 @@ protected:
LAMMPS *lmp; LAMMPS *lmp;
Info *info; Info *info;
void command(const std::string &line) { lmp->input->one(line.c_str()); }
void SetUp() override void SetUp() override
{ {
const char *args[] = {"PythonPackageTest", "-log", "none", "-echo", "screen", "-nocite"}; const char *args[] = {"PythonPackageTest", "-log", "none", "-echo", "screen", "-nocite"};
@ -51,16 +53,16 @@ protected:
ASSERT_NE(lmp, nullptr); ASSERT_NE(lmp, nullptr);
info = new Info(lmp); info = new Info(lmp);
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("units real"); command("units real");
lmp->input->one("dimension 3"); command("dimension 3");
lmp->input->one("region box block -4 4 -4 4 -4 4"); command("region box block -4 4 -4 4 -4 4");
lmp->input->one("create_box 1 box"); command("create_box 1 box");
lmp->input->one("create_atoms 1 single 0.0 0.0 0.0 units box"); command("create_atoms 1 single 0.0 0.0 0.0 units box");
lmp->input->one("create_atoms 1 single 1.9 -1.9 1.9999 units box"); command("create_atoms 1 single 1.9 -1.9 1.9999 units box");
lmp->input->one("pair_style zero 2.0"); command("pair_style zero 2.0");
lmp->input->one("pair_coeff * *"); command("pair_coeff * *");
lmp->input->one("mass * 1.0"); command("mass * 1.0");
lmp->input->one("variable input_dir index " + INPUT_FOLDER); command("variable input_dir index " + INPUT_FOLDER);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
} }
@ -78,31 +80,31 @@ TEST_F(PythonPackageTest, python_invoke)
if (!info->has_style("command", "python")) GTEST_SKIP(); if (!info->has_style("command", "python")) GTEST_SKIP();
// execute python function from file // execute python function from file
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("python printnum file ${input_dir}/func.py"); command("python printnum file ${input_dir}/func.py");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
lmp->input->one("python printnum invoke"); command("python printnum invoke");
std::string output = ::testing::internal::GetCapturedStdout(); std::string output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output; if (verbose) std::cout << output;
ASSERT_THAT(output, MatchesRegex("python.*2.25.*")); ASSERT_THAT(output, MatchesRegex("python.*2.25.*"));
// execute another python function from same file // execute another python function from same file
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("python printtxt exists"); command("python printtxt exists");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
lmp->input->one("python printtxt invoke"); command("python printtxt invoke");
output = ::testing::internal::GetCapturedStdout(); output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output; if (verbose) std::cout << output;
ASSERT_THAT(output, MatchesRegex("python.*sometext.*")); ASSERT_THAT(output, MatchesRegex("python.*sometext.*"));
// execute python function that uses the LAMMPS python module // execute python function that uses the LAMMPS python module
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("variable idx equal 2.25"); command("variable idx equal 2.25");
lmp->input->one("python getidxvar input 1 SELF format p exists"); command("python getidxvar input 1 SELF format p exists");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
lmp->input->one("python getidxvar invoke"); command("python getidxvar invoke");
output = ::testing::internal::GetCapturedStdout(); output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output; if (verbose) std::cout << output;
ASSERT_THAT(output, MatchesRegex("python.*2.25.*")); ASSERT_THAT(output, MatchesRegex("python.*2.25.*"));
@ -112,12 +114,12 @@ TEST_F(PythonPackageTest, python_variable)
{ {
if (!info->has_style("command", "python")) GTEST_SKIP(); if (!info->has_style("command", "python")) GTEST_SKIP();
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("variable sq python square"); command("variable sq python square");
lmp->input->one("variable val index 1.5"); command("variable val index 1.5");
lmp->input->one("python square input 1 v_val return v_sq format ff file ${input_dir}/func.py"); command("python square input 1 v_val return v_sq format ff file ${input_dir}/func.py");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
lmp->input->one("print \"${sq}\""); command("print \"${sq}\"");
std::string output = ::testing::internal::GetCapturedStdout(); std::string output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output; if (verbose) std::cout << output;
ASSERT_THAT(output, MatchesRegex("print.*2.25.*")); ASSERT_THAT(output, MatchesRegex("print.*2.25.*"));