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/internal/rpc_event_handler.h" 18 #include "pw_unit_test_proto/unit_test.pwpb.h" 19 #include "pw_unit_test_proto/unit_test.raw_rpc.pb.h" 20 21 namespace pw::unit_test { 22 23 class UnitTestService final : public generated::UnitTest<UnitTestService> { 24 public: UnitTestService()25 UnitTestService() : handler_(*this), verbose_(false) {} 26 27 void Run(ServerContext& ctx, ConstByteSpan request, RawServerWriter& writer); 28 29 private: 30 friend class internal::RpcEventHandler; 31 32 // TODO(frolv): This function essentially performs what a pw_protobuf RPC 33 // method would do. Once that API is implemented, this service should be 34 // migrated to it. 35 template <typename WriteFunction> WriteEvent(WriteFunction event_writer)36 void WriteEvent(WriteFunction event_writer) { 37 protobuf::NestedEncoder<2, 3> encoder(writer_.PayloadBuffer()); 38 Event::Encoder event(&encoder); 39 event_writer(event); 40 if (Result<ConstByteSpan> result = encoder.Encode(); result.ok()) { 41 writer_.Write(result.value()); 42 } 43 } 44 45 void WriteTestRunStart(); 46 void WriteTestRunEnd(const RunTestsSummary& summary); 47 void WriteTestCaseStart(const TestCase& test_case); 48 void WriteTestCaseEnd(TestResult result); 49 void WriteTestCaseDisabled(const TestCase& test_case); 50 void WriteTestCaseExpectation(const TestExpectation& expectation); 51 52 internal::RpcEventHandler handler_; 53 RawServerWriter writer_; 54 bool verbose_; 55 }; 56 57 } // namespace pw::unit_test 58