Skip to content

Commit

Permalink
Introduce TestReporter, LocalTest, ExternalTest and DirectoryScanner …
Browse files Browse the repository at this point in the history
…classes in libtest.

Modified /test/run to use libtest for test discovery and execution.
Implemented StdoutReporter which displays TestResults on the standard output.
  • Loading branch information
nieklinnenbank committed Oct 26, 2015
1 parent c962c66 commit c31df56
Show file tree
Hide file tree
Showing 16 changed files with 357 additions and 160 deletions.
75 changes: 75 additions & 0 deletions lib/libtest/DirectoryScanner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 <dirent.h>
#include <string.h>
#include <errno.h>
#include <String.h>
#include "ExternalTest.h"
#include "TestSuite.h"
#include "DirectoryScanner.h"

DirectoryScanner::DirectoryScanner(int argc, char **argv)
{
m_argc = argc;
m_argv = argv;
}

int DirectoryScanner::scan(const char *path)
{
DIR *d;
struct dirent *dent;
char subPath[255];

// Attempt to open the target directory.
if (!(d = opendir(path)))
{
printf("%s: failed to open '%s': %s\r\n",
m_argv[0], path, strerror(errno));
return EXIT_FAILURE;
}
// Read directory.
while ((dent = readdir(d)))
{
snprintf(subPath, sizeof(subPath), "%s/%s", path, dent->d_name);
String str = subPath;

// Check filetype
switch (dent->d_type)
{
// Directory
case DT_DIR:
if (dent->d_name[0] != '.')
scan(subPath);
break;

// Regular file
case DT_REG:
if (str.endsWith((const char *)"Test"))
new ExternalTest(subPath);
break;

default:
break;
}
}
// Close it.
closedir(d);
return EXIT_SUCCESS;
}
35 changes: 35 additions & 0 deletions lib/libtest/DirectoryScanner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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_DIRECTORYSCANNER_H
#define __LIBTEST_DIRECTORYSCANNER_H

class DirectoryScanner
{
public:

DirectoryScanner(int argc, char **argv);

int scan(const char *path);

private:

int m_argc;
char **m_argv;
};

#endif /* __LIBTEST_DIRECTORYSCANNER_H */
54 changes: 54 additions & 0 deletions lib/libtest/ExternalTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 <errno.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "ExternalTest.h"

ExternalTest::ExternalTest(const char *name)
: TestInstance(name)
{
// TODO: perhaps argc/argv?
}

TestResult ExternalTest::run()
{
int status;

#ifdef __HOST__
char tmp[255];

snprintf(tmp, sizeof(tmp), "%s -n", *m_name);
status = system(tmp);

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

pid_t pid = forkexec(*m_name, (const char **) argv);
waitpid(pid, &status, 0);
#endif

return status == 0 ? OK : FAIL;
}
32 changes: 32 additions & 0 deletions lib/libtest/ExternalTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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_EXTERNALTEST_H
#define __LIBTEST_EXTERNALTEST_H

#include "TestInstance.h"

class ExternalTest : public TestInstance
{
public:

ExternalTest(const char *name);

virtual TestResult run();
};

#endif /* __LIBTEST_EXTERNALTEST_H */
29 changes: 29 additions & 0 deletions lib/libtest/LocalTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 "LocalTest.h"

LocalTest::LocalTest(const char *name, TestFunction func)
: TestInstance(name)
{
m_func = func;
}

TestResult LocalTest::run(void)
{
return m_func();
}
38 changes: 38 additions & 0 deletions lib/libtest/LocalTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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_LOCALTEST_H
#define __LIBTEST_LOCALTEST_H

#include "TestInstance.h"

typedef TestResult TestFunction(void);

class LocalTest : public TestInstance
{
public:

LocalTest(const char *name, TestFunction func);

virtual TestResult run();

private:

TestFunction *m_func;
};

#endif /* __LIBTEST_LOCALTEST_H */
2 changes: 1 addition & 1 deletion lib/libtest/StdoutReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ StdoutReporter::StdoutReporter(int argc, char **argv)

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

void StdoutReporter::reportAfter(TestResult & result)
Expand Down
28 changes: 2 additions & 26 deletions lib/libtest/TestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,13 @@
#include <stdio.h>
#include <List.h>
#include <Macros.h>

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

#define TestCase(name) \
TestResult name (void); \
TestInstance instance_##name (QUOTE(name), name); \
LocalTest instance_##name (QUOTE(name), name); \
TestResult name (void)

typedef TestResult TestFunction(void);

class TestInstance
{
public:

TestInstance(const char *name, TestFunction func);

TestResult run();

private:

const char *m_name;

TestFunction *m_func;
};


/**
* Test if the given expression is true and return NOK otherwise.
*
Expand Down
13 changes: 3 additions & 10 deletions lib/libtest/TestCase.cpp → lib/libtest/TestInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@
*/

#include "TestSuite.h"
#include "TestCase.h"
#include "TestInstance.h"

TestInstance::TestInstance(const char *name, TestFunction func)
TestInstance::TestInstance(const char *name)
: m_name(name, true)
{
m_name = name;
m_func = func;

if (!TestSuite::instance)
{
TestSuite::instance = new TestSuite();
}
TestSuite::instance->addTest(this);
}

TestResult TestInstance::run(void)
{
return m_func();
}
43 changes: 43 additions & 0 deletions lib/libtest/TestInstance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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_TESTINSTANCE_H
#define __LIBTEST_TESTINSTANCE_H

#include <String.h>

enum TestResult
{
OK = 0,
FAIL = 1,
SKIP = 2
};

class TestInstance
{
public:

TestInstance(const char *name);

virtual TestResult run() = 0;

protected:

String m_name;
};

#endif /* __LIBTEST_TESTINSTANCE_H */
Loading

0 comments on commit c31df56

Please sign in to comment.