Skip to content

Commit

Permalink
Use custom exceptions for better testing
Browse files Browse the repository at this point in the history
Signed-off-by: Zachary Michaels <[email protected]>
  • Loading branch information
zmichaels11 committed Jan 23, 2020
1 parent ca530ce commit e378655
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ include_directories(include)
ament_export_include_directories(include)

add_library(${PROJECT_NAME}
src/asserts.cpp
src/find_library.cpp)
target_include_directories(${PROJECT_NAME}
PUBLIC
Expand All @@ -47,10 +48,12 @@ if(BUILD_TESTING)
ament_lint_auto_find_test_dependencies()

ament_add_gtest(test_asserts_ndebug test/test_asserts.cpp)
target_link_libraries(test_asserts_ndebug ${PROJECT_NAME})

target_compile_definitions(test_asserts_ndebug PUBLIC NDEBUG)

ament_add_gtest(test_asserts_debug test/test_asserts.cpp)
target_link_libraries(test_asserts_debug ${PROJECT_NAME})

ament_add_gtest(test_thread_safety_annotations test/test_thread_safety_annotations.cpp)

Expand Down
34 changes: 29 additions & 5 deletions include/rcpputils/asserts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,44 @@
#ifndef RCPPUTILS__ASSERTS_HPP_
#define RCPPUTILS__ASSERTS_HPP_

#include <stdexcept>
#include <exception>
#include <string>

namespace rcpputils
{

class AssertionException : public std::exception
{
std::string msg_;

public:
explicit AssertionException(const std::string & msg)
: msg_{msg} {}

virtual const char * what() const throw();
};

class IllegalStateException : public std::exception
{
std::string msg_;

public:
explicit IllegalStateException(const std::string & msg)
: msg_{msg} {}

virtual const char * what() const throw();
};

/**
* Checks that a state condition passes.
*
* \param condition
* \throw std::runtime_error if the condition is not met.
* \throw rcpputils::IllegalStateException if the condition is not met.
*/
inline void check_true(bool condition)
{
if (!condition) {
throw std::runtime_error{"Check reported invalid state!"};
throw rcpputils::IllegalStateException{"Check reported invalid state!"};
}
}

Expand All @@ -37,14 +61,14 @@ inline void check_true(bool condition)
*
* This function behaves similar to assert, except that it throws instead of invoking abort().
* \param condition
* \throw std::runtime_error if the macro NDEBUG is not set and the condition is not met.
* \throw rcpputils::AssertionException if the macro NDEBUG is not set and the condition is not met.
*/
inline void assert_true(bool condition)
{
// Same macro definition used by cassert
#ifndef NDEBUG
if (!condition) {
throw std::runtime_error{"Assertion failed!"};
throw rcpputils::AssertionException{"Assertion failed!"};
}
#else
(void) condition;
Expand Down
28 changes: 28 additions & 0 deletions src/asserts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// 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.

#include "rcpputils/asserts.hpp"

namespace rcpputils
{
const char * AssertionException::what() const throw()
{
return msg_.c_str();
}

const char * IllegalStateException::what() const throw()
{
return msg_.c_str();
}
} // namespace rcpputils
4 changes: 2 additions & 2 deletions test/test_asserts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "rcpputils/asserts.hpp"

TEST(test_asserts, check_throws_if_condition_is_false) {
EXPECT_THROW(rcpputils::check_true(false), std::runtime_error);
EXPECT_THROW(rcpputils::check_true(false), rcpputils::IllegalStateException);
}

TEST(test_asserts, check_does_not_throw_if_condition_is_true) {
Expand All @@ -28,7 +28,7 @@ TEST(test_asserts, check_does_not_throw_if_condition_is_true) {

#ifndef NDEBUG
TEST(test_asserts, ros_assert_throws_if_condition_is_false_and_ndebug_not_set) {
EXPECT_THROW(rcpputils::assert_true(false), std::runtime_error);
EXPECT_THROW(rcpputils::assert_true(false), rcpputils::AssertionException);
}

TEST(test_asserts, ros_assert_does_not_throw_if_condition_is_true_and_ndebug_not_set)
Expand Down

0 comments on commit e378655

Please sign in to comment.