blob: 4a60762ef59f0cc45b64680300c191d0386b1656 [file] [log] [blame]
/*
* 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);
MAKE_CONST_MOCK3(listHasKey, bool(const schemaPath_& location, const ModuleNodePair& node, const std::string& key), override);
MAKE_CONST_MOCK2(listHasKey, bool(const schemaPath_& listPath, const std::string& key), override);
IMPLEMENT_CONST_MOCK1(leafIsKey);
MAKE_CONST_MOCK2(listKeys, const std::set<std::string>(const schemaPath_& location, const ModuleNodePair& node), override);
MAKE_CONST_MOCK1(listKeys, const std::set<std::string>(const schemaPath_& listPath), override);
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<ModuleNodePair>{});
Interpreter(parser, datastore)(ls);
}