Repository navigation
Expand file tree
/
Copy pathtest_implicit_value.cpp
More file actions
157 lines (136 loc) · 5.78 KB
/
Copy pathtest_implicit_value.cpp
File metadata and controls
157 lines (136 loc) · 5.78 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <gtest/gtest.h>
#include <optional>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
namespace test_implicit_value {
template <class T, class... Ps>
rfl::Result<T> read(std::vector<const char*> _args) {
_args.insert(_args.begin(), "program");
return rfl::cli::read<T, Ps...>(static_cast<int>(_args.size()),
const_cast<char**>(_args.data()));
}
enum class Color { red, green, blue };
struct Opts {
rfl::Positional<std::optional<std::string>> input;
rfl::Short<"h", rfl::ImplicitValue<std::optional<std::string>>> help;
rfl::ImplicitValue<std::optional<std::string>, "all"> help_reports;
rfl::ImplicitValue<rfl::DefaultVal<int>, "2"> verbosity = 0;
rfl::ImplicitValue<std::optional<Color>, "green"> color;
std::optional<bool> force_export;
};
TEST(cli, test_implicit_value_bare) {
const auto result =
read<Opts>({"--help", "--help-reports", "--verbosity", "--color"});
ASSERT_TRUE(result) << result.error().what();
const auto& o = result.value();
EXPECT_EQ(o.help().value().value(), "");
EXPECT_EQ(o.help_reports().value(), "all");
EXPECT_EQ(o.verbosity().value(), 2);
EXPECT_EQ(o.color().value(), Color::green);
EXPECT_FALSE(o.input().has_value());
}
TEST(cli, test_implicit_value_with_equals) {
const auto result = read<Opts>({"--help=tag", "--help-reports=memory",
"--verbosity=5", "--color=blue"});
ASSERT_TRUE(result) << result.error().what();
const auto& o = result.value();
EXPECT_EQ(o.help().value().value(), "tag");
EXPECT_EQ(o.help_reports().value(), "memory");
EXPECT_EQ(o.verbosity().value(), 5);
EXPECT_EQ(o.color().value(), Color::blue);
}
TEST(cli, test_implicit_value_empty_with_equals) {
const auto result = read<Opts>({"--help-reports="});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().help_reports().value(), "");
}
TEST(cli, test_implicit_value_does_not_consume_next_token) {
for (const char* option : {"--help", "--help-reports", "--verbosity", "-h"}) {
const auto result = read<Opts>({option, "r.rep"});
ASSERT_TRUE(result) << option << ": " << result.error().what();
EXPECT_EQ(result.value().input().value(), "r.rep") << option;
}
const auto result = read<Opts>({"--help-reports", "--force-export"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().help_reports().value(), "all");
EXPECT_EQ(result.value().force_export, true);
}
TEST(cli, test_implicit_value_absent) {
const auto result = read<Opts>({"r.rep"});
ASSERT_TRUE(result) << result.error().what();
const auto& o = result.value();
EXPECT_FALSE(o.help().value().has_value());
EXPECT_FALSE(o.help_reports().has_value());
EXPECT_EQ(o.verbosity().value(), 0);
EXPECT_FALSE(o.color().has_value());
EXPECT_EQ(rfl::json::write(o), R"({"input":"r.rep","verbosity":0})");
}
TEST(cli, test_implicit_value_short) {
const auto bare = read<Opts>({"-h"});
ASSERT_TRUE(bare) << bare.error().what();
EXPECT_EQ(bare.value().help().value().value(), "");
const auto with_value = read<Opts>({"-h=tag", "r.rep"});
ASSERT_TRUE(with_value) << with_value.error().what();
EXPECT_EQ(with_value.value().help().value().value(), "tag");
EXPECT_EQ(with_value.value().input().value(), "r.rep");
}
struct Required {
rfl::ImplicitValue<std::string, "x"> tag;
};
TEST(cli, test_implicit_value_required) {
const auto absent = read<Required>({});
EXPECT_FALSE(absent);
const auto bare = read<Required>({"--tag"});
ASSERT_TRUE(bare) << bare.error().what();
EXPECT_EQ(bare.value().tag(), "x");
}
struct Database {
rfl::ImplicitValue<std::optional<std::string>, "require"> ssl_mode;
};
// The same option, wrapped in other orders and in a nested struct.
struct Orders {
rfl::ImplicitValue<rfl::Short<"a", std::optional<std::string>>, "A"> a;
rfl::Description<"B", rfl::Short<"b", rfl::ImplicitValue<
std::optional<std::string>, "B">>>
b;
rfl::Short<"c", rfl::Description<"C", rfl::ImplicitValue<
std::optional<std::string>, "C">>>
c;
rfl::Rename<"dee", rfl::ImplicitValue<std::optional<std::string>, "D">> d;
std::optional<rfl::ImplicitValue<std::string, "E">> e;
rfl::DefaultVal<rfl::ImplicitValue<std::string, "F">> f = std::string("f");
rfl::ImplicitValue<rfl::DefaultVal<std::string>, "G"> g = std::string("g");
std::optional<Database> database;
rfl::Positional<std::optional<std::string>> input;
};
TEST(cli, test_implicit_value_wrapper_orders) {
const auto bare =
read<Orders, rfl::NoExtraFields>({"-a", "-b", "-c", "--dee", "--e", "--f",
"--g", "--database.ssl-mode", "r.rep"});
ASSERT_TRUE(bare) << bare.error().what();
const auto& o = bare.value();
EXPECT_EQ(o.a().value().value(), "A");
EXPECT_EQ(o.b().value().value().value(), "B");
EXPECT_EQ(o.c().value().value().value(), "C");
EXPECT_EQ(o.d().value().value(), "D");
EXPECT_EQ(o.e.value().value(), "E");
EXPECT_EQ(o.f().value(), "F");
EXPECT_EQ(o.g().value(), "G");
EXPECT_EQ(o.database->ssl_mode().value(), "require");
EXPECT_EQ(o.input().value(), "r.rep");
// A nested option has its implicit value without rfl::NoExtraFields, too.
const auto nested = read<Orders>({"--database.ssl-mode", "r.rep"});
ASSERT_TRUE(nested) << nested.error().what();
EXPECT_EQ(nested.value().database->ssl_mode().value(), "require");
EXPECT_EQ(nested.value().input().value(), "r.rep");
const auto absent = read<Orders>({});
ASSERT_TRUE(absent) << absent.error().what();
EXPECT_FALSE(absent.value().a().value().has_value());
EXPECT_FALSE(absent.value().e.has_value());
EXPECT_EQ(absent.value().f().value(), "f");
EXPECT_EQ(absent.value().g().value(), "g");
}
} // namespace test_implicit_value