larra/tests/iq.cpp

47 lines
1.3 KiB
C++
Raw Normal View History

2024-10-11 18:26:16 +00:00
#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