Migrate tests to use MockWarningHandler for warning interception.
PiperOrigin-RevId: 933656759 Change-Id: Ia21514c302799034bf598445933dd90427284737
This commit is contained in:
committed by
Copybara-Service
parent
38c3388046
commit
cedaa47d61
@@ -24,9 +24,9 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <absl/strings/str_format.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include "src/cc/array_safety.h"
|
||||
#include <mujoco/mujoco.h>
|
||||
#include <mujoco/mjspec.h>
|
||||
#include <mujoco/mjplugin.h>
|
||||
@@ -38,9 +38,8 @@ namespace mujoco {
|
||||
namespace {
|
||||
|
||||
using ::testing::HasSubstr;
|
||||
using ::testing::NotNull;
|
||||
using ::testing::IsNull;
|
||||
|
||||
using ::testing::NotNull;
|
||||
|
||||
// -------------------------- test model manipulation -------------------------
|
||||
|
||||
@@ -243,7 +242,8 @@ int open_mock(mjResource* resource) {
|
||||
</mujoco>
|
||||
)";
|
||||
resource->data = mju_malloc(sizeof(parent_xml));
|
||||
std::strcpy((char*)resource->data, parent_xml);
|
||||
absl::SNPrintF(static_cast<char*>(resource->data), sizeof(parent_xml), "%s",
|
||||
parent_xml);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -2668,6 +2668,12 @@ void AttachNestedKeyframe(bool compile) {
|
||||
// compile required before further attachment
|
||||
mjModel* m_child = compile ? mj_compile(child, 0) : nullptr;
|
||||
|
||||
// check warning is issued, empty for a compiled model
|
||||
MockWarningHandler warning_handler;
|
||||
if (!compile) {
|
||||
warning_handler.ExpectWarnings("model has pending keyframes");
|
||||
}
|
||||
|
||||
// attach child to parent
|
||||
mjs_attach(mjs_findFrame(parent, "frame")->element,
|
||||
mjs_findBody(child, "body")->element, "child-", "");
|
||||
@@ -2679,7 +2685,6 @@ void AttachNestedKeyframe(bool compile) {
|
||||
EXPECT_THAT(mjs_getWarning(parent, 0),
|
||||
HasSubstr("model has pending keyframes"));
|
||||
}
|
||||
|
||||
// compare models
|
||||
mjtNum tol = 0;
|
||||
std::string field = "";
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
#include <mujoco/mjmodel.h>
|
||||
#include <mujoco/mjtype.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "src/cc/array_safety.h"
|
||||
#include "test/fixture.h"
|
||||
|
||||
namespace mujoco {
|
||||
@@ -1310,11 +1309,8 @@ TEST_F(MjCMeshTest, NaNConvexHullDisallowed) {
|
||||
</asset>
|
||||
</mujoco>
|
||||
)";
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings();
|
||||
std::array<char, 1024> error;
|
||||
mjModel* model = LoadModelFromString(xml, error.data(), error.size());
|
||||
EXPECT_THAT(model, testing::IsNull());
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include "src/cc/array_safety.h"
|
||||
#include <absl/strings/str_format.h>
|
||||
#include <mujoco/mjplugin.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "src/engine/engine_plugin.h"
|
||||
@@ -51,8 +51,9 @@ int open_str(mjResource* resource) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
resource->data = mju_malloc(100*sizeof(char));
|
||||
std::strcpy((char*) resource->data, "Hello World");
|
||||
const std::size_t kBufferSize = 100;
|
||||
resource->data = mju_malloc(kBufferSize * sizeof(char));
|
||||
absl::SNPrintF((char*) resource->data, kBufferSize, "Hello World");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -128,17 +129,10 @@ TEST_F(ResourceTest, RegisterProviderMissingCallbacks) {
|
||||
.prefix = "myprefix",
|
||||
};
|
||||
|
||||
// install warning handler
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings("callback");
|
||||
|
||||
int i = mjp_registerResourceProvider(&provider);
|
||||
|
||||
// warning message related to missing callbacks
|
||||
EXPECT_THAT(warning, HasSubstr("callback"));
|
||||
EXPECT_LT(i, 1);
|
||||
}
|
||||
|
||||
@@ -150,17 +144,10 @@ TEST_F(ResourceTest, RegisterProviderMissingPrefix) {
|
||||
.close = close_nop,
|
||||
};
|
||||
|
||||
// install warning handler
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings("prefix");
|
||||
|
||||
int i = mjp_registerResourceProvider(&provider);
|
||||
|
||||
// warning message related to missing prefix
|
||||
EXPECT_THAT(warning, HasSubstr("prefix"));
|
||||
EXPECT_LT(i, 1);
|
||||
}
|
||||
|
||||
@@ -172,17 +159,10 @@ TEST_F(ResourceTest, RegisterProviderInvalidPrefix1) {
|
||||
.close = close_nop,
|
||||
};
|
||||
|
||||
// install warning handler
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings("prefix");
|
||||
|
||||
int i = mjp_registerResourceProvider(&provider);
|
||||
|
||||
// warning message related to missing prefix
|
||||
EXPECT_THAT(warning, HasSubstr("prefix"));
|
||||
EXPECT_LT(i, 1);
|
||||
}
|
||||
|
||||
@@ -194,17 +174,10 @@ TEST_F(ResourceTest, RegisterProviderInvalidPrefix2) {
|
||||
.close = close_nop,
|
||||
};
|
||||
|
||||
// install warning handler
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings("prefix");
|
||||
|
||||
int i = mjp_registerResourceProvider(&provider);
|
||||
|
||||
// warning message related to missing prefix
|
||||
EXPECT_THAT(warning, HasSubstr("prefix"));
|
||||
EXPECT_LT(i, 1);
|
||||
}
|
||||
|
||||
@@ -316,13 +289,8 @@ TEST_F(ResourceTest, NameWithValidPrefix) {
|
||||
int i = mjp_registerResourceProvider(&provider);
|
||||
EXPECT_GT(i, 0);
|
||||
|
||||
|
||||
// install warning handler
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings();
|
||||
|
||||
// open resource
|
||||
mjResource* resource = mju_openResource("", "nop:found", nullptr, nullptr, 0);
|
||||
@@ -342,13 +310,8 @@ TEST_F(ResourceTest, NameWithUpperCasePrefix) {
|
||||
int i = mjp_registerResourceProvider(&provider);
|
||||
EXPECT_GT(i, 0);
|
||||
|
||||
|
||||
// install warning handler
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings();
|
||||
|
||||
// open resource
|
||||
mjResource* resource = mju_openResource("", "NOP:found", nullptr, nullptr, 0);
|
||||
@@ -368,13 +331,8 @@ TEST_F(ResourceTest, NameWithInvalidPrefix) {
|
||||
int i = mjp_registerResourceProvider(&provider);
|
||||
EXPECT_GT(i, 0);
|
||||
|
||||
|
||||
// install warning handler
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings();
|
||||
|
||||
// open resource
|
||||
mjResource* resource = mju_openResource("", "nopfound", nullptr, nullptr, 0);
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "test/fixture.h"
|
||||
|
||||
namespace mujoco {
|
||||
@@ -160,7 +159,8 @@ TEST_F(UserUtilTest, StringToVectorInvalidNumber) {
|
||||
}
|
||||
|
||||
TEST_F(UserUtilTest, StringToVectorNan) {
|
||||
mju_user_warning = nullptr;
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings();
|
||||
auto v = StringToVector<double>("1 2 nan 3.21");
|
||||
EXPECT_THAT(v[2], IsNan());
|
||||
EXPECT_EQ(v[3], 3.21);
|
||||
|
||||
Reference in New Issue
Block a user