Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for ISystemReset in test fixture #2647

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/gz/sim/TestFixture.hh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class GZ_SIM_VISIBLE TestFixture
public: TestFixture &OnPostUpdate(std::function<void(
const UpdateInfo &, const EntityComponentManager &)> _cb);

/// \brief Wrapper around a system's update callback
/// \param[in] _cb Function to be called every update
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
/// \return Reference to self.
public: TestFixture &OnReset(std::function<void(
const UpdateInfo &, EntityComponentManager &)> _cb);

/// \brief Finalize all the functions and add fixture to server.
/// Finalize must be called before running the server, otherwise none of the
/// `On*` functions will be called.
Expand Down
11 changes: 11 additions & 0 deletions python/src/gz/sim/TestFixture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ defineSimTestFixture(pybind11::object module)
),
pybind11::return_value_policy::reference,
"Wrapper around a system's post-update callback"
)
.def(
"on_reset", WrapCallbacks(
[](TestFixture* self, std::function<void(
const UpdateInfo &, EntityComponentManager &)> _cb)
{
self->OnReset(_cb);
}
),
pybind11::return_value_policy::reference,
"Wrapper around a system's post-update callback"
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
);
// TODO(ahcorde): This method is not compiling for the following reason:
// The EventManager class has an unordered_map which holds a unique_ptr
Expand Down
29 changes: 28 additions & 1 deletion src/TestFixture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class HelperSystem :
public ISystemConfigure,
public ISystemPreUpdate,
public ISystemUpdate,
public ISystemPostUpdate
public ISystemPostUpdate,
public ISystemReset
{
// Documentation inherited
public: void Configure(
Expand All @@ -50,6 +51,10 @@ class HelperSystem :
public: void PostUpdate(const UpdateInfo &_info,
const EntityComponentManager &_ecm) override;

// Documentation inherited
public: void Reset(const UpdateInfo &_info,
EntityComponentManager &_ecm) override;

/// \brief Function to call every time we configure a world
public: std::function<void(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
Expand All @@ -68,6 +73,10 @@ class HelperSystem :
/// \brief Function to call every post-update
public: std::function<void(const UpdateInfo &,
const EntityComponentManager &)> postUpdateCallback;

/// \brief Reset callback
public: std::function<void(const UpdateInfo &,
EntityComponentManager &)> resetCallback;
};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -105,6 +114,14 @@ void HelperSystem::PostUpdate(const UpdateInfo &_info,
this->postUpdateCallback(_info, _ecm);
}

/////////////////////////////////////////////////
void HelperSystem::Reset(const UpdateInfo &_info,
EntityComponentManager &_ecm)
{
if (this->resetCallback)
this->resetCallback(_info, _ecm);
}

//////////////////////////////////////////////////
class gz::sim::TestFixture::Implementation
{
Expand Down Expand Up @@ -200,6 +217,16 @@ TestFixture &TestFixture::OnPostUpdate(std::function<void(
return *this;
}

//////////////////////////////////////////////////
TestFixture &TestFixture::OnReset(std::function<void(
const UpdateInfo &, EntityComponentManager &)> _cb)
{
if (nullptr != this->dataPtr->helperSystem)
this->dataPtr->helperSystem->resetCallback = std::move(_cb);
return *this;
}


arjo129 marked this conversation as resolved.
Show resolved Hide resolved
//////////////////////////////////////////////////
std::shared_ptr<Server> TestFixture::Server() const
{
Expand Down
Loading