1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00
CLI11/tests/OptionalTest.cpp
Philip Top 418b7175f5 Type size refactor (#325)
* add expanded type_size specification

* add some more checks for type_size_max

* continued work on getting type sizes more flexible

* make some more tweaks to option to split up validate and reduce sections

* git rid of exceptions on the type_size functions exceptions,  allow any number to be entered for the min and max and don't make a distinction between flags and other types.

* add expected count

* add the allow extra args flag in an option

* start working in allow_extra_args

* write some stuff in the book,  and continue working on the failing test cases

* fix a few more of the helpers tests

* a few more test cases running

* all tests pass, fixing calls in ini files

* get vector<pair> working and all tests passing

* change callback to use reference to remove allocation and copy operation

* add support and test for vector<vector<X>>

* change Validators_ to validators_ for consistency

* fix linux warnings and errors by reording some templates and adding some typename keywords

* add support for std::vector<X> as the cross conversion type so optional<std::vector<X>> is supported using the full template of add_option.

* a few more test cases to take care of some coverage gaps

* add missing parenthesis

* add some more tests for coverage gaps

* add test for flag like option

* add transform test for `as<X>` function and make it pass through the defaults

* add a few more tests and have vector default string interpreted correctly.

* add test for defaulted integer,  and route default string for defaulted value which would otherwise be empty

* some code cleanup and comments and few more test coverage gap tests

* add more tests and fix a few bugs on the type size and different code paths

* remove path in results by fixing the clear of options so they go back to parsing state.

* get coverage back to 100%

* clang_tidy, and codacy fixes

* reorder the lexical_conversion definitions

* update some formatting

* update whitespace on book chapter
2019-11-10 05:36:16 +10:30

215 lines
4.5 KiB
C++

#include <cstdlib>
#include <iostream>
#include "app_helper.hpp"
// You can explicitly enable or disable support
// by defining to 1 or 0. Extra check here to ensure it's in the stdlib too.
// We nest the check for __has_include and it's usage
#ifndef CLI11_STD_OPTIONAL
#ifdef __has_include
#if defined(CLI11_CPP17) && __has_include(<optional>)
#define CLI11_STD_OPTIONAL 1
#else
#define CLI11_STD_OPTIONAL 0
#endif
#else
#define CLI11_STD_OPTIONAL 0
#endif
#endif
#ifndef CLI11_EXPERIMENTAL_OPTIONAL
#define CLI11_EXPERIMENTAL_OPTIONAL 0
#endif
#ifndef CLI11_BOOST_OPTIONAL
#define CLI11_BOOST_OPTIONAL 0
#endif
#if CLI11_BOOST_OPTIONAL
#include <boost/version.hpp>
#if BOOST_VERSION < 106100
#error "This boost::optional version is not supported, use 1.61 or better"
#endif
#endif
#if CLI11_STD_OPTIONAL
#include <optional>
#endif
#if CLI11_EXPERIMENTAL_OPTIONAL
#include <experimental/optional>
#endif
#if CLI11_BOOST_OPTIONAL
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
#endif
// [CLI11:verbatim]
#if CLI11_STD_OPTIONAL
TEST_F(TApp, StdOptionalTest) {
std::optional<int> opt;
app.add_option("-c,--count", opt);
run();
EXPECT_FALSE(opt);
args = {"-c", "1"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 1);
args = {"--count", "3"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 3);
}
#endif
#if CLI11_EXPERIMENTAL_OPTIONAL
TEST_F(TApp, ExperimentalOptionalTest) {
std::experimental::optional<int> opt;
app.add_option("-c,--count", opt);
run();
EXPECT_FALSE(opt);
args = {"-c", "1"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 1);
args = {"--count", "3"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 3);
}
#endif
#if CLI11_BOOST_OPTIONAL
TEST_F(TApp, BoostOptionalTest) {
boost::optional<int> opt;
app.add_option("-c,--count", opt);
run();
EXPECT_FALSE(opt);
args = {"-c", "1"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 1);
opt = {};
args = {"--count", "3"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 3);
}
TEST_F(TApp, BoostOptionalTestZarg) {
boost::optional<int> opt;
app.add_option("-c,--count", opt)->expected(0, 1);
run();
EXPECT_FALSE(opt);
args = {"-c", "1"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 1);
opt = {};
args = {"--count"};
run();
EXPECT_FALSE(opt);
}
TEST_F(TApp, BoostOptionalint64Test) {
boost::optional<int64_t> opt;
app.add_option("-c,--count", opt);
run();
EXPECT_FALSE(opt);
args = {"-c", "1"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 1);
opt = {};
args = {"--count", "3"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 3);
}
TEST_F(TApp, BoostOptionalStringTest) {
boost::optional<std::string> opt;
app.add_option("-s,--string", opt);
run();
EXPECT_FALSE(opt);
args = {"-s", "strval"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, "strval");
opt = {};
args = {"--string", "strv"};
run();
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, "strv");
}
TEST_F(TApp, BoostOptionalEnumTest) {
enum class eval : char { val0 = 0, val1 = 1, val2 = 2, val3 = 3, val4 = 4 };
boost::optional<eval> opt;
auto optptr = app.add_option<decltype(opt), eval>("-v,--val", opt);
optptr->capture_default_str();
auto dstring = optptr->get_default_str();
EXPECT_TRUE(dstring.empty());
run();
EXPECT_FALSE(opt);
args = {"-v", "3"};
run();
EXPECT_TRUE(opt);
EXPECT_TRUE(*opt == eval::val3);
opt = {};
args = {"--val", "1"};
run();
EXPECT_TRUE(opt);
EXPECT_TRUE(*opt == eval::val1);
}
TEST_F(TApp, BoostOptionalVector) {
boost::optional<std::vector<int>> opt;
app.add_option_function<std::vector<int>>(
"-v,--vec", [&opt](const std::vector<int> &v) { opt = v; }, "some vector")
->expected(3);
run();
EXPECT_FALSE(opt);
args = {"-v", "1", "4", "5"};
run();
EXPECT_TRUE(opt);
std::vector<int> expV{1, 4, 5};
EXPECT_EQ(*opt, expV);
}
TEST_F(TApp, BoostOptionalVectorEmpty) {
boost::optional<std::vector<int>> opt;
app.add_option<decltype(opt), std::vector<int>>("-v,--vec", opt)->expected(0, 3)->allow_extra_args();
run();
EXPECT_FALSE(opt);
args = {"-v"};
opt = std::vector<int>{4, 3};
run();
EXPECT_FALSE(opt);
args = {"-v", "1", "4", "5"};
run();
EXPECT_TRUE(opt);
std::vector<int> expV{1, 4, 5};
EXPECT_EQ(*opt, expV);
}
#endif
#if !CLI11_OPTIONAL
TEST_F(TApp, DISABLED_OptionalTest) {}
#endif