2024-10-06 18:56:10 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2024-10-19 18:15:24 +00:00
|
|
|
#include <larra/jid.hpp>
|
2024-10-06 18:56:10 +00:00
|
|
|
#include <larra/serialization.hpp>
|
2024-10-18 15:03:19 +00:00
|
|
|
#include <larra/serialization/auto.hpp>
|
|
|
|
#include <larra/serialization/error.hpp>
|
2024-10-06 18:56:10 +00:00
|
|
|
#include <larra/stream_error.hpp>
|
|
|
|
|
2024-10-18 15:03:19 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2024-10-06 18:56:10 +00:00
|
|
|
namespace larra::xmpp {
|
|
|
|
|
|
|
|
TEST(Parse, Variant) {
|
|
|
|
xmlpp::Document doc;
|
|
|
|
auto node = doc.create_root_node("stream:error");
|
|
|
|
node->add_child_element("unsupported-stanza-type");
|
|
|
|
node->set_namespace_declaration("urn:ietf:params:xml:ns:xmpp-streams");
|
|
|
|
auto streamError = Serialization<StreamError>::Parse(node);
|
|
|
|
EXPECT_TRUE(std::get_if<error::stream::UnsupportedStanzaType>(&streamError));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Serialize, Variant) {
|
|
|
|
using S = Serialization<StreamError>;
|
|
|
|
StreamError data = error::stream::UnsupportedStanzaType{};
|
|
|
|
xmlpp::Document doc;
|
|
|
|
auto node = doc.create_root_node("stream:error");
|
|
|
|
S::Serialize(node, data);
|
|
|
|
EXPECT_EQ(doc.write_to_string(),
|
2024-10-18 15:03:19 +00:00
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stream:error><unsupported-stanza-type "
|
|
|
|
"xmlns=\"urn:ietf:params:xml:ns:xmpp-streams\"/></stream:error>\n"sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace tests::serialization {
|
|
|
|
|
|
|
|
struct SomeStruct {
|
|
|
|
static constexpr auto kDefaultName = "some";
|
|
|
|
std::string value;
|
|
|
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SomeStruct2 {
|
|
|
|
static constexpr auto kDefaultName = "some2";
|
|
|
|
SomeStruct value;
|
|
|
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct2;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SomeStruct3 {
|
|
|
|
static constexpr auto kDefaultName = "some3";
|
|
|
|
int value;
|
|
|
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct3;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SomeStruct4 {
|
|
|
|
static constexpr auto kDefaultName = "some4";
|
|
|
|
SomeStruct3 value;
|
|
|
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct4;
|
|
|
|
};
|
|
|
|
|
2024-10-19 18:15:24 +00:00
|
|
|
struct SomeStruct5 {
|
|
|
|
static constexpr auto kDefaultName = "some5";
|
|
|
|
BareJid value;
|
|
|
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct5;
|
|
|
|
};
|
|
|
|
|
2024-10-18 15:03:19 +00:00
|
|
|
} // namespace tests::serialization
|
|
|
|
|
|
|
|
namespace serialization {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct> = SerializationConfig<tests::serialization::SomeStruct>{};
|
|
|
|
|
|
|
|
template <>
|
2024-10-19 18:15:24 +00:00
|
|
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct2> = SerializationConfig<tests::serialization::SomeStruct2>{};
|
2024-10-18 15:03:19 +00:00
|
|
|
|
|
|
|
template <>
|
2024-10-19 18:15:24 +00:00
|
|
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct4> = SerializationConfig<tests::serialization::SomeStruct4>{};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct5> = SerializationConfig<tests::serialization::SomeStruct5>{};
|
2024-10-18 15:03:19 +00:00
|
|
|
|
|
|
|
} // namespace serialization
|
|
|
|
|
|
|
|
auto tests::serialization::SomeStruct::Parse(xmlpp::Element* element) -> SomeStruct {
|
|
|
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct>(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tests::serialization::SomeStruct2::Parse(xmlpp::Element* element) -> SomeStruct2 {
|
|
|
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct2>(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tests::serialization::SomeStruct3::Parse(xmlpp::Element*) -> SomeStruct3 {
|
|
|
|
return {.value = 42}; // NOLINT
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tests::serialization::SomeStruct4::Parse(xmlpp::Element* element) -> SomeStruct4 {
|
|
|
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct4>(element);
|
|
|
|
}
|
|
|
|
|
2024-10-19 18:15:24 +00:00
|
|
|
auto tests::serialization::SomeStruct5::Parse(xmlpp::Element* element) -> SomeStruct5 {
|
|
|
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct5>(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AutoParse, Basic) {
|
2024-10-18 15:03:19 +00:00
|
|
|
xmlpp::Document doc;
|
|
|
|
auto node = doc.create_root_node("some2");
|
|
|
|
node = node->add_child_element("some");
|
|
|
|
node->set_attribute("value", "Hello");
|
|
|
|
auto a = Serialization<tests::serialization::SomeStruct>::Parse(node);
|
|
|
|
EXPECT_EQ(a.value, "Hello"sv);
|
|
|
|
auto b = Serialization<tests::serialization::SomeStruct2>::Parse(doc.get_root_node());
|
|
|
|
EXPECT_EQ(b.value.value, "Hello"sv);
|
|
|
|
EXPECT_THROW(std::ignore = tests::serialization::SomeStruct2::Parse(node), serialization::SerializationError);
|
|
|
|
auto node2 = node->add_child_element("some4");
|
|
|
|
node2->add_child_element("some3");
|
|
|
|
auto c = Serialization<tests::serialization::SomeStruct4>::Parse(node2);
|
|
|
|
EXPECT_EQ(c.value.value, 42);
|
2024-10-06 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 18:15:24 +00:00
|
|
|
TEST(AutoParse, Attribute) {
|
|
|
|
xmlpp::Document doc;
|
|
|
|
auto node = doc.create_root_node("some5");
|
|
|
|
node->set_attribute("value", "user@server.i2p");
|
|
|
|
auto a = Serialization<tests::serialization::SomeStruct5>::Parse(node);
|
|
|
|
EXPECT_EQ(a.value.server, "server.i2p"sv);
|
|
|
|
EXPECT_EQ(a.value.username, "user"sv);
|
|
|
|
}
|
|
|
|
|
2024-10-06 18:56:10 +00:00
|
|
|
} // namespace larra::xmpp
|