diff options
Diffstat (limited to 'test/args.cc')
| -rw-r--r-- | test/args.cc | 138 |
1 files changed, 65 insertions, 73 deletions
diff --git a/test/args.cc b/test/args.cc index a43c59c..22159d2 100644 --- a/test/args.cc +++ b/test/args.cc @@ -1,8 +1,7 @@ -#include <gtest/gtest.h> - #include "args.hh" #include <cstddef> +#include <gtest/gtest.h> #include <memory> #include <sstream> #include <string> @@ -10,17 +9,17 @@ #include <utility> #include <vector> -#define SETUP_OPTIONS(args) \ - auto short_only = (args)->option('a', "", "an option"); \ - auto short_long = (args)->option('b', "bold", "set font style to bold"); \ - auto long_only = (args)->option("cold", "use if it is cold outside"); \ - auto short_only_req = (args)->option_argument('d', "", "", "distance"); \ - auto short_long_req = (args)->option_argument( \ - 'e', "eat", "FOOD", "what to order, what to eat?"); \ - auto long_only_req = (args)->option_argument( \ - "form", "", "circle, shape or something else?"); \ - auto short_only_opt = (args)->option_argument('g', "", "", "", false); \ - auto short_long_opt = (args)->option_argument('h', "hold", "", "", false); \ +#define SETUP_OPTIONS(args) \ + auto short_only = (args)->option('a', "", "an option"); \ + auto short_long = (args)->option('b', "bold", "set font style to bold"); \ + auto long_only = (args)->option("cold", "use if it is cold outside"); \ + auto short_only_req = (args)->option_argument('d', "", "", "distance"); \ + auto short_long_req = (args)->option_argument( \ + 'e', "eat", "FOOD", "what to order, what to eat?"); \ + auto long_only_req = \ + (args)->option_argument("form", "", "circle, shape or something else?"); \ + auto short_only_opt = (args)->option_argument('g', "", "", "", false); \ + auto short_long_opt = (args)->option_argument('h', "hold", "", "", false); \ auto long_only_opt = (args)->option_argument("invert", "", "", false); namespace { @@ -30,8 +29,7 @@ class Arguments { [[nodiscard]] int c() const { return static_cast<int>(str_.size()); }; [[nodiscard]] char** v() const { return ptr_.get(); } - explicit Arguments(std::vector<std::string> str) - : str_(std::move(str)) { + explicit Arguments(std::vector<std::string> str) : str_(std::move(str)) { ptr_ = std::make_unique<char*[]>(str_.size()); for (size_t i = 0; i < str_.size(); ++i) { ptr_[i] = const_cast<char*>(str_[i].c_str()); @@ -48,9 +46,7 @@ class Arguments { class Builder { public: - Arguments build() { - return Arguments(std::move(str_)); - } + Arguments build() { return Arguments(std::move(str_)); } Builder& add(std::string str) { str_.emplace_back(std::move(str)); @@ -110,16 +106,18 @@ TEST(args, options_set) { SETUP_OPTIONS(args); Builder builder; auto arg = builder.add("foo") - .add("-a") - .add("-b") - .add("--cold") - .add("-d").add("10") - .add("-e").add("hamburger") - .add("--form=circle") - .add("-g") - .add("-h") - .add("--invert") - .build(); + .add("-a") + .add("-b") + .add("--cold") + .add("-d") + .add("10") + .add("-e") + .add("hamburger") + .add("--form=circle") + .add("-g") + .add("-h") + .add("--invert") + .build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -148,16 +146,18 @@ TEST(args, options_set_variant) { SETUP_OPTIONS(args); Builder builder; auto arg = builder.add("foo") - .add("-a") - .add("--bold") - .add("--cold") - .add("-d").add("10") - .add("--eat=hamburger") - .add("--form").add("circle") - .add("-g") - .add("--hold=foo") - .add("--invert=bar") - .build(); + .add("-a") + .add("--bold") + .add("--cold") + .add("-d") + .add("10") + .add("--eat=hamburger") + .add("--form") + .add("circle") + .add("-g") + .add("--hold=foo") + .add("--invert=bar") + .build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -187,9 +187,7 @@ TEST(args, options_short_missing_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("-d") - .build(); + auto arg = builder.add("foo").add("-d").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -200,9 +198,7 @@ TEST(args, options_short_unknown_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("-X") - .build(); + auto arg = builder.add("foo").add("-X").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -213,9 +209,7 @@ TEST(args, options_long_missing_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--form") - .build(); + auto arg = builder.add("foo").add("--form").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -226,9 +220,7 @@ TEST(args, options_long_unsupported_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--cold=feet") - .build(); + auto arg = builder.add("foo").add("--cold=feet").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -239,9 +231,7 @@ TEST(args, options_long_unknown_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--experience") - .build(); + auto arg = builder.add("foo").add("--experience").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -252,9 +242,7 @@ TEST(args, options_short_dash_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("-d").add("-") - .build(); + auto arg = builder.add("foo").add("-d").add("-").build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -267,9 +255,7 @@ TEST(args, options_long_dash_dash_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--eat").add("--") - .build(); + auto arg = builder.add("foo").add("--eat").add("--").build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -283,11 +269,13 @@ TEST(args, options_dash_dash) { SETUP_OPTIONS(args); Builder builder; auto arg = builder.add("foo") - .add("--") - .add("-d").add("10") - .add("--eat=hamburger") - .add("--form").add("circle") - .build(); + .add("--") + .add("-d") + .add("10") + .add("--eat=hamburger") + .add("--form") + .add("circle") + .build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); ASSERT_EQ(5, out.size()); @@ -305,9 +293,7 @@ TEST(args, options_dash_dash_end) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--") - .build(); + auto arg = builder.add("foo").add("--").build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -330,7 +316,8 @@ TEST(args, help) { -g -h, --hold=[ARG] --invert=[ARG] -)", ss.str()); +)", + ss.str()); } TEST(args, help_wide) { @@ -338,7 +325,8 @@ TEST(args, help_wide) { SETUP_OPTIONS(args); std::stringstream ss; args->print_help(ss, 100); - EXPECT_EQ(R"(Mandatory arguments to long options are mandatory for short options too. + EXPECT_EQ( + R"(Mandatory arguments to long options are mandatory for short options too. -a an option -b, --bold set font style to bold --cold use if it is cold outside @@ -348,7 +336,8 @@ TEST(args, help_wide) { -g -h, --hold=[ARG] --invert=[ARG] -)", ss.str()); +)", + ss.str()); } TEST(args, help_narrow) { @@ -372,7 +361,8 @@ circle, shape or something else? -g -h, --hold=[ARG] --invert=[ARG] -)", ss.str()); +)", + ss.str()); } TEST(args, help_very_narrow) { @@ -401,7 +391,8 @@ circle, shape or -g -h, --hold=[ARG] --invert=[ARG] -)", ss.str()); +)", + ss.str()); } TEST(args, help_long_word) { @@ -416,5 +407,6 @@ TEST(args, help_long_word) { -a, --arg aaaaaaaaaaaaaaaaaaa- aaaaaaaaaaaaaaaaaaa -)", ss.str()); +)", + ss.str()); } |
