OptionalTest.cpp
908 Bytes
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
#include <cstdlib>
#include <iostream>
#ifdef __has_include
#if defined(CLI11_CPP17) && __has_include(<optional>)
#include <optional>
#define have_optional 1
using std::experimental::optional;
#elif defined(CPP11_CPP14) && __has_include(<experimental/optional>)
#include <experimental/optional>
#define have_optional 1
using std::optional;
#else
#define have_optional 0
#endif
#endif
#if have_optional
template <typename T> std::istream &operator>>(std::istream &in, optional<T> &val) {
T v;
in >> v;
val = v;
return in;
}
#include "app_helper.hpp"
TEST_F(TApp, OptionalTest) {
optional<int> opt;
app.add_option("-c,--count", opt);
run();
EXPECT_FALSE(opt);
app.reset();
args = {"-c", "1"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 1);
app.reset();
args = {"--count", "3"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 3);
}
#endif