Added tests. Found serialize issue
Some checks failed
PR Check / on-push-commit-check (push) Failing after 3m32s

This commit is contained in:
Ivan-lis 2024-11-16 13:29:13 +00:00
parent 947076fe8f
commit bd80c9684d
4 changed files with 47 additions and 2 deletions

12
.vscode/launch.json vendored
View file

@ -12,6 +12,18 @@
"args": [], "args": [],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"preLaunchTask": "GCC: Build" "preLaunchTask": "GCC: Build"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug: tests",
"program": "${workspaceFolder}/build/larra_xmpp_tests",
"args": [
// --gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS]
"--gtest_filter=Roster*"
],
"cwd": "${workspaceFolder}",
"preLaunchTask": "GCC: Build"
} }
] ]
} }

View file

@ -47,7 +47,7 @@ using SetBind = Set<Bind>;
using ResultBind = Result<Bind>; using ResultBind = Result<Bind>;
using IqBind = Iq<Bind>; using IqBind = Iq<Bind>;
auto MakeSetBind() { inline auto MakeSetBind() {
return SetBind{.id = "1", .payload = Bind{}}; return SetBind{.id = "1", .payload = Bind{}};
} }
} // namespace larra::xmpp::iq } // namespace larra::xmpp::iq

View file

@ -2,6 +2,7 @@
#include <libxml++/libxml++.h> #include <libxml++/libxml++.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <cassert>
#include <larra/iq.hpp> #include <larra/iq.hpp>
#include <larra/jid.hpp> #include <larra/jid.hpp>
#include <larra/utils.hpp> #include <larra/utils.hpp>
@ -40,6 +41,8 @@ struct Roster {
} }
friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster) { friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster) {
element->set_attribute("xmlns", "jabber:iq:roster"); element->set_attribute("xmlns", "jabber:iq:roster");
// TODO: Add adding elements to xmlpp::Element
assert(false);
} }
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Roster { [[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Roster {
const auto& item_nodes = element->get_children("item"); const auto& item_nodes = element->get_children("item");
@ -68,7 +71,7 @@ using GetRoster = Get<Roster>;
using ResultRoster = Result<Roster>; using ResultRoster = Result<Roster>;
using IqRoster = Iq<Roster>; using IqRoster = Iq<Roster>;
auto MakeGetRoster(const FullJid& jid) { inline auto MakeGetRoster(const FullJid& jid) {
return GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}}; return GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}};
} }

30
tests/roster.cpp Normal file
View file

@ -0,0 +1,30 @@
#include <gtest/gtest.h>
#include <larra/roster.hpp>
#include <ranges>
#include "larra/jid.hpp"
namespace larra::xmpp {
TEST(Roster, SerializeAndParse) {
FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT
auto roster = iq::MakeGetRoster(jid);
roster.payload.items.emplace_back("u1", "s1");
roster.payload.items.emplace_back("u2", "s2");
roster.payload.items.emplace_back("u3", "s3");
using S = Serialization<iq::GetRoster>;
xmlpp::Document doc;
auto node = doc.create_root_node("iq");
node << roster;
auto parse_res = iq::Roster::Parse(node);
ASSERT_EQ(roster.payload.items.size(), parse_res.items.size());
for(const auto& [idx, expect_el, parsed_el] : std::views::zip(std::views::iota(0), roster.payload.items, parse_res.items)) {
EXPECT_EQ(expect_el, parsed_el) << "Mismatched on idx: " << idx;
// std::cerr << " " << "idx: " << idx << "; expect_el: " << expect_el << "; parsed_el: " << parsed_el << '\n';
}
}
} // namespace larra::xmpp