Jon Test
C++ Unit Test Tool. Not for production -- sample code only
Loading...
Searching...
No Matches
assertException_test.cpp

Actual test of assertException().

Actual test of assertException().

#include "JonTest/Assert.h"
#include <exception>
#include <string>
class MyTestException
: public std::exception
{
public:
std::string info;
inline virtual ~MyTestException()
{
// Nothing to do
}
inline explicit MyTestException(const char* info = "(MyTestException)") noexcept
:info(info)
{
// Nothing to do
}
inline virtual const char* what() const noexcept
{
return info.c_str();
}
};
void doNothing()
{
// Nothing to do
}
TEST_SUITE(AssertException)
TEST_CASE(jontest_fail_exception)
{
// This WILL fail the test to demonstrate feature, and will be monitored externally
throw MyTestException();
}
TEST_CASE(expected_assertException)
{
assertException(throw MyTestException(), MyTestException, "Failed to catch the exception!");
assertException(throw MyTestException(), std::exception, "Failed to catch the exception!");
}
TEST_CASE(jontest_fail_assertExceptionWrong)
{
// TestCase::run() will always catch (...), so a wrong exception cannot be an "expectedFailure()"
// This WILL fail the test to demonstrate feature, and will be monitored externally
assertException(throw int(5), MyTestException, "Failed to catch the int exception!")
}
TEST_CASE(did_not_assertException)
{
EXPECTED_FAILURE(assertException(doNothing(), MyTestException, "Failed to throw an exception!"), "No exception thrown, so test failed");
}
#define assertException(expression, expected, message)
Fail this test if (expression) does not throw the expected exception or a derived class exception wit...
Definition Assert.h:69
#define EXPECTED_FAILURE(expression, message)
Fail this test if evaluating (expression) does not result itself in a failed test.
Definition Assert.h:83
#define TEST_SUITE_END()
Finish definition of a Test Suite started with TEST_SUITE().
Definition TestSuite.h:77
#define TEST_CASE(testCaseName)
Defines a Test Case method named testCaseName within a Test Suite defined by TEST_SUITE().
Definition TestSuite.h:91
#define TEST_SUITE(testSuiteName)
Defines a Test Suite named testSuiteName.
Definition TestSuite.h:63