Core Library
Namespaces | Defines | Typedefs | Functions
UnitTest.hpp File Reference

Macros and functions for writing simple unit tests. More...

#include <set>
#include "tiostream.hpp"
#include "StringUtils.hpp"
#include <cstdlib>

Namespaces

namespace  Core
 

The Core Library namespace.


Defines

#define TEST_TRUE(x)
 Test that the expression result is true.
#define TEST_FALSE(x)
 Test that the expression result is false.
#define TEST_THROWS(x)
 Test that the expression casuses an exception.
#define TEST_PASSED(r)   Core::processAssertResult(__FILE__, __LINE__, TXT(r), true);
 Mark that the test has passed for the specified reason.
#define TEST_FAILED(r)   Core::processAssertResult(__FILE__, __LINE__, TXT(r), false);
 Mark that the test has failed for the specified reason.
#define TEST_SUITE(c, v)
 Test suite preparation.
#define TEST_SUITE_END
 Test suite reporting and cleanup.
#define TEST_SUITE_RUN()
 Run the self-registering test sets.
#define TEST_SUITE_MAIN(c, v)
 Handle the entire setup, execution and reporting of the test suite.
#define TEST_SET(t)
 Define a set of test cases.
#define TEST_SET_END
 End the test set definition.
#define TEST_CASE_SETUP()
 Define the test case setup function.
#define TEST_CASE_SETUP_END
 End the test case setup function definition.
#define TEST_CASE_TEARDOWN()
 Define the test case teardown function.
#define TEST_CASE_TEARDOWN_END
 End the test case teardown function definition.
#define TEST_CASE(t)
 Define a test case.
#define TEST_CASE_END
 End the test case definition.

Typedefs

typedef std::set< tstringCore::TestSetFilters
 A collection of test suite names.
typedef void(* Core::TestSetFn )()
 The Test Set run function type.
typedef void(* Core::TestCaseSetUpFn )()
 The test case SetUp function type.
typedef void(* Core::TestCaseTearDownFn )()
 The test case TearDown function type.

Functions

 __declspec (dllimport) void __stdcall DebugBreak()
bool Core::parseCmdLine (int argc, tchar *argv[], TestSetFilters &filters)
 Parse the command line.
bool Core::registerTestSet (const tchar *name, TestSetFn runner)
 Register a test set runner function.
bool Core::runTestSets (const TestSetFilters &filters)
 Run the self-registering test sets.
void Core::onStartTestSet (const tchar *name)
 Start the test set.
void Core::onEndTestSet ()
 End the test set.
void Core::defineTestCaseSetup (TestCaseSetUpFn setup)
 Define the test case setup function.
void Core::defineTestCaseTearDown (TestCaseTearDownFn teardown)
 Define the test case teardown function.
bool Core::onStartTestCase (const tchar *name)
 Start a new test case.
void Core::onEndTestCase ()
 End a test case.
void Core::processAssertResult (const char *file, size_t line, const tchar *expression, bool passed)
 Write the assert result to stdout.
void Core::processTestException (const char *file, size_t line, const tchar *error)
 Process an unexpected exception running a test case.
void Core::processSetupTeardownException (const tchar *function, const tchar *error)
 Process an unexpected exception during setup or teardown.
void Core::setTestRunFinalStatus (bool successful)
 Set how the test run completed.
void Core::writeTestsSummary ()
 Write the summary of the test results to the debugger stream and stdout.
int Core::getTestProcessResult ()
 Get the test process result code.

Detailed Description

Macros and functions for writing simple unit tests.

Author:
Chris Oldwood

Define Documentation

#define TEST_TRUE (   x)
Value:
try {                                                                       \
                            if (x)  Core::processAssertResult(__FILE__, __LINE__, TXT(#x), true);   \
                            else    Core::processAssertResult(__FILE__, __LINE__, TXT(#x), false);  \
                        } catch(const Core::Exception& e) {                                         \
                            Core::debugWrite(TXT("Unhandled Exception: %s\n"), e.twhat());          \
                            Core::processAssertResult(__FILE__, __LINE__, TXT(#x), false);          \
                        } catch(...) {                                                              \
                            Core::debugWrite(TXT("Unhandled Exception: %s\n"), TXT("UNKNOWN"));     \
                            Core::processAssertResult(__FILE__, __LINE__, TXT(#x), false);          \
                        }

Test that the expression result is true.

#define TEST_FALSE (   x)
Value:
try {                                                                       \
                            if (x)  Core::processAssertResult(__FILE__, __LINE__, TXT(#x), false);  \
                            else    Core::processAssertResult(__FILE__, __LINE__, TXT(#x), true);   \
                        } catch(const Core::Exception& e) {                                         \
                            Core::debugWrite(TXT("Unhandled Exception: %s\n"), e.twhat());          \
                            Core::processAssertResult(__FILE__, __LINE__, TXT(#x), false);          \
                        } catch(...) {                                                              \
                            Core::debugWrite(TXT("Unhandled Exception: %s\n"), TXT("UNKNOWN"));     \
                            Core::processAssertResult(__FILE__, __LINE__, TXT(#x), false);          \
                        }

Test that the expression result is false.

#define TEST_THROWS (   x)
Value:
try {                                                                   \
                            (x);                                                                \
							Core::processAssertResult(__FILE__, __LINE__, TXT(#x), false);       \
                        } catch(const Core::Exception& e) {                                     \
							Core::debugWrite(TXT("Thrown: %s\n"), e.twhat());                    \
							Core::processAssertResult(__FILE__, __LINE__, TXT(#x), true);        \
                        } catch(const std::exception& e) {                                      \
							Core::debugWrite(TXT("Thrown: %hs\n"), e.what());                    \
							Core::processAssertResult(__FILE__, __LINE__, TXT(#x), true);        \
                        } catch(...) {                                                          \
							Core::debugWrite(TXT("Unhandled Exception: %s\n"), TXT("UNKNOWN"));  \
							Core::processAssertResult(__FILE__, __LINE__, TXT(#x), false);       \
                        }

Test that the expression casuses an exception.

#define TEST_PASSED (   r)    Core::processAssertResult(__FILE__, __LINE__, TXT(r), true);

Mark that the test has passed for the specified reason.

#define TEST_FAILED (   r)    Core::processAssertResult(__FILE__, __LINE__, TXT(r), false);

Mark that the test has failed for the specified reason.

#define TEST_SUITE (   c,
 
)
Value:
Core::TestSetFilters filters;           \
                            if (!Core::parseCmdLine(c, v, filters)) \
                                return EXIT_FAILURE;                \
							Core::enableLeakReporting(true);     \
                            try {

Test suite preparation.

#define TEST_SUITE_END
Value:
Core::setTestRunFinalStatus(true);                                      \
                            }                                                                           \
                            catch(const Core::Exception& e) {                                           \
								Core::debugWrite(TXT("Unhandled Exception: %s\n"), e.twhat());          \
                                if (::IsDebuggerPresent()) ::DebugBreak();                              \
                                tcout << TXT("Unhandled Exception: ") << e.twhat() << std::endl;        \
                            } catch(...) {                                                              \
								Core::debugWrite(TXT("Unhandled Exception: %s\n"), TXT("UNKNOWN"));     \
                                if (::IsDebuggerPresent()) ::DebugBreak();                              \
                                tcout << TXT("Unhandled Exception: UNKNOWN") << std::endl;              \
                            }                                                                           \
							Core::writeTestsSummary();                                                   \
                            return Core::getTestProcessResult();

Test suite reporting and cleanup.

#define TEST_SUITE_RUN ( )
Value:
if (!Core::runTestSets(filters))    \
                                return EXIT_FAILURE;

Run the self-registering test sets.

#define TEST_SUITE_MAIN (   c,
 
)
Value:
TEST_SUITE(argc, argv)      \
                                {                           \
                                    TEST_SUITE_RUN();       \
                                }                           \
                                TEST_SUITE_END

Handle the entire setup, execution and reporting of the test suite.

#define TEST_SET (   t)
Value:
static void _##t##_TEST_SET();                                              \
                            static bool registered = Core::registerTestSet(TXT(#t), _##t##_TEST_SET);   \
                            static void _##t##_TEST_SET()                                               \
                            {                                                                           \
							Core::onStartTestSet(TXT(#t));

Define a set of test cases.

#define TEST_SET_END
Value:

End the test set definition.

#define TEST_CASE_SETUP ( )
Value:
struct TestCaseSetup {          \
                                    static void fn()

Define the test case setup function.

Value:
};                              \
								Core::defineTestCaseSetup(TestCaseSetup::fn);

End the test case setup function definition.

#define TEST_CASE_TEARDOWN ( )
Value:
struct TestCaseTearDown {           \
                                    static void fn()

Define the test case teardown function.

Value:
};                              \
								Core::defineTestCaseTearDown(TestCaseTearDown::fn);

End the test case teardown function definition.

#define TEST_CASE (   t)
Value:
{                                                                           \
                            if (Core::onStartTestCase(TXT(t)))  {                                       \
                                try {

Define a test case.

#define TEST_CASE_END
Value:
}                                                                       \
                                catch(const Core::Exception& e) {                                       \
									Core::debugWrite(TXT("Thrown: %s\n"), e.twhat());                  \
									Core::processTestException(__FILE__, __LINE__, e.twhat());         \
                                } catch(...) {                                                          \
									Core::debugWrite(TXT("Unhandled Exception: %s\n"), TXT("UNKNOWN"));    \
									Core::processTestException(__FILE__, __LINE__, TXT("UNKNOWN"));        \
                                }                                                                       \
                            }                                                                           \
							Core::onEndTestCase();                                                       \
                            }

End the test case definition.


Function Documentation

__declspec ( dllimport  )