iutest  1.17.1.0
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)
119 
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 #if IUTEST_HAS_AUTOFIXTURE_PARAM_TEST
168  virtual void Body() {}
169 #else
170  virtual void Body() = 0;
171 #endif
172  virtual void TearDown() {}
173 
174 public:
175  static void SetUpTestCase() {}
176  static void TearDownTestCase() {}
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 
221  detail::iuITestInfoMediator* m_test_info;
222 #if IUTEST_HAS_GENRAND
223  detail::iuRandom m_random;
224  unsigned int m_random_seed;
225 #endif
226 };
227 
231 template<typename ParamType>
232 struct TestParamInfo
233 {
234  TestParamInfo(const ParamType& p, size_t i)
235  : param(p), index(i) {}
236  ParamType param;
237  size_t index;
238 };
239 
244 template<typename T>
245 class WithParamInterface
246 {
247 public:
248  typedef T ParamType;
250 protected:
251  virtual ~WithParamInterface() {}
252 
253 public:
257  static const ParamType& GetParam()
258  {
259  IUTEST_CHECK_(s_params != NULL) << "GetParam() can only use the value-parameterized test";
260  IUTEST_ANALYSIS_ASSUME(s_params != NULL);
261  return *s_params;
262  }
263 
264 #if IUTEST_HAS_TUPLE
265 
268  template<int N>
269  static const typename tuples::tuple_element<N, ParamType>::type& GetParam()
270  {
271  return tuples::get<N>(GetParam());
272  }
273 #endif
274 
278  static const ::std::string MakeTestParamName(const TestParamInfoType& info)
279  {
280  return detail::MakeIndexName(info.index);
281  }
282 
284  static void SetParam(const ParamType* params) IUTEST_CXX_NOEXCEPT_SPEC { s_params = params; }
285 
286 private:
287  static const ParamType* s_params;
288 };
289 
290 template<typename T>
291 const T* WithParamInterface<T>::s_params = NULL;
292 
297 template<typename T>
298 class TestWithParam : public Test, public WithParamInterface<T>
299 {
300 };
301 
302 namespace detail
303 {
304 
305 #if !defined(IUTEST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(IUTEST_NO_FUNCTION_TEMPLATE_ORDERING)
306 
310 template<typename T>
311 class is_useful_testfixture : public iutest_type_traits::false_type {};
312 
313 namespace is_useful_testfixture_helper
314 {
315 
316 template<typename T>
317 class is_override_setup {
318  template<bool b, typename U>struct impl { typedef iutest_type_traits::false_type type; };
319  template<typename U>struct impl<true, U>
320  {
321  typedef int yes_t;
322  typedef char no_t;
323  static no_t check(void(Test::*)());
324  static yes_t check(...);
325  typedef iutest_type_traits::bool_constant< sizeof(check(&U::SetUp)) == sizeof(yes_t) > type;
326  };
327 public:
328  typedef typename impl< iutest_type_traits::is_base_of<Test, T>::value, T>::type type;
329 };
330 
331 } // end of namespace is_useful_testfixture_helper
332 
333 template<typename T>
334 class is_useful_testfixture<void (int(T))> : public is_useful_testfixture_helper::is_override_setup<T>::type // NOLINT
335 {
336 };
337 
338 #endif
339 
340 inline bool IsDisableTestName(const ::std::string& name)
341 {
342  if( detail::IsStringForwardMatching(name, "DISABLED_")
343  || detail::IsStringContains(name, "/DISABLED_") )
344  {
345  return true;
346  }
347  return false;
348 }
349 
350 } // end of namespace detail
351 
352 } // end of namespace iutest
353 
354 template<typename DMY>
355 ::iutest::Test* ::iutest::Test::Observer<DMY>::s_current = NULL;
356 
357 #if !IUTEST_HAS_LIB
358 # include "impl/iutest_body.ipp"
359 #endif
360 
361 #endif // INCG_IRIS_IUTEST_BODY_HPP_3EEA6706_9954_4330_B292_129667FA6B96_
iutest::Test
テストベース
Definition: iutest_body.hpp:43
iutest::WithParamInterface::GetParam
static const ParamType & GetParam()
パラメータの取得
Definition: iutest_body.hpp:258
iutest::TestInfo
テスト情報クラス
Definition: iutest_info.hpp:32
iutest_config.hpp
iris unit test config
iutest::Test::IsSkipped
static bool IsSkipped()
スキップされたかどうか
Definition: iutest_body.hpp:114
iutest::Test::RecordProperty
static void RecordProperty(const ::std::string &key, const T &value)
テスト結果の情報追加
Definition: iutest_body.hpp:134
iutest::PrintToString
std::string PrintToString(const T &v)
文字列化
Definition: iutest_printers.hpp:678
iutest::WithParamInterface::GetParam
static const tuples::tuple_element< N, ParamType >::type & GetParam()
パラメータの取得
Definition: iutest_body.hpp:270
IUTEST_CXX_NOEXCEPT_SPEC
#define IUTEST_CXX_NOEXCEPT_SPEC
noexcept specification definition
Definition: iutest_compiler.hpp:734
iutest::WithParamInterface::ParamType
T ParamType
パラメータ型
Definition: iutest_body.hpp:249
iutest::Test::HasFatalFailure
static bool HasFatalFailure()
致命的なエラーが出たかどうか
Definition: iutest_body.hpp:87
iutest::Test::genrand
unsigned int genrand(unsigned int max)
Definition: iutest_body.hpp:152
iutest::Test::test_info_
const TestInfo * test_info_
テスト中に簡単にアクセス可能なように
Definition: iutest_body.hpp:215
iutest
iutest root namespace
Definition: iutest_charcode.hpp:31
iutest::UnitTest
テスト全体の管理者
Definition: iutest_core.hpp:33
IUTEST_CXX_FINAL
#define IUTEST_CXX_FINAL
final definition
Definition: iutest_compiler.hpp:679
iutest::Test::GetCurrentTestInfo
static const TestInfo * GetCurrentTestInfo()
実行中の TestInfo の取得
Definition: iutest_body.hpp:67
iutest::Test::genrand
unsigned int genrand()
乱数の生成
Definition: iutest_body.hpp:146
iutest::WithParamInterface::MakeTestParamName
static const ::std::string MakeTestParamName(const TestParamInfoType &info)
テスト名の生成
Definition: iutest_body.hpp:279
iutest::Test::HasFailure
static bool HasFailure()
エラーが出たかどうか
Definition: iutest_body.hpp:105
iutest::TestWithParam
パラメータテストベース
Definition: iutest_body.hpp:29
iutest::WithParamInterface::TestParamInfoType
TestParamInfo< T > TestParamInfoType
パラメータ情報型
Definition: iutest_body.hpp:250
iutest::Test::random_seed
unsigned int random_seed() const IUTEST_CXX_NOEXCEPT_SPEC
Definition: iutest_body.hpp:159
iutest::Test::genrandf
float genrandf()
Definition: iutest_body.hpp:157
iutest::Test::HasNonfatalFailure
static bool HasNonfatalFailure()
致命的ではないエラーが出たかどうか
Definition: iutest_body.hpp:96
iutest::Test::random_engine
detail::iuRandom & random_engine()
Definition: iutest_body.hpp:162
iutest::Test::RecordProperty
static void RecordProperty(const ::std::string &key, const ::std::string &value)
テスト結果の情報追加
iutest::TestParamInfo
値のパラメータ化テストのパラメータ情報
Definition: iutest_body.hpp:233
iutest::Test::GetCurrentTest
static Test * GetCurrentTest()
実行中の Test の取得
Definition: iutest_body.hpp:80
iutest::WithParamInterface
パラメータテストインターフェース
Definition: iutest_body.hpp:28
iutest_mediator.hpp
iris unit test 仲介者 ファイル
IUTEST_CHECK_
#define IUTEST_CHECK_(condition)
内部エラーチェック
Definition: iutest_port.hpp:60