Skip to content

Commit

Permalink
Implemented TestResult class (instead of enum).
Browse files Browse the repository at this point in the history
This class provides representation of the TestInstance result,
including a textual description e.g. in the case an assertion failure.
  • Loading branch information
nieklinnenbank committed Oct 27, 2015
1 parent f034f59 commit d621503
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 25 deletions.
8 changes: 4 additions & 4 deletions lib/libtest/StdoutReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ void StdoutReporter::reportBefore(TestInstance & test)

void StdoutReporter::reportAfter(TestInstance & test, TestResult & result)
{
switch (result)
switch (result.getResult())
{
case OK: printf("%sOK\r\n", GREEN); break;
case FAIL: printf("%sFAIL\r\n", RED); break;
case SKIP: printf("%sSKIP\r\n", YELLOW); break;
case TestResult::Success: printf("%sOK\r\n", GREEN); break;
case TestResult::Failure: printf("%sFAIL\r\n%s\r\n", RED, *result.getDescription()); break;
case TestResult::Skipped: printf("%sSKIP\r\n", YELLOW); break;
}
printf("%s", WHITE);
}
Expand Down
10 changes: 4 additions & 6 deletions lib/libtest/TAPReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ void TAPReporter::reportBefore(TestInstance & test)

void TAPReporter::reportAfter(TestInstance & test, TestResult & result)
{
#warning TODO: how to get the assertion failure text in the test reporter output????

switch (result)
switch (result.getResult())
{
case OK: printf("ok %d %s\r\n", m_count, *test.getName()); break;
case FAIL: printf("not ok %d %s\r\n", m_count, *test.getName()); break;
case SKIP: printf("skip %d %s\r\n", m_count, *test.getName()); break;
case TestResult::Success: printf("ok %d %s\r\n", m_count, *test.getName()); break;
case TestResult::Failure: printf("not ok %d %s %s\r\n", m_count, *test.getName(), *result.getDescription()); break;
case TestResult::Skipped: printf("skip %d %s\r\n", m_count, *test.getName()); break;
}
m_count++;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/libtest/TestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
#define testAssert(expression) \
if(!(expression)) \
{ \
printf("%s:%d:%s testAssert failed: `%s' .. ", __FILE__, __LINE__, __FUNCTION__, QUOTE(expression)); \
return FAIL; \
char msg[256]; \
snprintf(msg, sizeof(msg), "%s:%d:%s testAssert failed: `%s' .. ", __FILE__, __LINE__, __FUNCTION__, QUOTE(expression)); \
return TestResult(TestResult::Failure, msg); \
}

/**
Expand Down
8 changes: 1 addition & 7 deletions lib/libtest/TestInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
#define __LIBTEST_TESTINSTANCE_H

#include <String.h>

enum TestResult
{
OK = 0,
FAIL = 1,
SKIP = 2
};
#include "TestResult.h"

class TestInstance
{
Expand Down
14 changes: 8 additions & 6 deletions lib/libtest/TestReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ void TestReporter::collect(TestInstance & test, TestResult & result)
if (m_report)
reportAfter(test, result);

switch (result)
{
case OK: m_ok++; break;
case FAIL: m_fail++; break;
case SKIP: m_skip++; break;
}
if (result.isOK())
m_ok++;

if (result.isFailed())
m_fail++;

if (result.isSkipped())
m_skip++;
}

void TestReporter::begin(List<TestInstance *> & tests)
Expand Down
48 changes: 48 additions & 0 deletions lib/libtest/TestResult.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2015 Niek Linnenbank
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "TestResult.h"

TestResult::TestResult(Result result, const char *description)
: m_result(result), m_description(description, true)
{
}

bool TestResult::isOK() const
{
return m_result == Success;
}

bool TestResult::isFailed() const
{
return m_result == Failure;
}

bool TestResult::isSkipped() const
{
return m_result == Skipped;
}

const TestResult::Result TestResult::getResult() const
{
return m_result;
}

String & TestResult::getDescription()
{
return m_description;
}
92 changes: 92 additions & 0 deletions lib/libtest/TestResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2015 Niek Linnenbank
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __LIBTEST_TESTRESULT_H
#define __LIBTEST_TESTRESULT_H

#include <String.h>

/**
* @group TestResult macros
* @{
*/

#define OK TestResult( TestResult::Success )
#define FAIL TestResult( TestResult::Failure )
#define SKIP TestResult( TestResult::Skipped )

/**
* @}
*/

/**
* Represents a Test result created by a TestInstance.
*/
class TestResult
{
public:

/**
* Result codes.
*/
enum Result
{
Success,
Failure,
Skipped
};

/**
* Constructor
*/
TestResult(Result result, const char *description = "");

/**
* Check if the test passed.
*/
bool isOK() const;

/**
* Check if the test failed.
*/
bool isFailed() const;

/**
* Check if the test is skipped.
*/
bool isSkipped() const;

/**
* Get result code.
*/
const Result getResult() const;

/**
* Get result description.
*/
String & getDescription();

private:

/** The result code for this test. */
const Result m_result;

/** Text describing the result. */
String m_description;
};

#endif /* __LIBTEST_TESTRESULT_H */

0 comments on commit d621503

Please sign in to comment.