blob: 9356c6960d8ceb5e053bdd3bd337e71ccad88a98 [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 {
39 input = "ls";
40 }
41
42 SECTION("with path argument")
43 {
Václav Kubernát37171a12018-08-31 17:01:48 +020044 SECTION("ls example:a")
45 {
46 input = "ls example:a";
47 expected.m_path = path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}};
48 }
49
50 SECTION("ls /example:a")
51 {
52 SECTION("cwd: /") {}
53 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
54
55 input = "ls /example:a";
56 expected.m_path = path_{Scope::Absolute, {node_(module_{"example"}, container_{"a"})}};
57 }
58
59 SECTION("ls /")
60 {
61 SECTION("cwd: /") {}
62 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
63 input = "ls /";
64 expected.m_path = path_{Scope::Absolute, {}};
65 }
66
67 SECTION("ls example:a/a2")
68 {
69 input = "ls example:a/a2";
70 expected.m_path = path_{Scope::Relative, {node_(module_{"example"}, container_{"a"}),
71 node_(container_{"a2"})}};
72 }
73
74 SECTION("ls a2")
75 {
76 parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});
77 input = "ls a2";
78 expected.m_path = path_{Scope::Relative, {node_(container_{"a2"})}};
79 }
80
81 SECTION("ls /example:a/a2")
82 {
83 SECTION("cwd: /") {}
84 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
85 input = "ls /example:a/a2";
86 expected.m_path = path_{Scope::Absolute, {node_(module_{"example"}, container_{"a"}),
87 node_(container_{"a2"})}};
88 }
89
90 SECTION("ls example:a/example:a2")
91 {
92 input = "ls example:a/example:a2";
93 expected.m_path = path_{Scope::Relative, {node_(module_{"example"}, container_{"a"}),
94 node_(module_{"example"}, container_{"a2"})}};
95 }
96
97 SECTION("ls example:a2")
98 {
99 parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});
100 input = "ls example:a2";
101 expected.m_path = path_{Scope::Relative, {node_(module_{"example"}, container_{"a2"})}};
102 }
103
104 SECTION("ls /example:a/example:a2")
105 {
106 SECTION("cwd: /") {}
107 SECTION("cwd: /example:a") {parser.changeNode(path_{Scope::Relative, {node_(module_{"example"}, container_{"a"})}});}
108
109 input = "ls /example:a/example:a2";
110 expected.m_path = path_{Scope::Absolute, {node_(module_{"example"}, container_{"a"}),
111 node_(module_{"example"}, container_{"a2"})}};
112 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200113 }
114
115 command_ command = parser.parseCommand(input, errorStream);
116 REQUIRE(command.type() == typeid(ls_));
117 REQUIRE(boost::get<ls_>(command) == expected);
118 }
119 SECTION("invalid input")
120 {
121 SECTION("invalid path")
122 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200123 SECTION("ls example:nonexistent")
124 input = "ls example:nonexistent";
125
126 SECTION("ls /example:nonexistent")
127 input = "ls /example:nonexistent";
128
129 SECTION("ls /bad:nonexistent")
130 input = "ls /bad:nonexistent";
131
132 SECTION( "ls example:a/nonexistent")
133 input = "ls example:a/nonexistent";
134
135 SECTION( "ls /example:a/nonexistent")
136 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200137 }
138
139 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
140 }
141}