| 23 | |
| 24 | template <typename ExceptionType = std::exception, typename Fn> |
| 25 | inline void ExpectThrowContains(Fn&& fn, |
| 26 | const std::string& expected_substr, |
| 27 | const std::string& context = "") { |
| 28 | try { |
| 29 | fn(); |
| 30 | if (context.empty()) { |
| 31 | FAIL() << "Expected exception containing: " << expected_substr; |
| 32 | } else { |
| 33 | FAIL() << "Expected exception containing: " << expected_substr |
| 34 | << ", context: " << context; |
| 35 | } |
| 36 | } catch (const ExceptionType& e) { |
| 37 | if (context.empty()) { |
| 38 | EXPECT_NE(std::string(e.what()).find(expected_substr), std::string::npos) |
| 39 | << "actual error: " << e.what(); |
| 40 | } else { |
| 41 | EXPECT_NE(std::string(e.what()).find(expected_substr), std::string::npos) |
| 42 | << "context: " << context << ", actual error: " << e.what(); |
| 43 | } |
| 44 | } catch (...) { |
| 45 | if (context.empty()) { |
| 46 | FAIL() << "Expected the specified exception type"; |
| 47 | } else { |
| 48 | FAIL() << "Expected the specified exception type, context: " << context; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | } // namespace utils |
| 54 | } // namespace test |