Rewrote to provide better feedback

Also allows variables as well as static text for assert text in
ut_assert
This commit is contained in:
Don Gagne 2014-03-27 13:05:03 -07:00
parent 521539897e
commit d5a7e7c52b
2 changed files with 41 additions and 55 deletions

View File

@ -32,17 +32,10 @@
* *
****************************************************************************/ ****************************************************************************/
/**
* @file unit_test.cpp
* A unit test library.
*
*/
#include "unit_test.h" #include "unit_test.h"
#include <systemlib/err.h> #include <systemlib/err.h>
UnitTest::UnitTest() UnitTest::UnitTest()
{ {
} }
@ -51,15 +44,15 @@ UnitTest::~UnitTest()
{ {
} }
void void UnitTest::printResults(void)
UnitTest::print_results(const char* result)
{ {
if (result != 0) { warnx(mu_tests_failed() ? "SOME TESTS FAILED" : "ALL TESTS PASSED");
warnx("Failed: %s:%d", mu_last_test(), mu_line()); warnx(" Tests passed : %d", mu_tests_passed());
warnx("%s", result); warnx(" Tests failed : %d", mu_tests_failed());
} else { warnx(" Assertions : %d", mu_assertion());
warnx("ALL TESTS PASSED"); }
warnx(" Tests run : %d", mu_tests_run());
warnx(" Assertion : %d", mu_assertion()); void UnitTest::printAssert(const char* msg, const char* test, const char* file, int line)
} {
warnx("Assertion failed: %s - %s (%s:%d)", msg, test, file, line);
} }

View File

@ -32,62 +32,55 @@
* *
****************************************************************************/ ****************************************************************************/
/**
* @file unit_test.h
* A unit test library based on MinUnit (http://www.jera.com/techinfo/jtns/jtn002.html).
*
*/
#ifndef UNIT_TEST_H_ #ifndef UNIT_TEST_H_
#define UNIT_TEST_ #define UNIT_TEST_H_
#include <systemlib/err.h> #include <systemlib/err.h>
class __EXPORT UnitTest class __EXPORT UnitTest
{ {
public: public:
#define xstr(s) str(s)
#define str(s) #s
#define INLINE_GLOBAL(type,func) inline type& func() { static type x; return x; } #define INLINE_GLOBAL(type,func) inline type& func() { static type x; return x; }
INLINE_GLOBAL(int, mu_tests_run) INLINE_GLOBAL(int, mu_tests_run)
INLINE_GLOBAL(int, mu_tests_failed)
INLINE_GLOBAL(int, mu_tests_passed)
INLINE_GLOBAL(int, mu_assertion) INLINE_GLOBAL(int, mu_assertion)
INLINE_GLOBAL(int, mu_line) INLINE_GLOBAL(int, mu_line)
INLINE_GLOBAL(const char*, mu_last_test) INLINE_GLOBAL(const char*, mu_last_test)
#define mu_assert(message, test) \
do \
{ \
if (!(test)) \
return __FILE__ ":" xstr(__LINE__) " " message " (" #test ")"; \
else \
mu_assertion()++; \
} while (0)
#define mu_run_test(test) \
do \
{ \
const char *message; \
mu_last_test() = #test; \
mu_line() = __LINE__; \
message = test(); \
mu_tests_run()++; \
if (message) \
return message; \
} while (0)
public:
UnitTest(); UnitTest();
virtual ~UnitTest(); virtual ~UnitTest();
virtual const char* run_tests() = 0; virtual void runTests(void) = 0;
virtual void print_results(const char* result); void printResults(void);
void printAssert(const char* msg, const char* test, const char* file, int line);
#define ut_assert(message, test) \
do { \
if (!(test)) { \
printAssert(message, #test, __FILE__, __LINE__); \
return false; \
} else { \
mu_assertion()++; \
} \
} while (0)
#define ut_run_test(test) \
do { \
warnx("RUNNING TEST: %s", #test); \
mu_tests_run()++; \
if (!test()) { \
warnx("TEST FAILED: %s", #test); \
mu_tests_failed()++; \
} else { \
warnx("TEST PASSED: %s", #test); \
mu_tests_passed()++; \
} \
} while (0)
}; };
#endif /* UNIT_TEST_H_ */ #endif /* UNIT_TEST_H_ */