diagnostic_information_test.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/exception/diagnostic_information.hpp>
  5. #include <boost/exception/info.hpp>
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/detail/workaround.hpp>
  8. #if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
  9. struct test_tag1 {};
  10. struct test_tag2 {};
  11. #endif
  12. typedef boost::error_info<struct test_tag1,int> tagged_int1;
  13. typedef boost::error_info<struct test_tag2,int> tagged_int2;
  14. std::string
  15. to_string( tagged_int2 const & x )
  16. {
  17. return '[' +boost::error_info_name(x) + "] = " + (x.value()==42 ? "fourty-two" : "bad value");
  18. }
  19. struct
  20. error1:
  21. std::exception,
  22. boost::exception
  23. {
  24. char const *
  25. what() const BOOST_NOEXCEPT_OR_NOTHROW
  26. {
  27. return "error1";
  28. }
  29. };
  30. struct
  31. error2:
  32. boost::exception
  33. {
  34. };
  35. struct
  36. error3:
  37. std::exception
  38. {
  39. char const *
  40. what() const BOOST_NOEXCEPT_OR_NOTHROW
  41. {
  42. return "error3";
  43. }
  44. };
  45. struct
  46. error4:
  47. std::exception,
  48. boost::exception
  49. {
  50. char const *
  51. what() const BOOST_NOEXCEPT_OR_NOTHROW
  52. {
  53. return diagnostic_information_what(*this);
  54. }
  55. };
  56. void
  57. test1( std::string const & di1, std::string const & di2 )
  58. {
  59. BOOST_TEST( di1!=di2 );
  60. #ifndef BOOST_NO_RTTI
  61. BOOST_TEST(di1.find("type:")!=std::string::npos);
  62. BOOST_TEST(di1.find("error1")!=std::string::npos);
  63. #endif
  64. BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
  65. BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
  66. BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
  67. #ifndef BOOST_NO_RTTI
  68. BOOST_TEST(di2.find("type:")!=std::string::npos);
  69. BOOST_TEST(di2.find("error1")!=std::string::npos);
  70. #endif
  71. BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
  72. BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
  73. BOOST_TEST(di2.find("bad value")!=std::string::npos);
  74. }
  75. void
  76. test2( std::string const & di1, std::string const & di2 )
  77. {
  78. BOOST_TEST( di1!=di2 );
  79. BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
  80. BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
  81. BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
  82. BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
  83. BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
  84. BOOST_TEST(di2.find("bad value")!=std::string::npos);
  85. }
  86. void
  87. test3( std::string const & di )
  88. {
  89. #ifndef BOOST_NO_RTTI
  90. BOOST_TEST(di.find("type:")!=std::string::npos);
  91. #endif
  92. BOOST_TEST(di.find("error3")!=std::string::npos);
  93. }
  94. void
  95. test4( std::string const & di1, std::string const & di2 )
  96. {
  97. BOOST_TEST( di1!=di2 );
  98. #ifndef BOOST_NO_RTTI
  99. BOOST_TEST(di1.find("type:")!=std::string::npos);
  100. #endif
  101. BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
  102. BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
  103. BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
  104. #ifndef BOOST_NO_RTTI
  105. BOOST_TEST(di2.find("type:")!=std::string::npos);
  106. #endif
  107. BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
  108. BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
  109. BOOST_TEST(di2.find("bad value")!=std::string::npos);
  110. }
  111. int
  112. main()
  113. {
  114. using namespace boost;
  115. try
  116. {
  117. error1 x; x << tagged_int1(42) << tagged_int2(42);
  118. BOOST_TEST(x.what()==std::string("error1"));
  119. throw x;
  120. }
  121. catch(
  122. error1 & x )
  123. {
  124. std::string di1=boost::diagnostic_information(x);
  125. x << tagged_int1(2) << tagged_int2(2);
  126. std::string di2 = diagnostic_information(x);
  127. test1(di1,di2);
  128. }
  129. try
  130. {
  131. error1 x; x << tagged_int1(42) << tagged_int2(42);
  132. BOOST_TEST(x.what()==std::string("error1"));
  133. throw x;
  134. }
  135. catch(
  136. error1 & x )
  137. {
  138. std::string di1=boost::current_exception_diagnostic_information();
  139. x << tagged_int1(2) << tagged_int2(2);
  140. std::string di2 = current_exception_diagnostic_information();
  141. test1(di1,di2);
  142. }
  143. try
  144. {
  145. error2 x;
  146. x << tagged_int1(42) << tagged_int2(42);
  147. throw x;
  148. }
  149. catch(
  150. error2 & x )
  151. {
  152. std::string di1 = diagnostic_information(x);
  153. x << tagged_int1(2) << tagged_int2(2);
  154. std::string di2 = diagnostic_information(x);
  155. test2(di1,di2);
  156. }
  157. try
  158. {
  159. error2 x;
  160. x << tagged_int1(42) << tagged_int2(42);
  161. throw x;
  162. }
  163. catch(
  164. error2 & x )
  165. {
  166. std::string di1 = current_exception_diagnostic_information();
  167. BOOST_TEST(di1==boost::diagnostic_information_what(x));
  168. x << tagged_int1(2) << tagged_int2(2);
  169. std::string di2 = current_exception_diagnostic_information();
  170. BOOST_TEST(di2==boost::diagnostic_information_what(x));
  171. test2(di1,di2);
  172. }
  173. try
  174. {
  175. error3 x;
  176. std::string di=diagnostic_information(x);
  177. test3(di);
  178. throw x;
  179. }
  180. catch(
  181. ... )
  182. {
  183. std::string di=current_exception_diagnostic_information();
  184. test3(di);
  185. }
  186. try
  187. {
  188. throw error4();
  189. }
  190. catch(
  191. error4 & x )
  192. {
  193. std::string di1=boost::diagnostic_information(x);
  194. std::string wh1=x.what();
  195. BOOST_TEST(wh1==di1);
  196. }
  197. try
  198. {
  199. error4 x; x << tagged_int1(42) << tagged_int2(42);
  200. throw x;
  201. }
  202. catch(
  203. error4 & x )
  204. {
  205. std::string di1=boost::diagnostic_information(x);
  206. std::string wh1=x.what();
  207. BOOST_TEST(wh1==di1);
  208. x << tagged_int1(2) << tagged_int2(2);
  209. std::string di2 = diagnostic_information(x);
  210. std::string wh2=x.what();
  211. BOOST_TEST(wh2==di2);
  212. test4(di1,di2);
  213. }
  214. try
  215. {
  216. error4 x; x << tagged_int1(42) << tagged_int2(42);
  217. throw x;
  218. }
  219. catch(
  220. error4 & x )
  221. {
  222. std::string di1=boost::current_exception_diagnostic_information();
  223. std::string wh1=x.what();
  224. BOOST_TEST(wh1==di1);
  225. x << tagged_int1(2) << tagged_int2(2);
  226. std::string di2 = current_exception_diagnostic_information();
  227. std::string wh2=x.what();
  228. BOOST_TEST(wh2==di2);
  229. test4(di1,di2);
  230. }
  231. return boost::report_errors();
  232. }