ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
ThrowsWith.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <gtest/gtest.h>
4
5#include <functional>
6#include <stdexcept>
7#include <string>
8
9namespace ppforest2::test {
10
11 namespace {
12 inline std::string expected_message(std::string const& needle, std::string const& what) {
13 return "expected message containing \"" + needle + "\", got \"" + what + "\"";
14 }
15
16 inline std::string wrong_exception(std::string const& what) {
17 return "wrong exception type: " + what;
18 }
19 }
20
34 inline ::testing::AssertionResult throws_with(std::function<void()> const& fn, std::string const& needle) {
35 try {
36 fn();
37 } catch (std::runtime_error const& e) {
38 std::string const what = e.what();
39 if (what.find(needle) != std::string::npos) {
40 return ::testing::AssertionSuccess();
41 } else {
42 return ::testing::AssertionFailure() << expected_message(needle, what);
43 }
44 } catch (std::exception const& e) {
45 return ::testing::AssertionFailure() << wrong_exception(e.what());
46 }
47 return ::testing::AssertionFailure() << "did not throw";
48 }
49}
Definition MockStop.hpp:10
inline ::testing::AssertionResult throws_with(std::function< void()> const &fn, std::string const &needle)
gtest assertion: fn throws a std::runtime_error whose what() contains needle.
Definition ThrowsWith.hpp:34