1.6.2. The Solution

Disabling RTTI in mockpp has little consequences. It is only used for a few diagnostic messages. In the worst case you might have to search for the problematic class because the name is not displayed.

Disabling the regular STL is also no problem as far as mockpp is affected. If you use the minimalistic built-in STL for your own code you might miss the one or the other feature current implementations have. Either you find a workaround for this or you implement what is missing. There are also STL implementations available which are optimised for embedded systems.

The only real problem is exception handling. You can't simply turn his feature off and continue with the program flow instead of throwing. When throwing an exception an application is typically in a critical state and its objects tend to be unusable. Continuing to use such an object is likely to crash the application. The other problem is the fact that the compiler can't prepare a way back to destroy automatic variables. This causes resource leaks.

The only workaround so far is to immediately terminate the application. A compromise might be to start a thread for each single test case and terminate the thread upon the first malfunction. Probably this significantly slows down the test execution. Another possibility is to split the tests into portable and platform specific sections. The portable sections can be run on the development machine with high speed very often whereas the complete tests are run on the target device only if appropriate.

Without exceptions you have to provide a way to forward the messages about the malfunction. mockpp uses some macros to run the built-in tests with or without exceptions using the same source files. When exceptions are enabled theses macros contain what you probably expect:


#define MOCKPP_THROW(x)  throw (x)
#define MOCKPP_RETHROW   throw
#define MOCKPP_TRY       try
#define MOCKPP_CATCH(x)  catch(x)
#define MOCKPP_CATCH_ALL catch(...)

One way to react upon bad test results without exceptions is to call a method which prints the results to the screen and exits with an according error level. Since the container with the resulting data can vary you might want to use a template with specialisations. The following example shows how this could look like. The macros should occur before the inclusion of mockpp.h. Otherwise they are defined with empty content.

[Caution]Caution
Please note that the macro is located in source (*.cpp) files as well as in header files. So changing the macro between different test files or projects might not work as expected. It is intended as immutable glue between mockpp and your favourite unittest framework.

To relax this constraint there is a mutable function pointer which can be changed at runtime to actually process the exception data. All internal assertions use this approach. The default is to throw an exception. But by calling setAssertionFailedForwarder() appropriately it is possible to print a message to the screen and exit the application. Alternatively you may as well call the according collector of your unittest framework. E.g. CPPUNIT_FAIL for CppUnit or TS_FAIL for CxxTest.

The following listing shows a possible scenario without exceptions:


#define MOCKPP_THROW(x)  printMalfunctionData(x)
#define MOCKPP_RETHROW   printMalfunctionData("unexpected rethrow")

...

template <class T>
inline
void printMalfunctionData(const T & /* dummy fallback */)
{
  std::cout << "unknown exception occurred" << std::end;
  exit(1);
}

template <>
inline
void printMalfunctionData(const mockpp::AssertionFailedError &e)
{
  std::cout << e.getMessage() << std::end;
  exit(1);
}

template <>
inline
void printMalfunctionData(const std::exception &e)
{
  std::cout << e.what() << std::end;
  exit(1);
}

...

setAssertionFailedForwarder(printMalfunctionData<mockpp::AssertionFailedError>);

The test file tests/NoException_test.cpp shows a working solution. This file is designed to work under all library settings by using special types. Using types like int or std::string might not work with the settings on your machine.