gtest_extensions_ut.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <util/generic/string.h>
  3. #include <util/generic/maybe.h>
  4. #include <util/stream/output.h>
  5. #include <util/stream/str.h>
  6. namespace {
  7. class IMock {
  8. public:
  9. virtual void M1(const TStringBuf&) = 0;
  10. virtual void M2(TStringBuf) = 0;
  11. virtual void M3(const TString&) = 0;
  12. virtual void M4(TString) = 0;
  13. };
  14. class TSampleMock : IMock {
  15. public:
  16. MOCK_METHOD(void, M1, (const TStringBuf&));
  17. MOCK_METHOD(void, M2, (TStringBuf));
  18. MOCK_METHOD(void, M3, (const TString&));
  19. MOCK_METHOD(void, M4, (TString));
  20. };
  21. }
  22. TEST(MatchersSpecializations, String) {
  23. TSampleMock mock;
  24. TStringBuf simpleStringBuf = "SimpleStringBuf";
  25. const TStringBuf constSimpleStringBuf = "ConstSimpleStringBuf";
  26. TString simpleString = "SimpleString";
  27. const TString constSimpleString = "ConstSimpleString";
  28. EXPECT_CALL(mock, M1("ConstSimpleStringBuf")).Times(1);
  29. EXPECT_CALL(mock, M2("SimpleStringBuf")).Times(1);
  30. EXPECT_CALL(mock, M3("ConstSimpleString")).Times(1);
  31. EXPECT_CALL(mock, M4("SimpleString")).Times(1);
  32. mock.M1(constSimpleStringBuf);
  33. mock.M2(simpleStringBuf);
  34. mock.M3(constSimpleString);
  35. mock.M4(simpleString);
  36. }
  37. template <typename T, typename M>
  38. std::pair<bool, std::string> Match(T&& t, M&& m) {
  39. testing::StringMatchResultListener listener;
  40. auto matches = testing::SafeMatcherCast<T>(std::forward<M>(m)).MatchAndExplain(std::forward<T>(t), &listener);
  41. return {matches, listener.str()};
  42. }
  43. TEST(Matchers, Throws) {
  44. auto matcher = testing::Throws<std::runtime_error>();
  45. {
  46. std::stringstream ss;
  47. testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss);
  48. auto explanation = ss.str();
  49. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  50. }
  51. {
  52. auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher);
  53. EXPECT_TRUE(matched);
  54. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  55. }
  56. {
  57. auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher);
  58. EXPECT_FALSE(matched);
  59. EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error"));
  60. EXPECT_THAT(explanation, testing::HasSubstr("\"error message\""));
  61. }
  62. {
  63. auto [matched, explanation] = Match([]() { throw 10; }, matcher);
  64. EXPECT_FALSE(matched);
  65. EXPECT_THAT(explanation, testing::HasSubstr("throws an exception of an unknown type"));
  66. }
  67. {
  68. auto [matched, explanation] = Match([]() { (void)0; }, matcher);
  69. EXPECT_FALSE(matched);
  70. EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception"));
  71. }
  72. }
  73. TEST(Matchers, ThrowsMessage) {
  74. auto matcher = testing::ThrowsMessage<std::runtime_error>(testing::HasSubstr("error message"));
  75. {
  76. std::stringstream ss;
  77. testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss);
  78. auto explanation = ss.str();
  79. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  80. EXPECT_THAT(explanation, testing::HasSubstr("\"error message\""));
  81. }
  82. {
  83. auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher);
  84. EXPECT_TRUE(matched);
  85. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  86. }
  87. {
  88. auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher);
  89. EXPECT_FALSE(matched);
  90. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  91. }
  92. {
  93. auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher);
  94. EXPECT_FALSE(matched);
  95. EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error"));
  96. EXPECT_THAT(explanation, testing::HasSubstr("\"error message\""));
  97. }
  98. {
  99. auto [matched, explanation] = Match([]() { throw 10; }, matcher);
  100. EXPECT_FALSE(matched);
  101. EXPECT_THAT(explanation, testing::HasSubstr("throws an exception of an unknown type"));
  102. }
  103. {
  104. auto [matched, explanation] = Match([]() { (void)0; }, matcher);
  105. EXPECT_FALSE(matched);
  106. EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception"));
  107. }
  108. }
  109. TEST(Matchers, ThrowsMessageHasSubstr) {
  110. auto matcher = testing::ThrowsMessage<std::runtime_error>(testing::HasSubstr("error message"));
  111. {
  112. std::stringstream ss;
  113. testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss);
  114. auto explanation = ss.str();
  115. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  116. EXPECT_THAT(explanation, testing::HasSubstr("\"error message\""));
  117. }
  118. {
  119. auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher);
  120. EXPECT_TRUE(matched);
  121. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  122. }
  123. {
  124. auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher);
  125. EXPECT_FALSE(matched);
  126. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  127. }
  128. {
  129. auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher);
  130. EXPECT_FALSE(matched);
  131. EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error"));
  132. EXPECT_THAT(explanation, testing::HasSubstr("\"error message\""));
  133. }
  134. {
  135. auto [matched, explanation] = Match([]() { throw 10; }, matcher);
  136. EXPECT_FALSE(matched);
  137. EXPECT_THAT(explanation, testing::HasSubstr("throws an exception of an unknown type"));
  138. }
  139. {
  140. auto [matched, explanation] = Match([]() { (void)0; }, matcher);
  141. EXPECT_FALSE(matched);
  142. EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception"));
  143. }
  144. }
  145. TEST(Matchers, ThrowsCondition) {
  146. auto matcher = testing::Throws<std::runtime_error>(
  147. testing::Property(&std::exception::what, testing::HasSubstr("error message")));
  148. {
  149. std::stringstream ss;
  150. testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss);
  151. auto explanation = ss.str();
  152. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  153. EXPECT_THAT(explanation, testing::HasSubstr("\"error message\""));
  154. }
  155. {
  156. auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher);
  157. EXPECT_TRUE(matched);
  158. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  159. }
  160. {
  161. auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher);
  162. EXPECT_FALSE(matched);
  163. EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
  164. EXPECT_THAT(explanation, testing::HasSubstr("\"message error\""));
  165. }
  166. {
  167. auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher);
  168. EXPECT_FALSE(matched);
  169. EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error"));
  170. EXPECT_THAT(explanation, testing::HasSubstr("\"error message\""));
  171. }
  172. {
  173. auto [matched, explanation] = Match([]() { throw 10; }, matcher);
  174. EXPECT_FALSE(matched);
  175. EXPECT_THAT(explanation, testing::HasSubstr("throws an exception of an unknown type"));
  176. }
  177. {
  178. auto [matched, explanation] = Match([]() { (void)0; }, matcher);
  179. EXPECT_FALSE(matched);
  180. EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception"));
  181. }
  182. }
  183. template <typename T>
  184. std::string GtestPrint(T&& v) {
  185. std::stringstream ss;
  186. testing::internal::UniversalPrint(std::forward<T>(v), &ss);
  187. return ss.str();
  188. }
  189. struct TThrowsOnMove {
  190. TThrowsOnMove() = default;
  191. TThrowsOnMove(TThrowsOnMove&&) {
  192. ythrow yexception() << "move failed";
  193. }
  194. };
  195. TEST(PrettyPrinters, String) {
  196. EXPECT_EQ(GtestPrint(TString("hello world")), "\"hello world\"");
  197. EXPECT_EQ(GtestPrint(TStringBuf("hello world")), "\"hello world\"");
  198. }
  199. TEST(PrettyPrinters, Maybe) {
  200. EXPECT_EQ(GtestPrint(TMaybe<TString>("hello world")), "\"hello world\"");
  201. EXPECT_EQ(GtestPrint(TMaybe<TString>()), "nothing");
  202. EXPECT_EQ(GtestPrint(Nothing()), "nothing");
  203. }
  204. struct T1 {
  205. int x;
  206. };
  207. void PrintTo(T1 value, std::ostream* stream) {
  208. *stream << "T1{" << value.x << "}";
  209. }
  210. struct T2 {
  211. int x;
  212. };
  213. Y_DECLARE_OUT_SPEC(inline, T2, stream, value) {
  214. stream << "T2{" << value.x << "}";
  215. }
  216. Y_GTEST_ARCADIA_PRINTER(T2)
  217. TEST(PrettyPrinters, Custom) {
  218. EXPECT_EQ(GtestPrint(T1{10}), "T1{10}");
  219. }
  220. TEST(PrettyPrinters, CustomArcadia) {
  221. EXPECT_EQ(GtestPrint(T2{10}), "T2{10}");
  222. }
  223. TEST(Exceptions, ExpectThrow) {
  224. EXPECT_THROW(ythrow yexception() << "msg", yexception);
  225. }
  226. TEST(Exceptions, ExpectThrowStructuredBindings) {
  227. auto [a, b] = std::make_pair("a", "b");
  228. EXPECT_THROW(throw yexception() << a << "-" << b, yexception);
  229. }
  230. TEST(Exceptions, ExpectThrowSkipInThrowTest) {
  231. // this test should be skipped, not failed
  232. EXPECT_THROW(GTEST_SKIP(), yexception);
  233. }
  234. TEST(Exceptions, AssertThrow) {
  235. ASSERT_THROW(ythrow yexception() << "msg", yexception);
  236. }
  237. TEST(Exceptions, AssertThrowStructuredBindings) {
  238. auto [a, b] = std::make_pair("a", "b");
  239. ASSERT_THROW(throw yexception() << a << "-" << b, yexception);
  240. }
  241. TEST(Exceptions, AssertThrowSkipInThrowTest) {
  242. // this test should be skipped, not failed
  243. ASSERT_THROW(GTEST_SKIP(), yexception);
  244. }
  245. TEST(Exceptions, ExpectThrowMessageHasSubstr) {
  246. EXPECT_THROW_MESSAGE_HAS_SUBSTR(ythrow yexception() << "msg", yexception, "msg");
  247. }
  248. TEST(Exceptions, ExpectThrowMessageHasSubstrStructuredBindings) {
  249. auto [a, b] = std::make_pair("a", "b");
  250. EXPECT_THROW_MESSAGE_HAS_SUBSTR(throw yexception() << a << "-" << b, yexception, "-");
  251. }
  252. TEST(Exceptions, ExpectThrowMessageHasSubstrSkipInThrowTest) {
  253. // this test should be skipped, not failed
  254. EXPECT_THROW_MESSAGE_HAS_SUBSTR(GTEST_SKIP(), yexception, "-");
  255. }
  256. TEST(Exceptions, AssertThrowMessageHasSubstr) {
  257. ASSERT_THROW_MESSAGE_HAS_SUBSTR(ythrow yexception() << "msg", yexception, "msg");
  258. }
  259. TEST(Exceptions, AssertThrowMessageHasSubstrStructuredBindings) {
  260. auto [a, b] = std::make_pair("a", "b");
  261. ASSERT_THROW_MESSAGE_HAS_SUBSTR(throw yexception() << a << "-" << b, yexception, "-");
  262. }
  263. TEST(Exceptions, AssertThrowMessageHasSubstrSkipInThrowTest) {
  264. // this test should be skipped, not failed
  265. ASSERT_THROW_MESSAGE_HAS_SUBSTR(GTEST_SKIP(), yexception, "-");
  266. }
  267. TEST(Exceptions, ExpectNoThrow) {
  268. EXPECT_NO_THROW((void)0);
  269. }
  270. TEST(Exceptions, AssertNoThrow) {
  271. ASSERT_NO_THROW((void)0);
  272. }
  273. TEST(Exceptions, ExpectAnyThrow) {
  274. EXPECT_ANY_THROW(ythrow yexception() << "msg");
  275. }
  276. TEST(Exceptions, AssertAnyThrow) {
  277. ASSERT_ANY_THROW(ythrow yexception() << "msg");
  278. }