Repository navigation
Expand file tree
/
Copy pathtest_short.cpp
More file actions
106 lines (94 loc) · 2.46 KB
/
Copy pathtest_short.cpp
File metadata and controls
106 lines (94 loc) · 2.46 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
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <string>
namespace test_short {
struct Config {
std::string host;
rfl::Short<"p", int> port;
rfl::Short<"v", bool> verbose;
};
TEST(cli, test_short_basic) {
const char* args[] = {
"program",
"--host=localhost",
"-p", "8080",
"-v"
};
const auto result = rfl::cli::read<Config>(5, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().host, "localhost");
EXPECT_EQ(result.value().port(), 8080);
EXPECT_TRUE(result.value().verbose());
}
TEST(cli, test_short_equals_syntax) {
const char* args[] = {
"program",
"--host=localhost",
"-p=8080",
"-v"
};
const auto result = rfl::cli::read<Config>(4, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().port(), 8080);
}
TEST(cli, test_short_as_long) {
const char* args[] = {
"program",
"--host=localhost",
"--port=8080",
"--verbose"
};
const auto result = rfl::cli::read<Config>(4, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().port(), 8080);
EXPECT_TRUE(result.value().verbose());
}
TEST(cli, test_short_unknown) {
const char* args[] = {
"program",
"--host=localhost",
"-x", "foo"
};
const auto result = rfl::cli::read<Config>(4, const_cast<char**>(args));
ASSERT_FALSE(result);
}
TEST(cli, test_short_conflict_with_long) {
const char* args[] = {
"program",
"--host=localhost",
"--port=8080",
"-p", "9090"
};
const auto result = rfl::cli::read<Config>(5, const_cast<char**>(args));
ASSERT_FALSE(result);
}
struct NegativeConfig {
std::string host;
rfl::Short<"p", int> port;
};
TEST(cli, test_short_negative_value) {
const char* args[] = {
"program",
"--host=localhost",
"-p", "-42"
};
const auto result = rfl::cli::read<NegativeConfig>(
4, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().port(), -42);
}
struct FloatConfig {
rfl::Short<"t", double> threshold;
};
TEST(cli, test_short_negative_float) {
const char* args[] = {
"program",
"-t", "-3.14"
};
const auto result = rfl::cli::read<FloatConfig>(
3, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_DOUBLE_EQ(result.value().threshold(), -3.14);
}
} // namespace test_short