From eebf49b92f85392d042044151aeb7206c18cf079 Mon Sep 17 00:00:00 2001 From: "Addisu Z. Taddese" Date: Fri, 23 Apr 2021 18:03:32 -0500 Subject: [PATCH] Add ScopeExit Signed-off-by: Addisu Z. Taddese --- test/integration/deprecated_specs.cc | 8 +++++--- test/test_utils.hh | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/test/integration/deprecated_specs.cc b/test/integration/deprecated_specs.cc index e1bc4132e..9b162a24f 100644 --- a/test/integration/deprecated_specs.cc +++ b/test/integration/deprecated_specs.cc @@ -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()); @@ -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 } diff --git a/test/test_utils.hh b/test/test_utils.hh index 5c94627ba..150f70b60 100644 --- a/test/test_utils.hh +++ b/test/test_utils.hh @@ -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 +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.