-
Notifications
You must be signed in to change notification settings - Fork 542
EXPECT_THROW_EQ and ASSERT_THROW_EQ macros for unittests #1232
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef UTILS__GTEST_MACROS_HPP_ | ||
| #define UTILS__GTEST_MACROS_HPP_ | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| #include <cstring> | ||
| #include <type_traits> | ||
|
|
||
| #include "rclcpp/exceptions/exceptions.hpp" | ||
|
|
||
| namespace rclcpp | ||
| { | ||
| namespace testing | ||
| { | ||
| namespace details | ||
| { | ||
|
|
||
| /** | ||
| * \brief Check if two thrown objects are equals. | ||
| * | ||
| * For generic thrown objects, probably is unlikely to be used. This type must | ||
| * overload the == and << operators. | ||
| */ | ||
| template<typename T, | ||
| typename = typename std::enable_if_t< | ||
| !std::is_convertible<T, std::exception>::value>> | ||
| ::testing::AssertionResult AreThrowableContentsEqual( | ||
| const T & expected, const T & actual, const char * expected_exception_expression, | ||
| const char * throwing_expression) | ||
| { | ||
| if (expected == actual) { | ||
| return ::testing::AssertionSuccess() << | ||
| "'\nThe value of the non-standard throwable thrown by the expression\n'" << | ||
| throwing_expression << "'\n\nmatches the value of the expected thrown object\n'" << | ||
| expected_exception_expression << "'\n\t(" << expected << " == " << actual << ")\n"; | ||
| } | ||
|
|
||
| return ::testing::AssertionFailure() << | ||
| "\nThe value of the non-standard throwable thrown by the expression\n'" << | ||
| throwing_expression << "'\n\ndoes not match the value of the expected thrown object\n'" << | ||
| expected_exception_expression << "'\n\t(" << expected << " != " << actual << ")\n"; | ||
| } | ||
|
|
||
| /** | ||
| * \brief Check if two std::exceptions are equal according to their message. | ||
| * | ||
| * If the exception type also derives from rclcpp::Exception, then the next overload is called | ||
| * instead | ||
| */ | ||
| template<typename T, | ||
| typename = typename std::enable_if_t< | ||
| !std::is_convertible<T, rclcpp::exceptions::RCLErrorBase>::value>> | ||
| ::testing::AssertionResult AreThrowableContentsEqual( | ||
| const std::exception & expected, const std::exception & actual, | ||
| const char * expected_exception_expression, | ||
| const char * throwing_expression) | ||
| { | ||
| if (std::strcmp(expected.what(), actual.what()) == 0) { | ||
| return ::testing::AssertionSuccess() << | ||
| "'\nThe contents of the std::exception thrown by the expression\n'" << | ||
| throwing_expression << "':\n\te.what(): '" << actual.what() << | ||
| "'\n\nmatch the contents of the expected std::exception\n'" << | ||
| expected_exception_expression << "'\n\te.what(): '" << expected.what() << "'\n"; | ||
| } | ||
|
|
||
| return ::testing::AssertionFailure() << | ||
| "\nThe contents of the std::exception thrown by the expression\n'" << | ||
| throwing_expression << "':\n\te.what(): '" << actual.what() << | ||
| "'\n\ndo not match the contents of the expected std::exception\n'" << | ||
| expected_exception_expression << "'\n\te.what(): '" << expected.what() << "'\n"; | ||
| } | ||
|
|
||
| /** | ||
| * \brief Check if two exceptions that derive from rclcpp::RCLErrorBase are equal. | ||
| * | ||
| * This checks equality based on their return and message. It does not check the formatted | ||
| * message, which is what is reported by std::exception::what() for RCLErrors. | ||
| */ | ||
| template<typename T, | ||
| typename = typename std::enable_if_t< | ||
| std::is_convertible<T, rclcpp::exceptions::RCLErrorBase>::value>> | ||
| ::testing::AssertionResult AreThrowableContentsEqual( | ||
| const rclcpp::exceptions::RCLErrorBase & expected, | ||
| const rclcpp::exceptions::RCLErrorBase & actual, | ||
| const char * expected_exception_expression, | ||
| const char * throwing_expression) | ||
| { | ||
| if ((expected.ret == actual.ret) && (expected.message == actual.message)) { | ||
| return ::testing::AssertionSuccess() << | ||
| "'\nThe contents of the RCLError thrown by the expression\n'" << throwing_expression << | ||
| "':\n\trcl_ret_t: " << actual.ret << "\n\tmessage: '" << actual.message << | ||
| "'\n\nmatch the contents of the expected RCLError\n'" << | ||
| expected_exception_expression << "'\n\trcl_ret_t: " << expected.ret << | ||
| "\n\tmessage: '" << expected.message << "'\n"; | ||
| } | ||
|
|
||
| return ::testing::AssertionFailure() << | ||
| "'\nThe contents of the RCLError thrown by the expression\n'" << throwing_expression << | ||
| "':\n\trcl_ret_t: " << actual.ret << "\n\tmessage: '" << actual.message << | ||
| "'\n\ndo not match the contents of the expected RCLError\n'" << | ||
| expected_exception_expression << "'\n\trcl_ret_t: " << expected.ret << "\n\tmessage: '" << | ||
| expected.message << "'\n"; | ||
| } | ||
|
|
||
| } // namespace details | ||
| } // namespace testing | ||
| } // namespace rclcpp | ||
|
|
||
| /** | ||
| * \def CHECK_THROW_EQ_IMPL | ||
| * \brief Implemented check if statement throws expected exception. don't use directly, use | ||
| * EXPECT_THROW_EQ or ASSERT_THROW_EQ instead. | ||
| */ | ||
| #define CHECK_THROW_EQ_IMPL(throwing_statement, expected_exception, assertion_result) \ | ||
| do { \ | ||
| using ExceptionT = decltype(expected_exception); \ | ||
| try { \ | ||
| throwing_statement; \ | ||
| assertion_result = ::testing::AssertionFailure() << \ | ||
| "\nExpected the expression:\n\t'" #throwing_statement "'\nto throw: \n\t'" << \ | ||
| #expected_exception "'\nbut it did not throw.\n"; \ | ||
| } catch (const ExceptionT & e) { \ | ||
| assertion_result = \ | ||
| rclcpp::testing::details::AreThrowableContentsEqual<ExceptionT>( \ | ||
| expected_exception, e, #expected_exception, #throwing_statement); \ | ||
| } catch (const std::exception & e) { \ | ||
| assertion_result = ::testing::AssertionFailure() << \ | ||
| "\nExpected the expression:\n\t'" #throwing_statement "'\nto throw: \n\t'" << \ | ||
| #expected_exception "'\nbut it threw:\n\tType: " << typeid(e).name() << \ | ||
| "\n\te.what(): '" << e.what() << "'\n"; \ | ||
| } catch (...) { \ | ||
| assertion_result = ::testing::AssertionFailure() << \ | ||
| "\nExpected the expression:\n\t'" #throwing_statement "'\nto throw: \n\t'" << \ | ||
| #expected_exception "'\nbut it threw an unrecognized throwable type.\n"; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| /** | ||
| * \def EXPECT_THROW_EQ | ||
| * \brief Check if a statement throws the expected exception type and that the exceptions matches | ||
| * the expected exception. | ||
| * | ||
| * Like other gtest EXPECT_ macros, this doesn't halt a test and return on failure. Instead it | ||
| * just adds a failure to the current test. | ||
| * | ||
| * See test_gtest_macros.cpp for examples | ||
| */ | ||
| #define EXPECT_THROW_EQ(throwing_statement, expected_exception) \ | ||
| do { \ | ||
| ::testing::AssertionResult \ | ||
| is_the_result_of_the_throwing_expression_equal_to_the_expected_throwable = \ | ||
| ::testing::AssertionSuccess(); \ | ||
| CHECK_THROW_EQ_IMPL( \ | ||
| throwing_statement, \ | ||
| expected_exception, \ | ||
| is_the_result_of_the_throwing_expression_equal_to_the_expected_throwable); \ | ||
| EXPECT_TRUE(is_the_result_of_the_throwing_expression_equal_to_the_expected_throwable); \ | ||
| } while (0) | ||
|
|
||
| /** | ||
| * \def ASSERT_THROW_EQ | ||
| * \brief Assert that a statement throws the expected exception type and that the exceptions | ||
| * matches the expected exception. | ||
| * | ||
| * See test_gtest_macros.cpp for examples | ||
| * | ||
| * Like other gtest ASSERT_ macros, this will halt the test on failure and return. | ||
| */ | ||
| #define ASSERT_THROW_EQ(throwing_statement, expected_exception) \ | ||
| do { \ | ||
| ::testing::AssertionResult \ | ||
| is_the_result_of_the_throwing_expression_equal_to_the_expected_throwable = \ | ||
| ::testing::AssertionSuccess(); \ | ||
| CHECK_THROW_EQ_IMPL( \ | ||
| throwing_statement, \ | ||
| expected_exception, \ | ||
| is_the_result_of_the_throwing_expression_equal_to_the_expected_throwable); \ | ||
| ASSERT_TRUE(is_the_result_of_the_throwing_expression_equal_to_the_expected_throwable); \ | ||
| } while (0) | ||
|
|
||
| #endif // UTILS__GTEST_MACROS_HPP_ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just out of curiousity, why do you think this is going to be unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, all code in rclcpp code either throws a std::exception or an rclcpp::exception type. Seems very unlikely that someone would throw something that doesn't derive from either.