From 74705c811107ebc036bf519e973c8ebb0e3bb28d Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 4 Oct 2022 09:27:52 -0400 Subject: [PATCH] port error check test from fortran wrapper to c-library interface test --- .../c-library/test_library_properties.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/unittest/c-library/test_library_properties.cpp b/unittest/c-library/test_library_properties.cpp index 56c89a6c13..bbb363dfab 100644 --- a/unittest/c-library/test_library_properties.cpp +++ b/unittest/c-library/test_library_properties.cpp @@ -434,6 +434,33 @@ TEST_F(LibraryProperties, neighlist) } }; +TEST_F(LibraryProperties, has_error) +{ + // need errors to throw exceptions to be able to intercept them. + if (!lammps_config_has_exceptions()) GTEST_SKIP(); + + EXPECT_EQ(lammps_has_error(lmp), 0); + + // trigger an error, but hide output + ::testing::internal::CaptureStdout(); + lammps_command(lmp, "this_is_not_a_known_command"); + ::testing::internal::GetCapturedStdout(); + + EXPECT_EQ(lammps_has_error(lmp), 1); + + // retrieve error message + char errmsg[1024]; + int err = lammps_get_last_error_message(lmp, errmsg, 1024); + EXPECT_EQ(err, 1); + EXPECT_THAT(errmsg, HasSubstr("ERROR: Unknown command: this_is_not_a_known_command")); + + // retrieving the error message clear the error status + EXPECT_EQ(lammps_has_error(lmp), 0); + err = lammps_get_last_error_message(lmp, errmsg, 1024); + EXPECT_EQ(err, 0); + EXPECT_THAT(errmsg, StrEq("")); +}; + class AtomProperties : public ::testing::Test { protected: void *lmp;