Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / tests / framework / src / test_results_collector_console.cpp
1 /*
2  * Copyright (c) 2014 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_results_collector_console.cpp
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
20  * @version     1.0
21  * @brief       Source file containing ConsoleCollector class definition
22  */
23
24 #include <fstream>
25 #include <sstream>
26
27 #include <dpl/assert.h>
28 #include <dpl/colors.h>
29 #include <dpl/test/test_results_collector_commons.h>
30
31 #include "dpl/test/test_results_collector_console.h"
32
33 namespace DPL {
34 namespace Test {
35
36 ConsoleCollector::ConsoleCollector()
37 {
38 }
39
40 TestResultsCollectorBase* ConsoleCollector::Constructor()
41 {
42     return new ConsoleCollector();
43 }
44
45 void ConsoleCollector::CollectCurrentTestGroupName(const std::string& name)
46 {
47     printf("Starting group %s\n", name.c_str());
48     m_currentGroup = name;
49 }
50
51 void ConsoleCollector::Finish()
52 {
53     using namespace DPL::Colors::Text;
54
55     // Show result
56     for (auto &group : m_groupsStats) {
57         PrintStats(group.first, group.second);
58     }
59     PrintStats("All tests together", m_stats);
60 }
61
62 void ConsoleCollector::CollectResult(const std::string& id,
63                                      const FailStatus status,
64                                      const std::string& reason,
65                                      const bool& isPerformanceTest,
66                                      const std::chrono::system_clock::duration& performanceTime,
67                                      const std::chrono::system_clock::duration& performanceMaxTime)
68 {
69     using namespace DPL::Colors::Text;
70     std::string tmp = "'" + id + "' ...";
71
72     printf("Running test case %-60s", tmp.c_str());
73     switch (status) {
74     case FailStatus::NONE:
75         if (isPerformanceTest) {
76             if (performanceMaxTime <= std::chrono::microseconds::zero()) {
77                 printf(GREEN_RESULT_OK_TIME,
78                         get_milliseconds(performanceTime));
79                 break;
80             }
81             else {
82                 if (performanceTime > performanceMaxTime)
83                     printf(GREEN_RESULT_OK_TIME_TOO_LONG(
84                             get_milliseconds(performanceTime),
85                             get_milliseconds(performanceMaxTime)));
86                 else
87                     printf(GREEN_RESULT_OK_TIME_MAX(
88                             get_milliseconds(performanceTime),
89                             get_milliseconds(performanceMaxTime)));
90                 break;
91             }
92         }
93         printf(GREEN_RESULT_OK);
94         break;
95     case FailStatus::FAILED:
96         PrintfErrorMessage(" FAILED ", reason, true);
97         break;
98     case FailStatus::IGNORED:
99         PrintfIgnoredMessage("Ignored ", reason, true);
100         break;
101     default:
102         Assert(false && "Bad status");
103     }
104     m_stats.AddTest(status);
105     m_groupsStats[m_currentGroup].AddTest(status);
106 }
107
108 void ConsoleCollector::PrintfErrorMessage(const char* type,
109                                           const std::string& message,
110                                           bool verbosity)
111 {
112     using namespace DPL::Colors::Text;
113     if (verbosity) {
114         printf("[%s%s%s] %s%s%s\n",
115                BOLD_RED_BEGIN,
116                type,
117                BOLD_RED_END,
118                BOLD_YELLOW_BEGIN,
119                message.c_str(),
120                BOLD_YELLOW_END);
121     } else {
122         printf("[%s%s%s]\n",
123                BOLD_RED_BEGIN,
124                type,
125                BOLD_RED_END);
126     }
127 }
128
129 void ConsoleCollector::PrintfIgnoredMessage(const char* type,
130                                             const std::string& message,
131                                             bool verbosity)
132 {
133     using namespace DPL::Colors::Text;
134     if (verbosity) {
135         printf("[%s%s%s] %s%s%s\n",
136                CYAN_BEGIN,
137                type,
138                CYAN_END,
139                BOLD_GOLD_BEGIN,
140                message.c_str(),
141                BOLD_GOLD_END);
142     } else {
143         printf("[%s%s%s]\n",
144                CYAN_BEGIN,
145                type,
146                CYAN_END);
147     }
148 }
149
150 void ConsoleCollector::PrintStats(const std::string& title, const Statistic& stats)
151 {
152     using namespace DPL::Colors::Text;
153     printf("\n%sResults [%s]: %s\n", BOLD_GREEN_BEGIN,
154            title.c_str(), BOLD_GREEN_END);
155     printf("%s%s%3zu%s\n",
156            CYAN_BEGIN,
157            "Total tests:            ",
158            stats.GetTotal(),
159            CYAN_END);
160     printf("  %s%s%3zu%s\n",
161            CYAN_BEGIN,
162            "Succeeded:            ",
163            stats.GetPassed(),
164            CYAN_END);
165     printf("  %s%s%3zu%s\n",
166            CYAN_BEGIN,
167            "Failed:               ",
168            stats.GetFailed(),
169            CYAN_END);
170     printf("  %s%s%3zu%s\n",
171            CYAN_BEGIN,
172            "Ignored:              ",
173            stats.GetIgnored(),
174            CYAN_END);
175 }
176
177 } // namespace Test
178 } // namespace DPL