Skip to content

Commit

Permalink
Implemented TAPReporter for outputing TAP formatted test results usin…
Browse files Browse the repository at this point in the history
…g libtest.
  • Loading branch information
nieklinnenbank committed Oct 27, 2015
1 parent c31df56 commit f034f59
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lib/libtest/DirectoryScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int DirectoryScanner::scan(const char *path)
// Regular file
case DT_REG:
if (str.endsWith((const char *)"Test"))
new ExternalTest(subPath);
new ExternalTest(subPath, m_argc, m_argv);
break;

default:
Expand Down
30 changes: 19 additions & 11 deletions lib/libtest/ExternalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,38 @@
#include <sys/wait.h>
#include "ExternalTest.h"

ExternalTest::ExternalTest(const char *name)
ExternalTest::ExternalTest(const char *name, int argc, char **argv)
: TestInstance(name)
{
// TODO: perhaps argc/argv?
m_argc = argc;
m_argv = argv;
}

TestResult ExternalTest::run()
{
int status;
pid_t pid;
char **argv = new char * [m_argc + 2];

#ifdef __HOST__
char tmp[255];
for (int i = 1; i < m_argc; i++)
argv[i] = m_argv[i];

snprintf(tmp, sizeof(tmp), "%s -n", *m_name);
status = system(tmp);
argv[0] = *m_name;
argv[m_argc] = (char *) "-n";
argv[m_argc+1] = 0;

if (WIFEXITED(status))
status = WEXITSTATUS(status);
#else
const char *argv[3] = { *m_name, "-n", 0 };
#ifdef __HOST__
if ((pid = fork()) == 0)
execv(argv[0], argv);

pid_t pid = forkexec(*m_name, (const char **) argv);
waitpid(pid, &status, 0);
//status = system(tmp);
//if (WIFEXITED(status))
// status = WEXITSTATUS(status);
#else
pid = forkexec(*m_name, (const char **) argv);
#endif
waitpid(pid, &status, 0);

return status == 0 ? OK : FAIL;
}
8 changes: 7 additions & 1 deletion lib/libtest/ExternalTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ class ExternalTest : public TestInstance
{
public:

ExternalTest(const char *name);
ExternalTest(const char *name, int argc, char **argv);

virtual TestResult run();

private:

int m_argc;
char ** m_argv;

};

#endif /* __LIBTEST_EXTERNALTEST_H */
23 changes: 12 additions & 11 deletions lib/libtest/StdoutReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ StdoutReporter::StdoutReporter(int argc, char **argv)
{
}

void StdoutReporter::reportBegin(List<TestInstance *> & tests)
{
}

void StdoutReporter::reportBefore(TestInstance & test)
{
printf("%s%s: %s .. ", WHITE, basename(m_argv[0]), *test.m_name);
}

void StdoutReporter::reportAfter(TestResult & result)
void StdoutReporter::reportAfter(TestInstance & test, TestResult & result)
{
switch (result)
{
Expand All @@ -45,18 +49,15 @@ void StdoutReporter::reportAfter(TestResult & result)
printf("%s", WHITE);
}

void StdoutReporter::reportFinish()
void StdoutReporter::reportFinish(List<TestInstance *> & tests)
{
if (m_showStatistics)
{
if (m_fail)
printf("%s: %sFAIL%s ", basename(m_argv[0]), RED, WHITE);
else
printf("%s: %sOK%s ", basename(m_argv[0]), GREEN, WHITE);
if (m_fail)
printf("%s: %sFAIL%s ", basename(m_argv[0]), RED, WHITE);
else
printf("%s: %sOK%s ", basename(m_argv[0]), GREEN, WHITE);

printf("(%d passed %d failed %d skipped %d total)\r\n",
m_ok, m_fail, m_skip, (m_ok + m_fail + m_skip));
}
printf("(%d passed %d failed %d skipped %d total)\r\n",
m_ok, m_fail, m_skip, (m_ok + m_fail + m_skip));

#ifdef __HOST__
fflush(stdout);
Expand Down
11 changes: 8 additions & 3 deletions lib/libtest/StdoutReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

#ifndef __LIBTEST_STDOUTREPORTER_H
#define __LIBTEST_STDOUTEPORTER_H
#define __LIBTEST_STDOUTREPORTER_H

#include "TestReporter.h"

Expand All @@ -32,6 +32,11 @@ class StdoutReporter : public TestReporter
*/
StdoutReporter(int argc, char **argv);

/**
* Report start of testing.
*/
virtual void reportBegin(List<TestInstance *> & tests);

/**
* Report start of a test.
*/
Expand All @@ -40,12 +45,12 @@ class StdoutReporter : public TestReporter
/**
* Report finish of a test.
*/
virtual void reportAfter(TestResult & result);
virtual void reportAfter(TestInstance & test, TestResult & result);

/**
* Report completion of all tests.
*/
virtual void reportFinish();
virtual void reportFinish(List<TestInstance *> & tests);
};

#endif /* __LIBTEST_STDOUTREPORTER_H */
56 changes: 56 additions & 0 deletions lib/libtest/TAPReporter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TAPReporter.h"

TAPReporter::TAPReporter(int argc, char **argv)
: TestReporter(argc, argv)
{
m_count = 1;
}

void TAPReporter::reportBegin(List<TestInstance *> & tests)
{
printf("1..%d\r\n", tests.count());
}

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)
{
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;
}
m_count++;
}

void TAPReporter::reportFinish(List<TestInstance *> & tests)
{
#ifdef __HOST__
fflush(stdout);
#endif /* __HOST__ */
}
63 changes: 63 additions & 0 deletions lib/libtest/TAPReporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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_TAPREPORTER_H
#define __LIBTEST_TAPREPORTER_H

#include "TestReporter.h"

/**
* Output TestResults in TAP format to stdout.
*
* @see https://testanything.org/tap-specification.html
*/
class TAPReporter : public TestReporter
{
public:

/**
* Constructor.
*/
TAPReporter(int argc, char **argv);

/**
* Report start of testing.
*/
virtual void reportBegin(List<TestInstance *> & tests);

/**
* Report start of a test.
*/
virtual void reportBefore(TestInstance & test);

/**
* Report finish of a test.
*/
virtual void reportAfter(TestInstance & test, TestResult & result);

/**
* Report completion of all tests.
*/
virtual void reportFinish(List<TestInstance *> & tests);

private:

/** Test counter. */
uint m_count;
};

#endif /* __LIBTEST_TAPREPORTER_H */
5 changes: 5 additions & 0 deletions lib/libtest/TestInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ TestInstance::TestInstance(const char *name)
}
TestSuite::instance->addTest(this);
}

const String & TestInstance::getName() const
{
return m_name;
}
2 changes: 2 additions & 0 deletions lib/libtest/TestInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class TestInstance

TestInstance(const char *name);

const String & getName() const;

virtual TestResult run() = 0;

protected:
Expand Down
26 changes: 21 additions & 5 deletions lib/libtest/TestReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ TestReporter::TestReporter(int argc, char **argv)
m_fail = 0;
m_skip = 0;
m_report = true;
m_showStatistics = !(argc > 1 && strcmp(argv[1], "-n") == 0);
m_showStatistics = true;
}

TestReporter::~TestReporter()
{
}

uint TestReporter::getOk() const
Expand All @@ -49,16 +53,21 @@ void TestReporter::setReport(bool value)
m_report = value;
}

void TestReporter::setStatistics(bool value)
{
m_showStatistics = value;
}

void TestReporter::prepare(TestInstance & test)
{
if (m_report)
reportBefore(test);
}

void TestReporter::collect(TestResult & result)
void TestReporter::collect(TestInstance & test, TestResult & result)
{
if (m_report)
reportAfter(result);
reportAfter(test, result);

switch (result)
{
Expand All @@ -68,7 +77,14 @@ void TestReporter::collect(TestResult & result)
}
}

void TestReporter::finish()
void TestReporter::begin(List<TestInstance *> & tests)
{
if (m_showStatistics)
reportBegin(tests);
}

void TestReporter::finish(List<TestInstance *> & tests)
{
reportFinish();
if (m_showStatistics)
reportFinish(tests);
}
Loading

0 comments on commit f034f59

Please sign in to comment.