From 6ce1c9805e27c59534d8e4826ce9d8780c0b6d3c Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Thu, 15 Dec 2016 19:05:26 +0100 Subject: [PATCH] refactor(tests): Use new factory methods --- tests/CMakeLists.txt | 3 +- tests/unit_tests/components/command_line.cpp | 144 ++++++++++--------- tests/unit_tests/components/di.cpp | 45 ------ 3 files changed, 74 insertions(+), 118 deletions(-) delete mode 100644 tests/unit_tests/components/di.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2032be63..78c9ac06 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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") diff --git a/tests/unit_tests/components/command_line.cpp b/tests/unit_tests/components/command_line.cpp index abf42fad..b3fc7a63 100644 --- a/tests/unit_tests/components/command_line.cpp +++ b/tests/unit_tests/components/command_line.cpp @@ -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("cmd", opts).create(); - cli.process_input(string_util::split("-f", ' ')); - expect(cli.has("flag")); - expect(!cli.has("option")); - - cli = configure_cliparser("cmd", opts).create(); - cli.process_input(string_util::split("-f -o foo", ' ')); - expect(cli.has("flag")); - expect(cli.has("option")); - - cli = configure_cliparser("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - cli.process_input(string_util::split("--option=foo --flag", ' ')); - expect(cli.has("flag")); - expect(cli.has("option")); - - cli = configure_cliparser("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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("cmd", opts).create(); - 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 (...) { diff --git a/tests/unit_tests/components/di.cpp b/tests/unit_tests/components/di.cpp deleted file mode 100644 index 8b843ee2..00000000 --- a/tests/unit_tests/components/di.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include - -#include "components/logger.cpp" -#include "utils/inotify.cpp" -#include "utils/string.cpp" - -#define CONFIGURE_ARGS(T, V, Args) configure_##T(Args).create() -#define CONFIGURE(T, V) configure_##T().create() - -int main() { - using namespace polybar; - - "singleton"_test = [] { - // clang-format off - logger& instance1{CONFIGURE(logger, instance1)}; - const logger& instance2{CONFIGURE(logger, instance2)}; - std::shared_ptr instance3{CONFIGURE(logger, instance3)}; - boost::shared_ptr 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 - }; -}