C++Spec 1.0.0
BDD testing for C++
Loading...
Searching...
No Matches
throw.hpp
1#pragma once
2
4#include "util.hpp"
5
6namespace CppSpec::Matchers {
7
8template <class A, class Ex>
9class Throw : public MatcherBase<A, void*> {
10 public:
11 explicit Throw(Expectation<A>& expectation) : MatcherBase<A, void*>(expectation, nullptr) {}
12 bool match() override;
13 std::string verb() override { return "throw"; }
14 std::string description() override;
15 std::string failure_message() override;
16 std::string failure_message_when_negated() override;
17};
18
19template <class A, class Ex>
20bool Throw<A, Ex>::match() {
21 bool caught = false;
22 try {
23 this->actual();
24 } catch (Ex& ex) {
25 caught = true;
26 }
27 return caught;
28}
29
30template <typename A, typename Ex>
32 return std::format("throw {}", Util::demangle(typeid(Ex).name()));
33}
34
35template <typename A, typename Ex>
37 return std::format("expected the given function ([] -> {} {{...}}) to {}", Util::demangle(typeid(A).name()),
38 description());
39}
40
41template <typename A, typename Ex>
43 return std::format("expected the given function ([] -> {} {{...}}) not to {}", Util::demangle(typeid(A).name()),
44 description());
45}
46} // namespace CppSpec::Matchers
Wraps the target of an expectation.
Definition expectation.hpp:47
std::string failure_message() override
Get message to give on match failure.
Definition throw.hpp:36
std::string failure_message_when_negated() override
Get message to give on match failure when negated.
Definition throw.hpp:42
std::string description() override
Get the description of the Matcher.
Definition throw.hpp:31
Contains the base class for all Matchers.
Utility functions and classes.
std::string demangle(const char *name)
Definition util.hpp:38