--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file test_group.h
+ * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version 1.0
+ * @brief Base/default class describing a group of tests
+ */
+
+#ifndef DPL_TEST_GROUP_H
+#define DPL_TEST_GROUP_H
+
+#include <string>
+#include <functional>
+
+#include <dpl/test/test_case.h>
+
+namespace DPL {
+namespace Test {
+
+class TestGroup
+{
+public:
+ TestGroup() {}
+
+ virtual ~TestGroup()
+ {
+ }
+
+ const TestCaseSet& GetTests() const
+ {
+ return m_tests;
+ }
+
+ void RemoveIf(const std::function<bool(const TestCasePtr)>& predFn)
+ {
+ m_tests.remove_if(predFn);
+ }
+
+ void Add(TestCase* test)
+ {
+ m_tests.emplace_back(test);
+ }
+
+ virtual void Init() {};
+ virtual void Finish() {};
+
+private:
+ std::string m_name;
+ TestCaseSet m_tests;
+};
+
+typedef TestGroup* TestGroupPtr;
+
+} // namespace Test
+} // namespace DPL
+
+#endif // DPL_TEST_CASE_H
#include <exception>
#include <functional>
#include <iostream>
-#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
+#include <memory>
#include <dpl/atomic.h>
#include <dpl/availability.h>
#include <dpl/gdbbacktrace.h>
#include <dpl/singleton.h>
#include <dpl/test/performance_result.h>
-#include <dpl/test/test_case.h>
+#include <dpl/test/test_group.h>
#include <dpl/test/test_case_extended.h>
#include <dpl/test/test_exception.h>
#include <dpl/test/test_failed.h>
~TestRunner();
private:
- typedef std::list<TestCasePtr> TestCaseList;
- typedef std::map<std::string, TestCaseList> TestCaseGroupMap;
- typedef std::set<TestCasePtr> TestCaseSet;
+ typedef std::map<std::string, TestGroup*> TestCaseGroupMap;
TestCaseGroupMap m_testGroups;
- TestCaseSet m_testCaseSet;
TestCasePtr m_currentTestCase;
-
- std::string m_currentGroup;
+ TestGroupPtr m_currentGroup;
// Terminate without any logs.
// Some test requires to call fork function.
void CollectResult(const std::string& id, const TestResult &result);
public:
- void InitGroup(const char* name);
- void RegisterTest(TestCasePtr testCase);
+ void InitGroup(const char* name, TestGroup* group = nullptr);
+ void RegisterTest(TestCase* testCase);
int ExecTestRunner(int argc, char *argv[]);
typedef std::vector<std::string> ArgsList;
int ExecTestRunner(ArgsList args);
} // Test
} // namespace DPL
+#define RUNNER_TEST_GROUP_INIT_ENV(GroupName, GroupEnv) \
+ static int Static##GroupName##Init() \
+ { \
+ DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName, new GroupEnv); \
+ return 0; \
+ } \
+ const int DPL_UNUSED Static##GroupName##InitVar = \
+ Static##GroupName##Init();
+
#define RUNNER_TEST_GROUP_INIT(GroupName) \
static int Static##GroupName##Init() \
{ \
return TestResult::FailStatus::NONE;
}
-void TestRunner::RegisterTest(TestCasePtr testCase)
+void TestRunner::RegisterTest(TestCase* testCase)
{
- m_testGroups[m_currentGroup].push_back(testCase);
- m_testCaseSet.insert(testCase);
+ m_currentGroup->Add(testCase);
}
-void TestRunner::InitGroup(const char* name)
+void TestRunner::InitGroup(const char* name, TestGroup* group)
{
- m_currentGroup = name;
+ if (group == nullptr)
+ group = new TestGroup();
+
+ m_testGroups[name] = group;
+ m_currentGroup = group;
}
void TestRunner::normalizeXMLTag(std::string& str, const std::string& testcase)
std::string testsuite;
if(!m_testGroups.empty())
{
- for(TestCaseGroupMap::const_iterator cit = m_testGroups.begin(); cit != m_testGroups.end(); ++cit)
+ for(auto cit = m_testGroups.begin(); cit != m_testGroups.end(); ++cit)
{
- if(!cit->second.empty())
+ const TestCaseSet& tl = cit->second->GetTests();
+ if(!tl.empty())
{
- for(TestCaseList::const_iterator cj = cit->second.begin(); cj != cit->second.end(); ++cj)
+ for(auto cj = tl.begin(); cj != tl.end(); ++cj)
{
std::string name = (*cj)->GetName();
std::string::size_type st = name.find('_');
bool TestRunner::filterByXML(std::map<std::string, bool> & casesMap)
{
for (auto &group : m_testGroups) {
- TestCaseList newList;
- for (auto &tc : group.second)
- {
- if (casesMap.find(tc->GetName()) != casesMap.end()) {
- casesMap[tc->GetName()] = true;
- newList.push_back(tc);
- }
- }
- group.second = newList;
+ group.second->RemoveIf([&](const TestCasePtr test){
+ return (casesMap.find(test->GetName()) == casesMap.end());
+ });
}
for (auto &cs : casesMap)
{
unsigned count = 0;
for (auto &group : m_testGroups) {
- count += group.second.size();
+ count += group.second->GetTests().size();
}
fprintf(stderr, "%sFound %d testcases...%s\n", GREEN_BEGIN, count, GREEN_END);
fprintf(stderr, "%s%s%s\n", GREEN_BEGIN, "Running tests...", GREEN_END);
for (auto &group : m_testGroups) {
- TestCaseList list = group.second;
- if (!list.empty()) {
+ const TestCaseSet& set = group.second->GetTests();
+ if (!set.empty()) {
+ group.second->Init();
+
for (auto &collector : m_collectors) {
collector.second->CollectCurrentTestGroupName(group.first);
}
- list.sort([](const TestCasePtr &a, const TestCasePtr &b) { return (*a < *b); });
- for (TestCaseList::const_iterator iterator = list.begin();
- iterator != list.end();
+ for (TestCaseSet::const_iterator iterator = set.begin();
+ iterator != set.end();
++iterator)
{
TestCasePtr test = *iterator;
return;
}
}
+ group.second->Finish();
}
}
TestRunner::~TestRunner()
{
- for(auto &t : m_testCaseSet)
- delete t;
+ for(auto &g : m_testGroups)
+ delete g.second;
}
void TestRunner::CollectResult(const std::string& id, const TestResult& result)
if (arg.find(startCmd) == 0) {
arg.erase(0, startCmd.length());
for (auto &group : m_testGroups) {
- for (auto &tc : group.second) {
+ for (auto &tc : group.second->GetTests()) {
if (tc->GetName() == arg) {
m_startTestId = arg;
break;
return 0;
} else if (arg.find(groupId) == 0) {
arg.erase(0, groupId.length());
- TestCaseGroupMap::iterator found = m_testGroups.find(arg);
- if (found != m_testGroups.end()) {
- std::string name = found->first;
- TestCaseList newList = found->second;
- m_testGroups.clear();
- m_testGroups[name] = newList;
- } else {
+ for (auto it = m_testGroups.begin(); it != m_testGroups.end();) {
+ if (it->first == arg)
+ it++;
+ else
+ it = m_testGroups.erase(it);
+ }
+
+ if (m_testGroups.empty()) {
fprintf(stderr, "Group %s not found\n", arg.c_str());
InvalidArgs();
Usage();
return 0;
} else if (arg.find(listInGroup) == 0) {
arg.erase(0, listInGroup.length());
- for (auto &test : m_testGroups[arg]) {
+ auto it = m_testGroups.find(arg);
+ if (it == m_testGroups.end()) {
+ fprintf(stderr, "Group %s not found\n", arg.c_str());
+ InvalidArgs();
+ Usage();
+ return -1;
+ }
+ for (auto &test : it->second->GetTests()) {
printf("ID:%s\n", test->GetName().c_str());
}
return 0;
pcrecpp::RE re(arg.c_str());
for (auto &group : m_testGroups) {
- TestCaseList newList;
- for (auto &tc : group.second)
- {
- if (re.PartialMatch(tc->GetName())) {
- newList.push_back(tc);
- }
- }
- group.second = newList;
+ group.second->RemoveIf([&](const TestCasePtr& test){
+ return !re.PartialMatch(test->GetName());
+ });
}
} else if (arg.find(test) == 0) {
arg.erase(0, test.length());
pcrecpp::RE re(arg.c_str());
for (auto &group : m_testGroups) {
- TestCaseList newList;
- for (auto &tc : group.second)
- {
- if (re.FullMatch(tc->GetName())) {
- newList.push_back(tc);
- }
- }
- group.second = newList;
+ group.second->RemoveIf([&](const TestCasePtr& test){
+ return !re.FullMatch(test->GetName());
+ });
}
} else if(arg.find(onlyFromXML) == 0) {
arg.erase(0, onlyFromXML.length());
if(justList)
{
for (auto &group : m_testGroups) {
- for (auto &tc : group.second) {
+ for (auto &tc : group.second->GetTests()) {
printf("ID:%s:%s\n", group.first.c_str(), tc->GetName().c_str());
}
}