Add --no-verbose option to HTML collector
[platform/core/test/security-tests.git] / src / 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     : m_verbosity(true)
38 {
39 }
40
41 TestResultsCollectorBase* ConsoleCollector::Constructor()
42 {
43     return new ConsoleCollector();
44 }
45
46 bool ConsoleCollector::ParseCollectorSpecificArg(const std::string& arg)
47 {
48     return ParseCollectorNoVerboseArg(arg, m_verbosity);
49 }
50
51 std::string ConsoleCollector::CollectorSpecificHelp() const
52 {
53     return COLLECTOR_NO_VERBOSE_HELP;
54 }
55
56 void ConsoleCollector::CollectCurrentTestGroupName(const std::string& name)
57 {
58     printf("Starting group %s\n", name.c_str());
59     m_currentGroup = name;
60 }
61
62 void ConsoleCollector::Finish()
63 {
64     using namespace DPL::Colors::Text;
65
66     // Show result
67     for (auto &group : m_groupsStats) {
68         PrintStats(group.first, group.second);
69     }
70     PrintStats("All tests together", m_stats);
71 }
72
73 void ConsoleCollector::CollectResult(const std::string& id,
74                                      const FailStatus status,
75                                      const std::string& reason,
76                                      const bool& isPerformanceTest,
77                                      const std::chrono::system_clock::duration& performanceTime,
78                                      const std::chrono::system_clock::duration& performanceMaxTime)
79 {
80     using namespace DPL::Colors::Text;
81     std::string tmp = "'" + id + "' ...";
82
83     printf("Running test case %-60s", tmp.c_str());
84     switch (status) {
85     case FailStatus::NONE:
86         if (isPerformanceTest) {
87             if (performanceMaxTime <= std::chrono::microseconds::zero()) {
88                 printf(GREEN_RESULT_OK_TIME,
89                         get_milliseconds(performanceTime));
90                 break;
91             }
92             else {
93                 if (performanceTime > performanceMaxTime)
94                     printf(GREEN_RESULT_OK_TIME_TOO_LONG(
95                             get_milliseconds(performanceTime),
96                             get_milliseconds(performanceMaxTime)));
97                 else
98                     printf(GREEN_RESULT_OK_TIME_MAX(
99                             get_milliseconds(performanceTime),
100                             get_milliseconds(performanceMaxTime)));
101                 break;
102             }
103         }
104         printf(GREEN_RESULT_OK);
105         break;
106     case FailStatus::FAILED:
107         PrintfErrorMessage(" FAILED ", reason);
108         break;
109     case FailStatus::IGNORED:
110         PrintfIgnoredMessage("Ignored ", reason);
111         break;
112     default:
113         Assert(false && "Bad status");
114     }
115     m_stats.AddTest(status);
116     m_groupsStats[m_currentGroup].AddTest(status);
117 }
118
119 void ConsoleCollector::PrintfErrorMessage(const char* type, const std::string& message)
120 {
121     using namespace DPL::Colors::Text;
122     printf("[%s%s%s]",
123            BOLD_RED_BEGIN,
124            type,
125            BOLD_RED_END);
126     if (m_verbosity) {
127         printf(" %s%s%s",
128                BOLD_YELLOW_BEGIN,
129                message.c_str(),
130                BOLD_YELLOW_END);
131     }
132     printf("\n");
133 }
134
135 void ConsoleCollector::PrintfIgnoredMessage(const char* type, const std::string& message)
136 {
137     using namespace DPL::Colors::Text;
138     printf("[%s%s%s]",
139            CYAN_BEGIN,
140            type,
141            CYAN_END);
142     if (m_verbosity) {
143         printf(" %s%s%s",
144                BOLD_GOLD_BEGIN,
145                message.c_str(),
146                BOLD_GOLD_END);
147     }
148     printf("\n");
149 }
150
151 void ConsoleCollector::PrintStats(const std::string& title, const Statistic& stats)
152 {
153     using namespace DPL::Colors::Text;
154     printf("\n%sResults [%s]: %s\n", BOLD_GREEN_BEGIN,
155            title.c_str(), BOLD_GREEN_END);
156     printf("%s%s%3zu%s\n",
157            CYAN_BEGIN,
158            "Total tests:            ",
159            stats.GetTotal(),
160            CYAN_END);
161     printf("  %s%s%3zu%s\n",
162            CYAN_BEGIN,
163            "Succeeded:            ",
164            stats.GetPassed(),
165            CYAN_END);
166     printf("  %s%s%3zu%s\n",
167            CYAN_BEGIN,
168            "Failed:               ",
169            stats.GetFailed(),
170            CYAN_END);
171     printf("  %s%s%3zu%s\n",
172            CYAN_BEGIN,
173            "Ignored:              ",
174            stats.GetIgnored(),
175            CYAN_END);
176 }
177
178 } // namespace Test
179 } // namespace DPL