Unify childNodes between YANG and static Schema

childNodes in YangSchema doesn't include module node prefixes when they
were the same as the top-level module, but StaticSchema always included
them. This changes StaticSchema to work the same as YangSchema does.
This change is good, because the tests for completion are now more
accurate.

Change-Id: I8378c22775d0a3bc66fe5e574dd0a0abfd0eeb15
diff --git a/src/static_schema.cpp b/src/static_schema.cpp
index 082d03b..d554cb9 100644
--- a/src/static_schema.cpp
+++ b/src/static_schema.cpp
@@ -162,15 +162,32 @@
         return res;
     }
 
-    std::string locationString =
-        path.type() == typeid(schemaPath_) ?
-        pathToSchemaString(boost::get<schemaPath_>(path), Prefixes::Always) :
-        pathToSchemaString(boost::get<dataPath_>(path), Prefixes::Always);
+    auto getTopLevelModule = [] (const auto& path) -> boost::optional<std::string> {
+        if (!path.m_nodes.empty()) {
+            return path.m_nodes.begin()->m_prefix.flat_map([] (const auto& module) {return boost::optional<std::string>(module.m_name);});
+        }
+
+        return boost::none;
+    };
+
+    std::string locationString;
+    boost::optional<std::string> topLevelModule;
+    if (path.type() == typeid(schemaPath_)) {
+        locationString = pathToSchemaString(boost::get<schemaPath_>(path), Prefixes::Always);
+        topLevelModule = getTopLevelModule(boost::get<schemaPath_>(path));
+    } else {
+        locationString = pathToSchemaString(boost::get<dataPath_>(path), Prefixes::Always);
+        topLevelModule = getTopLevelModule(boost::get<dataPath_>(path));
+    }
 
     auto childrenRef = children(locationString);
 
-    std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [](const auto& it) {
-        return splitModuleNode(it.first);
+    std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [path, topLevelModule](const auto& it) {
+        auto res = splitModuleNode(it.first);
+        if (topLevelModule == res.first) {
+            res.first = boost::none;
+        }
+        return res;
     });
     return res;
 }
diff --git a/tests/path_completion.cpp b/tests/path_completion.cpp
index c00a26c..bfc83d2 100644
--- a/tests/path_completion.cpp
+++ b/tests/path_completion.cpp
@@ -84,11 +84,11 @@
             expectedContextLength = 11;
         }
 
-        SECTION("ls example:ano/example:a")
+        SECTION("ls example:ano/a")
         {
-            input = "ls example:ano/example:a";
-            expectedCompletions = {"example:a2/"};
-            expectedContextLength = 9;
+            input = "ls example:ano/a";
+            expectedCompletions = {"a2/"};
+            expectedContextLength = 1;
         }
 
         SECTION("ls x")
@@ -136,21 +136,28 @@
         SECTION("ls /example:list/")
         {
             input = "ls /example:list/";
-            expectedCompletions = {"example:contInList/", "example:number "};
+            expectedCompletions = {"contInList/", "number "};
             expectedContextLength = 0;
         }
 
         SECTION("ls /example:list[number=3]/")
         {
             input = "ls /example:list[number=3]/";
-            expectedCompletions = {"example:contInList/", "example:number "};
+            expectedCompletions = {"contInList/", "number "};
             expectedContextLength = 0;
         }
 
         SECTION("ls /example:list[number=3]/c")
         {
-            input = "ls /example:list[number=3]/e";
-            expectedCompletions = {"example:contInList/", "example:number "};
+            input = "ls /example:list[number=3]/c";
+            expectedCompletions = {"contInList/"};
+            expectedContextLength = 1;
+        }
+
+        SECTION("ls /example:list[number=3]/n")
+        {
+            input = "ls /example:list[number=3]/n";
+            expectedCompletions = {"number "};
             expectedContextLength = 1;
         }
 
@@ -161,9 +168,9 @@
             expectedContextLength = 1;
         }
 
-        SECTION("ls /example:list/example:contInList/")
+        SECTION("ls /example:list/contInList/")
         {
-            input = "ls /example:list/example:contInList/";
+            input = "ls /example:list/contInList/";
             expectedCompletions = {};
             expectedContextLength = 0;
         }