Compare commits

..

2 commits

Author SHA1 Message Date
1ac4f0905c Added tests for Iq Stanza errors
All checks were successful
PR Check / on-push-commit-check (push) Successful in 16m8s
2024-12-19 19:57:12 +00:00
b322076552 Added std::variant and Visitor for all IQ Stanza errors 2024-12-19 19:55:56 +00:00
3 changed files with 10 additions and 10 deletions

View file

@ -88,7 +88,7 @@ struct Client {
jid.resource = std::move(r.payload.jid->resource);
SPDLOG_INFO("Allocated resource: {}", jid.resource);
},
IqErrVisitor<::iq::Bind>{"Error response on IQ: Set::Bind"}),
IqErrThrowVisitor<::iq::Bind>{"Error response on IQ: Set::Bind"}),
std::move(set_bind_response));
co_return;
}
@ -103,7 +103,7 @@ struct Client {
roster = std::move(r.payload);
SPDLOG_INFO("New roster: {}", ToString(roster));
},
IqErrVisitor<::iq::Roster>{"Error response on IQ: Get::Roster"}),
IqErrThrowVisitor<::iq::Roster>{"Error response on IQ: Get::Roster"}),
std::move(get_roster_response));
co_return;
}

View file

@ -124,8 +124,8 @@ template <typename Payload>
using Iq = std::variant<iq::Get<Payload>, iq::Set<Payload>, iq::Result<Payload>, IqError>;
template <typename Payload>
struct IqErrVisitor {
constexpr IqErrVisitor(std::string_view baseErrorMsg) : baseErrorMsg(baseErrorMsg) {
struct IqErrThrowVisitor {
constexpr IqErrThrowVisitor(std::string_view baseErrorMsg) : baseErrorMsg(baseErrorMsg) {
}
void operator()(const iq::Get<Payload>&) {

View file

@ -72,7 +72,7 @@ TEST(IQ, ParseForbiddenError) {
ASSERT_TRUE(std::holds_alternative<iq::error::impl::Forbidden>(errorRes.payload));
}
TEST(IQ, IqErrVisitorThrow) {
TEST(IQ, IqErrThrowVisitorThrow) {
std::istringstream xml_stream(kForbiddenErrorData);
xmlpp::DomParser parser;
@ -86,7 +86,7 @@ TEST(IQ, IqErrVisitorThrow) {
static constexpr auto visitorErrMsg = "Test Error";
static constexpr auto throwErrMsg = "Stanza IQ Error: Forbidden";
try {
std::visit(utempl::Overloaded([](iq::Result<SomeStruct> r) {}, IqErrVisitor<SomeStruct>{visitorErrMsg}), std::move(iqRes));
std::visit(utempl::Overloaded([](iq::Result<SomeStruct> r) {}, IqErrThrowVisitor<SomeStruct>{visitorErrMsg}), std::move(iqRes));
} catch(const iq::error::impl::IqBaseError& err) {
ASSERT_STREQ(throwErrMsg, err.what());
return;
@ -99,7 +99,7 @@ TEST(IQ, IqErrVisitorThrow) {
ASSERT_TRUE(false) << "Expected throwing an exception due to an error in output";
}
TEST(IQ, IqErrVisitorThrowGet) {
TEST(IQ, IqErrThrowVisitorThrowGet) {
std::istringstream xml_stream(kExpectedData);
xmlpp::DomParser parser;
@ -113,7 +113,7 @@ TEST(IQ, IqErrVisitorThrowGet) {
static constexpr auto visitorErrMsg = "Test Error";
static constexpr auto throwErrMsg = "Test Error: 'Get' is an invalid type for IQ result. Expected 'Result' or 'Error'";
try {
std::visit(utempl::Overloaded([](iq::Result<SomeStruct> r) {}, IqErrVisitor<SomeStruct>{"Test Error"}), std::move(iqRes));
std::visit(utempl::Overloaded([](iq::Result<SomeStruct> r) {}, IqErrThrowVisitor<SomeStruct>{"Test Error"}), std::move(iqRes));
} catch(const iq::error::impl::IqBaseError& err) {
ASSERT_TRUE(false) << "\tERROR: Invalid throw type throw";
} catch(const std::runtime_error& err) {
@ -126,7 +126,7 @@ TEST(IQ, IqErrVisitorThrowGet) {
ASSERT_TRUE(false) << "\tERROR: Expected throwing an exception due to an error in output";
}
TEST(IQ, IqErrVisitorThrowSet) {
TEST(IQ, IqErrThrowVisitorThrowSet) {
std::istringstream xml_stream(kExpectedSetData);
xmlpp::DomParser parser;
@ -140,7 +140,7 @@ TEST(IQ, IqErrVisitorThrowSet) {
static constexpr auto visitorErrMsg = "Test Error";
static constexpr auto throwErrMsg = "Test Error: 'Set' is an invalid type for IQ result. Expected 'Result' or 'Error'";
try {
std::visit(utempl::Overloaded([](iq::Result<SomeStruct> r) {}, IqErrVisitor<SomeStruct>{"Test Error"}), std::move(iqRes));
std::visit(utempl::Overloaded([](iq::Result<SomeStruct> r) {}, IqErrThrowVisitor<SomeStruct>{"Test Error"}), std::move(iqRes));
} catch(const iq::error::impl::IqBaseError& err) {
ASSERT_TRUE(false) << "\tERROR: Invalid throw type throw";
} catch(const std::runtime_error& err) {