Skip to content

Commit

Permalink
Add ScopeExit
Browse files Browse the repository at this point in the history
Signed-off-by: Addisu Z. Taddese <[email protected]>
  • Loading branch information
azeey committed Apr 23, 2021
1 parent 9592c00 commit eebf49b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions test/integration/deprecated_specs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ TEST(DeprecatedElements, CanEmitWarningWithErrorEnforcmentPolicy)

#ifdef _WIN32
sdf::Console::Instance()->SetQuiet(false);
sdf::testing::ScopeExit revertSetQuiet(
[]
{
sdf::Console::Instance()->SetQuiet(true);
});
#endif

sdf::SDFPtr sdf(new sdf::SDF());
Expand Down Expand Up @@ -123,7 +128,4 @@ TEST(DeprecatedElements, CanEmitWarningWithErrorEnforcmentPolicy)
ASSERT_FALSE(errors.empty());
EXPECT_EQ(sdf::ErrorCode::ELEMENT_DEPRECATED, errors[0].Code());
}
#ifdef _WIN32
sdf::Console::Instance()->SetQuiet(true);
#endif
}
21 changes: 21 additions & 0 deletions test/test_utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ namespace sdf
namespace testing
{

/// \brief Calls a function when going out of scope.
/// Taken from:
/// https://github.com/ros2/rclcpp/blob/master/rclcpp/include/rclcpp/scope_exit.hpp
template <typename Callable>
struct ScopeExit
{
/// \brief Constructor
/// \param[in] _callable Any callable object that does not throw.
explicit ScopeExit(Callable _callable)
: callable(_callable)
{
}

~ScopeExit()
{
this->callable();
}

private: Callable callable;
};

/// \brief A class used for redirecting the output of sdferr, sdfwarn, etc to a
/// more convenient stream object like a std::stringstream for testing purposes.
/// The class reverts to the original stream object when it goes out of scope.
Expand Down

0 comments on commit eebf49b

Please sign in to comment.