Remove unnecessary test result from test runner
[platform/core/test/security-tests.git] / src / framework / src / test_results_collector_console.cpp
1 /*
2  * Copyright (c) 2014-2015 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, const TestResult &result)
74 {
75     using namespace DPL::Colors::Text;
76     std::string tmp = "'" + id + "' ...";
77
78     printf("Running test case %-60s", tmp.c_str());
79
80     ConstPerformanceResultPtr performanceResult;
81     switch (result.GetFailStatus()) {
82     case TestResult::FailStatus::NONE:
83         performanceResult = result.GetPerformanceResult();
84         if (!performanceResult) {
85             printf(GREEN_RESULT_OK);
86             break;
87         }
88         if (!performanceResult->IsMaxDuration()) {
89             printf(GREEN_RESULT_OK_TIME,
90                    get_milliseconds(performanceResult->GetDuration()));
91             break;
92         }
93         if (!performanceResult->IsDurationOk()) {
94             printf(GREEN_RESULT_OK_TIME_TOO_LONG(
95                        get_milliseconds(performanceResult->GetDuration()),
96                        get_milliseconds(performanceResult->GetMaxDuration())));
97             break;
98         }
99         printf(GREEN_RESULT_OK_TIME_MAX(
100                    get_milliseconds(performanceResult->GetDuration()),
101                    get_milliseconds(performanceResult->GetMaxDuration())));
102         break;
103     case TestResult::FailStatus::FAILED:
104         PrintfErrorMessage(" FAILED ", result.GetReason());
105         break;
106     case TestResult::FailStatus::IGNORED:
107         PrintfIgnoredMessage("Ignored ", result.GetReason());
108         break;
109     default:
110         Assert(false && "Bad status");
111     }
112     m_stats.AddTest(result.GetFailStatus());
113     m_groupsStats[m_currentGroup].AddTest(result.GetFailStatus());
114 }
115
116 void ConsoleCollector::PrintfErrorMessage(const char* type, const std::string& message)
117 {
118     using namespace DPL::Colors::Text;
119     printf("[%s%s%s]",
120            BOLD_RED_BEGIN,
121            type,
122            BOLD_RED_END);
123     if (m_verbosity) {
124         printf(" %s%s%s",
125                BOLD_YELLOW_BEGIN,
126                message.c_str(),
127                BOLD_YELLOW_END);
128     }
129     printf("\n");
130 }
131
132 void ConsoleCollector::PrintfIgnoredMessage(const char* type, const std::string& message)
133 {
134     using namespace DPL::Colors::Text;
135     printf("[%s%s%s]",
136            CYAN_BEGIN,
137            type,
138            CYAN_END);
139     if (m_verbosity) {
140         printf(" %s%s%s",
141                BOLD_GOLD_BEGIN,
142                message.c_str(),
143                BOLD_GOLD_END);
144     }
145     printf("\n");
146 }
147
148 void ConsoleCollector::PrintStats(const std::string& title, const Statistic& stats)
149 {
150     using namespace DPL::Colors::Text;
151     printf("\n%sResults [%s]: %s\n", BOLD_GREEN_BEGIN,
152            title.c_str(), BOLD_GREEN_END);
153     printf("%s%s%3zu%s\n",
154            CYAN_BEGIN,
155            "Total tests:            ",
156            stats.GetTotal(),
157            CYAN_END);
158     printf("  %s%s%3zu%s\n",
159            CYAN_BEGIN,
160            "Succeeded:            ",
161            stats.GetPassed(),
162            CYAN_END);
163     printf("  %s%s%3zu%s\n",
164            CYAN_BEGIN,
165            "Failed:               ",
166            stats.GetFailed(),
167            CYAN_END);
168     printf("  %s%s%3zu%s\n",
169            CYAN_BEGIN,
170            "Ignored:              ",
171            stats.GetIgnored(),
172            CYAN_END);
173 }
174
175 } // namespace Test
176 } // namespace DPL