mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 12:13:52 +00:00
Adding optional test
This commit is contained in:
parent
11444a4e3a
commit
5c0d6617d5
@ -10,6 +10,7 @@ set(CLI_TESTS
|
||||
SubcommandTest
|
||||
HelpTest
|
||||
NewParseTest
|
||||
OptionalTest
|
||||
)
|
||||
|
||||
set(CLI_MULTIONLY_TESTS
|
||||
|
48
tests/OptionalTest.cpp
Normal file
48
tests/OptionalTest.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef __has_include
|
||||
#if __has_include(<optional>)
|
||||
#include <optional>
|
||||
#define have_optional 1
|
||||
using std::experimental::optional;
|
||||
#elif __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
|
Loading…
x
Reference in New Issue
Block a user