• 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 <type_traits>
17 
18 #include "pw_rpc/internal/hash.h"
19 #include "pw_rpc/internal/method_info.h"
20 #include "pw_rpc/method_info.h"
21 
22 namespace pw::rpc::internal {
23 
24 // Tests the MethodTraits specializations for test.proto for any implementation.
25 template <typename GeneratedClass, typename ServiceImpl>
26 class MethodInfoTests {
27  public:
Pass()28   constexpr bool Pass() const {
29     return Ids().Pass() && MethodFunction().Pass() &&
30            MethodRequestResponseTypes().Pass();
31   }
32 
33  private:
34   struct Ids {
PassIds35     constexpr bool Pass() const { return true; }
36 
37 #define PW_RPC_TEST_METHOD_INFO_IDS(function)                             \
38   static_assert(MethodInfo<GeneratedClass::function>::kServiceId ==       \
39                     Hash("pw.rpc.test.TestService"),                      \
40                 #function " service ID doesn't match!");                  \
41   static_assert(                                                          \
42       MethodInfo<GeneratedClass::function>::kMethodId == Hash(#function), \
43       #function " method ID doesn't match!")
44 
45     PW_RPC_TEST_METHOD_INFO_IDS(TestUnaryRpc);
46     PW_RPC_TEST_METHOD_INFO_IDS(TestAnotherUnaryRpc);
47     PW_RPC_TEST_METHOD_INFO_IDS(TestServerStreamRpc);
48     PW_RPC_TEST_METHOD_INFO_IDS(TestClientStreamRpc);
49     PW_RPC_TEST_METHOD_INFO_IDS(TestBidirectionalStreamRpc);
50 #undef PW_RPC_TEST_METHOD_INFO_IDS
51   };
52 
53   static_assert(MethodInfo<GeneratedClass::TestClientStreamRpc>::kServiceId !=
54                     Hash("TestService"),
55                 "Wrong service name should not match");
56   static_assert(
57       MethodInfo<GeneratedClass::TestBidirectionalStreamRpc>::kMethodId !=
58           Hash("TestUnaryRpc"),
59       "Wrong method name should not match");
60 
61   struct MethodFunction {
PassMethodFunction62     constexpr bool Pass() const { return true; }
63 
64 #define PW_RPC_TEST_METHOD_INFO_FUNCTION(function)                       \
65   static_assert(MethodInfo<GeneratedClass::function>::template Function< \
66                     ServiceImpl>() == &ServiceImpl::function)
67 
68     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestUnaryRpc);
69     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestAnotherUnaryRpc);
70     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestServerStreamRpc);
71     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestClientStreamRpc);
72     PW_RPC_TEST_METHOD_INFO_FUNCTION(TestBidirectionalStreamRpc);
73 #undef PW_RPC_TEST_METHOD_INFO_FUNCTION
74   };
75 
76   struct MethodRequestResponseTypes {
PassMethodRequestResponseTypes77     constexpr bool Pass() const { return true; }
78 
79 #define PW_RPC_TEST_PUBLIC_METHOD_INFO_TYPES(function)                         \
80   static_assert(                                                               \
81       std::is_same_v<typename MethodInfo<GeneratedClass::function>::Request,   \
82                      ::pw::rpc::MethodRequestType<GeneratedClass::function>>); \
83   static_assert(                                                               \
84       std::is_same_v<typename MethodInfo<GeneratedClass::function>::Response,  \
85                      ::pw::rpc::MethodResponseType<GeneratedClass::function>>)
86 
87     PW_RPC_TEST_PUBLIC_METHOD_INFO_TYPES(TestUnaryRpc);
88     PW_RPC_TEST_PUBLIC_METHOD_INFO_TYPES(TestAnotherUnaryRpc);
89     PW_RPC_TEST_PUBLIC_METHOD_INFO_TYPES(TestServerStreamRpc);
90     PW_RPC_TEST_PUBLIC_METHOD_INFO_TYPES(TestClientStreamRpc);
91     PW_RPC_TEST_PUBLIC_METHOD_INFO_TYPES(TestBidirectionalStreamRpc);
92 #undef PW_RPC_TEST_PUBLIC_METHOD_INFO_TYPES
93   };
94 };
95 
96 }  // namespace pw::rpc::internal
97