Skip to content

Commit

Permalink
Add checks for exceptions (#19)
Browse files Browse the repository at this point in the history
Add TEST_CHECK_EXC and TEST_CHECK_EXC_ macros for testing, that given piece of code throws exception of specified type.
sakateka authored and mity committed Mar 6, 2019
1 parent dfb3b5b commit 3fc4f02
Showing 2 changed files with 62 additions and 2 deletions.
21 changes: 19 additions & 2 deletions examples/cpp-example.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include "acutest.h"

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


@@ -13,7 +13,7 @@ class TestException : public std::exception
virtual const char* what() const throw()
{ return msg_.c_str(); }

TestException(const std::string& msg) throw()
explicit TestException(const std::string& msg) throw()
: msg_(msg)
{}

@@ -33,9 +33,26 @@ void test_strange_exception(void)
throw "Acutest can also catch exceptions not derived from std::exception.";
}

void test_catch_exception_by_type(void)
{
TEST_CATCH_EXC(throw TestException("expected"), TestException);
}

void test_catch_unexpected_exception(void)
{
TEST_CATCH_EXC(throw std::invalid_argument("unexpected"), TestException);
}

void test_catch_nostd_exception(void)
{
TEST_CATCH_EXC_(test_strange_exception(), TestException, "invoke test_strange_exception()");
}

TEST_LIST = {
{ "std-exception", test_std_exception },
{ "strange-exception", test_strange_exception },
{ "expected-exception", test_catch_exception_by_type },
{ "unexpected-exception", test_catch_unexpected_exception },
{ "nostd-exception", test_catch_nostd_exception },
{ NULL, NULL }
};
43 changes: 43 additions & 0 deletions include/acutest.h
Original file line number Diff line number Diff line change
@@ -81,6 +81,49 @@
#define TEST_CHECK_(cond,...) test_check__((cond), __FILE__, __LINE__, __VA_ARGS__)
#define TEST_CHECK(cond) test_check__((cond), __FILE__, __LINE__, "%s", #cond)

/* Macros to verify that the code throws an exception. The exception type
* its passed as the second argument. TEST_CATCH_EXC_ behaves like TEST_CHECK_
* accept additional arguments for formatting the error message.
*
* TEST_CATCH_EXC(function_that_throw(), ExceptionType);
*
* If the function_that_throw throws ExceptionType, the test passes
*/
#ifdef __cplusplus
#define TEST_CATCH_EXC(code, exctype) \
do { \
bool exc_ok__ = false; \
const char *msg__ = NULL; \
try { \
code; \
msg__ = "No exception thrown."; \
} catch(const exctype &) { \
exc_ok__= true; \
} catch(...) { \
msg__ = "Unexpected exception thrown."; \
} \
test_check__(exc_ok__, __FILE__, __LINE__, #code " throws " #exctype); \
if(msg__ != NULL) \
test_message__("%s", msg__); \
} while(0)
#define TEST_CATCH_EXC_(code, exctype, ...) \
do { \
bool exc_ok__ = false; \
const char *msg__ = NULL; \
try { \
code; \
msg__ = "No exception thrown."; \
} catch(const exctype &) { \
exc_ok__= true; \
} catch(...) { \
msg__ = "Unexpected exception thrown."; \
} \
test_check__(exc_ok__, __FILE__, __LINE__, __VA_ARGS__); \
if(msg__ != NULL) \
test_message__("%s", msg__); \
} while(0)
#endif


/* Sometimes it is useful to split execution of more complex unit tests to some
* smaller parts and associate those parts with some names.

0 comments on commit 3fc4f02

Please sign in to comment.