• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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_rpc/internal/hash.h"
17 #include "pw_rpc/internal/method_info.h"
18 
19 namespace pw::rpc::internal {
20 
21 // Tests the MethodTraits specializations for test.proto for any implementation.
22 template <typename GeneratedClass, typename ServiceImpl>
23 class MethodInfoTests {
24  public:
Pass()25   constexpr bool Pass() const {
26     return Ids().Pass() && MethodFunction().Pass();
27   }
28 
29  private:
30   struct Ids {
PassIds31     constexpr bool Pass() const { return true; }
32 
33 #define PW_RPC_TEST_METHOD_INFO_IDS(function)                             \
34   static_assert(MethodInfo<GeneratedClass::function>::kServiceId ==       \
35                     Hash("pw.rpc.test.TestService"),                      \
36                 #function " service ID doesn't match!");                  \
37   static_assert(                                                          \
38       MethodInfo<GeneratedClass::function>::kMethodId == Hash(#function), \
39       #function " method ID doesn't match!")
40 
41     PW_RPC_TEST_METHOD_INFO_IDS(TestUnaryRpc);
42     PW_RPC_TEST_METHOD_INFO_IDS(TestAnotherUnaryRpc);
43     PW_RPC_TEST_METHOD_INFO_IDS(TestServerStreamRpc);
44     PW_RPC_TEST_METHOD_INFO_IDS(TestClientStreamRpc);
45     PW_RPC_TEST_METHOD_INFO_IDS(TestBidirectionalStreamRpc);
46 #undef PW_RPC_TEST_METHOD_INFO_IDS
47   };
48 
49   static_assert(MethodInfo<GeneratedClass::TestClientStreamRpc>::kServiceId !=
50                     Hash("TestService"),
51                 "Wrong service name should not match");
52   static_assert(
53       MethodInfo<GeneratedClass::TestBidirectionalStreamRpc>::kMethodId !=
54           Hash("TestUnaryRpc"),
55       "Wrong method name should not match");
56 
57   struct MethodFunction {
PassMethodFunction58     constexpr bool Pass() const { return true; }
59 
60 #define PW_RPC_TEST_METHOD_INFO_FUNCTION(function)                       \
61   static_assert(MethodInfo<GeneratedClass::function>::template Function< \
62                     ServiceImpl>() == &ServiceImpl::function)
63 
64     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestUnaryRpc);
65     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestAnotherUnaryRpc);
66     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestServerStreamRpc);
67     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestClientStreamRpc);
68     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestBidirectionalStreamRpc);
69 #undef PW_RPC_TEST_METHOD_INFO_FUNCTION
70   };
71 };
72 
73 }  // namespace pw::rpc::internal
74