xref: /aosp_15_r20/external/pigweed/pw_unit_test/public/pw_unit_test/unit_test_service.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 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 #pragma once
15 
16 #include "pw_log/log.h"
17 #include "pw_unit_test/config.h"
18 #include "pw_unit_test/event_handler.h"
19 #include "pw_unit_test/internal/rpc_event_handler.h"
20 #include "pw_unit_test_proto/unit_test.pwpb.h"
21 #include "pw_unit_test_proto/unit_test.raw_rpc.pb.h"
22 
23 namespace pw::unit_test {
24 
25 class UnitTestService final
26     : public pw_rpc::raw::UnitTest::Service<UnitTestService> {
27  public:
UnitTestService()28   UnitTestService() : handler_(*this), verbose_(false) {}
29 
30   void Run(ConstByteSpan request, RawServerWriter& writer);
31 
32  private:
33   friend class internal::RpcEventHandler;
34 
35   // TODO(frolv): This function essentially performs what a pw_protobuf RPC
36   // method would do. Once that API is implemented, this service should be
37   // migrated to it.
38   template <typename WriteFunction>
WriteEvent(WriteFunction event_writer)39   void WriteEvent(WriteFunction event_writer) {
40     pwpb::Event::MemoryEncoder event(encoding_buffer_);
41     event_writer(event);
42     if (event.status().ok()) {
43       writer_.Write(event)
44           .IgnoreError();  // TODO: b/242598609 - Handle Status properly
45     }
46   }
47 
48   void WriteTestRunStart();
49   void WriteTestRunEnd(const RunTestsSummary& summary);
50   void WriteTestCaseStart(const TestCase& test_case);
51   void WriteTestCaseEnd(TestResult result);
52   void WriteTestCaseDisabled(const TestCase& test_case);
53   void WriteTestCaseExpectation(const TestExpectation& expectation);
54 
55   internal::RpcEventHandler handler_;
56   RawServerWriter writer_;
57   bool verbose_;
58   std::array<std::byte, config::kEventBufferSize> encoding_buffer_ = {};
59 };
60 
61 }  // namespace pw::unit_test
62