iutest  1.17.99.14
iutest_body.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_BODY_HPP_3EEA6706_9954_4330_B292_129667FA6B96_
16 #define INCG_IRIS_IUTEST_BODY_HPP_3EEA6706_9954_4330_B292_129667FA6B96_
17 
18 //======================================================================
19 // include
21 
22 namespace iutest
23 {
24 
25 //======================================================================
26 // declare
27 template<typename T>class WithParamInterface;
28 template<typename T>class TestWithParam;
29 
30 namespace detail
31 {
32 
33 ::std::string MakeIndexName(size_t index);
34 
35 }
36 
37 //======================================================================
38 // class
42 class Test
43 {
44  IUTEST_PP_DISALLOW_COPY_AND_ASSIGN(Test);
45 
46 public:
47  Test()
48  : test_info_(NULL)
49  , m_test_info(NULL)
50 #if IUTEST_HAS_GENRAND
51  , m_random_seed(0)
52 #endif
53  {
54  CurrentTestObserver::s_current = this;
55  }
56 
57  virtual ~Test()
58  {
59  CurrentTestObserver::s_current = NULL;
60  }
61 
62 public:
66  static const TestInfo* GetCurrentTestInfo()
67  {
68  const Test* curr = GetCurrentTest();
69  if( curr == NULL || curr->m_test_info == NULL )
70  {
71  return NULL;
72  }
73  return curr->m_test_info->ptr();
74  }
75 
79  static Test* GetCurrentTest() { return CurrentTestObserver::GetCurrentTest(); }
80 
81 
86  static bool HasFatalFailure()
87  {
88  return GetCurrentTest()->m_test_info->HasFatalFailure();
89  }
90 
95  static bool HasNonfatalFailure()
96  {
97  return GetCurrentTest()->m_test_info->HasNonfatalFailure();
98  }
99 
104  static bool HasFailure()
105  {
106  return GetCurrentTest()->m_test_info->HasFailure();
107  }
108 
113  static bool IsSkipped()
114  {
115  return GetCurrentTest()->m_test_info->IsSkipped();
116  }
117 
118 #if !defined(IUTEST_NO_FUNCTION_TEMPLATE_ORDERING)
124  static void RecordProperty(const ::std::string& key, const ::std::string& value);
125 #endif
126 
132  template<typename T>
133  static void RecordProperty(const ::std::string& key, const T& value)
134  {
135  RecordPropertyString(key, PrintToString(value));
136  }
137 
138 #if IUTEST_HAS_GENRAND
139 
145  unsigned int genrand() { return m_random.genrand(); }
151  unsigned int genrand(unsigned int max) { return m_random.genrand(max); }
156  float genrandf() { return m_random.genrandf(); }
158  unsigned int random_seed() const IUTEST_CXX_NOEXCEPT_SPEC { return m_random_seed; }
159 
161  detail::iuRandom& random_engine() { return m_random; }
162 
163 #endif
164 
165 protected:
166  virtual void SetUp() {}
167  virtual void Body() {}
168  virtual void TearDown() {}
169 
170 public:
171  static void SetUpTestSuite() {}
172  static void TearDownTestSuite() {}
173 #if IUTEST_HAS_TESTCASE
174  static void SetUpTestCase() {}
175  static void TearDownTestCase() {}
176 #endif
177 
178 private:
182  void Run(detail::iuITestInfoMediator* test_info);
183 
189  static void RecordPropertyString(const ::std::string& key, const ::std::string& value);
190 
191 private:
192  struct should_be_SetUp {};
193  virtual should_be_SetUp* Setup() IUTEST_CXX_FINAL { return NULL; }
194 
195 private:
196  template<typename DMY>
197  class Observer
198  {
199  public:
200  static Test* s_current;
201  public:
202  static Test* GetCurrentTest() IUTEST_CXX_NOEXCEPT_SPEC { return s_current; }
203  };
204 
205  typedef Observer<void> CurrentTestObserver;
206 
207 private:
208  class TestRecordPropertyHelper
209  {
210  public:
211  static void RecordProperty(const TestProperty& prop);
212  };
213 protected:
214  const TestInfo* test_info_;
215 
216 private:
217  friend class UnitTest;
218  friend class UnitTestImpl;
219  friend class TestInfo;
220  friend class detail::UncaughtScopedTrace;
221 
222  detail::iuITestInfoMediator* m_test_info;
223 #if IUTEST_HAS_GENRAND
224  detail::iuRandom m_random;
225  unsigned int m_random_seed;
226 #endif
227 };
228 
232 template<typename ParamType>
233 struct TestParamInfo
234 {
235  TestParamInfo(const ParamType& p, size_t i)
236  : param(p), index(i) {}
237  ParamType param;
238  size_t index;
239 };
240 
245 template<typename T>
246 class WithParamInterface
247 {
248 public:
249  typedef T ParamType;
251 protected:
252  virtual ~WithParamInterface() {}
253 
254 public:
258  static const ParamType& GetParam()
259  {
260  IUTEST_CHECK_(s_params != NULL) << "GetParam() can only use the value-parameterized test";
261  IUTEST_ANALYSIS_ASSUME(s_params != NULL);
262  return *s_params;
263  }
264 
265 #if IUTEST_HAS_TUPLE
269  template<int N>
270  static const typename tuples::tuple_element<N, ParamType>::type& GetParam()
271  {
272  return tuples::get<N>(GetParam());
273  }
274 #endif
275 
279  static const ::std::string MakeTestParamName(const TestParamInfoType& info)
280  {
281  return detail::MakeIndexName(info.index);
282  }
283 
285  static void SetParam(const ParamType* params) IUTEST_CXX_NOEXCEPT_SPEC { s_params = params; }
286 
287 private:
288  static const ParamType* s_params;
289 };
290 
291 template<typename T>
292 const T* WithParamInterface<T>::s_params = NULL;
293 
298 template<typename T>
299 class TestWithParam : public Test, public WithParamInterface<T>
300 {
301 };
302 
303 namespace detail
304 {
305 
306 #if !defined(IUTEST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(IUTEST_NO_FUNCTION_TEMPLATE_ORDERING)
307 
311 template<typename T>
312 class is_useful_testfixture : public iutest_type_traits::false_type {};
313 
314 namespace is_useful_testfixture_helper
315 {
316 
317 template<typename T>
318 class is_override_setup {
319  template<bool b, typename U>struct impl { typedef iutest_type_traits::false_type type; };
320  template<typename U>struct impl<true, U>
321  {
322  typedef int yes_t;
323  typedef char no_t;
324  static no_t check(void(Test::*)());
325  static yes_t check(...);
326  typedef iutest_type_traits::bool_constant< sizeof(check(&U::SetUp)) == sizeof(yes_t) > type;
327  };
328 public:
329  typedef typename impl< iutest_type_traits::is_base_of<Test, T>::value, T>::type type;
330 };
331 
332 } // end of namespace is_useful_testfixture_helper
333 
334 template<typename T>
335 class is_useful_testfixture<void (int(T))> : public is_useful_testfixture_helper::is_override_setup<T>::type // NOLINT
336 {
337 };
338 
339 #endif
340 
341 inline bool IsDisableTestName(const ::std::string& name)
342 {
343  if( detail::IsStringForwardMatching(name, "DISABLED_")
344  || detail::IsStringContains(name, "/DISABLED_") )
345  {
346  return true;
347  }
348  return false;
349 }
350 
351 } // end of namespace detail
352 
353 } // end of namespace iutest
354 
355 template<typename DMY>
356 ::iutest::Test* ::iutest::Test::Observer<DMY>::s_current = NULL;
357 
358 #if !IUTEST_HAS_LIB
359 # include "impl/iutest_body.ipp" // IWYU pragma: export
360 #endif
361 
362 #endif // INCG_IRIS_IUTEST_BODY_HPP_3EEA6706_9954_4330_B292_129667FA6B96_
テストベース
Definition: iutest_body.hpp:44
detail::iuRandom & random_engine()
Definition: iutest_body.hpp:162
unsigned int random_seed() const IUTEST_CXX_NOEXCEPT_SPEC
Definition: iutest_body.hpp:159
const TestInfo * test_info_
テスト中に簡単にアクセス可能なように
Definition: iutest_body.hpp:215
static bool HasFatalFailure()
致命的なエラーが出たかどうか
Definition: iutest_body.hpp:87
float genrandf()
Definition: iutest_body.hpp:157
static Test * GetCurrentTest()
実行中の Test の取得
Definition: iutest_body.hpp:80
static void RecordProperty(const ::std::string &key, const ::std::string &value)
テスト結果の情報追加
static void TearDownTestSuite()
test suite tear down
Definition: iutest_body.hpp:173
virtual void TearDown()
実行後処理
Definition: iutest_body.hpp:169
virtual void Body()
テスト実装部
Definition: iutest_body.hpp:168
static const TestInfo * GetCurrentTestInfo()
実行中の TestInfo の取得
Definition: iutest_body.hpp:67
static bool HasNonfatalFailure()
致命的ではないエラーが出たかどうか
Definition: iutest_body.hpp:96
static bool IsSkipped()
スキップされたかどうか
Definition: iutest_body.hpp:114
static bool HasFailure()
エラーが出たかどうか
Definition: iutest_body.hpp:105
virtual void SetUp()
実行前処理
Definition: iutest_body.hpp:167
static void SetUpTestSuite()
test suite setup
Definition: iutest_body.hpp:172
unsigned int genrand()
乱数の生成
Definition: iutest_body.hpp:146
テスト情報クラス
Definition: iutest_info.hpp:38
テスト全体の管理者
Definition: iutest_core.hpp:36
パラメータテストインターフェース
Definition: iutest_body.hpp:248
static const ParamType & GetParam()
パラメータの取得
Definition: iutest_body.hpp:259
static const ::std::string MakeTestParamName(const TestParamInfoType &info)
テスト名の生成
Definition: iutest_body.hpp:280
T ParamType
パラメータ型
Definition: iutest_body.hpp:250
TestParamInfo< T > TestParamInfoType
パラメータ情報型
Definition: iutest_body.hpp:251
#define IUTEST_CXX_FINAL
final definition
Definition: iutest_compiler.hpp:756
#define IUTEST_CXX_NOEXCEPT_SPEC
noexcept specification definition
Definition: iutest_compiler.hpp:811
iris unit test 仲介者 ファイル
#define IUTEST_CHECK_(condition)
内部エラーチェック
Definition: iutest_port.hpp:63
iutest root namespace
Definition: iutest_charcode.hpp:33
std::string PrintToString(const T &v)
文字列化
Definition: iutest_printers.hpp:767
値のパラメータ化テストのパラメータ情報
Definition: iutest_body.hpp:235