forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subcommand_test.cc
190 lines (167 loc) · 6.59 KB
/
subcommand_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/util/subcommand.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "absl/strings/match.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace verible {
namespace {
TEST(SubcommandRegistryTest, NoNewCommands) {
SubcommandRegistry registry;
const std::string command_list = registry.ListCommands();
EXPECT_TRUE(absl::StrContains(command_list, "help"));
// Make sure the internal 'error' command is not shown in help.
EXPECT_FALSE(absl::StrContains(command_list, "error"));
}
TEST(SubcommandRegistryTest, HelpNoArgsTest) {
SubcommandRegistry registry;
// Test built-in help command.
const SubcommandEntry& help(registry.GetSubcommandEntry("help"));
std::istringstream ins;
std::ostringstream outs, errs;
std::vector<absl::string_view> args;
const SubcommandArgsRange range(args.begin(), args.end());
const auto status = help.main(range, ins, outs, errs);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
EXPECT_TRUE(outs.str().empty()) << outs.str();
EXPECT_TRUE(absl::StrContains(errs.str(), "available commands:"));
}
TEST(SubcommandRegistryTest, HelpHelpCommand) {
SubcommandRegistry registry;
// Test built-in help command.
const SubcommandEntry& help(registry.GetSubcommandEntry("help"));
std::istringstream ins;
std::ostringstream outs, errs;
std::vector<absl::string_view> args{"help"};
const SubcommandArgsRange range(args.begin(), args.end());
const auto status = help.main(range, ins, outs, errs);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
EXPECT_TRUE(outs.str().empty()) << outs.str();
EXPECT_TRUE(absl::StrContains(errs.str(), "Prints command help."));
}
static const SubcommandEntry fizz_func{
[](const SubcommandArgsRange, std::istream&, std::ostream& outs,
std::ostream&) {
outs << 42;
return absl::OkStatus();
},
"fizz does almost nothing",
true, // public
};
TEST(SubcommandRegistryTest, RegisterCommandPublicOk) {
SubcommandRegistry registry;
{
const auto status = registry.RegisterCommand("fizz", fizz_func);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
EXPECT_TRUE(absl::StrContains(registry.ListCommands(), "fizz"));
}
std::vector<absl::string_view> args;
const SubcommandArgsRange range(args.begin(), args.end());
const SubcommandEntry& fizz_entry(registry.GetSubcommandEntry("fizz"));
std::istringstream ins;
std::ostringstream outs, errs;
const auto status = fizz_entry.main(range, ins, outs, errs);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
EXPECT_EQ(outs.str(), "42");
}
TEST(SubcommandRegistryTest, ShowRegisteredCommandsOnWrongCommandRequest) {
SubcommandRegistry registry;
{
const auto status = registry.RegisterCommand("fizz", fizz_func);
EXPECT_TRUE(absl::StrContains(registry.ListCommands(), "fizz"));
}
const SubcommandEntry& cmd(registry.GetSubcommandEntry("wrong_command"));
std::vector<absl::string_view> args{"foo", "bar"};
const SubcommandArgsRange range(args.begin(), args.end());
std::istringstream ins;
std::ostringstream outs, errs;
const auto status = cmd.main(range, ins, outs, errs);
EXPECT_FALSE(status.ok()) << status.message();
EXPECT_TRUE(absl::StrContains(errs.str(), "available commands"));
EXPECT_TRUE(absl::StrContains(errs.str(), "fizz"));
}
static const SubcommandEntry buzz_func{
[](const SubcommandArgsRange, std::istream&, std::ostream& outs,
std::ostream&) {
outs << 99;
return absl::OkStatus();
},
"buzz does almost nothing",
false, // hidden
};
TEST(SubcommandRegistryTest, RegisterCommandHiddenOk) {
SubcommandRegistry registry;
{
const auto status = registry.RegisterCommand("buzz", buzz_func);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
EXPECT_FALSE(absl::StrContains(registry.ListCommands(), "buzz"));
}
std::vector<absl::string_view> args;
const SubcommandArgsRange range(args.begin(), args.end());
const SubcommandEntry& buzz_entry(registry.GetSubcommandEntry("buzz"));
std::istringstream ins;
std::ostringstream outs, errs;
const auto status = buzz_entry.main(range, ins, outs, errs);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
EXPECT_EQ(outs.str(), "99");
}
TEST(SubcommandRegistryTest, RegisterCommandErrorAlreadyRegistered) {
SubcommandRegistry registry;
// "help" is already a registered command
const auto status = registry.RegisterCommand("help", fizz_func);
EXPECT_FALSE(status.ok());
EXPECT_TRUE(
absl::StrContains(status.message(), "has already been registered"));
}
TEST(SubcommandRegistryTest, RegisterCommandMultipleCommands) {
SubcommandRegistry registry;
{
{
const auto status = registry.RegisterCommand("buzz", buzz_func);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
}
{
const auto status = registry.RegisterCommand("fizz", fizz_func);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
}
const std::string command_list = registry.ListCommands();
EXPECT_TRUE(absl::StrContains(command_list, "fizz"));
EXPECT_FALSE(absl::StrContains(command_list, "buzz"));
}
// Run each subcommand once.
std::vector<absl::string_view> args;
const SubcommandArgsRange range(args.begin(), args.end());
{
const SubcommandEntry& fizz_entry(registry.GetSubcommandEntry("fizz"));
std::istringstream ins;
std::ostringstream outs, errs;
const auto status = fizz_entry.main(range, ins, outs, errs);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
EXPECT_EQ(outs.str(), "42");
}
{
const SubcommandEntry& buzz_entry(registry.GetSubcommandEntry("buzz"));
std::istringstream ins;
std::ostringstream outs, errs;
const auto status = buzz_entry.main(range, ins, outs, errs);
EXPECT_TRUE(status.ok()) << "Unexpected error:\n" << status.message();
EXPECT_EQ(outs.str(), "99");
}
}
} // namespace
} // namespace verible