xUnit++ / xUnit++ / src / xUnitTest.cpp

#include "xUnitTest.h"
#include "EventLevel.h"
#include "TestEventRecorder.h"
#include "xUnitAssert.h"

namespace xUnitpp
{

xUnitTest::xUnitTest(std::function<void()> &&test, std::string &&name, int testInstance, std::string &&params,
                     const std::string &suite, AttributeCollection &&attributes, Time::Duration timeLimit,
                     std::string &&filename, int line, const std::vector<std::shared_ptr<TestEventRecorder>> &testEventRecorders)
    : test(std::move(test))
    , testDetails(std::move(name), testInstance, std::move(params), suite, std::move(attributes), timeLimit, std::move(filename), line)
    , testEventRecorders(testEventRecorders)
    , failureEventLogged(false)
{
}

const TestDetails &xUnitTest::TestDetails() const
{
    return testDetails;
}

TestResult xUnitTest::Run()
{
    for (auto &recorder : testEventRecorders)
    {
        recorder->Tie([&](TestEvent &&evt) { AddEvent(std::move(evt)); });
    }

    testStart = Time::Clock::now();

    try
    {
        test();
    }
    catch (const xUnitAssert &assert)
    {
        AddEvent(TestEvent(EventLevel::Assert, assert));
    }
    catch (const std::exception &e)
    {
        AddEvent(TestEvent(e));
    }
    catch (...)
    {
        AddEvent(TestEvent(EventLevel::Fatal, "Unknown exception caught: test has crashed."));
    }

    testStop = Time::Clock::now();

    return failureEventLogged ? TestResult::Failure : TestResult::Success;
}

Time::Duration xUnitTest::Duration() const
{
    return Time::ToDuration(testStop - testStart);
}

void xUnitTest::AddEvent(TestEvent &&evt)
{
    std::lock_guard<std::mutex> lock(eventLock);

    testEvents.push_back(std::move(evt));

    if (testEvents.back().GetIsFailure())
    {
        failureEventLogged = true;
    }
}

const std::vector<TestEvent> &xUnitTest::TestEvents() const
{
    return testEvents;
}

}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.