blob: 9c73c6186bec6f5acb4ce698566b23ad1b415565 [file] [log] [blame]
Václav Kubernátd6662962018-03-22 17:41:33 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
9#include "trompeloeil_catch.h"
10#include "CParser.hpp"
11#include "CTree.hpp"
12#include "ast.hpp"
13
14TEST_CASE("cd")
15{
16 CTree tree;
17 tree.addContainer("", "a");
18 tree.addContainer("", "b");
19 tree.addContainer("a", "a2");
20 tree.addContainer("b", "b2");
21 tree.addContainer("a/a2", "a3");
22 tree.addContainer("b/b2", "b3");
Václav Kubernátb96eef72018-05-04 19:10:22 +020023 tree.addList("", "list", {"number"});
24 tree.addContainer("list", "contInList");
25 tree.addList("", "twoKeyList", {"number", "name"});
Václav Kubernátd6662962018-03-22 17:41:33 +010026 CParser parser(tree);
Václav Kubernátd6662962018-03-22 17:41:33 +010027 std::string input;
Václav Kubernát2315c732018-05-16 20:25:55 +020028 std::ostringstream errorStream;
Václav Kubernátd6662962018-03-22 17:41:33 +010029
Václav Kubernátb96eef72018-05-04 19:10:22 +020030 SECTION("valid input")
Václav Kubernátd6662962018-03-22 17:41:33 +010031 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020032 cd_ expected;
33
34 SECTION("container")
Václav Kubernátd6662962018-03-22 17:41:33 +010035 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020036 SECTION("a")
37 {
38 input = "cd a";
39 expected.m_path.m_nodes.push_back(container_("a"));
40 }
41
42 SECTION("b")
43 {
44 input = "cd b";
45 expected.m_path.m_nodes.push_back(container_("b"));
46 }
47
48 SECTION("a/a2")
49 {
50 input = "cd a/a2";
51 expected.m_path.m_nodes.push_back(container_("a"));
52 expected.m_path.m_nodes.push_back(container_("a2"));
53 }
54
55 SECTION("b/b2")
56 {
57 input = "cd b/b2";
58 expected.m_path.m_nodes.push_back(container_("b"));
59 expected.m_path.m_nodes.push_back(container_("b2"));
60 }
61
Václav Kubernátd6662962018-03-22 17:41:33 +010062 }
63
Václav Kubernátb96eef72018-05-04 19:10:22 +020064 SECTION("list elements")
Václav Kubernátd6662962018-03-22 17:41:33 +010065 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020066 SECTION("list[number=1]")
67 {
68 input = "cd list[number=1]";
69 auto keys = std::map<std::string, std::string>{
70 {"number", "1"}
71 };
72 expected.m_path.m_nodes.push_back(listElement_("list", keys));
73 }
74
75 SECTION("list[number=1]/contInList")
76 {
77 input = "cd list[number=1]/contInList";
78 auto keys = std::map<std::string, std::string>{
79 {"number", "1"}};
80 expected.m_path.m_nodes.push_back(listElement_("list", keys));
81 expected.m_path.m_nodes.push_back(container_("contInList"));
82 }
83
84 SECTION("twoKeyList[number=4 name=abcd]")
85 {
86 input = "cd twoKeyList[number=4 name=abcd]";
87 auto keys = std::map<std::string, std::string>{
88 {"number", "4"},
89 {"name", "abcd"}};
90 expected.m_path.m_nodes.push_back(listElement_("twoKeyList", keys));
91 }
92
Václav Kubernátd6662962018-03-22 17:41:33 +010093 }
Václav Kubernát2315c732018-05-16 20:25:55 +020094 cd_ command = parser.parseCommand(input, errorStream);
Václav Kubernátd6662962018-03-22 17:41:33 +010095 REQUIRE(command == expected);
96 }
Václav Kubernátb96eef72018-05-04 19:10:22 +020097 SECTION("invalid input")
Václav Kubernátd6662962018-03-22 17:41:33 +010098 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020099 SECTION("invalid identifiers")
Václav Kubernátd6662962018-03-22 17:41:33 +0100100 {
Václav Kubernátb96eef72018-05-04 19:10:22 +0200101 SECTION("nonexistent")
102 {
103 input = "cd nonexistent";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200104 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100105
Václav Kubernátb96eef72018-05-04 19:10:22 +0200106 SECTION("nonexistent/lol")
107 {
108 input = "cd nonexistent/lol";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200109 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100110 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200111 SECTION("invalid list key identifiers")
112 {
113 SECTION("twoKeyList[invalidKey=4]")
114 {
115 input = "cd twoKeyList[invalidKey=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200116 }
117
118 SECTION("twoKeyList[number=4 number=5]")
119 {
120 input = "cd twoKeyList[number=4 number=5]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200121 }
122
123 SECTION("twoKeyList[number=4 name=lol number=7]")
124 {
125 input = "cd twoKeyList[number=4 name=lol number=7]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200126 }
127
128 SECTION("twoKeyList[number=4]")
129 {
130 input = "cd twoKeyList[number=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200131 }
132 }
Václav Kubernát2315c732018-05-16 20:25:55 +0200133 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
Václav Kubernátd6662962018-03-22 17:41:33 +0100134 }
135}