Skip to content

Commit 406d016

Browse files
committed
add test_set= parameter to command line argument
1 parent f137bdb commit 406d016

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

test/test_main.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1+
#include<vector>
2+
#include<string>
3+
#include<sstream>
4+
15
#include "test_util.hpp"
26

3-
int main()
7+
int main(int argc,const char* argv[])
48
{
9+
std::vector<std::string> test_set;
10+
11+
const std::string run_test_opt = "--run_test=";
12+
for (int i = 1; i < argc; ++i)
13+
{
14+
if (strncmp(argv[i], run_test_opt.c_str(), run_test_opt.size()) == 0)
15+
{
16+
std::stringstream opt(argv[i] + run_test_opt.size());
17+
while (opt.good())
18+
{
19+
std::string testname;
20+
getline(opt, testname, ',');
21+
test_set.push_back(testname);
22+
}
23+
}
24+
}
25+
kaguya_test_util::TestRunner::instance().set_test_set(test_set);
26+
527
bool test_result = kaguya_test_util::TestRunner::instance().execute();
628
return test_result ? 0 : -1;
729
}

test/test_util.hpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#pragma once
2+
#include <map>
3+
#include <set>
24
#include <iostream>
35
#include <cassert>
46
#include <sstream>
@@ -56,7 +58,13 @@ namespace kaguya_test_util
5658
typedef std::map<std::string, TestFunctionType> TestFunctionMapType;
5759

5860
TestFunctionMapType test_functions_;
61+
62+
std::vector<std::string> test_set_;
5963
public:
64+
void set_test_set(std::vector<std::string> container)
65+
{
66+
test_set_ = container;
67+
}
6068
static TestRunner& instance()
6169
{
6270
static TestRunner ins;
@@ -66,6 +74,29 @@ namespace kaguya_test_util
6674
{
6775
throw std::runtime_error(std::string(message));
6876
}
77+
bool is_name_execute(const std::string& name)const
78+
{
79+
if (test_set_.empty()) { return true; }
80+
for (std::vector<std::string>::const_iterator it = test_set_.begin(); it != test_set_.end(); ++it)
81+
{
82+
if (name.find(*it) != std::string::npos)
83+
{
84+
return true;
85+
}
86+
}
87+
88+
return false;
89+
}
90+
TestFunctionMapType tests()
91+
{
92+
TestFunctionMapType tests;
93+
for (TestFunctionMapType::const_iterator it = test_functions_.begin(); it != test_functions_.end(); ++it)
94+
{
95+
if (!is_name_execute(it->first)) { continue; }
96+
tests.insert(*it);
97+
}
98+
return tests;
99+
}
69100

70101
void addTest(const std::string& name, TestFunctionType function)
71102
{
@@ -74,19 +105,21 @@ namespace kaguya_test_util
74105

75106
bool execute()
76107
{
108+
TestFunctionMapType test_function = tests();
77109
bool fail = false;
78-
size_t testcount = test_functions_.size();
110+
size_t testcount = test_function.size();
79111
size_t testindex = 1;
80112

81113
std::vector<std::string> pass_tests;
82114
std::vector<std::string> fail_tests;
83-
for (TestFunctionMapType::const_iterator it = test_functions_.begin(); it != test_functions_.end(); ++it, ++testindex)
115+
for (TestFunctionMapType::const_iterator it = test_function.begin(); it != test_function.end(); ++it, ++testindex)
84116
{
117+
const std::string& test_name = it->first;
118+
85119
kaguya::State state;
86120

87121
state.setErrorHandler(test_error_handler);
88122

89-
const std::string& test_name = it->first;
90123

91124
std::cout << test_name << " (" << testindex << "/" << testcount << ") ...";
92125

0 commit comments

Comments
 (0)