Merge pull request #2904 from AmatanHead:throw-matchers
PiperOrigin-RevId: 327294137
This commit is contained in:
		@@ -8117,6 +8117,190 @@ TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
 | 
			
		||||
  EXPECT_THAT(p, Not(UniquePointee(2)));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if GTEST_HAS_EXCEPTIONS
 | 
			
		||||
 | 
			
		||||
// std::function<void()> is used below for compatibility with older copies of
 | 
			
		||||
// GCC. Normally, a raw lambda is all that is needed.
 | 
			
		||||
 | 
			
		||||
// Test that examples from documentation compile
 | 
			
		||||
TEST(ThrowsTest, Examples) {
 | 
			
		||||
  EXPECT_THAT(
 | 
			
		||||
      std::function<void()>([]() { throw std::runtime_error("message"); }),
 | 
			
		||||
      Throws<std::runtime_error>());
 | 
			
		||||
 | 
			
		||||
  EXPECT_THAT(
 | 
			
		||||
      std::function<void()>([]() { throw std::runtime_error("message"); }),
 | 
			
		||||
      ThrowsMessage<std::runtime_error>(HasSubstr("message")));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(ThrowsTest, DoesNotGenerateDuplicateCatchClauseWarning) {
 | 
			
		||||
  EXPECT_THAT(std::function<void()>([]() { throw std::exception(); }),
 | 
			
		||||
              Throws<std::exception>());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(ThrowsTest, CallableExecutedExactlyOnce) {
 | 
			
		||||
  size_t a = 0;
 | 
			
		||||
 | 
			
		||||
  EXPECT_THAT(std::function<void()>([&a]() {
 | 
			
		||||
                a++;
 | 
			
		||||
                throw 10;
 | 
			
		||||
              }),
 | 
			
		||||
              Throws<int>());
 | 
			
		||||
  EXPECT_EQ(a, 1u);
 | 
			
		||||
 | 
			
		||||
  EXPECT_THAT(std::function<void()>([&a]() {
 | 
			
		||||
                a++;
 | 
			
		||||
                throw std::runtime_error("message");
 | 
			
		||||
              }),
 | 
			
		||||
              Throws<std::runtime_error>());
 | 
			
		||||
  EXPECT_EQ(a, 2u);
 | 
			
		||||
 | 
			
		||||
  EXPECT_THAT(std::function<void()>([&a]() {
 | 
			
		||||
                a++;
 | 
			
		||||
                throw std::runtime_error("message");
 | 
			
		||||
              }),
 | 
			
		||||
              ThrowsMessage<std::runtime_error>(HasSubstr("message")));
 | 
			
		||||
  EXPECT_EQ(a, 3u);
 | 
			
		||||
 | 
			
		||||
  EXPECT_THAT(std::function<void()>([&a]() {
 | 
			
		||||
                a++;
 | 
			
		||||
                throw std::runtime_error("message");
 | 
			
		||||
              }),
 | 
			
		||||
              Throws<std::runtime_error>(
 | 
			
		||||
                  Property(&std::runtime_error::what, HasSubstr("message"))));
 | 
			
		||||
  EXPECT_EQ(a, 4u);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(ThrowsTest, Describe) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
 | 
			
		||||
  std::stringstream ss;
 | 
			
		||||
  matcher.DescribeTo(&ss);
 | 
			
		||||
  auto explanation = ss.str();
 | 
			
		||||
  EXPECT_THAT(explanation, HasSubstr("std::runtime_error"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(ThrowsTest, Success) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_TRUE(matcher.MatchAndExplain(
 | 
			
		||||
      []() { throw std::runtime_error("error message"); }, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(ThrowsTest, FailWrongType) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_FALSE(matcher.MatchAndExplain(
 | 
			
		||||
      []() { throw std::logic_error("error message"); }, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("std::logic_error"));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("\"error message\""));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(ThrowsTest, FailWrongTypeNonStd) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(),
 | 
			
		||||
              HasSubstr("throws an exception of an unknown type"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(ThrowsTest, FailNoThrow) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_FALSE(matcher.MatchAndExplain([]() { (void)0; }, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("does not throw any exception"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ThrowsPredicateTest
 | 
			
		||||
    : public TestWithParam<Matcher<std::function<void()>>> {};
 | 
			
		||||
 | 
			
		||||
TEST_P(ThrowsPredicateTest, Describe) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = GetParam();
 | 
			
		||||
  std::stringstream ss;
 | 
			
		||||
  matcher.DescribeTo(&ss);
 | 
			
		||||
  auto explanation = ss.str();
 | 
			
		||||
  EXPECT_THAT(explanation, HasSubstr("std::runtime_error"));
 | 
			
		||||
  EXPECT_THAT(explanation, HasSubstr("error message"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST_P(ThrowsPredicateTest, Success) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = GetParam();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_TRUE(matcher.MatchAndExplain(
 | 
			
		||||
      []() { throw std::runtime_error("error message"); }, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST_P(ThrowsPredicateTest, FailWrongType) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = GetParam();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_FALSE(matcher.MatchAndExplain(
 | 
			
		||||
      []() { throw std::logic_error("error message"); }, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("std::logic_error"));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("\"error message\""));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = GetParam();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(),
 | 
			
		||||
              HasSubstr("throws an exception of an unknown type"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST_P(ThrowsPredicateTest, FailWrongMessage) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = GetParam();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_FALSE(matcher.MatchAndExplain(
 | 
			
		||||
      []() { throw std::runtime_error("wrong message"); }, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error"));
 | 
			
		||||
  EXPECT_THAT(listener.str(), Not(HasSubstr("wrong message")));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST_P(ThrowsPredicateTest, FailNoThrow) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher = GetParam();
 | 
			
		||||
  StringMatchResultListener listener;
 | 
			
		||||
  EXPECT_FALSE(matcher.MatchAndExplain([]() {}, &listener));
 | 
			
		||||
  EXPECT_THAT(listener.str(), HasSubstr("does not throw any exception"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
INSTANTIATE_TEST_SUITE_P(
 | 
			
		||||
    AllMessagePredicates, ThrowsPredicateTest,
 | 
			
		||||
    Values(Matcher<std::function<void()>>(
 | 
			
		||||
        ThrowsMessage<std::runtime_error>(HasSubstr("error message")))));
 | 
			
		||||
 | 
			
		||||
// Tests that Throws<E1>(Matcher<E2>{}) compiles even when E2 != const E1&.
 | 
			
		||||
TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) {
 | 
			
		||||
  {
 | 
			
		||||
    Matcher<std::function<void()>> matcher =
 | 
			
		||||
        ThrowsMessage<std::runtime_error>(HasSubstr("error message"));
 | 
			
		||||
    EXPECT_TRUE(
 | 
			
		||||
        matcher.Matches([]() { throw std::runtime_error("error message"); }));
 | 
			
		||||
    EXPECT_FALSE(
 | 
			
		||||
        matcher.Matches([]() { throw std::runtime_error("wrong message"); }));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  {
 | 
			
		||||
    Matcher<uint64_t> inner = Eq(10);
 | 
			
		||||
    Matcher<std::function<void()>> matcher = Throws<uint32_t>(inner);
 | 
			
		||||
    EXPECT_TRUE(matcher.Matches([]() { throw(uint32_t) 10; }));
 | 
			
		||||
    EXPECT_FALSE(matcher.Matches([]() { throw(uint32_t) 11; }));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Tests that ThrowsMessage("message") is equivalent
 | 
			
		||||
// to ThrowsMessage(Eq<std::string>("message")).
 | 
			
		||||
TEST(ThrowsPredicateCompilesTest, MessageMatcherAcceptsNonMatcher) {
 | 
			
		||||
  Matcher<std::function<void()>> matcher =
 | 
			
		||||
      ThrowsMessage<std::runtime_error>("error message");
 | 
			
		||||
  EXPECT_TRUE(
 | 
			
		||||
      matcher.Matches([]() { throw std::runtime_error("error message"); }));
 | 
			
		||||
  EXPECT_FALSE(matcher.Matches(
 | 
			
		||||
      []() { throw std::runtime_error("wrong error message"); }));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif  // GTEST_HAS_EXCEPTIONS
 | 
			
		||||
 | 
			
		||||
}  // namespace
 | 
			
		||||
}  // namespace gmock_matchers_test
 | 
			
		||||
}  // namespace testing
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user