Skip to content

Commit e895a52

Browse files
committed
compare Module instances
I want to compare if a meta entry's module is the same as a module I have. The comparison is implemented on the value of lys_module*. It seems that successive calls to ly_ctx_get_module always return the same pointer so it *should* be safe. However, same modules in different contexts compare as not equal. Change-Id: I96efca783d5e780e98693330803e73847151d7af
1 parent a863b8d commit e895a52

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

include/libyang-cpp/Module.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class LIBYANG_CPP_EXPORT Module {
8989

9090
std::string printStr(const SchemaOutputFormat format, const std::optional<SchemaPrintFlags> flags = std::nullopt, std::optional<size_t> lineLength = std::nullopt) const;
9191

92+
bool operator==(const Module& other) const;
93+
9294
friend Context;
9395
friend DataNode;
9496
friend Meta;

src/Module.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ std::string Module::printStr(const SchemaOutputFormat format, const std::optiona
231231
return printModule(lys_print_module, m_module, format, flags, lineLength, "lys_print_module");
232232
}
233233

234+
bool Module::operator==(const Module& other) const
235+
{
236+
return m_module == other.m_module;
237+
}
238+
234239
SubmoduleParsed::SubmoduleParsed(const lysp_submodule* submodule, std::shared_ptr<ly_ctx> ctx)
235240
: m_ctx(ctx)
236241
, m_submodule(submodule)

tests/context.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,25 @@ TEST_CASE("context")
369369
REQUIRE(modules.at(7).revision() == std::nullopt);
370370
}
371371

372+
DOCTEST_SUBCASE("Module comparison")
373+
{
374+
ctx->setSearchDir(TESTS_DIR / "yang");
375+
auto mod = ctx->loadModule("mod1", std::nullopt, {});
376+
377+
DOCTEST_SUBCASE("Same module loaded later")
378+
{
379+
REQUIRE(mod == ctx->loadModule("mod1", std::nullopt, {}));
380+
}
381+
382+
DOCTEST_SUBCASE("Same module, different contexts")
383+
{
384+
std::optional<libyang::Context> ctx2{std::in_place, std::nullopt, libyang::ContextOptions::NoYangLibrary | libyang::ContextOptions::DisableSearchCwd};
385+
ctx2->setSearchDir(TESTS_DIR / "yang");
386+
387+
REQUIRE(mod != ctx2->loadModule("mod1", std::nullopt, {}));
388+
}
389+
}
390+
372391
DOCTEST_SUBCASE("Context::registerModuleCallback")
373392
{
374393
auto numCalled = 0;

0 commit comments

Comments
 (0)