From: sangwan.kwon Date: Wed, 20 Feb 2019 07:23:36 +0000 (+0900) Subject: Add command line parser on Testbench X-Git-Tag: submit/tizen/20190430.085417^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F16%2F200216%2F2;p=platform%2Fcore%2Fsecurity%2Fklay.git Add command line parser on Testbench $ klay-test --help Usage: klay-test [Option] Options : -a, --run-all run all TESTCASES -r, --run=[TESTCASE] run TESTCASE -l, --list list TESTCASES -h, --help show this Change-Id: I6591550dba254d833165c63ffc85178db1b9da99 Signed-off-by: sangwan.kwon --- diff --git a/include/klay/testbench.h b/include/klay/testbench.h index f63521a..f7550dd 100644 --- a/include/klay/testbench.h +++ b/include/klay/testbench.h @@ -30,7 +30,9 @@ namespace testbench { class KLAY_EXPORT Testbench { public: - static void runAllTestSuites(); + static void run(int argc, char* argv[]); + + static void runAllTestSuites(); // Deprecated Function }; struct KLAY_EXPORT Source; diff --git a/include/klay/testbench/test-driver.h b/include/klay/testbench/test-driver.h index b7b298d..cbd7d46 100644 --- a/include/klay/testbench/test-driver.h +++ b/include/klay/testbench/test-driver.h @@ -39,10 +39,12 @@ public: 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; diff --git a/include/klay/testbench/test-reporter.h b/include/klay/testbench/test-reporter.h index bc74fcd..50f225a 100644 --- a/include/klay/testbench/test-reporter.h +++ b/include/klay/testbench/test-reporter.h @@ -46,6 +46,7 @@ public: 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; diff --git a/src/testbench.cpp b/src/testbench.cpp index d337e2f..97af95c 100644 --- a/src/testbench.cpp +++ b/src/testbench.cpp @@ -16,9 +16,67 @@ #include +#include +#include + +#include + 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(); diff --git a/src/testbench/test-driver.cpp b/src/testbench/test-driver.cpp index 218d849..5b6628a 100644 --- a/src/testbench/test-driver.cpp +++ b/src/testbench/test-driver.cpp @@ -31,17 +31,17 @@ TestDriver& TestDriver::GetInstance() 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) { @@ -59,5 +59,35 @@ void TestDriver::run(void) 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 diff --git a/src/testbench/test-reporter.cpp b/src/testbench/test-reporter.cpp index 2859053..50278a0 100644 --- a/src/testbench/test-reporter.cpp +++ b/src/testbench/test-reporter.cpp @@ -80,5 +80,10 @@ void TestReporter::report(void) const noexcept << Colorize(DEFAULT); } +void TestReporter::print(const std::string& name) const noexcept +{ + std::cout << "Testcase name: " << name << std::endl; +} + } //namespace testbench } //namespace klay diff --git a/test/main.cpp b/test/main.cpp index 050f96a..3219d3e 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -17,9 +17,9 @@ #include -int main(int /*argc*/, char** /*argv*/) +int main(int argc, char* argv[]) { - testbench::Testbench::runAllTestSuites(); + testbench::Testbench::run(argc, argv); return 0; }