From: Andrei Pavel Date: Mon, 31 Oct 2022 12:40:27 +0000 (+0200) Subject: [#2601] EXPECT_THROW -> EXPECT_THROW_MSG X-Git-Tag: Kea-2.3.3~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad9859aad83ada4367fdf12bb796fbd457a37ca1;p=thirdparty%2Fkea.git [#2601] EXPECT_THROW -> EXPECT_THROW_MSG --- diff --git a/src/bin/netconf/tests/control_socket_unittests.cc b/src/bin/netconf/tests/control_socket_unittests.cc index 80ae233bdf..1c79a061ed 100644 --- a/src/bin/netconf/tests/control_socket_unittests.cc +++ b/src/bin/netconf/tests/control_socket_unittests.cc @@ -91,7 +91,8 @@ TEST(StdoutControlSocketTest, configGet) { ASSERT_TRUE(cfg); StdoutControlSocketPtr scs(new StdoutControlSocket(cfg)); ASSERT_TRUE(scs); - EXPECT_THROW(scs->configGet("foo"), NotImplemented); + EXPECT_THROW_MSG(scs->configGet("foo"), NotImplemented, + "No config-get for stdout control socket"); } // Verifies that a stdout control socket does not nothing for configTest. @@ -397,7 +398,8 @@ TEST_F(UnixControlSocketTest, timeout) { thread_.reset(new thread([this]() { waitReady(); })); // Try configGet: it should get a communication error, - EXPECT_THROW(ucs->configGet("foo"), ControlSocketError); + EXPECT_THROW_MSG(ucs->configGet("foo"), ControlSocketError, + "communication error: No such file or directory"); signalReady(); } diff --git a/src/bin/netconf/tests/netconf_cfg_mgr_unittests.cc b/src/bin/netconf/tests/netconf_cfg_mgr_unittests.cc index c0cb392deb..23eea6d497 100644 --- a/src/bin/netconf/tests/netconf_cfg_mgr_unittests.cc +++ b/src/bin/netconf/tests/netconf_cfg_mgr_unittests.cc @@ -102,7 +102,7 @@ TEST(NetconfCfgMgr, contextServer) { EXPECT_EQ(1, ctx.getCfgServersMap()->size()); ASSERT_NO_THROW_LOG(ctx.getCfgServersMap()->at("d2")); EXPECT_EQ(server1, ctx.getCfgServersMap()->at("d2")); - EXPECT_THROW(ctx.getCfgServersMap()->at("dhcp4"), std::out_of_range); + EXPECT_FALSE(ctx.getCfgServersMap()->contains("dhcp4")); // Now set the v6 server and sanity check again EXPECT_NO_THROW_LOG(ctx.getCfgServersMap()->insert(make_pair("dhcp6", server2))); @@ -152,7 +152,8 @@ TEST(NetconfCfgMgr, contextGlobals) { EXPECT_EQ(0, globals->mapValue().size()); // Attempting to extract globals from a non-map should throw. - ASSERT_THROW(ctx.extractConfiguredGlobals(Element::create(777)), BadValue); + EXPECT_THROW_MSG(ctx.extractConfiguredGlobals(Element::create(777)), BadValue, + "extractConfiguredGlobals must be given a map element"); // Now let's create a configuration from which to extract global scalars. // Extraction (currently) has no business logic, so the elements we use diff --git a/src/bin/netconf/tests/netconf_controller_unittests.cc b/src/bin/netconf/tests/netconf_controller_unittests.cc index d4f3606ead..7fc2a4a3fc 100644 --- a/src/bin/netconf/tests/netconf_controller_unittests.cc +++ b/src/bin/netconf/tests/netconf_controller_unittests.cc @@ -136,7 +136,7 @@ TEST_F(NetconfControllerTest, commandLineArgs) { char* argv2[] = { const_cast("progName"), const_cast("-x") }; argc = 2; - EXPECT_THROW(parseArgs(argc, argv2), InvalidUsage); + EXPECT_THROW_MSG(parseArgs(argc, argv2), InvalidUsage, "unsupported option: [x] "); } // Tests application process creation and initialization. diff --git a/src/bin/netconf/tests/netconf_process_unittests.cc b/src/bin/netconf/tests/netconf_process_unittests.cc index 5e44e70a2d..4cf9105fce 100644 --- a/src/bin/netconf/tests/netconf_process_unittests.cc +++ b/src/bin/netconf/tests/netconf_process_unittests.cc @@ -52,8 +52,8 @@ TEST(NetconfProcess, construction) { // Verify that the constructor will fail if given an empty // io service. IOServicePtr lcl_io_service; - EXPECT_THROW(NetconfProcess("TestProcess", lcl_io_service), - DProcessBaseError); + EXPECT_THROW_MSG(NetconfProcess("TestProcess", lcl_io_service), DProcessBaseError, + "IO Service cannot be null"); // Verify that the constructor succeeds with a valid io_service lcl_io_service.reset(new IOService()); diff --git a/src/bin/netconf/tests/parser_unittests.cc b/src/bin/netconf/tests/parser_unittests.cc index 30134110a8..27d9f93dee 100644 --- a/src/bin/netconf/tests/parser_unittests.cc +++ b/src/bin/netconf/tests/parser_unittests.cc @@ -850,14 +850,14 @@ TEST(ParserTest, mapEntries) { /// @param json the JSON configuration with the duplicate entry. void testDuplicate(ConstElementPtr json) { string config = json->str(); + SCOPED_TRACE("\n* Tested config: \n---\n" + json->str() + "\n---"); + size_t where = config.find("DDDD"); ASSERT_NE(string::npos, where); string before = config.substr(0, where); string after = config.substr(where + 4, string::npos); ParserContext ctx; - EXPECT_THROW(ctx.parseString(before + after, - ParserContext::PARSER_NETCONF), - ParseError) << "config: " << config; + EXPECT_THROW(ctx.parseString(before + after, ParserContext::PARSER_NETCONF), ParseError); } // This test checks that duplicate entries make parsing to fail. @@ -888,6 +888,8 @@ TEST(ParserTest, duplicateMapEntries) { continue; } + SCOPED_TRACE("\n* Tested duplicate element: " + elem.first); + // Perform tests. string dup = elem.first + "DDDD"; json->set(dup, elem.second); diff --git a/src/lib/yang/tests/adaptor_option_unittests.cc b/src/lib/yang/tests/adaptor_option_unittests.cc index 25ffb8d0b3..3c151f56ff 100644 --- a/src/lib/yang/tests/adaptor_option_unittests.cc +++ b/src/lib/yang/tests/adaptor_option_unittests.cc @@ -66,7 +66,8 @@ TEST(AdaptorOptionTest, checkTypeNoType) { "}"; ConstElementPtr json; ASSERT_NO_THROW_LOG(json = Element::fromJSON(config)); - EXPECT_THROW(AdaptorOption::checkType(json), MissingKey); + EXPECT_THROW_MSG(AdaptorOption::checkType(json), MissingKey, + "missing type in option definition { }"); } // Verifies that checkCode accepts an option with code. @@ -85,7 +86,7 @@ TEST(AdaptorOptionTest, checkCodeNoCode) { "}"; ConstElementPtr json; ASSERT_NO_THROW_LOG(json = Element::fromJSON(config)); - EXPECT_THROW(AdaptorOption::checkCode(json), MissingKey); + EXPECT_THROW_MSG(AdaptorOption::checkCode(json), MissingKey, "missing code in option { }"); } // Verifies that collect works as expected. @@ -101,7 +102,7 @@ TEST(AdaptorOptionTest, collect) { ASSERT_NO_THROW_LOG(AdaptorOption::collect(json, codes)); EXPECT_EQ(1, codes.size()); EXPECT_EQ(123, codes["bar@foo"]); - EXPECT_THROW(codes.at("foo@bar"), out_of_range); + EXPECT_FALSE(codes.contains("foo@bar")); } // Verifies that collect skips an already known option definition. @@ -159,7 +160,8 @@ TEST(AdaptorOptionTest, setCodeNoName) { ElementPtr json; ASSERT_NO_THROW_LOG(json = Element::fromJSON(config)); OptionCodes codes; - EXPECT_THROW(AdaptorOption::setCode(json, codes), MissingKey); + EXPECT_THROW_MSG(AdaptorOption::setCode(json, codes), MissingKey, + "missing name and code in option { \"space\": \"bar\" }"); } // Note the code assumes there is a space, i.e. setSpace was called. @@ -174,7 +176,8 @@ TEST(AdaptorOptionTest, setCodeNotInMap) { ElementPtr json; ASSERT_NO_THROW_LOG(json = Element::fromJSON(config)); OptionCodes codes; - EXPECT_THROW(AdaptorOption::setCode(json, codes), MissingKey); + EXPECT_THROW_MSG(AdaptorOption::setCode(json, codes), MissingKey, + "can't get code from option { \"name\": \"foo\", \"space\": \"bar\" }"); } /// @brief Test class to make initCodes public. @@ -208,7 +211,7 @@ TEST(AdaptorOptionTest, initCodes4) { ASSERT_NO_THROW_LOG(AdaptorOption::initCodes(codes, DHCP4_OPTION_SPACE)); EXPECT_EQ(DHO_SUBNET_MASK, codes["dhcp4@subnet-mask"]); EXPECT_EQ(DHO_TIME_OFFSET, codes["dhcp4@time-offset"]); - EXPECT_THROW(codes.at("dhcp6@clientid"), out_of_range); + EXPECT_FALSE(codes.contains("dhcp6@clientid")); // initCodes loads last resort too. EXPECT_EQ(DHO_VENDOR_ENCAPSULATED_OPTIONS, @@ -224,7 +227,7 @@ TEST(AdaptorOptionTest, initCodes6) { ASSERT_NO_THROW_LOG(AdaptorOption::initCodes(codes, DHCP6_OPTION_SPACE)); EXPECT_EQ(D6O_CLIENTID, codes["dhcp6@clientid"]); EXPECT_EQ(D6O_SERVERID, codes["dhcp6@serverid"]); - EXPECT_THROW(codes.at("dhcp4@subnet-mask"), out_of_range); + EXPECT_FALSE(codes.contains("dhcp4@subnet-mask")); // initCodes loads DOCSIS3 too. EXPECT_EQ(32, codes["vendor-4491@tftp-servers"]); diff --git a/src/lib/yang/tests/adaptor_pool_unittests.cc b/src/lib/yang/tests/adaptor_pool_unittests.cc index 6f3d764bac..05d77259c2 100644 --- a/src/lib/yang/tests/adaptor_pool_unittests.cc +++ b/src/lib/yang/tests/adaptor_pool_unittests.cc @@ -118,8 +118,8 @@ TEST(AdaptorPoolTest, fromSubnetKea) { EXPECT_TRUE(copied->equals(*json)); // Check that the model name is actually checked. - EXPECT_THROW(AdaptorPool::fromSubnet("non-existent-module", json, pools), - NotImplemented); + EXPECT_THROW_MSG(AdaptorPool::fromSubnet("non-existent-module", json, pools), NotImplemented, + "fromSubnet not implemented for the model: non-existent-module"); } // Verifies that fromSubnet works as expected. @@ -190,8 +190,8 @@ TEST(AdaptorPoolTest, toSubnetKea) { EXPECT_NO_THROW_LOG(AdaptorPool::toSubnet(KEA_DHCP6_SERVER, json, pools)); EXPECT_TRUE(copied->equals(*json)); // Model name is not free: an error is raised if it is not expected. - EXPECT_THROW(AdaptorPool::toSubnet("keatest-module", json, pools), - NotImplemented); + EXPECT_THROW_MSG(AdaptorPool::toSubnet("keatest-module", json, pools), NotImplemented, + "toSubnet not implemented for the model: keatest-module"); } // Verifies that toSubnet works as expected. @@ -267,8 +267,10 @@ TEST(AdaptorPoolTest, toSubnetBad) { ElementPtr json; ASSERT_NO_THROW_LOG(json = Element::fromJSON(config)); ConstElementPtr pools = json->get("pools"); - EXPECT_THROW(AdaptorPool::toSubnet(IETF_DHCPV6_SERVER, json, pools), - BadValue); + EXPECT_THROW_MSG(AdaptorPool::toSubnet(IETF_DHCPV6_SERVER, json, pools), BadValue, + "inconsistent value of rebind-timer in [ { \"pool\": \"2001:db8:1::/80\", " + "\"rebind-timer\": 2000 }, { \"pool\": \"2001:db8:1:0:1::/80\", " + "\"rebind-timer\": 20 } ]"); } }; // end of anonymous namespace diff --git a/src/lib/yang/tests/adaptor_unittests.cc b/src/lib/yang/tests/adaptor_unittests.cc index 2dd2e1c293..b819e25d65 100644 --- a/src/lib/yang/tests/adaptor_unittests.cc +++ b/src/lib/yang/tests/adaptor_unittests.cc @@ -160,12 +160,16 @@ TEST(AdaptorTest, toParent) { EXPECT_TRUE(json->equals(*Element::fromJSON(expected))); // param[345] have different values so it should throw. - EXPECT_THROW(Adaptor::toParent("param3",json, json->get("list")), - BadValue); - EXPECT_THROW(Adaptor::toParent("param4",json, json->get("list")), - BadValue); - EXPECT_THROW(Adaptor::toParent("param5",json, json->get("list")), - BadValue); + EXPECT_THROW_MSG(Adaptor::toParent("param3", json, json->get("list")), BadValue, + "inconsistent value of param3 in [ { \"param3\": 234, \"param4\": true }, { " + "\"another\": \"entry\", \"param3\": 123, \"param5\": false } ]"); + EXPECT_THROW_MSG(Adaptor::toParent("param4", json, json->get("list")), BadValue, + "inconsistent value of param4 in [ { \"param3\": 234, \"param4\": true }, { " + "\"another\": \"entry\", \"param3\": 123, \"param5\": false } ]"); + EXPECT_THROW_MSG(Adaptor::toParent("param5", json, json->get("list")), BadValue, + "inconsistent value of param5 in [ { \"param3\": 234, \"param4\": true }, { " + "\"another\": \"entry\", \"param3\": 123, \"param5\": false } ]"); + // And not modify the value. EXPECT_TRUE(json->equals(*Element::fromJSON(expected))); } diff --git a/src/lib/yang/tests/translator_unittests.cc b/src/lib/yang/tests/translator_unittests.cc index 477e8b8d89..c17f8020c6 100644 --- a/src/lib/yang/tests/translator_unittests.cc +++ b/src/lib/yang/tests/translator_unittests.cc @@ -222,8 +222,12 @@ TEST_F(TranslatorBasicTest, getItem) { // Not found. xpath = "/keatest-module:main/no_such_string"; - EXPECT_THROW(t_obj->deleteItem(xpath), NetconfError); - EXPECT_THROW(elem = t_obj->getItem(xpath), NetconfError); + EXPECT_THROW_MSG(t_obj->deleteItem(xpath), NetconfError, + "sysrepo error getting item at '/keatest-module:main/no_such_string': " + "Couldn't find schema node: /keatest-module:main/no_such_string"); + EXPECT_THROW_MSG(elem = t_obj->getItem(xpath), NetconfError, + "sysrepo error getting item at '/keatest-module:main/no_such_string': " + "Couldn't find schema node: /keatest-module:main/no_such_string"); EXPECT_FALSE(elem); elem.reset(); @@ -246,11 +250,13 @@ TEST_F(TranslatorBasicTest, valueTo) { // Container. elem = Element::createMap(); - EXPECT_THROW(TranslatorBasic::translate(elem, LeafBaseType::Unknown), NotImplemented); + EXPECT_THROW_MSG(TranslatorBasic::translate(elem, LeafBaseType::Unknown), NotImplemented, + "TranslatorBasic::value(): map element"); // List. elem = Element::createList(); - EXPECT_THROW(TranslatorBasic::translate(elem, LeafBaseType::Unknown), NotImplemented); + EXPECT_THROW_MSG(TranslatorBasic::translate(elem, LeafBaseType::Unknown), NotImplemented, + "TranslatorBasic::value(): list element"); // String. string str("foo");