Repository navigation
Expand file tree
/
Copy pathtest_pure_flags.cpp
More file actions
79 lines (69 loc) · 3.02 KB
/
Copy pathtest_pure_flags.cpp
File metadata and controls
79 lines (69 loc) · 3.02 KB
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
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <string>
#include <vector>
namespace test_pure_flags {
struct GetIdConfig {
rfl::Short<"H", rfl::DefaultVal<bool>> with_history = false;
rfl::Short<"r", rfl::DefaultVal<bool>> add_referenced = false;
rfl::Positional<std::string> input;
rfl::Positional<rfl::DefaultVal<std::vector<std::string>>> ids;
};
TEST(cli, test_pure_flags_numeric_positionals) {
const std::vector<std::string> args = {"in.osm", "-H", "1", "0", "42"};
const auto res =
rfl::cli::read<GetIdConfig, rfl::cli::PureFlags,
rfl::cli::ListDelimiter<"">>(args);
ASSERT_TRUE(res) << res.error().what();
EXPECT_TRUE(res->with_history.value().get());
EXPECT_FALSE(res->add_referenced.value().get());
EXPECT_EQ(res->input.value(), "in.osm");
EXPECT_EQ(res->ids.value().get(),
(std::vector<std::string>{"1", "0", "42"}));
}
TEST(cli, test_pure_flags_bool_word_positionals) {
const std::vector<std::string> args = {"in.osm", "-r", "false", "true"};
const auto res =
rfl::cli::read<GetIdConfig, rfl::cli::PureFlags,
rfl::cli::ListDelimiter<"">>(args);
ASSERT_TRUE(res) << res.error().what();
EXPECT_TRUE(res->add_referenced.value().get());
EXPECT_EQ(res->ids.value().get(),
(std::vector<std::string>{"false", "true"}));
}
TEST(cli, test_pure_flags_grouped_and_explicit_equals) {
const std::vector<std::string> args = {"-Hr", "in.osm", "0"};
const auto res = rfl::cli::read<GetIdConfig, rfl::cli::PureFlags>(args);
ASSERT_TRUE(res) << res.error().what();
EXPECT_TRUE(res->with_history.value().get());
EXPECT_TRUE(res->add_referenced.value().get());
EXPECT_EQ(res->input.value(), "in.osm");
EXPECT_EQ(res->ids.value().get(), (std::vector<std::string>{"0"}));
const std::vector<std::string> explicit_false = {"-H=false", "in.osm", "1"};
const auto res2 =
rfl::cli::read<GetIdConfig, rfl::cli::PureFlags>(explicit_false);
ASSERT_TRUE(res2) << res2.error().what();
EXPECT_FALSE(res2->with_history.value().get());
EXPECT_EQ(res2->ids.value().get(), (std::vector<std::string>{"1"}));
}
struct UsageFlagConfig {
rfl::Short<"v", rfl::Description<"Verbose output", rfl::DefaultVal<bool>>>
verbose = false;
rfl::Description<"Use color", rfl::DefaultVal<bool>> color = true;
rfl::Positional<std::string> input;
};
TEST(cli, test_pure_flags_usage_formatting) {
const auto text =
rfl::cli::usage<UsageFlagConfig, rfl::cli::PureFlags>("tool", true);
ASSERT_TRUE(text) << text.error().what();
// Pure flags omit `[=<bool>]` in both synopsis and argument rows.
EXPECT_NE(text->find("Usage: tool [-v|--verbose] [--color] <input>\n"),
std::string::npos)
<< *text;
EXPECT_EQ(text->find("[=<bool>]"), std::string::npos) << *text;
// Default: false. is suppressed for pure flags, while Default: true. is kept.
EXPECT_EQ(text->find("Default: false."), std::string::npos) << *text;
EXPECT_NE(text->find("Default: true."), std::string::npos) << *text;
}
} // namespace test_pure_flags