1c4d28a53787e907b07d744bed57576fbb83f247
[framework/web/wrt-commons.git] / modules / test / include / dpl / test / test_runner.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        test_runner.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
20  * @version     1.0
21  * @brief       This file is the header file of test runner
22  */
23 #ifndef DPL_TEST_RUNNER_H
24 #define DPL_TEST_RUNNER_H
25
26 #include <dpl/singleton.h>
27 #include <dpl/unused.h>
28 #include <dpl/atomic.h>
29 #include <dpl/test/test_results_collector.h>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 #include <list>
34 #include <set>
35 #include <map>
36
37 namespace DPL
38 {
39 namespace Test
40 {
41 class TestRunner
42 {
43     typedef std::map<std::string, TestResultsCollectorBasePtr>
44             TestResultsCollectors;
45     TestResultsCollectors m_collectors;
46
47     std::string m_startTestId;
48     bool m_runIgnored;
49
50 public:
51     TestRunner()
52       : m_terminate(false)
53     {}
54
55     typedef void (*TestCase)();
56
57 private:
58     struct TestCaseStruct
59     {
60         std::string name;
61         TestCase proc;
62
63         bool operator <(const TestCaseStruct &other) const
64         {
65             return name < other.name;
66         }
67
68         bool operator ==(const TestCaseStruct &other) const
69         {
70             return name == other.name;
71         }
72
73         TestCaseStruct(const std::string &n, TestCase p)
74             : name(n),
75               proc(p)
76         {
77         }
78     };
79
80     typedef std::list<TestCaseStruct> TestCaseStructList;
81     typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
82     TestCaseGroupMap m_testGroups;
83
84     typedef std::set<std::string> SelectedTestNameSet;
85     SelectedTestNameSet m_selectedTestNamesSet;
86     typedef std::set<std::string> SelectedTestGroupSet;
87     SelectedTestGroupSet m_selectedTestGroupSet;
88     std::string m_currentGroup;
89
90     DPL::Atomic m_totalAssertions;
91
92     // Terminate without any logs.
93     // Some test requires to call fork function.
94     // Child process must not produce any logs and should die quietly.
95     bool m_terminate;
96     void Banner();
97     void InvalidArgs(const std::string& message = "Invalid arguments!");
98     void Usage();
99
100     enum Status { FAILED, IGNORED, PASS };
101
102     Status RunTestCase(const TestCaseStruct& testCase);
103
104     void RunTests();
105
106     void CollectResult(const std::string& id,
107                        const std::string& description,
108                        const TestResultsCollectorBase::FailStatus::Type status
109                            = TestResultsCollectorBase::FailStatus::NONE,
110                        const std::string& reason = std::string());
111
112 public:
113     class TestFailed
114     {
115     private:
116         std::string m_message;
117
118     public:
119         TestFailed()
120         {
121         }
122
123         //! \brief Failed test message creator
124         //!
125         //! \param[in] aTest string for tested expression
126         //! \param[in] aFile source file name
127         //! \param[in] aLine source file line
128         //! \param[in] aMessage error message
129         TestFailed(const char* aTest, const char* aFile, int aLine, const std::string &aMessage);
130
131         TestFailed(const std::string &message);
132
133         std::string GetMessage() const
134         {
135             return m_message;
136         }
137     };
138
139     class Ignored
140     {
141     private:
142         std::string m_message;
143
144     public:
145         Ignored()
146         {
147         }
148
149         Ignored(const std::string &message)
150             : m_message(message)
151         {
152         }
153
154         std::string GetMessage() const
155         {
156             return m_message;
157         }
158     };
159
160     void MarkAssertion();
161
162     void RegisterTest(const char *testName, TestCase proc);
163     void InitGroup(const char* name);
164
165     int ExecTestRunner(int argc, char *argv[]);
166     typedef std::vector<std::string> ArgsList;
167     int ExecTestRunner(const ArgsList& args);
168     bool getRunIgnored() const;
169     // The runner will terminate as soon as possible (after current test).
170     void terminate();
171 };
172
173 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
174
175 }
176 } // namespace DPL
177
178 #define RUNNER_TEST_GROUP_INIT(GroupName)                                \
179     static int Static##GroupName##Init()                                 \
180     {                                                                    \
181         DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName);\
182         return 0;                                                        \
183     }                                                                    \
184     const int DPL_UNUSED  Static##GroupName##InitVar =                   \
185         Static##GroupName##Init();
186
187 #define RUNNER_TEST(Proc)                                                \
188     void Proc();                                                         \
189     static int Static##Proc##Init()                                      \
190     {                                                                    \
191         DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
192         return 0;                                                        \
193     }                                                                    \
194     const int DPL_UNUSED  Static##Proc##InitVar = Static##Proc##Init();  \
195     void Proc()
196
197 //! \brief Returns base name for path
198
199 #define RUNNER_ASSERT_MSG(test, message)                                               \
200 do                                                                                     \
201 {                                                                                      \
202     DPL::Test::TestRunnerSingleton::Instance().MarkAssertion();                        \
203                                                                                        \
204     if (!(test))                                                                       \
205     {                                                                                  \
206         std::ostringstream assertMsg;                                                  \
207         assertMsg << message;                                                          \
208         throw DPL::Test::TestRunner::TestFailed(#test, __FILE__, __LINE__, assertMsg.str()); \
209     }                                                                                  \
210 } while (0)
211
212 #define RUNNER_ASSERT(test) RUNNER_ASSERT_MSG(test, "")
213
214 #define RUNNER_FAIL RUNNER_ASSERT(false)
215
216 #define RUNNER_IGNORED_MSG(message) do { std::ostringstream assertMsg; assertMsg << message; throw DPL::Test::TestRunner::Ignored(assertMsg.str()); } while (0)
217
218 #endif // DPL_TEST_RUNNER_H