Fix identityref completion not giving module names

Issue: https://tree.taiga.io/project/jktjkt-netconf-cli/issue/168
Change-Id: Ib1f4532e5282591e929e121d30ebbbf5d1571e0b
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 78e67f0..abbe97b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -274,8 +274,8 @@
     cli_test(utils)
     cli_test(path_completion)
     cli_test(command_completion)
-    cli_test(enum_completion)
-    target_link_libraries(test_enum_completion leaf_data_type)
+    cli_test(set_value_completion)
+    target_link_libraries(test_set_value_completion leaf_data_type)
     cli_test(list_manipulation)
     cli_test(ls_interpreter)
     cli_test(path_utils)
diff --git a/src/leaf_data.hpp b/src/leaf_data.hpp
index 2f84a05..b33d4c1 100644
--- a/src/leaf_data.hpp
+++ b/src/leaf_data.hpp
@@ -124,7 +124,14 @@
         std::transform(type.m_allowedValues.begin(),
                 type.m_allowedValues.end(),
                 std::inserter(parserContext.m_suggestions, parserContext.m_suggestions.end()),
-                [](auto it) { return Completion{it.m_value}; });
+                [](auto it) {
+            std::string res;
+            if constexpr (std::is_same<Type, yang::IdentityRef>()) {
+                res = it.m_prefix ? it.m_prefix->m_name + ":" : "";
+            }
+            res += it.m_value;
+            return Completion{res};
+        });
         parserContext.m_completionIterator = first;
     }
     bool operator()(const yang::Enum& type) const
diff --git a/tests/enum_completion.cpp b/tests/set_value_completion.cpp
similarity index 76%
rename from tests/enum_completion.cpp
rename to tests/set_value_completion.cpp
index 07ea32c..5ae7deb 100644
--- a/tests/enum_completion.cpp
+++ b/tests/set_value_completion.cpp
@@ -14,7 +14,7 @@
 #include "pretty_printers.hpp"
 #include "static_schema.hpp"
 
-TEST_CASE("enum completion")
+TEST_CASE("set value completion")
 {
     auto schema = std::make_shared<StaticSchema>();
     schema->addModule("mod");
@@ -24,6 +24,12 @@
     schema->addList("/", "mod:list", {"number"});
     schema->addLeaf("/mod:list", "mod:number", yang::Int32{});
     schema->addLeaf("/mod:list", "mod:leafInList", createEnum({"ano", "anoda", "ne", "katoda"}));
+    schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "food"});
+    schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "vehicle"});
+    schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "pizza"});
+    schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "spaghetti"});
+    schema->addIdentity(ModuleValuePair{"mod", "pizza"}, ModuleValuePair{"pizza-module", "hawaii"});
+    schema->addLeaf("/", "mod:foodIdentRef", yang::IdentityRef{schema->validIdentities("mod", "food")});
     auto mockDatastore = std::make_shared<MockDatastoreAccess>();
     // The parser will use DataQuery for key value completion, but I'm not testing that here, so I don't return anything.
     ALLOW_CALL(*mockDatastore, listInstances("/mod:list"))
@@ -76,5 +82,12 @@
         expectedContextLength = 0;
     }
 
+    SECTION("set mod:foodIdentRef ")
+    {
+        input = "set mod:foodIdentRef ";
+        expectedCompletions = {"mod:food", "mod:pizza", "mod:spaghetti", "pizza-module:hawaii"};
+        expectedContextLength = 0;
+    }
+
     REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength}));
 }