github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/common/ipc/ipc_test.h (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #pragma once
    21  #include "test/common/ipc/ipc_instance.h"
    22  
    23  #define IPC_CHECK_THROW(statement, expected_exception)                         \
    24    {                                                                            \
    25      std::string internal_fail_msg = "";                                        \
    26      bool ipc_test_caught_expected = false;                                     \
    27      try {                                                                      \
    28        internal_fail_msg =                                                      \
    29            "Expected: " #statement " throws " #expected_exception               \
    30            ".\n Actual: it throws nothing. ";                                   \
    31        statement;                                                               \
    32      } catch (expected_exception const &) {                                     \
    33        ipc_test_caught_expected = true;                                         \
    34      } catch (...) {                                                            \
    35        ipc_test_caught_expected = false;                                        \
    36        internal_fail_msg =                                                      \
    37            "Expected: " #statement " throws " #expected_exception               \
    38            ".\n Actual: it throws a different type";                            \
    39      }                                                                          \
    40      if (!ipc_test_caught_expected) {                                           \
    41        std::cerr << internal_fail_msg << std::endl;                             \
    42        exit(1);                                                                 \
    43      }                                                                          \
    44    }
    45  
    46  #define IPC_CHECK_NO_THROW(statement)                                          \
    47    {                                                                            \
    48      std::string internal_fail_msg = "";                                        \
    49      bool ipc_test_caught_nothing = true;                                       \
    50      try {                                                                      \
    51        statement;                                                               \
    52      } catch (...) {                                                            \
    53        ipc_test_caught_nothing = false;                                         \
    54        internal_fail_msg = "Expected: " #statement " throws nothing"            \
    55                            ".\n Actual: it throws an exception";                \
    56      }                                                                          \
    57      if (!ipc_test_caught_nothing) {                                            \
    58        std::cerr << internal_fail_msg << std::endl;                             \
    59        exit(1);                                                                 \
    60      }                                                                          \
    61    }
    62  
    63  #define IPC_CHECK_ANY_THROW(statement)                                         \
    64    {                                                                            \
    65      std::string internal_fail_msg = "";                                        \
    66      bool ipc_test_caught_nothing = true;                                       \
    67      try {                                                                      \
    68        statement;                                                               \
    69      } catch (...) {                                                            \
    70        ipc_test_caught_nothing = false;                                         \
    71      }                                                                          \
    72      if (ipc_test_caught_nothing) {                                             \
    73        internal_fail_msg = "Expected: " #statement " throws anything"           \
    74                            ".\n Actual: it throws nothing.";                    \
    75        std::cerr << internal_fail_msg << std::endl;                             \
    76        exit(1);                                                                 \
    77      }                                                                          \
    78    }
    79  
    80  class IPCCmpExpectHelper {
    81  public:
    82    inline IPCCmpExpectHelper(bool val, const std::string &label)
    83        : m_value(val), m_label(label), m_ss() {}
    84    ~IPCCmpExpectHelper() {
    85      if (!m_value) {
    86        std::cerr << m_label << " expected true.\n Actual: it's false. "
    87                  << m_ss.str() << std::endl;
    88        exit(1);
    89      }
    90    }
    91  
    92    template <typename T1, typename T2>
    93    IPCCmpExpectHelper(T1 &&t1, T2 &&t2, const std::string &label)
    94        : m_value(t1 == t2), m_label(label) {}
    95  
    96    template <typename T> IPCCmpExpectHelper &operator<<(T &&t) {
    97      m_ss << t;
    98      return *this;
    99    }
   100  
   101  protected:
   102    bool m_value;
   103    std::string m_label;
   104    std::stringstream m_ss;
   105  };
   106  
   107  #define IPC_EXPECT(statement) IPCCmpExpectHelper(statement, #statement)
   108  
   109  #define IPC_EXPECT_EQ(a, b)                                                    \
   110    IPCCmpExpectHelper(a, b, std::string(#a) + " == " + std::string(#b))