-
Notifications
You must be signed in to change notification settings - Fork 353
/
NewParseTest.cpp
101 lines (74 loc) · 2.2 KB
/
NewParseTest.cpp
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
#include "app_helper.hpp"
#include "gmock/gmock.h"
#include <complex>
using ::testing::HasSubstr;
using cx = std::complex<double>;
CLI::Option *
add_option(CLI::App &app, std::string name, cx &variable, std::string description = "", bool defaulted = false) {
CLI::callback_t fun = [&variable](CLI::results_t res) {
if(res.size() != 2)
return false;
double x, y;
bool worked = CLI::detail::lexical_cast(res[0], x) && CLI::detail::lexical_cast(res[1], y);
if(worked)
variable = cx(x, y);
return worked;
};
CLI::Option *opt = app.add_option(name, fun, description, defaulted);
opt->set_custom_option("COMPLEX", 2);
if(defaulted) {
std::stringstream out;
out << variable;
opt->set_default_str(out.str());
}
return opt;
}
TEST_F(TApp, AddingComplexParser) {
cx comp{0, 0};
add_option(app, "-c,--complex", comp);
args = {"-c", "1.5", "2.5"};
run();
EXPECT_EQ(1.5, comp.real());
EXPECT_EQ(2.5, comp.imag());
}
TEST_F(TApp, DefaultComplex) {
cx comp{1, 2};
add_option(app, "-c,--complex", comp, "", true);
args = {"-c", "4", "3"};
std::string help = app.help();
EXPECT_THAT(help, HasSubstr("1"));
EXPECT_THAT(help, HasSubstr("2"));
EXPECT_EQ(1, comp.real());
EXPECT_EQ(2, comp.imag());
run();
EXPECT_EQ(4, comp.real());
EXPECT_EQ(3, comp.imag());
}
TEST_F(TApp, BuiltinComplex) {
cx comp{1, 2};
app.add_complex("-c,--complex", comp, "", true);
args = {"-c", "4", "3"};
std::string help = app.help();
EXPECT_THAT(help, HasSubstr("1"));
EXPECT_THAT(help, HasSubstr("2"));
EXPECT_THAT(help, HasSubstr("COMPLEX"));
EXPECT_EQ(1, comp.real());
EXPECT_EQ(2, comp.imag());
run();
EXPECT_EQ(4, comp.real());
EXPECT_EQ(3, comp.imag());
}
TEST_F(TApp, BuiltinComplexIgnoreI) {
cx comp{1, 2};
app.add_complex("-c,--complex", comp);
args = {"-c", "4", "3i"};
run();
EXPECT_EQ(4, comp.real());
EXPECT_EQ(3, comp.imag());
}
TEST_F(TApp, BuiltinComplexFail) {
cx comp{1, 2};
app.add_complex("-c,--complex", comp);
args = {"-c", "4"};
EXPECT_THROW(run(), CLI::ArgumentMismatch);
}