1 |
//====================================================================== |
2 |
//----------------------------------------------------------------------- |
3 |
/** |
4 |
* @file iutest_spi.hpp |
5 |
* @brief iris unit test spi 対応用 ファイル |
6 |
* |
7 |
* @author t.shirayanagi |
8 |
* @par copyright |
9 |
* Copyright (C) 2012-2022, Takazumi Shirayanagi\n |
10 |
* This software is released under the new BSD License, |
11 |
* see LICENSE |
12 |
*/ |
13 |
//----------------------------------------------------------------------- |
14 |
//====================================================================== |
15 |
#ifndef INCG_IRIS_IUTEST_SPI_HPP_ |
16 |
#define INCG_IRIS_IUTEST_SPI_HPP_ |
17 |
|
18 |
//====================================================================== |
19 |
// include |
20 |
// IWYU pragma: begin_exports |
21 |
#include "iutest.hpp" |
22 |
// IWYU pragma: end_exports |
23 |
|
24 |
#if !defined(IUTEST_USE_GTEST) |
25 |
|
26 |
#if defined(_MSC_VER) && !defined(__clang__) |
27 |
// /ZI オプションだと __LINE__ が __LINE__Var+N(Nは番号) になりコンパイルエラーになるための対応 |
28 |
# ifndef __LINE__Var |
29 |
# define __LINE__Var 0 |
30 |
# endif |
31 |
#endif |
32 |
|
33 |
//====================================================================== |
34 |
// define |
35 |
/** |
36 |
* @ingroup IUTEST_ASSERT_ |
37 |
* @brief Fatal Failure 発生確認テスト |
38 |
*/ |
39 |
#define IUTEST_ASSERT_FATAL_FAILURE(statement, substr) \ |
40 |
IUTEST_TEST_FATAL_FAILURE_(statement, #statement, substr, IUTEST_ASSERT_FAILURE) |
41 |
|
42 |
/** |
43 |
* @ingroup IUTEST_ASSERT_ |
44 |
* @brief Non Fatal Failure 発生確認テスト |
45 |
*/ |
46 |
#define IUTEST_ASSERT_NONFATAL_FAILURE(statement, substr) \ |
47 |
IUTEST_TEST_NONFATAL_FAILURE_(statement, #statement, substr, IUTEST_ASSERT_FAILURE) |
48 |
|
49 |
/** |
50 |
* @ingroup IUTEST_EXPECT_ |
51 |
* @brief Fatal Failure 発生確認テスト |
52 |
*/ |
53 |
#define IUTEST_EXPECT_FATAL_FAILURE(statement, substr) \ |
54 |
IUTEST_TEST_FATAL_FAILURE_(statement, #statement, substr, IUTEST_EXPECT_FAILURE) |
55 |
|
56 |
/** |
57 |
* @ingroup IUTEST_EXPECT_ |
58 |
* @brief Non Fatal Failure 発生確認テスト |
59 |
*/ |
60 |
#define IUTEST_EXPECT_NONFATAL_FAILURE(statement, substr) \ |
61 |
IUTEST_TEST_NONFATAL_FAILURE_(statement, #statement, substr, IUTEST_EXPECT_FAILURE) |
62 |
|
63 |
|
64 |
/** |
65 |
* @private |
66 |
* @{ |
67 |
*/ |
68 |
|
69 |
#if IUTEST_HAS_SPI_LAMBDA_SUPPORT |
70 |
|
71 |
#if IUTEST_HAS_EXCEPTIONS && IUTEST_USE_THROW_ON_ASSERTION_FAILURE |
72 |
# define IIUT_SPI_STATEMENT_EXECUTER(statement) [&](){ try { \ |
73 |
::iutest::detail::ScopedSPITestFlag guard; \ |
74 |
statement; \ |
75 |
} catch(...) {} \ |
76 |
}() |
77 |
#else |
78 |
# define IIUT_SPI_STATEMENT_EXECUTER(statement) \ |
79 |
[&](){ ::iutest::detail::ScopedSPITestFlag guard; statement; }() |
80 |
#endif |
81 |
|
82 |
#define IUTEST_TEST_FATAL_FAILURE_(statement, text, substr, on_failure) \ |
83 |
IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \ |
84 |
if( ::iutest::AssertionResult iutest_spi_ar = [&]() -> ::iutest::AssertionResult { \ |
85 |
::iutest::detail::SPIFailureChecker iutest_failure_checker(::iutest::TestPartResult::kFatalFailure); \ |
86 |
IIUT_SPI_STATEMENT_EXECUTER(statement); \ |
87 |
return iutest_failure_checker.GetResult(substr); \ |
88 |
}() ) \ |
89 |
; \ |
90 |
else \ |
91 |
on_failure(iutest_spi_ar.message()) |
92 |
|
93 |
#define IUTEST_TEST_NONFATAL_FAILURE_(statement, text, substr, on_failure) \ |
94 |
IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \ |
95 |
if( ::iutest::AssertionResult iutest_spi_ar = [&]() -> ::iutest::AssertionResult { \ |
96 |
::iutest::detail::SPIFailureChecker iutest_failure_checker(::iutest::TestPartResult::kNonFatalFailure); \ |
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 iutest_failure_checker(::iutest::TestPartResult::kFatalFailure); \ |
123 |
IIUT_SPI_STATEMENT_EXECUTER(statement); \ |
124 |
::iutest::AssertionResult ar = iutest_failure_checker.GetResult(substr); \ |
125 |
if( !ar ) { \ |
126 |
iutest_spi_ar << ar.message(); \ |
127 |
goto IUTEST_PP_CAT(iutest_label_test_fatalfailure_, __LINE__); \ |
128 |
} \ |
129 |
} else \ |
130 |
IUTEST_PP_CAT(iutest_label_test_fatalfailure_, __LINE__): \ |
131 |
on_failure(iutest_spi_ar.message()) |
132 |
|
133 |
#define IUTEST_TEST_NONFATAL_FAILURE_(statement, text, substr, on_failure) \ |
134 |
IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \ |
135 |
if( ::iutest::AssertionResult iutest_spi_ar = ::iutest::AssertionSuccess() ) { \ |
136 |
::iutest::detail::SPIFailureChecker iutest_failure_checker(::iutest::TestPartResult::kNonFatalFailure); \ |
137 |
IIUT_SPI_STATEMENT_EXECUTER(statement); \ |
138 |
::iutest::AssertionResult ar = iutest_failure_checker.GetResult(substr); \ |
139 |
if( !ar ) { \ |
140 |
iutest_spi_ar << ar.message(); \ |
141 |
goto IUTEST_PP_CAT(iutest_label_test_fatalfailure_, __LINE__); \ |
142 |
} \ |
143 |
} else \ |
144 |
IUTEST_PP_CAT(iutest_label_test_fatalfailure_, __LINE__): \ |
145 |
on_failure(iutest_spi_ar.message()) |
146 |
|
147 |
// IUTEST_SUPPRESS_UNREACHABLE_CODE_WARNING(statement); |
148 |
|
149 |
#endif |
150 |
|
151 |
/** |
152 |
* @} |
153 |
*/ |
154 |
|
155 |
namespace iutest { |
156 |
namespace detail |
157 |
{ |
158 |
|
159 |
//====================================================================== |
160 |
// class |
161 |
|
162 |
/** |
163 |
* @brief SPI チェッカー |
164 |
*/ |
165 |
class SPIFailureChecker |
166 |
: public NewTestPartResultCheckHelper::Collector<NoTestPartResultReporter> |
167 |
{ |
168 |
public: |
169 |
explicit SPIFailureChecker(TestPartResult::Type type) : m_Type(type) {} |
170 |
public: |
171 |
AssertionResult GetResult(const ::std::string& substr) |
172 |
{ |
173 |
const char* expected = |
174 |
(m_Type == TestPartResult::kFatalFailure) ? "1 fatal failure" : "1 non-fatal failure"; |
175 |
const size_t num = count(); |
176 |
if( num != 1 ) |
177 |
{ |
178 |
AssertionResult ar = AssertionFailure() << "error: Expected: " << expected |
179 |
<< "\n Actual: " << num << " failures\n"; |
180 |
for( size_t i=0; i < num; ++i ) |
181 |
{ |
182 |
ar << GetTestPartResult(i); |
183 |
} |
184 |
return ar; |
185 |
} |
186 |
|
187 |
const TestPartResult& tr = GetTestPartResult(0); |
188 |
if( tr.type() != m_Type ) |
189 |
{ |
190 |
return AssertionFailure() << "error: Expected: " << expected |
191 |
<< "\"\n Actual:\n" << tr; |
192 |
} |
193 |
|
194 |
if( strstr(tr.message(), substr.c_str()) == NULL ) |
195 |
{ |
196 |
return AssertionFailure() << "error: Expected: " << expected |
197 |
<< " containing \"" << substr |
198 |
<< "\"\n Actual:\n" << tr; |
199 |
} |
200 |
return AssertionSuccess(); |
201 |
} |
202 |
private: |
203 |
TestPartResult::Type m_Type; |
204 |
}; |
205 |
|
206 |
/** |
207 |
* @brief フラグ変更スコープ |
208 |
*/ |
209 |
class ScopedSPITestFlag : public TestFlag::ScopedGuard |
210 |
{ |
211 |
public: |
212 |
ScopedSPITestFlag() |
213 |
{ |
214 |
IUTEST_FLAG(throw_on_failure) = false; |
215 |
IUTEST_FLAG(break_on_failure) = false; |
216 |
} |
217 |
}; |
218 |
|
219 |
} // end of namespace detail |
220 |
} // end of namespace iutest |
221 |
|
222 |
#else |
223 |
|
224 |
#include "gtest/iutest_spi_switch.hpp" |
225 |
|
226 |
#endif |
227 |
|
228 |
#endif // INCG_IRIS_IUTEST_SPI_HPP_ |