CMS 3D CMS Logo

TestGetByLabelAnalyzer.cc
Go to the documentation of this file.
4 
5 #include "DataFormats/TestObjects/interface/ToyProducts.h"
6 #include "DataFormats/TestObjects/interface/ThingCollection.h"
7 
8 namespace edmtest {
9  template <typename T>
11  public:
13  : src_(iPSet.getUntrackedParameter<edm::InputTag>("src")),
14  getCategory_(iPSet.getUntrackedParameter<std::string>("getExceptionCategory")),
15  accessCategory_(iPSet.getUntrackedParameter<std::string>("accessExceptionCategory")) {
16  if (iPSet.getUntrackedParameter<bool>("consumes")) {
17  consumes<T>(src_);
18  }
19  }
20 
21  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
23  desc.addUntracked<edm::InputTag>("src");
24  desc.addUntracked<std::string>("getExceptionCategory", "");
25  desc.addUntracked<std::string>("accessExceptionCategory", "");
26  desc.addUntracked<bool>("consumes", false);
27  descriptions.addDefault(desc);
28  }
29 
30  void analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const&) const override {
32 
33  auto test = [](std::string const& category, std::string const& msg, auto func) {
34  bool caught = false;
35  try {
36  func();
37  } catch (cms::Exception& e) {
38  caught = true;
39  if (category.empty())
40  throw;
41  if (e.category() != category) {
42  throw cms::Exception("Assert")
43  << "Expected cms::Exception from " << msg << " with category " << category << ", got " << e.category();
44  }
45  return false;
46  }
47  if (not category.empty() and not caught) {
48  throw cms::Exception("Assert") << "Expected cms::Exception to be thrown from " << msg << ", but got nothing";
49  }
50  return true;
51  };
52 
53  bool noException = test(getCategory_, "getByLabel(InputTag)", [&]() { event.getByLabel(src_, handle); });
54  if (noException) {
55  test(accessCategory_, "*handle from InputTag", [&]() { *handle; });
56  }
57 
58  noException =
59  test(getCategory_, "getByLabel(strings)", [&]() { event.getByLabel(src_.label(), src_.instance(), handle); });
60  if (noException) {
61  test(accessCategory_, "*handle from strings", [&]() { *handle; });
62  }
63  }
64 
65  private:
69  };
70 
73 } // namespace edmtest
74 
std::string const & instance() const
Definition: InputTag.h:37
void analyze(edm::StreamID, edm::Event const &event, edm::EventSetup const &) const override
TestGetByLabelAnalyzerT(edm::ParameterSet const &iPSet)
std::string const & label() const
Definition: InputTag.h:36
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T getUntrackedParameter(std::string const &, T const &) const
void addDefault(ParameterSetDescription const &psetDescription)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
tuple msg
Definition: mps_check.py:286
HLT enums.
Definition: event.py:1