155275522c88e785d81bb07d2221383a410ef270
[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 std::string& /*description*/,
64                                      const FailStatus::Type status,
65                                      const std::string& reason,
66                                      const bool& isPerformanceTest,
67                                      const std::chrono::system_clock::duration& performanceTime,
68                                      const std::chrono::system_clock::duration& performanceMaxTime)
69 {
70     using namespace DPL::Colors::Text;
71     std::string tmp = "'" + id + "' ...";
72
73     printf("Running test case %-60s", tmp.c_str());
74     switch (status) {
75     case TestResultsCollectorBase::FailStatus::NONE:
76         if (isPerformanceTest) {
77             if (performanceMaxTime <= std::chrono::microseconds::zero()) {
78                 printf(GREEN_RESULT_OK_TIME,
79                         get_milliseconds(performanceTime));
80                 break;
81             }
82             else {
83                 if (performanceTime > performanceMaxTime)
84                     printf(GREEN_RESULT_OK_TIME_TOO_LONG(
85                             get_milliseconds(performanceTime),
86                             get_milliseconds(performanceMaxTime)));
87                 else
88                     printf(GREEN_RESULT_OK_TIME_MAX(
89                             get_milliseconds(performanceTime),
90                             get_milliseconds(performanceMaxTime)));
91                 break;
92             }
93         }
94         printf(GREEN_RESULT_OK);
95         break;
96     case TestResultsCollectorBase::FailStatus::FAILED:
97         PrintfErrorMessage(" FAILED ", reason, true);
98         break;
99     case TestResultsCollectorBase::FailStatus::IGNORED:
100         PrintfIgnoredMessage("Ignored ", reason, true);
101         break;
102     case TestResultsCollectorBase::FailStatus::INTERNAL:
103         PrintfErrorMessage("INTERNAL", reason, true);
104         break;
105     default:
106         Assert(false && "Bad status");
107     }
108     m_stats.AddTest(status);
109     m_groupsStats[m_currentGroup].AddTest(status);
110 }
111
112 void ConsoleCollector::PrintfErrorMessage(const char* type,
113                                           const std::string& message,
114                                           bool verbosity)
115 {
116     using namespace DPL::Colors::Text;
117     if (verbosity) {
118         printf("[%s%s%s] %s%s%s\n",
119                BOLD_RED_BEGIN,
120                type,
121                BOLD_RED_END,
122                BOLD_YELLOW_BEGIN,
123                message.c_str(),
124                BOLD_YELLOW_END);
125     } else {
126         printf("[%s%s%s]\n",
127                BOLD_RED_BEGIN,
128                type,
129                BOLD_RED_END);
130     }
131 }
132
133 void ConsoleCollector::PrintfIgnoredMessage(const char* type,
134                                             const std::string& message,
135                                             bool verbosity)
136 {
137     using namespace DPL::Colors::Text;
138     if (verbosity) {
139         printf("[%s%s%s] %s%s%s\n",
140                CYAN_BEGIN,
141                type,
142                CYAN_END,
143                BOLD_GOLD_BEGIN,
144                message.c_str(),
145                BOLD_GOLD_END);
146     } else {
147         printf("[%s%s%s]\n",
148                CYAN_BEGIN,
149                type,
150                CYAN_END);
151     }
152 }
153
154 void ConsoleCollector::PrintStats(const std::string& title, const Statistic& stats)
155 {
156     using namespace DPL::Colors::Text;
157     printf("\n%sResults [%s]: %s\n", BOLD_GREEN_BEGIN,
158            title.c_str(), BOLD_GREEN_END);
159     printf("%s%s%3zu%s\n",
160            CYAN_BEGIN,
161            "Total tests:            ",
162            stats.GetTotal(),
163            CYAN_END);
164     printf("  %s%s%3zu%s\n",
165            CYAN_BEGIN,
166            "Succeeded:            ",
167            stats.GetPassed(),
168            CYAN_END);
169     printf("  %s%s%3zu%s\n",
170            CYAN_BEGIN,
171            "Failed:               ",
172            stats.GetFailed(),
173            CYAN_END);
174     printf("  %s%s%3zu%s\n",
175            CYAN_BEGIN,
176            "Ignored:              ",
177            stats.GetIgnored(),
178            CYAN_END);
179 }
180
181 } // namespace Test
182 } // namespace DPL