diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c3d8b756..29011457 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,7 @@ set(CLI_TESTS SubcommandTest HelpTest NewParseTest + OptionalTest ) set(CLI_MULTIONLY_TESTS diff --git a/tests/OptionalTest.cpp b/tests/OptionalTest.cpp new file mode 100644 index 00000000..c38a3cac --- /dev/null +++ b/tests/OptionalTest.cpp @@ -0,0 +1,48 @@ +#include +#include + +#ifdef __has_include +#if __has_include() +#include +#define have_optional 1 +using std::experimental::optional; +#elif __has_include() +#include +#define have_optional 1 +using std::optional; +#else +#define have_optional 0 +#endif +#endif + +#if have_optional + +template std::istream &operator>>(std::istream &in, optional &val) { + T v; + in >> v; + val = v; + return in; +} + +#include "app_helper.hpp" + +TEST_F(TApp, OptionalTest) { + optional 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