refactor(tests): Use new factory methods

This commit is contained in:
Michael Carlberg 2016-12-15 19:05:26 +01:00
parent a227543da5
commit 6ce1c9805e
3 changed files with 74 additions and 118 deletions

View File

@ -21,8 +21,7 @@ unit_test("utils/math")
unit_test("utils/memory")
unit_test("utils/string")
unit_test("components/command_line")
unit_test("components/di")
unit_test("x11/color")
#unit_test("x11/color")
# XXX: Requires mocked xcb connection
#unit_test("x11/connection")

View File

@ -4,81 +4,83 @@
int main() {
using namespace polybar;
// clang-format off
const command_line::options opts{
command_line::option{"-f", "--flag", "Flag description"},
command_line::option{"-o", "--option", "Option description", "OPTION", {"foo", "bar", "baz"}},
};
// clang-format on
"has_short"_test = [&opts] {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("-f", ' '));
expect(cli.has("flag"));
expect(!cli.has("option"));
cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("-f -o foo", ' '));
expect(cli.has("flag"));
expect(cli.has("option"));
cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("-o baz", ' '));
expect(!cli.has("flag"));
expect(cli.has("option"));
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
};
"has_long"_test = [&opts] {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--flag", ' '));
expect(cli.has("flag"));
expect(!cli.has("option"));
"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 = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
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("-f -o foo", ' '));
expect(cli->has("flag"));
expect(cli->has("option"));
cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=foo --flag", ' '));
expect(cli.has("flag"));
expect(cli.has("option"));
cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=baz", ' '));
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"));
};
"compare"_test = [&opts] {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("-o baz", ' '));
expect(cli.compare("option", "baz"));
"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 = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=foo", ' '));
expect(cli.compare("option", "foo"));
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"));
};
"get"_test = [&opts] {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=baz", ' '));
expect("baz" == cli.get("option"));
"compare"_test = [&] {
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(string_util::split("-o baz", ' '));
expect(cli->compare("option", "baz"));
cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=foo", ' '));
expect("foo" == cli.get("option"));
cli = cliparser::make("cmd", get_opts());;
cli->process_input(string_util::split("--option=foo", ' '));
expect(cli->compare("option", "foo"));
};
"missing_value"_test = [&opts] {
"get"_test = [&] {
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(string_util::split("--option=baz", ' '));
expect("baz" == cli->get("option"));
cli = cliparser::make("cmd", get_opts());;
cli->process_input(string_util::split("--option=foo", ' '));
expect("foo" == cli->get("option"));
};
"missing_value"_test = [&] {
auto input1 = string_util::split("--option", ' ');
auto input2 = string_util::split("-o", ' ');
auto input3 = string_util::split("--option baz", ' ');
bool exception_thrown = false;
try {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input1);
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(input1);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
@ -87,8 +89,8 @@ int main() {
exception_thrown = false; // reset
try {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input2);
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(input2);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
@ -97,8 +99,8 @@ int main() {
exception_thrown = false; // reset
try {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input3);
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(input3);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
@ -106,14 +108,14 @@ int main() {
expect(exception_thrown);
};
"invalid_value"_test = [&opts] {
"invalid_value"_test = [&] {
auto input1 = string_util::split("--option=invalid", ' ');
auto input2 = string_util::split("-o invalid_value", ' ');
bool exception_thrown = false;
try {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input1);
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(input1);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
@ -122,8 +124,8 @@ int main() {
exception_thrown = false; // reset
try {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input2);
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(input2);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
@ -131,14 +133,14 @@ int main() {
expect(exception_thrown);
};
"unrecognized"_test = [&opts] {
"unrecognized"_test = [&] {
auto input1 = string_util::split("-x", ' ');
auto input2 = string_util::split("--unrecognized", ' ');
bool exception_thrown = false;
try {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input1);
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(input1);
} catch (const command_line::argument_error&) {
exception_thrown = true;
} catch (...) {
@ -147,8 +149,8 @@ int main() {
exception_thrown = false; // reset
try {
auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input2);
auto cli = cliparser::make("cmd", get_opts());;
cli->process_input(input2);
} catch (const command_line::argument_error&) {
exception_thrown = true;
} catch (...) {

View File

@ -1,45 +0,0 @@
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include "components/logger.cpp"
#include "utils/inotify.cpp"
#include "utils/string.cpp"
#define CONFIGURE_ARGS(T, V, Args) configure_##T<decltype(V)>(Args).create<decltype(V)>()
#define CONFIGURE(T, V) configure_##T<decltype(V)>().create<decltype(V)>()
int main() {
using namespace polybar;
"singleton"_test = [] {
// clang-format off
logger& instance1{CONFIGURE(logger, instance1)};
const logger& instance2{CONFIGURE(logger, instance2)};
std::shared_ptr<logger> instance3{CONFIGURE(logger, instance3)};
boost::shared_ptr<logger> instance4{CONFIGURE(logger, instance4)};
// clang-format on
string mem_addr1{string_util::from_stream(stringstream() << &instance1)};
string mem_addr2{string_util::from_stream(stringstream() << &instance2)};
string mem_addr3{string_util::from_stream(stringstream() << instance3.get())};
expect(mem_addr1 == mem_addr2);
expect(mem_addr2 == mem_addr3);
expect(instance3.get() == instance4.get());
};
"unique"_test = [] {
inotify_util::watch_t instance1{inotify_util::make_watch("A")};
inotify_util::watch_t instance2{inotify_util::make_watch("B")};
inotify_util::watch_t instance3{inotify_util::make_watch("B")};
inotify_util::watch_t instance4{inotify_util::make_watch("B")};
expect(instance1.get() != instance2.get());
expect(instance2.get() != instance3.get());
expect(instance3.get() != instance4.get());
};
"instance"_test = [] {
// TODO
};
}