From bb605dc9a8d3a7d0c6c3bc0822c18f0fa194ac2d Mon Sep 17 00:00:00 2001 From: sha512sum Date: Fri, 11 Oct 2024 18:26:16 +0000 Subject: [PATCH] Add tests for iq::*, add iq::*::Parse --- library/include/larra/iq.hpp | 26 ++++++++++++++++++++ tests/iq.cpp | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tests/iq.cpp diff --git a/library/include/larra/iq.hpp b/library/include/larra/iq.hpp index 93b07cd..bd7b291 100644 --- a/library/include/larra/iq.hpp +++ b/library/include/larra/iq.hpp @@ -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 [[nodiscard]] constexpr auto Id(this Self&& self, std::string id) -> std::decay_t { return utils::FieldSetHelper::With<"id", BaseImplWithPayload>(std::forward(self), std::move(id)); @@ -27,6 +29,30 @@ struct BaseImplWithPayload { using S = Serialization; 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; + auto payload = element->get_first_child(S::kDefaultName); + if(!payload) { + throw std::runtime_error("Not found payload for parse Iq"); + } + auto payload2 = dynamic_cast(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"; diff --git a/tests/iq.cpp b/tests/iq.cpp new file mode 100644 index 0000000..4638ffe --- /dev/null +++ b/tests/iq.cpp @@ -0,0 +1,46 @@ +#include + +#include + +namespace { + +static constexpr auto kExpectedData = "\n37\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>::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>; + S::Serialize(node, iq); + EXPECT_EQ(S::Parse(node), iq); +} + +} // namespace larra::xmpp