Tizen 2.0 Release
[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 namespace Test {
39 class TestRunner
40 {
41     typedef std::map<std::string, TestResultsCollectorBasePtr>
42     TestResultsCollectors;
43     TestResultsCollectors m_collectors;
44
45     std::string m_startTestId;
46     bool m_runIgnored;
47
48   public:
49     TestRunner() :
50         m_terminate(false)
51       , m_allowChildLogs(false)
52     {}
53
54     typedef void (*TestCase)();
55
56   private:
57     struct TestCaseStruct
58     {
59         std::string name;
60         TestCase proc;
61
62         bool operator <(const TestCaseStruct &other) const
63         {
64             return name < other.name;
65         }
66
67         bool operator ==(const TestCaseStruct &other) const
68         {
69             return name == other.name;
70         }
71
72         TestCaseStruct(const std::string &n, TestCase p) :
73             name(n),
74             proc(p)
75         {}
76     };
77
78     typedef std::list<TestCaseStruct> TestCaseStructList;
79     typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
80     TestCaseGroupMap m_testGroups;
81
82     typedef std::set<std::string> SelectedTestNameSet;
83     SelectedTestNameSet m_selectedTestNamesSet;
84     typedef std::set<std::string> SelectedTestGroupSet;
85     SelectedTestGroupSet m_selectedTestGroupSet;
86     std::string m_currentGroup;
87
88     DPL::Atomic m_totalAssertions;
89
90     // Terminate without any logs.
91     // Some test requires to call fork function.
92     // Child process must not produce any logs and should die quietly.
93     bool m_terminate;
94     bool m_allowChildLogs;
95
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         //! \brief Failed test message creator
123         //!
124         //! \param[in] aTest string for tested expression
125         //! \param[in] aFile source file name
126         //! \param[in] aLine source file line
127         //! \param[in] aMessage error message
128         TestFailed(const char* aTest,
129                    const char* aFile,
130                    int aLine,
131                    const std::string &aMessage);
132
133         TestFailed(const std::string &message);
134
135         std::string GetMessage() const
136         {
137             return m_message;
138         }
139     };
140
141     class Ignored
142     {
143       private:
144         std::string m_message;
145
146       public:
147         Ignored()
148         {}
149
150         Ignored(const std::string &message) :
151             m_message(message)
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     bool GetAllowChildLogs();
172 };
173
174 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
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, \
209                                                     __FILE__, \
210                                                     __LINE__, \
211                                                     assertMsg.str()); \
212         }                                                                                  \
213     } while (0)
214
215 #define RUNNER_ASSERT(test) RUNNER_ASSERT_MSG(test, "")
216
217 #define RUNNER_FAIL RUNNER_ASSERT(false)
218
219 #define RUNNER_IGNORED_MSG(message) do { std::ostringstream assertMsg; \
220                                          assertMsg << message; \
221                                          throw DPL::Test::TestRunner::Ignored( \
222                                                    assertMsg.str()); \
223 } while (0)
224
225 #endif // DPL_TEST_RUNNER_H