forked from Archive/PX4-Autopilot
Rewrote to provide better feedback
Also allows variables as well as static text for assert text in ut_assert
This commit is contained in:
parent
521539897e
commit
d5a7e7c52b
|
@ -32,17 +32,10 @@
|
|||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file unit_test.cpp
|
||||
* A unit test library.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "unit_test.h"
|
||||
|
||||
#include <systemlib/err.h>
|
||||
|
||||
|
||||
UnitTest::UnitTest()
|
||||
{
|
||||
}
|
||||
|
@ -51,15 +44,15 @@ UnitTest::~UnitTest()
|
|||
{
|
||||
}
|
||||
|
||||
void
|
||||
UnitTest::print_results(const char* result)
|
||||
void UnitTest::printResults(void)
|
||||
{
|
||||
if (result != 0) {
|
||||
warnx("Failed: %s:%d", mu_last_test(), mu_line());
|
||||
warnx("%s", result);
|
||||
} else {
|
||||
warnx("ALL TESTS PASSED");
|
||||
warnx(" Tests run : %d", mu_tests_run());
|
||||
warnx(" Assertion : %d", mu_assertion());
|
||||
warnx(mu_tests_failed() ? "SOME TESTS FAILED" : "ALL TESTS PASSED");
|
||||
warnx(" Tests passed : %d", mu_tests_passed());
|
||||
warnx(" Tests failed : %d", mu_tests_failed());
|
||||
warnx(" Assertions : %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);
|
||||
}
|
||||
|
|
|
@ -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_
|
||||
#define UNIT_TEST_
|
||||
#define UNIT_TEST_H_
|
||||
|
||||
#include <systemlib/err.h>
|
||||
|
||||
|
||||
class __EXPORT UnitTest
|
||||
{
|
||||
|
||||
public:
|
||||
#define xstr(s) str(s)
|
||||
#define str(s) #s
|
||||
#define INLINE_GLOBAL(type,func) inline type& func() { static type x; return x; }
|
||||
|
||||
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_line)
|
||||
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();
|
||||
virtual ~UnitTest();
|
||||
|
||||
virtual const char* run_tests() = 0;
|
||||
virtual void print_results(const char* result);
|
||||
virtual void runTests(void) = 0;
|
||||
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_ */
|
||||
|
|
Loading…
Reference in New Issue