blob: 2fadec77724f557cfafd4eb6cfe432ef995d0af5 [file] [log] [blame]
Wyatt Heplerd1591422020-09-15 10:04:41 -07001// 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
15#include "pw_rpc/service.h"
16
17#include "gtest/gtest.h"
18#include "pw_rpc/internal/base_method.h"
19
20namespace pw::rpc {
21
22class ServiceTestHelper {
23 public:
24 static const internal::BaseMethod* FindMethod(Service& service, uint32_t id) {
25 return service.FindMethod(id);
26 }
27};
28
29namespace {
30
31void InvokeIt(const internal::BaseMethod&,
32 internal::ServerCall&,
33 const internal::Packet&) {}
34
35class ServiceTestMethod : public internal::BaseMethod {
36 public:
37 constexpr ServiceTestMethod(uint32_t id, char the_value)
38 : internal::BaseMethod(id, InvokeIt), value(the_value) {}
39
40 char value; // Add a member so the class is larger than the base Method.
41};
42
43class TestService : public Service {
44 public:
45 constexpr TestService() : Service(0xabcd, kMethods) {}
46
47 static constexpr std::array<ServiceTestMethod, 3> kMethods = {{
48 ServiceTestMethod(123, 'a'),
49 ServiceTestMethod(456, 'b'),
50 ServiceTestMethod(789, 'c'),
51 }};
52};
53
54TEST(Service, MultipleMethods_FindMethod_Present) {
55 TestService service;
56 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 123),
57 &TestService::kMethods[0]);
58 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 456),
59 &TestService::kMethods[1]);
60 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 789),
61 &TestService::kMethods[2]);
62}
63
64TEST(Service, MultipleMethods_FindMethod_NotPresent) {
65 TestService service;
66 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 0), nullptr);
67 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 457), nullptr);
68 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 999), nullptr);
69}
70
71class EmptyTestService : public Service {
72 public:
73 constexpr EmptyTestService() : Service(0xabcd, kMethods) {}
74 static constexpr std::array<ServiceTestMethod, 0> kMethods = {{}};
75};
76
77TEST(Service, NoMethods_FindMethod_NotPresent) {
78 EmptyTestService service;
79 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 123), nullptr);
80 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 456), nullptr);
81 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 789), nullptr);
82}
83
84} // namespace
85} // namespace pw::rpc