iutest  1.17.1.0
iutest_spi.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_SPI_HPP_
16 #define INCG_IRIS_IUTEST_SPI_HPP_
17 
18 //======================================================================
19 // include
20 #include "iutest.hpp"
21 
22 #if !defined(IUTEST_USE_GTEST)
23 
24 #if defined(_MSC_VER) && !defined(__clang__)
25 // /ZI オプションだと __LINE__ が __LINE__Var+N(Nは番号) になりコンパイルエラーになるための対応
26 # ifndef __LINE__Var
27 # define __LINE__Var 0
28 # endif
29 #endif
30 
31 //======================================================================
32 // define
37 #define IUTEST_ASSERT_FATAL_FAILURE(statement, substr) \
38  IUTEST_TEST_FATAL_FAILURE_(statement, #statement, substr, IUTEST_ASSERT_FAILURE)
39 
44 #define IUTEST_ASSERT_NONFATAL_FAILURE(statement, substr) \
45  IUTEST_TEST_NONFATAL_FAILURE_(statement, #statement, substr, IUTEST_ASSERT_FAILURE)
46 
51 #define IUTEST_EXPECT_FATAL_FAILURE(statement, substr) \
52  IUTEST_TEST_FATAL_FAILURE_(statement, #statement, substr, IUTEST_EXPECT_FAILURE)
53 
58 #define IUTEST_EXPECT_NONFATAL_FAILURE(statement, substr) \
59  IUTEST_TEST_NONFATAL_FAILURE_(statement, #statement, substr, IUTEST_EXPECT_FAILURE)
60 
61 
67 #if IUTEST_HAS_SPI_LAMBDA_SUPPORT
68 
69 #if IUTEST_HAS_EXCEPTIONS && IUTEST_USE_THROW_ON_ASSERTION_FAILURE
70 # define IIUT_SPI_STATEMENT_EXECUTER(statement) [&](){ try { \
71  ::iutest::detail::ScopedSPITestFlag guard; \
72  statement; \
73  } catch(...) {} \
74  }()
75 #else
76 # define IIUT_SPI_STATEMENT_EXECUTER(statement) \
77  [&](){ ::iutest::detail::ScopedSPITestFlag guard; statement; }()
78 #endif
79 
80 #define IUTEST_TEST_FATAL_FAILURE_(statement, text, substr, on_failure) \
81  IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \
82  if( ::iutest::AssertionResult iutest_spi_ar = [&]() -> ::iutest::AssertionResult { \
83  ::iutest::detail::SPIFailureChecker< \
84  ::iutest::TestPartResult::kFatalFailure> iutest_failure_checker; \
85  IIUT_SPI_STATEMENT_EXECUTER(statement); \
86  return iutest_failure_checker.GetResult(substr); \
87  }() ) \
88  ; \
89  else \
90  on_failure(iutest_spi_ar.message())
91 
92 #define IUTEST_TEST_NONFATAL_FAILURE_(statement, text, substr, on_failure) \
93  IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \
94  if( ::iutest::AssertionResult iutest_spi_ar = [&]() -> ::iutest::AssertionResult { \
95  ::iutest::detail::SPIFailureChecker< \
96  ::iutest::TestPartResult::kNonFatalFailure> iutest_failure_checker; \
97  IIUT_SPI_STATEMENT_EXECUTER(statement); \
98  return iutest_failure_checker.GetResult(substr); \
99  }() ) \
100  ; \
101  else \
102  on_failure(iutest_spi_ar.message())
103 
104 #else
105 
106 #if IUTEST_HAS_EXCEPTIONS && IUTEST_USE_THROW_ON_ASSERTION_FAILURE
107 # define IIUT_SPI_STATEMENT_EXECUTER(statement) struct IUTestFatalFailureStatement { \
108  static void Execute() { ::iutest::detail::ScopedSPITestFlag guard; \
109  try { statement; } catch(...) {} } \
110  }; \
111  IUTestFatalFailureStatement::Execute()
112 #else
113 # define IIUT_SPI_STATEMENT_EXECUTER(statement) struct IUTestFatalFailureStatement { \
114  static void Execute() { ::iutest::detail::ScopedSPITestFlag guard; statement; } \
115  }; \
116  IUTestFatalFailureStatement::Execute()
117 #endif
118 
119 #define IUTEST_TEST_FATAL_FAILURE_(statement, text, substr, on_failure) \
120  IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \
121  if( ::iutest::AssertionResult iutest_spi_ar = ::iutest::AssertionSuccess() ) { \
122  ::iutest::detail::SPIFailureChecker< \
123  ::iutest::TestPartResult::kFatalFailure> iutest_failure_checker; \
124  IIUT_SPI_STATEMENT_EXECUTER(statement); \
125  ::iutest::AssertionResult ar = iutest_failure_checker.GetResult(substr); \
126  if( !ar ) { \
127  iutest_spi_ar << ar.message(); \
128  goto IUTEST_PP_CAT(iutest_label_test_fatalfailure_, __LINE__); \
129  } \
130  } else \
131  IUTEST_PP_CAT(iutest_label_test_fatalfailure_, __LINE__): \
132  on_failure(iutest_spi_ar.message())
133 
134 #define IUTEST_TEST_NONFATAL_FAILURE_(statement, text, substr, on_failure) \
135  IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \
136  if( ::iutest::AssertionResult iutest_spi_ar = ::iutest::AssertionSuccess() ) { \
137  ::iutest::detail::SPIFailureChecker< \
138  ::iutest::TestPartResult::kNonFatalFailure> iutest_failure_checker; \
139  IIUT_SPI_STATEMENT_EXECUTER(statement); \
140  ::iutest::AssertionResult ar = iutest_failure_checker.GetResult(substr); \
141  if( !ar ) { \
142  iutest_spi_ar << ar.message(); \
143  goto IUTEST_PP_CAT(iutest_label_test_fatalfailure_, __LINE__); \
144  } \
145  } else \
146  IUTEST_PP_CAT(iutest_label_test_fatalfailure_, __LINE__): \
147  on_failure(iutest_spi_ar.message())
148 
149 // IUTEST_SUPPRESS_UNREACHABLE_CODE_WARNING(statement);
150 
151 #endif
152 
157 namespace iutest {
158 namespace detail
159 {
160 
161 //======================================================================
162 // class
163 
167 template<TestPartResult::Type Type>
168 class SPIFailureChecker
169  : public NewTestPartResultCheckHelper::Collector<NoTestPartResultReporter>
170 {
171 public:
172  AssertionResult GetResult(const ::std::string& substr)
173  {
174  const char* expected =
175  (Type == TestPartResult::kFatalFailure) ? "1 fatal failure" : "1 non-fatal failure";
176  const size_t num = count();
177  if( num != 1 )
178  {
179  AssertionResult ar = AssertionFailure() << "error: Expected: " << expected
180  << "\n Actual: " << num << " failures\n";
181  for( size_t i=0; i < num; ++i )
182  {
183  ar << GetTestPartResult(i);
184  }
185  return ar;
186  }
187 
188  const TestPartResult& tr = GetTestPartResult(0);
189  if( tr.type() != Type )
190  {
191  return AssertionFailure() << "error: Expected: " << expected
192  << "\"\n Actual:\n" << tr;
193  }
194 
195  if( strstr(tr.message(), substr.c_str()) == NULL )
196  {
197  return AssertionFailure() << "error: Expected: " << expected
198  << " containing \"" << substr
199  << "\"\n Actual:\n" << tr;
200  }
201  return AssertionSuccess();
202  }
203 };
204 
208 class ScopedSPITestFlag : public TestFlag::ScopedGuard
209 {
210 public:
211  ScopedSPITestFlag()
212  {
213  IUTEST_FLAG(throw_on_failure) = false;
214  IUTEST_FLAG(break_on_failure) = false;
215  }
216 };
217 
218 } // end of namespace detail
219 } // end of namespace iutest
220 
221 #else
222 
223 #include "gtest/iutest_spi_switch.hpp"
224 
225 #endif
226 
227 #endif // INCG_IRIS_IUTEST_SPI_HPP_
iutest_config.hpp
iris unit test config
iutest.hpp
iris unit test
iutest
iutest root namespace
Definition: iutest_charcode.hpp:31
IUTEST_FLAG
#define IUTEST_FLAG(name)
フラグセット
Definition: iutest_env.hpp:55
iutest::TestPartResult::kFatalFailure
@ kFatalFailure
致命的な失敗
Definition: iutest_result.hpp:67
iutest::AssertionFailure
AssertionResult AssertionFailure()
テスト失敗を示す AssertionResult オブジェクトの取得
Definition: iutest_assertion.hpp:386
iutest::AssertionSuccess
AssertionResult AssertionSuccess()
テスト成功を示す AssertionResult オブジェクトの取得
Definition: iutest_assertion.hpp:382