107 lines
3.7 KiB
C++
107 lines
3.7 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <larra/serialization.hpp>
|
|
#include <larra/serialization/auto.hpp>
|
|
#include <larra/serialization/error.hpp>
|
|
#include <larra/stream_error.hpp>
|
|
|
|
using namespace std::literals;
|
|
|
|
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(),
|
|
"<?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;
|
|
};
|
|
|
|
} // namespace tests::serialization
|
|
|
|
namespace serialization {
|
|
|
|
template <>
|
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct> = SerializationConfig<tests::serialization::SomeStruct>{};
|
|
|
|
template <>
|
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct2> =
|
|
SerializationConfig<tests::serialization::SomeStruct2>{}.With<"value">(serialization::Config<tests::serialization::SomeStruct>{});
|
|
|
|
template <>
|
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct4> =
|
|
SerializationConfig<tests::serialization::SomeStruct4>{}.With<"value">(serialization::Config<tests::serialization::SomeStruct3>{});
|
|
|
|
} // 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);
|
|
}
|
|
|
|
TEST(Parse, Auto) {
|
|
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);
|
|
}
|
|
|
|
} // namespace larra::xmpp
|