exception_message_matcher.h
Go to the documentation of this file.
1 #ifndef cetlib_exception_message_matcher_h
2 #define cetlib_exception_message_matcher_h
3 ////////////////////////////////////////////////////////////////////////
4 // exception_message_matcher
5 //
6 // Utility for matching cet::exception messages when one is using the
7 // Catch unit testing package.
8 //
9 // Initialize with any valid dstring matcher function (Contains(),
10 // EndsWith(), Regex(), etc.).
11 //
12 // Composable with other bases of Catch::MatcherBase<cet::exception>
13 // such as cet::exception_category_matcher.
14 ////////////////////////////////////////////////////////////////////////
15 #include "cetlib_except/exception.h"
16 #include <catch/catch.hpp>
17 
18 #include <memory>
19 #include <string>
20 
21 namespace cet {
22  class exception_message_matcher;
23 }
24 
26  : public Catch::MatcherBase<cet::exception> {
27 public:
29  Catch::Matchers::StdString::EqualsMatcher const& matcher)
30  : matcher_{std::make_shared<Catch::Matchers::StdString::EqualsMatcher>(
31  matcher)}
32  , description_{matcher.describe()}
33  {}
34 
36  Catch::Matchers::StdString::ContainsMatcher const& matcher)
37  : matcher_{std::make_shared<Catch::Matchers::StdString::ContainsMatcher>(
38  matcher)}
39  , description_{matcher.describe()}
40  {}
41 
43  Catch::Matchers::StdString::StartsWithMatcher const& matcher)
44  : matcher_{std::make_shared<Catch::Matchers::StdString::StartsWithMatcher>(
45  matcher)}
46  , description_{matcher.describe()}
47  {}
48 
50  Catch::Matchers::StdString::EndsWithMatcher const& matcher)
51  : matcher_{std::make_shared<Catch::Matchers::StdString::EndsWithMatcher>(
52  matcher)}
53  , description_{matcher.describe()}
54  {}
55 
57  Catch::Matchers::StdString::RegexMatcher const& matcher)
58  : matcher_{std::make_shared<Catch::Matchers::StdString::RegexMatcher>(
59  matcher)}
60  , description_{matcher.describe()}
61  {}
62 
63  bool
64  match(cet::exception const& e) const override
65  {
66  return matcher_->match(e.what());
67  }
68 
70  describe() const override
71  {
72  return description_;
73  }
74 
75 private:
76  std::shared_ptr<Catch::StringMatcher> matcher_;
78 };
79 
80 #endif /* cetlib_exception_message_matcher_h */
81 
82 // Local Variables:
83 // mode: c++
84 // End:
std::shared_ptr< Catch::StringMatcher > matcher_
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
exception_message_matcher(Catch::Matchers::StdString::ContainsMatcher const &matcher)
exception_message_matcher(Catch::Matchers::StdString::RegexMatcher const &matcher)
std::string describe() const override
exception_message_matcher(Catch::Matchers::StdString::EndsWithMatcher const &matcher)
Float_t e
Definition: plot.C:35
bool match(cet::exception const &e) const override
exception_message_matcher(Catch::Matchers::StdString::StartsWithMatcher const &matcher)
exception_message_matcher(Catch::Matchers::StdString::EqualsMatcher const &matcher)
enum BeamMode string