File indexing completed on 2023-10-25 09:48:02
0001 #include "catch.hpp"
0002 #include "FWCore/Utilities/interface/EDMException.h"
0003
0004 #include <iostream>
0005 #include <string>
0006 #include <iomanip>
0007 #include <typeinfo>
0008 #include <memory>
0009
0010 namespace {
0011 void func3() {
0012 edm::Exception ex(edm::errors::NotFound);
0013 ex << "This is just a test";
0014 ex.addContext("new1");
0015 ex.addAdditionalInfo("info1");
0016 throw ex;
0017 }
0018
0019 void func2() { func3(); }
0020
0021 void func1() {
0022 try {
0023 func2();
0024 } catch (edm::Exception& e) {
0025 edm::Exception toThrow(edm::errors::Unknown, "In func2", e);
0026 edm::Exception toThrowString(edm::errors::Unknown, std::string("In func2"), e);
0027 REQUIRE(toThrow.explainSelf() == toThrowString.explainSelf());
0028 toThrow << "\nGave up";
0029 toThrow.addContext("new2");
0030 toThrow.addAdditionalInfo("info2");
0031 REQUIRE(toThrow.returnCode() == 8003);
0032 REQUIRE(toThrow.categoryCode() == edm::errors::Unknown);
0033 cms::Exception* ptr = &toThrow;
0034 ptr->raise();
0035 }
0036 }
0037
0038 const char answer[] =
0039 "An exception of category 'Unknown' occurred while\n"
0040 " [0] new2\n"
0041 " [1] new1\n"
0042 "Exception Message:\n"
0043 "In func2\n"
0044 "This is just a test\n"
0045 "Gave up\n"
0046 " Additional Info:\n"
0047 " [a] info2\n"
0048 " [b] info1\n";
0049 }
0050
0051 TEST_CASE("Test edm::Exception", "[edm::Exception]") {
0052 SECTION("throw") { REQUIRE_THROWS_WITH(func1(), answer); }
0053 edm::Exception ex(edm::errors::NotFound);
0054 ex << "This is just a test";
0055 ex.addContext("new1");
0056 ex.addAdditionalInfo("info1");
0057
0058 SECTION("copy constructor") {
0059 edm::Exception cpy(ex);
0060 REQUIRE(ex.explainSelf() == cpy.explainSelf());
0061 }
0062 edm::Exception e1(edm::errors::Unknown, "blah");
0063 SECTION("returnCode") {
0064 REQUIRE(e1.returnCode() == 8003);
0065 REQUIRE(ex.returnCode() == 8026);
0066 }
0067 SECTION("category") {
0068 REQUIRE(e1.category() == std::string("Unknown"));
0069 REQUIRE(ex.category() == std::string("NotFound"));
0070 }
0071 SECTION("message") { REQUIRE(e1.message() == std::string("blah ")); }
0072
0073 SECTION("equivalence") {
0074 edm::Exception e1String(edm::errors::Unknown, std::string("blah"));
0075 REQUIRE(e1.explainSelf() == e1String.explainSelf());
0076 }
0077 SECTION("clone") {
0078 cms::Exception* ptr = &e1;
0079 cms::Exception* ptrCloneCopy = ptr->clone();
0080 REQUIRE(ptrCloneCopy->returnCode() == 8003);
0081 REQUIRE(e1.explainSelf() == ptrCloneCopy->explainSelf());
0082 }
0083 SECTION("throwThis") {
0084 REQUIRE_THROWS_WITH(edm::Exception::throwThis(edm::errors::ProductNotFound, "a", "b", "c", "d", "e"),
0085 "An exception of category 'ProductNotFound' occurred.\n"
0086 "Exception Message:\n"
0087 "a bcde\n");
0088 REQUIRE_THROWS_WITH(edm::Exception::throwThis(edm::errors::ProductNotFound, "a", 1, "b"),
0089 "An exception of category 'ProductNotFound' occurred.\n"
0090 "Exception Message:\n"
0091 "a 1b\n");
0092 }
0093 }