2016-11-20 17:04:31 -05:00
|
|
|
#include "components/command_line.cpp"
|
|
|
|
#include "utils/string.cpp"
|
2016-10-24 19:47:00 -04:00
|
|
|
|
|
|
|
int main() {
|
2016-11-19 00:22:44 -05:00
|
|
|
using namespace polybar;
|
2016-10-24 19:47:00 -04:00
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
const auto get_opts = []() -> const command_line::options {
|
|
|
|
// clang-format off
|
|
|
|
return command_line::options{
|
|
|
|
command_line::option{"-f", "--flag", "Flag description"},
|
|
|
|
command_line::option{"-o", "--option", "Option description", "OPTION", {"foo", "bar", "baz"}},
|
|
|
|
};
|
|
|
|
// clang-format on
|
2016-10-24 19:47:00 -04:00
|
|
|
};
|
2016-12-15 13:05:26 -05:00
|
|
|
|
|
|
|
"has_short"_test = [&] {
|
|
|
|
auto cli = cliparser::make("cmd", get_opts());
|
|
|
|
cli->process_input(string_util::split("-f", ' '));
|
|
|
|
expect(cli->has("flag"));
|
|
|
|
expect(!cli->has("option"));
|
|
|
|
|
|
|
|
cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("-f -o foo", ' '));
|
|
|
|
expect(cli->has("flag"));
|
|
|
|
expect(cli->has("option"));
|
|
|
|
|
|
|
|
cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("-o baz", ' '));
|
|
|
|
expect(!cli->has("flag"));
|
|
|
|
expect(cli->has("option"));
|
2016-10-24 19:47:00 -04:00
|
|
|
};
|
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
"has_long"_test = [&] {
|
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("--flag", ' '));
|
|
|
|
expect(cli->has("flag"));
|
|
|
|
expect(!cli->has("option"));
|
|
|
|
|
|
|
|
cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("--flag --option=foo", ' '));
|
|
|
|
expect(cli->has("flag"));
|
|
|
|
expect(cli->has("option"));
|
|
|
|
|
|
|
|
cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("--option=foo --flag", ' '));
|
|
|
|
expect(cli->has("flag"));
|
|
|
|
expect(cli->has("option"));
|
|
|
|
|
|
|
|
cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("--option=baz", ' '));
|
|
|
|
expect(!cli->has("flag"));
|
|
|
|
expect(cli->has("option"));
|
2016-10-24 19:47:00 -04:00
|
|
|
};
|
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
"compare"_test = [&] {
|
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("-o baz", ' '));
|
|
|
|
expect(cli->compare("option", "baz"));
|
2016-10-24 19:47:00 -04:00
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("--option=foo", ' '));
|
|
|
|
expect(cli->compare("option", "foo"));
|
2016-10-24 19:47:00 -04:00
|
|
|
};
|
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
"get"_test = [&] {
|
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("--option=baz", ' '));
|
|
|
|
expect("baz" == cli->get("option"));
|
2016-10-24 19:47:00 -04:00
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(string_util::split("--option=foo", ' '));
|
|
|
|
expect("foo" == cli->get("option"));
|
2016-10-24 19:47:00 -04:00
|
|
|
};
|
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
"missing_value"_test = [&] {
|
2016-10-24 19:47:00 -04:00
|
|
|
auto input1 = string_util::split("--option", ' ');
|
|
|
|
auto input2 = string_util::split("-o", ' ');
|
|
|
|
auto input3 = string_util::split("--option baz", ' ');
|
|
|
|
|
|
|
|
bool exception_thrown = false;
|
|
|
|
try {
|
2016-12-15 13:05:26 -05:00
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(input1);
|
2016-10-24 19:47:00 -04:00
|
|
|
} catch (const command_line::value_error&) {
|
|
|
|
exception_thrown = true;
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
expect(exception_thrown);
|
|
|
|
|
|
|
|
exception_thrown = false; // reset
|
|
|
|
try {
|
2016-12-15 13:05:26 -05:00
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(input2);
|
2016-10-24 19:47:00 -04:00
|
|
|
} catch (const command_line::value_error&) {
|
|
|
|
exception_thrown = true;
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
expect(exception_thrown);
|
|
|
|
|
|
|
|
exception_thrown = false; // reset
|
|
|
|
try {
|
2016-12-15 13:05:26 -05:00
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(input3);
|
2016-10-24 19:47:00 -04:00
|
|
|
} catch (const command_line::value_error&) {
|
|
|
|
exception_thrown = true;
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
expect(exception_thrown);
|
|
|
|
};
|
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
"invalid_value"_test = [&] {
|
2016-10-24 19:47:00 -04:00
|
|
|
auto input1 = string_util::split("--option=invalid", ' ');
|
|
|
|
auto input2 = string_util::split("-o invalid_value", ' ');
|
|
|
|
|
|
|
|
bool exception_thrown = false;
|
|
|
|
try {
|
2016-12-15 13:05:26 -05:00
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(input1);
|
2016-10-24 19:47:00 -04:00
|
|
|
} catch (const command_line::value_error&) {
|
|
|
|
exception_thrown = true;
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
expect(exception_thrown);
|
|
|
|
|
|
|
|
exception_thrown = false; // reset
|
|
|
|
try {
|
2016-12-15 13:05:26 -05:00
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(input2);
|
2016-10-24 19:47:00 -04:00
|
|
|
} catch (const command_line::value_error&) {
|
|
|
|
exception_thrown = true;
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
expect(exception_thrown);
|
|
|
|
};
|
|
|
|
|
2016-12-15 13:05:26 -05:00
|
|
|
"unrecognized"_test = [&] {
|
2016-10-24 19:47:00 -04:00
|
|
|
auto input1 = string_util::split("-x", ' ');
|
|
|
|
auto input2 = string_util::split("--unrecognized", ' ');
|
|
|
|
|
|
|
|
bool exception_thrown = false;
|
|
|
|
try {
|
2016-12-15 13:05:26 -05:00
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(input1);
|
2016-10-24 19:47:00 -04:00
|
|
|
} catch (const command_line::argument_error&) {
|
|
|
|
exception_thrown = true;
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
expect(exception_thrown);
|
|
|
|
|
|
|
|
exception_thrown = false; // reset
|
|
|
|
try {
|
2016-12-15 13:05:26 -05:00
|
|
|
auto cli = cliparser::make("cmd", get_opts());;
|
|
|
|
cli->process_input(input2);
|
2016-10-24 19:47:00 -04:00
|
|
|
} catch (const command_line::argument_error&) {
|
|
|
|
exception_thrown = true;
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
expect(exception_thrown);
|
|
|
|
};
|
|
|
|
}
|