1 /* Copyright 2015 Paul Shmakov
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 #include "tests/gtest/teamcity/include/teamcity_gtest.h"
18
19 namespace jetbrains {
20 namespace teamcity {
21
22 using namespace testing;
23
TeamcityGoogleTestEventListener()24 TeamcityGoogleTestEventListener::TeamcityGoogleTestEventListener() {
25 flowid = getFlowIdFromEnvironment();
26 }
27
TeamcityGoogleTestEventListener(const std::string & flowid_)28 TeamcityGoogleTestEventListener::TeamcityGoogleTestEventListener(const std::string& flowid_)
29 : flowid(flowid_) {
30 }
31
32 // Fired before the test case starts.
OnTestCaseStart(const TestCase & test_case)33 void TeamcityGoogleTestEventListener::OnTestCaseStart(const TestCase& test_case) {
34 messages.suiteStarted(test_case.name(), flowid);
35 }
36
37 // Fired before the test starts.
OnTestStart(const TestInfo & test_info)38 void TeamcityGoogleTestEventListener::OnTestStart(const TestInfo& test_info) {
39 messages.testStarted(test_info.name(), flowid);
40 }
41
42 // Fired after the test ends.
OnTestEnd(const TestInfo & test_info)43 void TeamcityGoogleTestEventListener::OnTestEnd(const TestInfo& test_info) {
44 const TestResult* result = test_info.result();
45 if (result->Failed()) {
46 std::string message;
47 std::string details;
48 for (int i = 0; i < result->total_part_count(); ++i) {
49 const TestPartResult& partResult = result->GetTestPartResult(i);
50 if (partResult.passed()) {
51 continue;
52 }
53
54 if (message.empty()) {
55 message = partResult.summary();
56 }
57
58 if (!details.empty()) {
59 details.append("\n");
60 }
61 details.append(partResult.message());
62
63 if (partResult.file_name() && partResult.line_number() >= 0) {
64 std::stringstream ss;
65 ss << "\n at " << partResult.file_name() << ":" << partResult.line_number();
66 details.append(ss.str());
67 }
68 }
69
70 messages.testFailed(
71 test_info.name(),
72 !message.empty() ? message : "failed",
73 details,
74 flowid
75 );
76 }
77 messages.testFinished(test_info.name(), static_cast<int>(result->elapsed_time()), flowid);
78 }
79
80 // Fired after the test case ends.
OnTestCaseEnd(const TestCase & test_case)81 void TeamcityGoogleTestEventListener::OnTestCaseEnd(const TestCase& test_case) {
82 messages.suiteFinished(test_case.name(), flowid);
83 }
84
85 }
86 }
87