Refactorization: Moved code out of macro TEST_CASE

This commit is contained in:
PKEuS 2014-08-02 20:20:07 +02:00
parent d3501e77a8
commit bcf3a1e9e1
2 changed files with 16 additions and 6 deletions

View File

@ -20,6 +20,7 @@
#include "options.h" #include "options.h"
#include <iostream> #include <iostream>
#include <cstdio>
#include <list> #include <list>
std::ostringstream errout; std::ostringstream errout;
@ -73,15 +74,19 @@ TestFixture::TestFixture(const std::string &_name)
} }
bool TestFixture::runTest(const char testname[]) bool TestFixture::prepareTest(const char testname[])
{ {
// Check if tests should be executed
if (testToRun.empty() || testToRun == testname) { if (testToRun.empty() || testToRun == testname) {
// Tests will be executed - prepare them
++countTests; ++countTests;
if (quiet_tests) { if (quiet_tests) {
std::cout << '.'; std::putchar('.'); // Use putchar to write through redirection of std::cout/cerr
} else { } else {
std::cout << classname << "::" << testname << std::endl; std::cout << classname << "::" << testname << std::endl;
} }
_lib = Library();
currentTest = classname + "::" + testname;
return true; return true;
} }
return false; return false;
@ -232,7 +237,11 @@ void TestFixture::run(const std::string &str)
if (quiet_tests) { if (quiet_tests) {
std::cout << '\n' << classname << ':'; std::cout << '\n' << classname << ':';
} }
run(); if (quiet_tests) {
REDIRECT;
run();
} else
run();
} }
void TestFixture::warn(const char msg[]) void TestFixture::warn(const char msg[])

View File

@ -47,7 +47,7 @@ protected:
virtual void run() = 0; virtual void run() = 0;
bool runTest(const char testname[]); bool prepareTest(const char testname[]);
void assert_(const char *filename, unsigned int linenr, bool condition) const; void assert_(const char *filename, unsigned int linenr, bool condition) const;
void todoAssert(const char *filename, unsigned int linenr, bool condition) const; void todoAssert(const char *filename, unsigned int linenr, bool condition) const;
@ -65,6 +65,7 @@ protected:
long long current, long long actual) const; long long current, long long actual) const;
void assertThrowFail(const char *filename, unsigned int linenr) const; void assertThrowFail(const char *filename, unsigned int linenr) const;
void complainMissingLib(const char* libname) const; void complainMissingLib(const char* libname) const;
void processOptions(const options& args); void processOptions(const options& args);
public: public:
virtual void reportOut(const std::string &outmsg); virtual void reportOut(const std::string &outmsg);
@ -78,12 +79,12 @@ public:
static std::size_t runTests(const options& args); static std::size_t runTests(const options& args);
}; };
#define TEST_CASE( NAME ) if ( runTest(#NAME) ) { _lib = Library(); currentTest = classname + "::" + #NAME; if (quiet_tests) { REDIRECT; NAME(); } else { NAME ();} } #define TEST_CASE( NAME ) if ( prepareTest(#NAME) ) { NAME(); }
#define ASSERT( CONDITION ) assert_(__FILE__, __LINE__, CONDITION) #define ASSERT( CONDITION ) assert_(__FILE__, __LINE__, CONDITION)
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL) #define ASSERT_EQUALS( EXPECTED , ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
#define ASSERT_EQUALS_DOUBLE( EXPECTED , ACTUAL ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL) #define ASSERT_EQUALS_DOUBLE( EXPECTED , ACTUAL ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL)
#define ASSERT_EQUALS_MSG( EXPECTED , ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG) #define ASSERT_EQUALS_MSG( EXPECTED , ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
#define ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; assertThrowFail(__FILE__, __LINE__); } catch (EXCEPTION &) { } catch (...) { assertThrowFail(__FILE__, __LINE__); } #define ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&) { } catch (...) { assertThrowFail(__FILE__, __LINE__); }
#define TODO_ASSERT_EQUALS( WANTED , CURRENT , ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL) #define TODO_ASSERT_EQUALS( WANTED , CURRENT , ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL)
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance; } #define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance; }