Add group and run type to UnitTest

This commit is contained in:
Vinnie Falco
2013-07-20 07:17:40 -07:00
parent 46c6489fc8
commit 4893508348
14 changed files with 111 additions and 68 deletions

View File

@@ -129,7 +129,7 @@ void AbstractFifo::finishedRead (int numRead) noexcept
class AbstractFifoTests : public UnitTest class AbstractFifoTests : public UnitTest
{ {
public: public:
AbstractFifoTests() : UnitTest ("Abstract Fifo") AbstractFifoTests() : UnitTest ("Abstract Fifo", "beast")
{ {
} }

View File

@@ -21,8 +21,13 @@
*/ */
//============================================================================== //==============================================================================
UnitTest::UnitTest (const String& name_) UnitTest::UnitTest (String const& name,
: name (name_), runner (nullptr) String const& group,
When when)
: m_name (name)
, m_group (group)
, m_when (when)
, m_runner (nullptr)
{ {
getAllTests().add (this); getAllTests().add (this);
} }
@@ -32,19 +37,25 @@ UnitTest::~UnitTest()
getAllTests().removeFirstMatchingValue (this); getAllTests().removeFirstMatchingValue (this);
} }
Array<UnitTest*>& UnitTest::getAllTests() UnitTest::TestList& UnitTest::getAllTests()
{ {
static Array<UnitTest*> tests; static TestList s_tests;
return tests;
return s_tests;
} }
void UnitTest::initialise() {} void UnitTest::initialise()
void UnitTest::shutdown() {}
void UnitTest::performTest (UnitTests* const runner_)
{ {
bassert (runner_ != nullptr); }
runner = runner_;
void UnitTest::shutdown()
{
}
void UnitTest::performTest (UnitTests* const runner)
{
bassert (runner != nullptr);
m_runner = runner;
initialise(); initialise();
runTest(); runTest();
@@ -53,23 +64,24 @@ void UnitTest::performTest (UnitTests* const runner_)
void UnitTest::logMessage (const String& message) void UnitTest::logMessage (const String& message)
{ {
runner->logMessage (message); m_runner->logMessage (message);
} }
void UnitTest::beginTest (const String& testName) void UnitTest::beginTest (const String& testName)
{ {
runner->beginNewTest (this, testName); m_runner->beginNewTest (this, testName);
} }
void UnitTest::expect (const bool result, const String& failureMessage) void UnitTest::expect (const bool result, const String& failureMessage)
{ {
if (result) if (result)
runner->addPass(); m_runner->addPass();
else else
runner->addFail (failureMessage); m_runner->addFail (failureMessage);
} }
//============================================================================== //==============================================================================
UnitTests::UnitTests() UnitTests::UnitTests()
: currentTest (nullptr), : currentTest (nullptr),
assertOnFailure (true), assertOnFailure (true),
@@ -105,35 +117,52 @@ void UnitTests::resultsUpdated()
{ {
} }
void UnitTests::runTest (String const& name) void UnitTests::runTest (UnitTest& test)
{
results.clear();
resultsUpdated();
Array<UnitTest*>& tests = UnitTest::getAllTests ();
for (int i = 0; i < tests.size(); ++i)
{
UnitTest& test = *tests [i];
if (test.getName () == name)
{ {
try try
{ {
test.performTest (this); test.performTest (this);
} }
catch (std::exception& e)
{
String s;
s << "Got an exception: " << e.what ();
addFail (s);
}
catch (...) catch (...)
{ {
addFail ("An unhandled exception was thrown!"); addFail ("Got an unhandled exception");
}
} }
void UnitTests::runTest (String const& name)
{
results.clear();
resultsUpdated();
UnitTest::TestList& tests (UnitTest::getAllTests ());
for (int i = 0; i < tests.size(); ++i)
{
UnitTest& test = *tests [i];
if (test.getGroup () == name && test.getWhen () == UnitTest::runAlways)
{
runTest (test);
}
else if (test.getName () == name)
{
runTest (test);
break; break;
} }
} }
} }
void UnitTests::runTests (const Array<UnitTest*>& tests) void UnitTests::runAllTests ()
{ {
UnitTest::TestList& tests (UnitTest::getAllTests ());
results.clear(); results.clear();
resultsUpdated(); resultsUpdated();
@@ -142,22 +171,14 @@ void UnitTests::runTests (const Array<UnitTest*>& tests)
if (shouldAbortTests()) if (shouldAbortTests())
break; break;
try UnitTest& test = *tests [i];
{
tests.getUnchecked(i)->performTest (this); if (test.getWhen () == UnitTest::runAlways)
} runTest (test);
catch (...)
{
addFail ("An unhandled exception was thrown!");
}
} }
endTest(); endTest();
}
void UnitTests::runAllTests()
{
runTests (UnitTest::getAllTests());
} }
void UnitTests::logMessage (const String& message) void UnitTests::logMessage (const String& message)
@@ -177,7 +198,7 @@ void UnitTests::beginNewTest (UnitTest* const test, const String& subCategory)
TestResult* const r = new TestResult(); TestResult* const r = new TestResult();
results.add (r); results.add (r);
r->unitTestName = test->getName(); r->unitTestName = test->getGroup() + "::" + test->getName();
r->subcategoryName = subCategory; r->subcategoryName = subCategory;
r->passes = 0; r->passes = 0;
r->failures = 0; r->failures = 0;

View File

@@ -28,7 +28,6 @@
#include "../containers/beast_OwnedArray.h" #include "../containers/beast_OwnedArray.h"
class UnitTests; class UnitTests;
/** This is a base class for classes that perform a unit test. /** This is a base class for classes that perform a unit test.
To write a test using this class, your code should look something like this: To write a test using this class, your code should look something like this:
@@ -70,15 +69,38 @@ class UnitTests;
class BEAST_API UnitTest : Uncopyable class BEAST_API UnitTest : Uncopyable
{ {
public: public:
enum When
{
runAlways,
runManual
};
/** The type of a list of tests.
*/
typedef Array <UnitTest*, CriticalSection> TestList;
//============================================================================== //==============================================================================
/** Creates a test with the given name. */ /** Creates a test with the given name, group, and run option.
explicit UnitTest (String const& name);
The group is used when you want to run all tests in a particular group
instead of all tests in general. The run option allows you to write some
tests that are only available manually. For examplem, a performance unit
test that takes a long time which you might not want to run every time
you run all tests.
*/
explicit UnitTest (String const& name, String const& group = "", When when = runAlways);
/** Destructor. */ /** Destructor. */
virtual ~UnitTest(); virtual ~UnitTest();
/** Returns the name of the test. */ /** Returns the name of the test. */
const String& getName() const noexcept { return name; } const String& getName() const noexcept { return m_name; }
/** Returns the group of the test. */
String const& getGroup () const noexcept { return m_group; }
/** Returns the run option of the test. */
When getWhen () const noexcept { return m_when; }
/** Runs the test, using the specified UnitTests. /** Runs the test, using the specified UnitTests.
You shouldn't need to call this method directly - use You shouldn't need to call this method directly - use
@@ -87,7 +109,7 @@ public:
void performTest (UnitTests* runner); void performTest (UnitTests* runner);
/** Returns the set of all UnitTest objects that currently exist. */ /** Returns the set of all UnitTest objects that currently exist. */
static Array<UnitTest*>& getAllTests(); static TestList& getAllTests();
//============================================================================== //==============================================================================
/** You can optionally implement this method to set up your test. /** You can optionally implement this method to set up your test.
@@ -156,14 +178,16 @@ public:
//============================================================================== //==============================================================================
/** Writes a message to the test log. /** Writes a message to the test log.
This can only be called from within your runTest() method. This can only be called during your runTest() method.
*/ */
void logMessage (const String& message); void logMessage (const String& message);
private: private:
//============================================================================== //==============================================================================
const String name; String const m_name;
UnitTests* runner; String const m_group;
When const m_when;
UnitTests* m_runner;
}; };
//============================================================================== //==============================================================================
@@ -188,17 +212,15 @@ public:
/** Destructor. */ /** Destructor. */
virtual ~UnitTests(); virtual ~UnitTests();
/** Run a particular test. /** Run the specified unit test.
Subclasses can override this to do extra stuff.
*/ */
virtual void runTest (UnitTest& test);
/** Run a particular test or group. */
void runTest (String const& name); void runTest (String const& name);
/** Runs a set of tests.
The tests are performed in order, and the results are logged. To run all the
registered UnitTest objects that exist, use runAllTests().
*/
void runTests (const Array<UnitTest*>& tests);
/** Runs all the UnitTest objects that currently exist. /** Runs all the UnitTest objects that currently exist.
This calls runTests() for all the objects listed in UnitTest::getAllTests(). This calls runTests() for all the objects listed in UnitTest::getAllTests().
*/ */

View File

@@ -926,7 +926,7 @@ MemoryMappedFile::MemoryMappedFile (const File& file, const Range<int64>& fileRa
class FileTests : public UnitTest class FileTests : public UnitTest
{ {
public: public:
FileTests() : UnitTest ("File") {} FileTests() : UnitTest ("File", "beast") {}
void runTest() void runTest()
{ {

View File

@@ -158,7 +158,7 @@ class RandomAccessFileTests : public UnitTest
{ {
public: public:
RandomAccessFileTests () RandomAccessFileTests ()
: UnitTest ("RandomAccessFile") : UnitTest ("RandomAccessFile", "beast")
, numRecords (1000) , numRecords (1000)
, seedValue (50) , seedValue (50)
{ {

View File

@@ -535,7 +535,7 @@ void JSON::writeToStream (OutputStream& output, const var& data, const bool allO
class JSONTests : public UnitTest class JSONTests : public UnitTest
{ {
public: public:
JSONTests() : UnitTest ("JSON") { } JSONTests() : UnitTest ("JSON", "beast") { }
static String createRandomWideCharString (Random& r) static String createRandomWideCharString (Random& r)
{ {

View File

@@ -159,7 +159,7 @@ void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numB
class RandomTests : public UnitTest class RandomTests : public UnitTest
{ {
public: public:
RandomTests() : UnitTest ("Random") {} RandomTests() : UnitTest ("Random", "beast") {}
void runTest() void runTest()
{ {

View File

@@ -92,7 +92,7 @@ int64 MemoryInputStream::getPosition()
class MemoryStreamTests : public UnitTest class MemoryStreamTests : public UnitTest
{ {
public: public:
MemoryStreamTests() : UnitTest ("MemoryStream") { } MemoryStreamTests() : UnitTest ("MemoryStream", "beast") { }
void runTest() void runTest()
{ {

View File

@@ -2078,7 +2078,7 @@ String String::fromUTF8 (const char* const buffer, int bufferSizeBytes)
class StringTests : public UnitTest class StringTests : public UnitTest
{ {
public: public:
StringTests() : UnitTest ("String") { } StringTests() : UnitTest ("String", "beast") { }
template <class CharPointerType> template <class CharPointerType>
struct TestUTFConversion struct TestUTFConversion

View File

@@ -177,7 +177,7 @@ String TextDiff::Change::appliedTo (const String& text) const noexcept
class DiffTests : public UnitTest class DiffTests : public UnitTest
{ {
public: public:
DiffTests() : UnitTest ("TextDiff") {} DiffTests() : UnitTest ("TextDiff", "beast") {}
static String createString() static String createString()
{ {

View File

@@ -61,7 +61,7 @@ String ChildProcess::readAllProcessOutput()
class ChildProcessTests : public UnitTest class ChildProcessTests : public UnitTest
{ {
public: public:
ChildProcessTests() : UnitTest ("ChildProcess") {} ChildProcessTests() : UnitTest ("ChildProcess", "beast") {}
void runTest() void runTest()
{ {

View File

@@ -255,7 +255,7 @@ void SpinLock::enter() const noexcept
class AtomicTests : public UnitTest class AtomicTests : public UnitTest
{ {
public: public:
AtomicTests() : UnitTest ("Atomic") {} AtomicTests() : UnitTest ("Atomic", "beast") {}
void runTest() void runTest()
{ {

View File

@@ -161,7 +161,7 @@ bool GZIPCompressorOutputStream::setPosition (int64 /*newPosition*/)
class GZIPTests : public UnitTest class GZIPTests : public UnitTest
{ {
public: public:
GZIPTests() : UnitTest ("GZIP") {} GZIPTests() : UnitTest ("GZIP", "beast") {}
void runTest() void runTest()
{ {

View File

@@ -20,7 +20,7 @@
class UnsignedIntegerTests : public UnitTest class UnsignedIntegerTests : public UnitTest
{ {
public: public:
UnsignedIntegerTests () : UnitTest ("UnsignedInteger") UnsignedIntegerTests () : UnitTest ("UnsignedInteger", "beast")
{ {
} }