xref: /aosp_15_r20/external/pigweed/pw_unit_test/rpc_gtest_event_handler.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <tuple>
16 
17 #include "gtest/gtest.h"
18 #include "pw_unit_test/internal/rpc_event_handler.h"
19 #include "pw_unit_test/unit_test_service.h"
20 
21 namespace pw::unit_test::internal {
22 
RpcEventHandler(UnitTestService & service)23 RpcEventHandler::RpcEventHandler(UnitTestService& service) : service_(service) {
24   // Initialize GoogleTest and disable the default result printer.
25   testing::InitGoogleTest();
26   auto unit_test = testing::UnitTest::GetInstance();
27   auto default_listener = unit_test->listeners().default_result_printer();
28   unit_test->listeners().Release(default_listener);
29   delete default_listener;
30 }
31 
ExecuteTests(span<std::string_view> suites_to_run)32 void RpcEventHandler::ExecuteTests(span<std::string_view> suites_to_run) {
33   if (!suites_to_run.empty()) {
34     PW_LOG_WARN(
35         "GoogleTest backend does not support test suite filtering. Running all "
36         "suites.");
37   }
38   if (service_.verbose_) {
39     PW_LOG_WARN(
40         "GoogleTest backend does not support reporting passed expectations.");
41   }
42 
43   auto unit_test = testing::UnitTest::GetInstance();
44   unit_test->listeners().Append(this);
45 
46   std::ignore = RUN_ALL_TESTS();
47 
48   unit_test->listeners().Release(this);
49 }
50 
OnTestProgramStart(const testing::UnitTest &)51 void RpcEventHandler::OnTestProgramStart(const testing::UnitTest&) {
52   service_.WriteTestRunStart();
53 }
54 
OnTestProgramEnd(const testing::UnitTest & unit_test)55 void RpcEventHandler::OnTestProgramEnd(const testing::UnitTest& unit_test) {
56   RunTestsSummary run_tests_summary{
57       .passed_tests = unit_test.successful_test_count(),
58       .failed_tests = unit_test.failed_test_count(),
59       .skipped_tests = unit_test.skipped_test_count(),
60       .disabled_tests = unit_test.disabled_test_count(),
61   };
62   service_.WriteTestRunEnd(run_tests_summary);
63 }
64 
OnTestStart(const testing::TestInfo & test_info)65 void RpcEventHandler::OnTestStart(const testing::TestInfo& test_info) {
66   TestCase test_case{
67       .suite_name = test_info.test_suite_name(),
68       .test_name = test_info.name(),
69       .file_name = test_info.file(),
70   };
71   service_.WriteTestCaseStart(test_case);
72 }
73 
OnTestEnd(const testing::TestInfo & test_info)74 void RpcEventHandler::OnTestEnd(const testing::TestInfo& test_info) {
75   TestResult result;
76   if (test_info.result()->Passed()) {
77     result = TestResult::kSuccess;
78   } else if (test_info.result()->Skipped()) {
79     result = TestResult::kSkipped;
80   } else {
81     result = TestResult::kFailure;
82   }
83 
84   service_.WriteTestCaseEnd(result);
85 }
86 
OnTestPartResult(const testing::TestPartResult & result)87 void RpcEventHandler::OnTestPartResult(const testing::TestPartResult& result) {
88   TestExpectation expectation{
89       .expression = "",
90       .evaluated_expression = result.summary(),
91       .line_number = result.line_number(),
92       .success = result.passed(),
93   };
94   service_.WriteTestCaseExpectation(expectation);
95 }
96 
OnTestDisabled(const testing::TestInfo & test_info)97 void RpcEventHandler::OnTestDisabled(const testing::TestInfo& test_info) {
98   TestCase test_case{
99       .suite_name = test_info.test_suite_name(),
100       .test_name = test_info.name(),
101       .file_name = test_info.file(),
102   };
103   service_.WriteTestCaseDisabled(test_case);
104 }
105 
106 }  // namespace pw::unit_test::internal
107