LCOV - code coverage report
Current view: top level - tests - method_suite.cpp (source / functions) Hit Total Coverage
Test: wjrpc-coverage.info Lines: 41 47 87.2 %
Date: 2019-09-13 Functions: 25 27 92.6 %

          Line data    Source code
       1             : #include <iostream>
       2             : #include <wjrpc/method.hpp>
       3             : #include <wjrpc/noparams_json.hpp>
       4             : #include <wjrpc/method/aspect/name.hpp>
       5             : #include <wjrpc/memory.hpp>
       6             : #include <wjrpc/handler.hpp>
       7             : #include <fas/testing.hpp>
       8             : #include "req.hpp"
       9             : 
      10             : namespace 
      11             : {
      12             : 
      13             : 
      14             : namespace request{
      15             : 
      16             :   
      17             : template<typename T>
      18           2 : struct base 
      19             : {
      20             :   int first=0; 
      21             :   int second=0;
      22             :   typedef std::unique_ptr<T> ptr;
      23             : };
      24           2 : struct plus: base<plus> {};
      25             : struct munus: base<munus> {};
      26             : 
      27             : template<typename T>
      28             : struct T_json
      29             : {
      30           1 :   FAS_NAME(first)
      31           1 :   FAS_NAME(second)
      32             : 
      33             :   typedef ::wjson::object<
      34             :     T,
      35             :     ::wjson::member_list<
      36             :       ::wjson::member<n_first, base<T>,  int,  &base<T>::first>,
      37             :       ::wjson::member<n_second, base<T>, int,  &base<T>::second>
      38             :     >
      39             :   > type;
      40             :   typedef typename type::serializer serializer;
      41             :   typedef typename type::target target;
      42             : };
      43             : 
      44             : struct plus_json: T_json<plus> {};
      45             : struct munus_json: T_json<munus> {};
      46             : 
      47             : }
      48             : 
      49             : namespace response{
      50             : 
      51             : template<typename T>
      52           1 : struct base 
      53             : {
      54             :   int value=0;
      55             :   typedef std::unique_ptr<T> ptr;
      56             :   typedef std::function<void(ptr)> callback;
      57             :   typedef std::function<void(ptr, ::wjrpc::error::ptr)> callback2;
      58             : };
      59             : 
      60           1 : struct plus: base<plus> {};
      61             : struct munus: base<munus> {};
      62             : 
      63             : template<typename T>
      64             : struct T_json
      65             : {
      66           1 :   FAS_NAME(value)
      67             : 
      68             :   typedef ::wjson::object<
      69             :     T,
      70             :     ::wjson::member_list<
      71             :       ::wjson::member<n_value, base<T>, int, &base<T>::value>
      72             :     >
      73             :   > type;
      74             :   typedef typename type::serializer serializer;
      75             :   typedef typename type::target target;
      76             : };
      77             : 
      78             : struct plus_json: T_json<plus> {};
      79             : struct munus_json: T_json<munus> {};
      80             : 
      81             : }
      82             : 
      83             : struct plus_handler
      84             : {
      85             :   template<typename T>
      86           0 :   void operator() ( T&, request::plus::ptr) const
      87             :   {
      88           0 :   }
      89             :   
      90             :   template<typename T>
      91           1 :   void operator() ( T&, request::plus::ptr req, response::plus::callback2 cb) const
      92             :   {
      93           1 :     auto res = std::make_unique<response::plus>();
      94           1 :     res->value = req->first + req->second;
      95           1 :     cb( std::move(res), nullptr );
      96           1 :   }
      97             : };
      98             : 
      99           1 : JSONRPC_TAG(plus)
     100             : JSONRPC_TAG(minus)
     101             : 
     102             : struct invoke_method_plus:
     103             :   ::wjrpc::basic_method<
     104             :     ::wjrpc::name<_plus_>,
     105             :     ::wjrpc::invoke< request::plus_json, response::plus_json, plus_handler >,
     106             :     ::wjrpc::send_result<80>
     107             : >
     108             : {};
     109             : 
     110           1 : struct call_method_plus:
     111             :   ::wjrpc::basic_method<
     112             :     ::wjrpc::name<_plus_>,
     113             :     ::wjrpc::remote_call< request::plus_json, response::plus_json>
     114             :   >
     115             :   {};
     116             : 
     117           3 : UNIT(method1, "")
     118             : {
     119             :   using namespace fas::testing;
     120             :   //using namespace ::wjrpc;
     121             : 
     122           1 :   ::wjrpc::handler< ::wjrpc::method_list<> > h;
     123           2 :   std::string sreq="{\"method\":\"plus\",\"id\":1,\"params\":{\"first\":1,\"second\":2}}";
     124             :   invoke_method_plus im;
     125           2 :   ::wjrpc::incoming_holder holder( std::make_unique<::wjrpc::data_type>( sreq.begin(), sreq.end() ) );
     126           1 :   ::wjson::json_error e;
     127           1 :   holder.parse(&e);
     128           1 :   t << is_false<assert>( e ) << FAS_FL;
     129           1 :   t << stop;
     130           3 :   im( h, std::move(holder), [&t]( ::wjrpc::outgoing_holder res )
     131             :   {
     132           1 :     auto d = res.detach();
     133           1 :     t << message("TRACE:") << std::string(d->begin(), d->end() );
     134           5 :   } );
     135             :   
     136           1 : }
     137             : 
     138           3 : UNIT(method2, "")
     139             : {
     140             :   using namespace fas::testing;
     141             :   
     142           1 :   ::wjrpc::handler< ::wjrpc::method_list<call_method_plus> > h;
     143           2 :   ::wjrpc::handler_options<> opt;
     144           1 :   opt.sender_handler = []( 
     145             :     const char* , 
     146             :     ::wjrpc::handler_types::notify_serializer_t, 
     147             :     ::wjrpc::handler_types::request_serializer_t, 
     148             :     ::wjrpc::handler_types::result_handler_t
     149           1 :   )
     150             :   {
     151             :     
     152           1 :   };
     153           1 :   h.start(opt, 1);
     154             :   //std::string sreq="{\"method\":\"plus\",\"id\":1,\"params\":{\"first\":1,\"first\":2}}";
     155             :   call_method_plus cm;
     156           2 :   auto req = std::make_unique< request::plus >();
     157           1 :   req->first=1; req->second=2;
     158           1 :   cm.call( h, std::move(req), [&t]( response::plus::ptr res, ::wjrpc::error::ptr err )
     159             :   {
     160           0 :     t << equal<expect>( res->value, 2 ) << FAS_FL;
     161           0 :     ::wjrpc::error::ptr errchk;
     162           0 :     t << equal<expect>( err, errchk ) << FAS_FL;
     163           0 :   });
     164             :   
     165           2 :   t << nothing;
     166           1 : }
     167             : 
     168             : }
     169             : 
     170           1 : BEGIN_SUITE(method_suite, "")
     171             :   ADD_UNIT(method1)
     172             :   ADD_UNIT(method2)
     173           7 : END_SUITE(method_suite)

Generated by: LCOV version 1.10