util.hpp
Go to the documentation of this file.
1 #ifndef TEST__UNIT__UTIL_HPP
2 #define TEST__UNIT__UTIL_HPP
3 
4 #include <string>
5 
6 #define EXPECT_THROW_MSG(expr, T_e, msg) \
7  try { \
8  expr; \
9  } catch(const T_e& e) { \
10  EXPECT_EQ(1, count_matches(msg, e.what())) \
11  << "expected message: " << msg << std::endl \
12  << "found message: " << e.what(); \
13  } catch(...) { \
14  FAIL() \
15  << "Wrong exception type thrown" << std::endl; \
16  }
17 
18 
20  const std::string& s) {
21  if (target.size() == 0) return -1; // error
22  int count = 0;
23  for (size_t pos = 0; (pos = s.find(target,pos)) != std::string::npos; pos += target.size())
24  ++count;
25  return count;
26 }
27 
28 namespace stan {
29  namespace test {
30  std::streambuf *cout_buf = 0;
31  std::streambuf *cerr_buf = 0;
32 
33  std::stringstream cout_ss;
34  std::stringstream cerr_ss;
35 
37  cout_ss.str("");
38  cerr_ss.str("");
39 
40  cout_buf = std::cout.rdbuf();
41  cerr_buf = std::cerr.rdbuf();
42 
43  std::cout.rdbuf(cout_ss.rdbuf());
44  std::cerr.rdbuf(cerr_ss.rdbuf());
45  }
46 
48  std::cout.rdbuf(cout_buf);
49  std::cerr.rdbuf(cerr_buf);
50  cout_buf = 0;
51  cerr_buf = 0;
52  }
53  }
54 }
55 
56 #endif
int count_matches(const std::string &target, const std::string &s)
Definition: util.hpp:19
const XML_Char * target
Definition: expat.h:268
OStream cerr
Definition: OStream.cxx:7
void reset_std_streams()
Definition: util.hpp:47
const XML_Char * s
Definition: expat.h:262
std::streambuf * cerr_buf
Definition: util.hpp:31
OStream cout
Definition: OStream.cxx:6
void capture_std_streams()
Definition: util.hpp:36
std::streambuf * cout_buf
Definition: util.hpp:30
std::stringstream cerr_ss
Definition: util.hpp:34
std::stringstream cout_ss
Definition: util.hpp:33
enum BeamMode string