class KLAY_EXPORT Testbench {
public:
- static void runAllTestSuites();
+ static void run(int argc, char* argv[]);
+
+ static void runAllTestSuites(); // Deprecated Function
};
struct KLAY_EXPORT Source;
static TestDriver& GetInstance(void);
- void addTestCase(TestCase* testCase);
- void addFailure(const std::string& name, const Source& source);
+ void addTestCase(TestCase* testCase) noexcept;
+ void addFailure(const std::string& name, const Source& source) noexcept;
- void run(void);
+ void run(void) noexcept;
+ void run(const std::string& name) noexcept;
+ void list(void) const noexcept;
private:
TestDriver() = default;
void addFailure(const std::string& name, const Source& source) noexcept;
void addException(const std::string& name) noexcept;
void report(void) const noexcept;
+ void print(const std::string& name) const noexcept;
private:
std::size_t total = 0;
#include <klay/testbench.h>
+#include <iostream>
+#include <string>
+
+#include <getopt.h>
+
namespace klay {
namespace testbench {
+namespace {
+
+void usage(const std::string name)
+{
+ std::cout << "Usage: " << name << " [Option]" << std::endl
+ << std::endl
+ << "Options :" << std::endl
+ << " -a, --run-all run all TESTCASES" << std::endl
+ << " -r, --run=[TESTCASE] run TESTCASE" << std::endl
+ << " -l, --list list TESTCASES" << std::endl
+ << " -h, --help show this" << std::endl
+ << std::endl;
+}
+
+} // anonymous namespace
+
+void Testbench::run(int argc, char* argv[])
+{
+ struct option options[] = {
+ {"run-all", no_argument, 0, 'a'},
+ {"run", required_argument, 0, 'r'},
+ {"list", no_argument, 0, 'l'},
+ {"help", no_argument, 0, 'h'},
+ {0, 0, 0, 0}
+ };
+
+ if (argc <= 1) {
+ usage(argv[0]);
+ TestDriver::GetInstance().run();
+ return;
+ }
+
+ while (int opt = getopt_long(argc, argv, "ar:lh", options, 0)) {
+ if (opt == -1)
+ break;
+
+ switch (opt) {
+ case 'a':
+ TestDriver::GetInstance().run();
+ break;
+ case 'r':
+ TestDriver::GetInstance().run(optarg);
+ break;
+ case 'l':
+ TestDriver::GetInstance().list();
+ break;
+ case 'h':
+ default:
+ usage(argv[0]);
+ }
+ }
+}
+
void Testbench::runAllTestSuites()
{
TestDriver::GetInstance().run();
return *TestDriver::instance;
}
-void TestDriver::addTestCase(TestCase* testCase)
+void TestDriver::addTestCase(TestCase* testCase) noexcept
{
this->testSuite.addTestCase(testCase);
}
-void TestDriver::addFailure(const std::string& name, const Source& source)
+void TestDriver::addFailure(const std::string& name, const Source& source) noexcept
{
this->reporter.addFailure(name, source);
}
-void TestDriver::run(void)
+void TestDriver::run(void) noexcept
{
const auto& testCases = this->testSuite.getTestCases();
for (const auto& tc : testCases) {
this->reporter.report();
}
+void TestDriver::run(const std::string& name) noexcept
+{
+ const auto& testCases = this->testSuite.getTestCases();
+ for (const auto& tc : testCases) {
+ if (name.compare(tc->getName()) != 0)
+ continue;
+
+ auto startTime = this->reporter.start(tc->getName());
+
+ try {
+ tc->task();
+ } catch (...) {
+ this->reporter.addException(tc->getName());
+ }
+
+ this->reporter.end(tc->getName(), startTime);
+
+ break;
+ }
+
+ this->reporter.report();
+}
+
+void TestDriver::list(void) const noexcept
+{
+ const auto& testCases = this->testSuite.getTestCases();
+ for (const auto& tc : testCases)
+ this->reporter.print(tc->getName());
+}
+
} //namespace testbench
} //namespace klay
<< Colorize(DEFAULT);
}
+void TestReporter::print(const std::string& name) const noexcept
+{
+ std::cout << "Testcase name: " << name << std::endl;
+}
+
} //namespace testbench
} //namespace klay
#include <klay/testbench.h>
-int main(int /*argc*/, char** /*argv*/)
+int main(int argc, char* argv[])
{
- testbench::Testbench::runAllTestSuites();
+ testbench::Testbench::run(argc, argv);
return 0;
}