2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
21 * @brief This file is the header file of test runner
23 #ifndef DPL_TEST_RUNNER_H
24 #define DPL_TEST_RUNNER_H
26 #include <dpl/singleton.h>
27 #include <dpl/unused.h>
28 #include <dpl/atomic.h>
29 #include <dpl/test/test_results_collector.h>
43 typedef std::map<std::string, TestResultsCollectorBasePtr>
44 TestResultsCollectors;
45 TestResultsCollectors m_collectors;
47 std::string m_startTestId;
51 typedef void (*TestCase)();
59 bool operator <(const TestCaseStruct &other) const
61 return name < other.name;
64 bool operator ==(const TestCaseStruct &other) const
66 return name == other.name;
69 TestCaseStruct(const std::string &n, TestCase p)
76 typedef std::list<TestCaseStruct> TestCaseStructList;
77 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
78 TestCaseGroupMap m_testGroups;
80 typedef std::set<std::string> SelectedTestNameSet;
81 SelectedTestNameSet m_selectedTestNamesSet;
82 typedef std::set<std::string> SelectedTestGroupSet;
83 SelectedTestGroupSet m_selectedTestGroupSet;
84 std::string m_currentGroup;
86 DPL::Atomic m_totalAssertions;
89 void InvalidArgs(const std::string& message = "Invalid arguments!");
92 enum Status { FAILED, IGNORED, PASS };
94 Status RunTestCase(const TestCaseStruct& testCase);
98 void CollectResult(const std::string& id,
99 const std::string& description,
100 const TestResultsCollectorBase::FailStatus::Type status
101 = TestResultsCollectorBase::FailStatus::NONE,
102 const std::string& reason = std::string());
108 std::string m_message;
115 //! \brief Failed test message creator
117 //! \param[in] aTest string for tested expression
118 //! \param[in] aFile source file name
119 //! \param[in] aLine source file line
120 //! \param[in] aMessage error message
121 TestFailed(const char* aTest, const char* aFile, int aLine, const std::string &aMessage);
123 std::string GetMessage() const
132 std::string m_message;
139 Ignored(const std::string &message)
144 std::string GetMessage() const
150 void MarkAssertion();
152 void RegisterTest(const char *testName, TestCase proc);
153 void InitGroup(const char* name);
155 int ExecTestRunner(int argc, char *argv[]);
156 typedef std::vector<std::string> ArgsList;
157 int ExecTestRunner(const ArgsList& args);
158 bool getRunIgnored() const;
161 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
166 #define RUNNER_TEST_GROUP_INIT(GroupName) \
167 static int Static##GroupName##Init() \
169 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName);\
172 const int DPL_UNUSED Static##GroupName##InitVar = \
173 Static##GroupName##Init();
175 #define RUNNER_TEST(Proc) \
177 static int Static##Proc##Init() \
179 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
182 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
185 //! \brief Returns base name for path
187 #define RUNNER_ASSERT_MSG(test, message) \
190 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
194 std::ostringstream assertMsg; \
195 assertMsg << message; \
196 throw DPL::Test::TestRunner::TestFailed(#test, __FILE__, __LINE__, assertMsg.str()); \
200 #define RUNNER_ASSERT(test) RUNNER_ASSERT_MSG(test, "")
202 #define RUNNER_FAIL RUNNER_ASSERT(false)
204 #define RUNNER_IGNORED_MSG(message) do { std::ostringstream assertMsg; assertMsg << message; throw DPL::Test::TestRunner::Ignored(assertMsg.str()); } while (0)
206 #endif // DPL_TEST_RUNNER_H