Add tests for iq::*, add iq::*::Parse

This commit is contained in:
sha512sum 2024-10-11 18:26:16 +00:00
parent dffd7eb56e
commit bb605dc9a8
2 changed files with 72 additions and 0 deletions

View file

@ -13,6 +13,8 @@ struct BaseImplWithPayload {
std::string id;
PayloadType payload;
static const inline std::string kName = Name;
static constexpr auto kDefaultName = "iq";
constexpr auto operator==(const BaseImplWithPayload&) const -> bool = default;
template <typename Self>
[[nodiscard]] constexpr auto Id(this Self&& self, std::string id) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"id", BaseImplWithPayload>(std::forward<Self>(self), std::move(id));
@ -27,6 +29,30 @@ struct BaseImplWithPayload {
using S = Serialization<PayloadType>;
S::Serialize(element->add_child_element(S::kDefaultName, S::kPrefix), self.payload);
}
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> BaseImplWithPayload {
auto node = element->get_attribute("type");
if(!node) {
throw std::runtime_error("Not found attribute type for parse Iq");
}
if(node->get_value() != Name) {
throw std::runtime_error("Invalid attribute type for parse Iq");
}
auto idNode = element->get_attribute("id");
if(!idNode) {
throw std::runtime_error("Not found attribute id for parse Iq");
}
using S = Serialization<PayloadType>;
auto payload = element->get_first_child(S::kDefaultName);
if(!payload) {
throw std::runtime_error("Not found payload for parse Iq");
}
auto payload2 = dynamic_cast<xmlpp::Element*>(payload);
if(!payload2) {
throw std::runtime_error("Invalid payload for parse Iq");
}
return {.id = idNode->get_value(), .payload = S::Parse(payload2)};
}
};
static constexpr auto kGetName = "get";

46
tests/iq.cpp Normal file
View file

@ -0,0 +1,46 @@
#include <gtest/gtest.h>
#include <larra/iq.hpp>
namespace {
static constexpr auto kExpectedData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<iq id=\"id\" type=\"get\"><some>37</some></iq>\n";
struct SomeStruct {
int value;
constexpr auto operator==(const SomeStruct&) const -> bool = default;
static constexpr auto kDefaultName = "some";
static constexpr auto Parse(xmlpp::Element* element) -> SomeStruct {
auto node = element->get_first_child_text();
if(!node) {
throw std::runtime_error{"Not found value"};
}
return {std::stoi(node->get_content())};
}
friend constexpr auto operator<<(xmlpp::Element* element, const SomeStruct& self) {
auto node = element->add_child_text(std::to_string(self.value));
node->set_name("value");
}
};
} // namespace
namespace larra::xmpp {
TEST(IQ, Serialize) {
iq::Get iq{.id = "id", .payload = SomeStruct{.value = 37}}; // NOLINT
xmlpp::Document doc;
Serialization<iq::Get<SomeStruct>>::Serialize(doc.create_root_node("iq"), iq);
EXPECT_EQ(doc.write_to_string(), kExpectedData);
}
TEST(IQ, Parse) {
iq::Get iq{.id = "id", .payload = SomeStruct{.value = 37}}; // NOLINT
xmlpp::Document doc;
auto node = doc.create_root_node("iq");
using S = Serialization<iq::Get<SomeStruct>>;
S::Serialize(node, iq);
EXPECT_EQ(S::Parse(node), iq);
}
} // namespace larra::xmpp