LCOV - code coverage report
Current view: top level - tests - engine_suite.cpp (source / functions) Hit Total Coverage
Test: wjrpc-coverage.info Lines: 47 53 88.7 %
Date: 2019-09-13 Functions: 37 40 92.5 %

          Line data    Source code
       1             : #include <wjrpc/handler.hpp>
       2             : #include <wjrpc/engine.hpp>
       3             : #include <wjrpc/method.hpp>
       4             : #include <wjson/_json.hpp>
       5             : #include <fas/testing.hpp>
       6             : 
       7             : #include <algorithm>
       8             : #include <memory>
       9             : 
      10             : namespace {
      11             : 
      12             : 
      13             : namespace request{
      14             : 
      15             : /// -------------------
      16             : template<typename T>
      17           4 : struct base 
      18             : {
      19             :   int first=0; 
      20             :   int second=0;
      21             :   typedef std::unique_ptr<T> ptr;
      22             : };
      23             : 
      24           2 : struct plus: base<plus> {};
      25           2 : struct minus: base<minus> 
      26             : {
      27           1 :   static minus create_schema()
      28             :   {
      29           1 :     minus m;
      30           1 :     m.first = 11;
      31           1 :     m.second = 22;
      32           1 :     return m;
      33             :   }
      34             : };
      35             : 
      36             : /// -------------------
      37             : template<typename T>
      38             : struct T_json
      39             : {
      40           3 :   FAS_NAME(first)
      41           3 :   FAS_NAME(second)
      42             : 
      43             :   typedef ::wjson::object<
      44             :     T,
      45             :     ::wjson::member_list<
      46             :       ::wjson::member<n_first, base<T>,  int,  &base<T>::first>,
      47             :       ::wjson::member<n_second, base<T>, int,  &base<T>::second>
      48             :     >
      49             :   > type;
      50             :   typedef typename type::serializer serializer;
      51             :   typedef typename type::target target;
      52             : };
      53             : 
      54             : struct plus_json: T_json<plus> {};
      55             : struct minus_json: T_json<minus> {};
      56             : 
      57             : }
      58             : 
      59             : namespace response{
      60             : 
      61             : template<typename T>
      62           3 : struct base 
      63             : {
      64             :   int value=0;
      65             :   typedef std::unique_ptr<T> ptr;
      66             :   typedef std::function<void(ptr)> callback;
      67             : };
      68             : 
      69           2 : struct plus: base<plus> {};
      70           1 : struct minus: base<minus> {};
      71             : 
      72             : /// -------------------
      73             : 
      74             : template<typename T>
      75             : struct T_json
      76             : {
      77           3 :   FAS_NAME(value)
      78             : 
      79             :   typedef ::wjson::object<
      80             :     T,
      81             :     ::wjson::member_list<
      82             :       ::wjson::member<n_value, base<T>, int, &base<T>::value>
      83             :     >
      84             :   > type;
      85             :   typedef typename type::serializer serializer;
      86             :   typedef typename type::target target;
      87             : };
      88             : 
      89             : struct plus_json: T_json<plus> {};
      90             : struct minus_json: T_json<minus> {};
      91             : 
      92             : }
      93             : 
      94           1 : struct icalc
      95             : {
      96           1 :   virtual ~icalc() {}
      97             :   virtual void plus( request::plus::ptr req, response::plus::callback cb) = 0;
      98             :   virtual void minus( request::minus::ptr req, response::minus::callback cb) = 0;
      99             : };
     100             : 
     101           2 : class calc
     102             :   : public icalc
     103             : {
     104             : public:
     105           1 :   virtual void plus( request::plus::ptr req, response::plus::callback cb) override
     106             :   {
     107           1 :     if ( cb == nullptr )
     108           0 :       return;
     109             : 
     110           1 :     if ( req == nullptr )
     111           0 :       return cb(nullptr);
     112             : 
     113           1 :     auto res = std::make_unique<response::plus>();
     114           1 :     res->value = req->first + req->second;
     115           1 :     cb( std::move(res) );
     116             :   }
     117             : 
     118           0 :   virtual void minus( request::minus::ptr , response::minus::callback cb) override
     119             :   {
     120           0 :     if ( cb == nullptr )
     121           0 :       return;
     122           0 :     return cb(nullptr);
     123             :   }
     124             : };
     125             : 
     126           2 : JSONRPC_TAG(plus)
     127           1 : JSONRPC_TAG(minus)
     128             : 
     129           2 : struct method_list: wjrpc::method_list
     130             : <
     131             :   wjrpc::target<icalc>,
     132             :   wjrpc::invoke_method<_plus_, request::plus_json, response::plus_json, icalc, &icalc::plus>,
     133             :   wjrpc::invoke_method<_minus_, request::minus_json, response::minus_json, icalc, &icalc::minus>
     134             : >{};
     135             : 
     136           2 : struct handler: ::wjrpc::handler<method_list> {};
     137             : 
     138           3 : UNIT(engine1, "")
     139             : {
     140             :   using namespace ::fas::testing;
     141             :   using namespace ::wjrpc;
     142             :   using namespace wjson::literals;
     143             :   typedef ::wjrpc::engine< handler > engine_type;
     144             :   //typedef std::shared_ptr<engine_type> engine_ptr;
     145           1 :   engine_type::options_type opt;
     146           2 :   auto pcalc = std::make_shared<calc>();
     147           1 :   opt.target = pcalc;
     148             :   /*opt.sender_handler = [](const char*, notify_serializer_t, request_serializer_t, result_handler_t )
     149             :   {
     150             :     
     151             :   };*/
     152           2 :   auto e = std::make_shared<engine_type>();
     153           1 :   e->start(opt, 1);
     154           2 :   std::string sreq = "{\"method\":\"plus\",\"params\":{\"first\":2, \"second\":3},\"id\":1}";
     155           2 :   e->perform_io( std::make_unique<data_type>(sreq.begin(), sreq.end()), 1, [&t](data_ptr d) 
     156             :   {
     157             :     using namespace ::fas::testing;
     158           1 :     std::string ress( d->begin(), d->end() );
     159           1 :     t << message("responce: ") << ress;
     160           1 :     t << equal<expect, std::string>( ress, "{'jsonrpc':'2.0','result':{'value':5},'id':1}"_json);
     161           1 :   });
     162             :   
     163           2 :   auto schl = e->create_schema();
     164           2 :   std::string sch_json;
     165           1 :   decltype(schl)::value_type::json::list::serializer()(schl, std::back_inserter(sch_json) );
     166           1 :   t << equal<expect, std::string>( sch_json, "[{'name':'plus','params':{'first':0,'second':0},'result':{'value':0},'error':{'code':0,'message':'No error'}},{'name':'minus','params':{'first':11,'second':22},'result':{'value':0},'error':{'code':0,'message':'No error'}}]"_json);
     167           2 :   t << nothing;
     168           1 : }
     169             : 
     170             : }
     171             : 
     172           1 : BEGIN_SUITE(engine_suite, "")
     173             :   ADD_UNIT(engine1)
     174           7 : END_SUITE(engine_suite)

Generated by: LCOV version 1.10