Skip to content
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

add checks for exceptions #19

Merged
merged 10 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>


Expand All @@ -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)
{}

Expand All @@ -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
Expand Up @@ -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.
Expand Down