blob: 376e74c4ab5e02bd5975332647dad327ac39169a [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;
28
Václav Kubernátb96eef72018-05-04 19:10:22 +020029 SECTION("valid input")
Václav Kubernátd6662962018-03-22 17:41:33 +010030 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020031 cd_ expected;
32
33 SECTION("container")
Václav Kubernátd6662962018-03-22 17:41:33 +010034 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020035 SECTION("a")
36 {
37 input = "cd a";
38 expected.m_path.m_nodes.push_back(container_("a"));
39 }
40
41 SECTION("b")
42 {
43 input = "cd b";
44 expected.m_path.m_nodes.push_back(container_("b"));
45 }
46
47 SECTION("a/a2")
48 {
49 input = "cd a/a2";
50 expected.m_path.m_nodes.push_back(container_("a"));
51 expected.m_path.m_nodes.push_back(container_("a2"));
52 }
53
54 SECTION("b/b2")
55 {
56 input = "cd b/b2";
57 expected.m_path.m_nodes.push_back(container_("b"));
58 expected.m_path.m_nodes.push_back(container_("b2"));
59 }
60
Václav Kubernátd6662962018-03-22 17:41:33 +010061 }
62
Václav Kubernátb96eef72018-05-04 19:10:22 +020063 SECTION("list elements")
Václav Kubernátd6662962018-03-22 17:41:33 +010064 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020065 SECTION("list[number=1]")
66 {
67 input = "cd list[number=1]";
68 auto keys = std::map<std::string, std::string>{
69 {"number", "1"}
70 };
71 expected.m_path.m_nodes.push_back(listElement_("list", keys));
72 }
73
74 SECTION("list[number=1]/contInList")
75 {
76 input = "cd list[number=1]/contInList";
77 auto keys = std::map<std::string, std::string>{
78 {"number", "1"}};
79 expected.m_path.m_nodes.push_back(listElement_("list", keys));
80 expected.m_path.m_nodes.push_back(container_("contInList"));
81 }
82
83 SECTION("twoKeyList[number=4 name=abcd]")
84 {
85 input = "cd twoKeyList[number=4 name=abcd]";
86 auto keys = std::map<std::string, std::string>{
87 {"number", "4"},
88 {"name", "abcd"}};
89 expected.m_path.m_nodes.push_back(listElement_("twoKeyList", keys));
90 }
91
Václav Kubernátd6662962018-03-22 17:41:33 +010092 }
Václav Kubernátd6662962018-03-22 17:41:33 +010093 cd_ command = parser.parseCommand(input);
94 REQUIRE(command == expected);
95 }
Václav Kubernátb96eef72018-05-04 19:10:22 +020096 SECTION("invalid input")
Václav Kubernátd6662962018-03-22 17:41:33 +010097 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020098 SECTION("invalid identifiers")
Václav Kubernátd6662962018-03-22 17:41:33 +010099 {
Václav Kubernátb96eef72018-05-04 19:10:22 +0200100 SECTION("nonexistent")
101 {
102 input = "cd nonexistent";
103 REQUIRE_THROWS(parser.parseCommand(input));
104 }
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";
109 REQUIRE_THROWS(parser.parseCommand(input));
110 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100111 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200112 SECTION("invalid list key identifiers")
113 {
114 SECTION("twoKeyList[invalidKey=4]")
115 {
116 input = "cd twoKeyList[invalidKey=4]";
117 REQUIRE_THROWS(parser.parseCommand(input));
118 }
119
120 SECTION("twoKeyList[number=4 number=5]")
121 {
122 input = "cd twoKeyList[number=4 number=5]";
123 REQUIRE_THROWS(parser.parseCommand(input));
124 }
125
126 SECTION("twoKeyList[number=4 name=lol number=7]")
127 {
128 input = "cd twoKeyList[number=4 name=lol number=7]";
129 REQUIRE_THROWS(parser.parseCommand(input));
130 }
131
132 SECTION("twoKeyList[number=4]")
133 {
134 input = "cd twoKeyList[number=4]";
135 REQUIRE_THROWS(parser.parseCommand(input));
136 }
137 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100138 }
139}