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