Pothos  0.1.1
The Pothos dataflow programming software suite
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
Testing.hpp
Go to the documentation of this file.
1 
13 #pragma once
14 #include <Pothos/Config.hpp>
15 #include <Pothos/Plugin.hpp>
16 #include <Pothos/Callable.hpp>
17 #include <Pothos/Exception.hpp>
18 #include <sstream>
19 #include <memory>
20 #include <string>
21 
39 #define POTHOS_TEST_BLOCK(path, name) \
40 struct name : Pothos::TestingBase \
41 { \
42  void runTestsImpl(void); \
43  void runTestsImpl_(void); \
44 }; \
45 pothos_static_block(name) \
46 { \
47  std::shared_ptr<Pothos::TestingBase> testObj(new name()); \
48  Pothos::PluginRegistry::add(Pothos::Plugin( \
49  Pothos::PluginPath(path).join(#name), Pothos::Object(testObj))); \
50 } \
51 void name::runTestsImpl(void) \
52 { \
53  POTHOS_TEST_CHECKPOINT(); \
54  this->runTestsImpl_(); \
55 } \
56 void name::runTestsImpl_(void)
57 
59 #define POTHOS_TEST_CHECKPOINT() \
60  Pothos::TestingBase::current().report("checkpoint", "", "", __LINE__, __FILE__)
61 
63 #define __POTHOS_TEST_STATEMENT(message, statement) \
64 { \
65  Pothos::TestingBase::current().report(message, #statement, "", __LINE__, __FILE__); \
66  try{statement;} \
67  catch(const std::string &ex){Pothos::TestingBase::current().report(message, #statement, ex, __LINE__, __FILE__);} \
68  catch(const Pothos::Exception &ex){Pothos::TestingBase::current().report(message, #statement, ex.displayText(), __LINE__, __FILE__);} \
69  catch(const std::exception &ex){Pothos::TestingBase::current().report(message, #statement, ex.what(), __LINE__, __FILE__);} \
70  catch(...){Pothos::TestingBase::current().report(message, #statement, "unknown", __LINE__, __FILE__);} \
71 }
72 
74 #define __POTHOS_TEST_ASSERT(message, statement) \
75 { \
76  __POTHOS_TEST_STATEMENT(message, if (statement) {} else throw std::string("statement \"" #statement "\" evaluated false");); \
77 }
78 
80 #define POTHOS_TEST_TRUE(statement) \
81 { \
82  __POTHOS_TEST_ASSERT(#statement, statement) \
83 }
84 
86 #define POTHOS_TEST_EQUAL(lhs, rhs) \
87 { \
88  __POTHOS_TEST_ASSERT(Pothos::TestingBase::current().toString(lhs) + \
89  " == " + Pothos::TestingBase::current().toString(rhs), (lhs) == (rhs)); \
90 }
91 
93 #define POTHOS_TEST_EQUALV(lhs, rhs) \
94 { \
95  POTHOS_TEST_EQUAL((lhs).size(), (rhs).size()); \
96  for (size_t i = 0; i < (lhs).size(); i++) \
97  { \
98  __POTHOS_TEST_ASSERT( \
99  "index " + Pothos::TestingBase::current().toString(i) + \
100  " asserts " + Pothos::TestingBase::current().toString((lhs)[i]) + \
101  " == " + Pothos::TestingBase::current().toString((rhs)[i]), (lhs)[i] == (rhs)[i]); \
102  } \
103 }
104 
106 #define POTHOS_TEST_THROWS(statement, expectedException) \
107 { \
108  Pothos::TestingBase::current().report(#statement, #statement, "", __LINE__, __FILE__); \
109  __POTHOS_TEST_STATEMENT(#statement " must throw " #expectedException, \
110  try{statement;throw std::string("statement \"" #statement "\" did not throw");} \
111  catch(const expectedException &){}); \
112 }
113 
114 namespace Pothos {
115 
117 {
118  TestingBase(void);
119  ~TestingBase(void);
120  static TestingBase &current(void);
121  void runTests();
122  virtual void runTestsImpl() = 0;
123  void report(
124  const std::string &message,
125  const std::string &statement,
126  const std::string &error,
127  const int line,
128  const std::string &file);
129  template <typename T>
130  std::string toString(const T &v)
131  {
132  std::stringstream ss;
133  ss << v;
134  return ss.str();
135  }
136 };
137 
138 } //namespace Pothos
#define POTHOS_API
Definition: Config.hpp:41
Definition: Testing.hpp:116
std::string toString(const T &v)
Definition: Testing.hpp:130