Fix `prepare`

The parsing algorithm was broken and prepare couldn't recognize RPC
nodes. The mistake probably happened when I added Action support, but
with no tests. Tests are now included.

Change-Id: I1b4eb2eb76a30c4495e2aacbb6848f449eba3abb
diff --git a/tests/prepare.cpp b/tests/prepare.cpp
new file mode 100644
index 0000000..139ef4b
--- /dev/null
+++ b/tests/prepare.cpp
@@ -0,0 +1,45 @@
+
+/*
+ * 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 "trompeloeil_doctest.hpp"
+#include "parser.hpp"
+#include "pretty_printers.hpp"
+#include "static_schema.hpp"
+
+TEST_CASE("prepare command")
+{
+    auto schema = std::make_shared<StaticSchema>();
+    schema->addModule("example");
+    schema->addRpc("/", "example:fire");
+    schema->addList("/", "example:port", {"name"});
+    schema->addLeaf("/example:port", "example:name", yang::String{});
+    schema->addAction("/example:port", "example:shutdown");
+    Parser parser(schema);
+    std::string input;
+    std::ostringstream errorStream;
+    prepare_ expected;
+    expected.m_path.m_scope = Scope::Relative;
+    SECTION("rpc")
+    {
+        input = "prepare example:fire";
+        expected.m_path.m_nodes.push_back({module_{"example"}, rpcNode_{"fire"}});
+    }
+
+    SECTION("action")
+    {
+        input = "prepare example:port[name='eth0']/shutdown";
+        expected.m_path.m_nodes.push_back({module_{"example"}, listElement_{"port", {{"name", std::string{"eth0"}}}}});
+        expected.m_path.m_nodes.push_back({actionNode_{"shutdown"}});
+    }
+
+    command_ command = parser.parseCommand(input, errorStream);
+    REQUIRE(command.type() == typeid(prepare_));
+    auto lol = boost::get<prepare_>(command);
+    REQUIRE(boost::get<prepare_>(command) == expected);
+}
diff --git a/tests/pretty_printers.hpp b/tests/pretty_printers.hpp
index ab6b917..b459a11 100644
--- a/tests/pretty_printers.hpp
+++ b/tests/pretty_printers.hpp
@@ -199,3 +199,8 @@
 {
     return s << "Command SET {path: " << pathToSchemaString(cmd.m_path, Prefixes::Always) << ", type " << boost::core::demangle(cmd.m_data.type().name()) << ", data: " << leafDataToString(cmd.m_data) << "}";
 }
+
+std::ostream& operator<<(std::ostream& s, const prepare_ cmd)
+{
+    return s << "Command PREPARE {path: " << pathToDataString(cmd.m_path, Prefixes::Always) << "}";
+}