larra/tests/message.cpp
sha512sum 64e2aeb8aa
All checks were successful
PR Check / on-push-commit-check (push) Successful in 12m18s
Add message serialization and deserialization
2024-12-21 19:17:11 +11:00

51 lines
1.3 KiB
C++

#include <gtest/gtest.h>
#include <larra/jid.hpp>
#include <larra/message.hpp>
namespace larra::xmpp {
namespace {
auto CreateTestData() {
auto doc = std::make_unique<xmlpp::Document>();
auto node = doc->create_root_node("message");
node->set_attribute("from", "user1@server.i2p");
node->set_attribute("to", "user2@server.i2p");
node->set_attribute("type", "chat");
node->set_attribute("id", "1");
node->set_attribute("lang", "en", "xml");
auto bodyNode = node->add_child_element("body");
bodyNode->add_child_text("hello");
return doc;
}
constexpr Message<BareJid, BareJid> kMessage{
.from = {.username = "user1", .server = "server.i2p"},
.to = {.username = "user2", .server = "server.i2p"},
.type = "chat",
.id = "1",
.language = "en",
.body = "hello" //
};
} // namespace
TEST(Parse, Message) {
auto doc = CreateTestData();
auto node = doc->get_root_node();
auto message = Serialization<Message<BareJid, BareJid>>::Parse(node);
EXPECT_EQ(message, kMessage);
}
TEST(Serialize, Message) {
auto expected = CreateTestData()->write_to_string();
xmlpp::Document doc;
auto node = doc.create_root_node("message");
Serialization<Message<BareJid, BareJid>>::Serialize(node, kMessage);
EXPECT_EQ(doc.write_to_string(), expected);
}
} // namespace larra::xmpp