blob: 47d28ed0ddeba0525fcf1fe91781d10770fe1464 [file] [log] [blame]
Václav Kubernát11afac72018-07-18 14:59:53 +02001/*
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 "ast_commands.hpp"
11#include "parser.hpp"
12#include "static_schema.hpp"
13
14TEST_CASE("ls")
15{
16 auto schema = std::make_shared<StaticSchema>();
17 schema->addModule("example");
18 schema->addModule("second");
19 schema->addContainer("", "example:a");
20 schema->addContainer("", "second:a");
21 schema->addContainer("", "example:b");
22 schema->addContainer("example:a", "example:a2");
23 schema->addContainer("example:b", "example:b2");
24 schema->addContainer("example:a/example:a2", "example:a3");
25 schema->addContainer("example:b/example:b2", "example:b3");
26 schema->addList("", "example:list", {"number"});
27 schema->addContainer("example:list", "example:contInList");
28 schema->addList("", "example:twoKeyList", {"number", "name"});
29 Parser parser(schema);
30 std::string input;
31 std::ostringstream errorStream;
32
33 SECTION("valid input")
34 {
35 ls_ expected;
36
37 SECTION("no arguments")
38 {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020039 SECTION("ls")
40 input = "ls";
41
42 SECTION("ls --recursive")
43 {
44 input = "ls --recursive";
45 expected.m_options.push_back(LsOption::Recursive);
46 }
Václav Kubernát11afac72018-07-18 14:59:53 +020047 }
48
49 SECTION("with path argument")
50 {
Václav Kubernát37171a12018-08-31 17:01:48 +020051 SECTION("ls example:a")
52 {
53 input = "ls example:a";
54 expected.m_path = path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}};
55 }
56
57 SECTION("ls /example:a")
58 {
59 SECTION("cwd: /") {}
60 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
61
62 input = "ls /example:a";
63 expected.m_path = path_{Scope::Absolute, {node_(module_{"example"}, container_{"a"})}};
64 }
65
66 SECTION("ls /")
67 {
68 SECTION("cwd: /") {}
69 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
70 input = "ls /";
71 expected.m_path = path_{Scope::Absolute, {}};
72 }
73
74 SECTION("ls example:a/a2")
75 {
76 input = "ls example:a/a2";
77 expected.m_path = path_{Scope::Relative, {node_(module_{"example"}, container_{"a"}),
78 node_(container_{"a2"})}};
79 }
80
81 SECTION("ls a2")
82 {
83 parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});
84 input = "ls a2";
85 expected.m_path = path_{Scope::Relative, {node_(container_{"a2"})}};
86 }
87
88 SECTION("ls /example:a/a2")
89 {
90 SECTION("cwd: /") {}
91 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
92 input = "ls /example:a/a2";
93 expected.m_path = path_{Scope::Absolute, {node_(module_{"example"}, container_{"a"}),
94 node_(container_{"a2"})}};
95 }
96
97 SECTION("ls example:a/example:a2")
98 {
99 input = "ls example:a/example:a2";
100 expected.m_path = path_{Scope::Relative, {node_(module_{"example"}, container_{"a"}),
101 node_(module_{"example"}, container_{"a2"})}};
102 }
103
104 SECTION("ls example:a2")
105 {
106 parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});
107 input = "ls example:a2";
108 expected.m_path = path_{Scope::Relative, {node_(module_{"example"}, container_{"a2"})}};
109 }
110
111 SECTION("ls /example:a/example:a2")
112 {
113 SECTION("cwd: /") {}
114 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
115
116 input = "ls /example:a/example:a2";
117 expected.m_path = path_{Scope::Absolute, {node_(module_{"example"}, container_{"a"}),
118 node_(module_{"example"}, container_{"a2"})}};
119 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200120
121 SECTION("ls --recursive /example:a")
122 {
123 SECTION("cwd: /") {}
124 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
125
126 input = "ls --recursive /example:a";
127 expected.m_options.push_back(LsOption::Recursive);
128 expected.m_path = path_{Scope::Absolute, {node_(module_{"example"}, container_{"a"})}};
129 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200130 }
131
132 command_ command = parser.parseCommand(input, errorStream);
133 REQUIRE(command.type() == typeid(ls_));
134 REQUIRE(boost::get<ls_>(command) == expected);
135 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200136
Václav Kubernát11afac72018-07-18 14:59:53 +0200137 SECTION("invalid input")
138 {
139 SECTION("invalid path")
140 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200141 SECTION("ls example:nonexistent")
142 input = "ls example:nonexistent";
143
144 SECTION("ls /example:nonexistent")
145 input = "ls /example:nonexistent";
146
147 SECTION("ls /bad:nonexistent")
148 input = "ls /bad:nonexistent";
149
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200150 SECTION("ls example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200151 input = "ls example:a/nonexistent";
152
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200153 SECTION("ls /example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200154 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200155 }
156
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200157 SECTION("whitespace before path")
158 {
159 SECTION("ls --recursive/")
160 input = "ls --recursive/";
161
162 SECTION("ls/")
163 input = "ls/";
164
165 SECTION("ls --recursive/example:a")
166 input = "ls --recursive/example:a";
167
168 SECTION("ls/example:a")
169 input = "ls/example:a";
170
171 SECTION("lssecond:a")
172 input = "lssecond:a";
173 }
174
Václav Kubernátb7a279d2018-10-04 15:27:50 +0200175 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException&);
Václav Kubernát11afac72018-07-18 14:59:53 +0200176 }
177}