This commit is contained in:
parent
b3a4757b2f
commit
fe5b652240
3 changed files with 30 additions and 9 deletions
|
@ -157,7 +157,11 @@ struct Error {
|
|||
if(!attr || attr->get_value() != "error") {
|
||||
return std::nullopt;
|
||||
}
|
||||
return serialization::TryParse<Error>(element);
|
||||
try {
|
||||
return serialization::Parse<Error>(element);
|
||||
} catch(...) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
friend auto operator<<(xmlpp::Element* element, const Error& message) -> void {
|
||||
element->set_attribute("type", "error");
|
||||
|
|
|
@ -37,10 +37,7 @@ constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T
|
|||
template <typename T>
|
||||
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T;
|
||||
|
||||
template <typename T>
|
||||
constexpr auto TryParse(xmlpp::Element* element, Tag<T> = {}) -> std::optional<T> {
|
||||
return Serialization<std::optional<T>>::Parse(element);
|
||||
}
|
||||
// TODO(sha512sum): Add TryParse
|
||||
|
||||
template <typename T>
|
||||
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void
|
||||
|
|
|
@ -21,6 +21,19 @@ auto CreateTestData() {
|
|||
return doc;
|
||||
}
|
||||
|
||||
auto CreateErrorTestData() {
|
||||
auto doc = std::make_unique<xmlpp::Document>();
|
||||
auto node = doc->create_root_node("message");
|
||||
node->set_attribute("from", "user1@server.i2p");
|
||||
node->set_attribute("to", "user2@server.i2p");
|
||||
node->set_attribute("type", "error");
|
||||
auto error = node->add_child_element("error");
|
||||
error->set_attribute("type", "auth");
|
||||
auto notAuthorized = error->add_child_element("not-authorized");
|
||||
notAuthorized->set_namespace_declaration("urn:ietf:params:xml:ns:xmpp-stanzas");
|
||||
return doc;
|
||||
}
|
||||
|
||||
const message::Message<BareJid, BareJid> kMessage{
|
||||
.from = {.username = "user1", .server = "server.i2p"},
|
||||
.to = {.username = "user2", .server = "server.i2p"},
|
||||
|
@ -37,11 +50,18 @@ const message::Error<BareJid, BareJid> kError{.from = {.username = "user1", .ser
|
|||
} // namespace
|
||||
|
||||
TEST(Parse, Message) {
|
||||
auto doc = CreateTestData();
|
||||
auto node = doc->get_root_node();
|
||||
auto message = Serialization<message::Message<BareJid, BareJid>>::Parse(node);
|
||||
using T = Message<BareJid, BareJid>;
|
||||
{
|
||||
auto doc = CreateTestData();
|
||||
auto node = doc->get_root_node();
|
||||
auto message = Serialization<T>::Parse(node);
|
||||
EXPECT_EQ(message, static_cast<T>(kMessage));
|
||||
}
|
||||
|
||||
EXPECT_EQ(message, kMessage);
|
||||
auto doc = CreateErrorTestData();
|
||||
auto node = doc->get_root_node();
|
||||
auto message = Serialization<T>::Parse(node);
|
||||
EXPECT_EQ(message, static_cast<T>(kError));
|
||||
}
|
||||
|
||||
TEST(Serialize, Message) {
|
||||
|
|
Loading…
Reference in a new issue