Move ls logic to Interpreter

Change-Id: I9d1e04573be5d7701e4e66e53aefb9dc1a1fd8ab
diff --git a/tests/ls_interpreter.cpp b/tests/ls_interpreter.cpp
new file mode 100644
index 0000000..6b02d80
--- /dev/null
+++ b/tests/ls_interpreter.cpp
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
+ * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
+ *
+ * Written by Václav Kubernát <kubervac@fit.cvut.cz>
+ *
+*/
+
+#include <experimental/iterator>
+#include "trompeloeil_doctest.hpp"
+#include "ast_commands.hpp"
+#include "interpreter.hpp"
+#include "datastoreaccess_mock.hpp"
+#include "parser.hpp"
+#include "pretty_printers.hpp"
+#include "static_schema.hpp"
+
+class MockSchema : public trompeloeil::mock_interface<Schema> {
+public:
+    IMPLEMENT_CONST_MOCK1(defaultValue);
+    IMPLEMENT_CONST_MOCK1(description);
+    IMPLEMENT_CONST_MOCK2(availableNodes);
+    IMPLEMENT_CONST_MOCK1(isConfig);
+    MAKE_CONST_MOCK1(leafType, yang::TypeInfo(const std::string&), override);
+    MAKE_CONST_MOCK2(leafType, yang::TypeInfo(const schemaPath_&, const ModuleNodePair&), override);
+    IMPLEMENT_CONST_MOCK1(leafTypeName);
+    IMPLEMENT_CONST_MOCK1(isModule);
+    IMPLEMENT_CONST_MOCK1(leafrefPath);
+    IMPLEMENT_CONST_MOCK3(listHasKey);
+    IMPLEMENT_CONST_MOCK1(leafIsKey);
+    IMPLEMENT_CONST_MOCK2(listKeys);
+    MAKE_CONST_MOCK1(nodeType, yang::NodeTypes(const std::string&), override);
+    MAKE_CONST_MOCK2(nodeType, yang::NodeTypes(const schemaPath_&, const ModuleNodePair&), override);
+    IMPLEMENT_CONST_MOCK1(status);
+};
+
+TEST_CASE("ls interpreter")
+{
+    auto schema = std::make_shared<MockSchema>();
+    Parser parser(schema);
+
+    boost::variant<dataPath_, schemaPath_, module_> expectedPath;
+    boost::optional<boost::variant<dataPath_, schemaPath_, module_>> lsArg{boost::none};
+    SECTION("cwd: /")
+    {
+        SECTION("arg: <none>")
+        {
+            expectedPath = schemaPath_{};
+        }
+
+        SECTION("arg: example:a")
+        {
+            lsArg = dataPath_{Scope::Relative, {{module_{"example"}, container_{"a"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
+        }
+
+        SECTION("arg: example:list")
+        {
+            lsArg = dataPath_{Scope::Relative, {{module_{"example"}, list_{"list"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
+        }
+
+        SECTION("arg: /example:a")
+        {
+            lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
+        }
+
+        SECTION("arg: /example:list")
+        {
+            lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
+        }
+
+        SECTION("arg example:*")
+        {
+            lsArg = module_{"example"};
+            expectedPath = module_{"example"};
+        }
+    }
+
+    SECTION("cwd: /example:a")
+    {
+        parser.changeNode({Scope::Relative, {{module_{"example"}, container_{"a"}}}});
+
+        SECTION("arg: <none>")
+        {
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
+        }
+
+        SECTION("arg: example:a2")
+        {
+            lsArg = dataPath_{Scope::Relative, {{container_{"a2"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}, {container_{"a2"}}}};
+        }
+
+        SECTION("arg: example:listInCont")
+        {
+            lsArg = dataPath_{Scope::Relative, {{list_{"listInCont"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}, {list_{"listInCont"}}}};
+        }
+
+        SECTION("arg: /example:a")
+        {
+            lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
+        }
+
+        SECTION("arg: /example:list")
+        {
+            lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
+        }
+    }
+    SECTION("cwd: /example:list")
+    {
+        parser.changeNode({Scope::Relative, {{module_{"example"}, list_{"list"}}}});
+
+        SECTION("arg: <none>")
+        {
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
+        }
+
+        SECTION("arg: example:contInList")
+        {
+            lsArg = dataPath_{Scope::Relative, {{container_{"contInList"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}, {container_{"contInList"}}}};
+        }
+
+        SECTION("arg: /example:a")
+        {
+            lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
+        }
+
+        SECTION("arg: /example:list")
+        {
+            lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
+            expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
+        }
+
+        SECTION("arg example:*")
+        {
+            lsArg = module_{"example"};
+            expectedPath = module_{"example"};
+        }
+    }
+    MockDatastoreAccess datastore;
+    REQUIRE_CALL(datastore, schema()).RETURN(schema);
+    ls_ ls;
+    ls.m_path = lsArg;
+    REQUIRE_CALL(*schema, availableNodes(expectedPath, Recursion::NonRecursive)).RETURN(std::set<std::string>{});
+    Interpreter(parser, datastore)(ls);
+}