Issue 1086 (#1087)

Fixes issue #1086.

In the default_val enums of uint8_t would read in as a string as they
could be converted to a string. This worked ok for normal values, but
when a check was added for specific strings, it caused an error when the
default_val was added. This PR fixes the issue.

The builder for coverage was updated to CMake 3.31 (by github), this
triggered an error in the coverage tool script. This led to updating
that script, which led to uncovering some missing coverage, which led to
additional tests, which led to some issues around single element tuples
support from #1081, which led to a few other issues that came up in the
to_string operation and templates.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
Philip Top
2024-11-18 05:40:37 -08:00
committed by GitHub
parent d9473a486b
commit 3539bd185f
8 changed files with 706 additions and 81 deletions

View File

@@ -504,6 +504,19 @@ TEST_CASE_METHOD(TApp, "TupleDefault", "[app]") {
CHECK(pr == pr2);
}
TEST_CASE_METHOD(TApp, "TupleDefaultSingle", "[app]") {
std::tuple<std::string> pr{"test_tuple"};
auto *opt = app.add_option("-i", pr)->expected(0, 1);
args = {"-i"};
run();
CHECK(app.count("-i") == 1u);
std::tuple<std::string> pr2{"total3"};
opt->default_val(pr2);
run();
CHECK(pr == pr2);
}
TEST_CASE_METHOD(TApp, "TupleComplex", "[app]") {
std::tuple<double, std::string, int, std::pair<std::string, std::string>> pr{57.5, "test", 5, {"total", "total2"}};
auto *opt = app.add_option("-i", pr)->expected(0, 4);
@@ -518,6 +531,16 @@ TEST_CASE_METHOD(TApp, "TupleComplex", "[app]") {
CHECK(pr == pr2);
}
TEST_CASE_METHOD(TApp, "invalidDefault", "[app]") {
int pr{5};
auto *opt = app.add_option("-i", pr)
->expected(1)
->multi_option_policy(CLI::MultiOptionPolicy::Throw)
->delimiter(',')
->force_callback();
CHECK_THROWS(opt->default_val("4,6,2,8"));
}
TEST_CASE_METHOD(TApp, "TogetherInt", "[app]") {
int i{0};
app.add_option("-i,--int", i);

View File

@@ -286,7 +286,7 @@ endif()
if(CMAKE_BUILD_TYPE STREQUAL Coverage)
include(CodeCoverage)
setup_target_for_coverage(
setup_target_for_coverage_lcov(
NAME
CLI11_coverage
EXECUTABLE

View File

@@ -52,6 +52,12 @@ TEST_CASE("TypeTools: tuple", "[helpers]") {
TEST_CASE("TypeTools: tuple_to_string", "[helpers]") {
std::pair<double, std::string> p1{0.999, "kWh"};
CHECK(CLI::detail::to_string(p1) == "[0.999,kWh]");
const std::tuple<std::string> t1{"kWh"};
CHECK(CLI::detail::to_string(t1) == "kWh");
const std::tuple<double> td{0.999};
CHECK(CLI::detail::to_string(td) == "0.999");
}
TEST_CASE("TypeTools: type_size", "[helpers]") {

View File

@@ -117,6 +117,25 @@ TEST_CASE_METHOD(TApp, "EnumCheckedTransform", "[transform]") {
CHECK_THROWS_AS(run(), CLI::ValidationError);
}
// from to-mas-kral Issue #1086
TEST_CASE_METHOD(TApp, "EnumCheckedTransformUint8", "[transform]") {
enum class FooType : std::uint8_t { A, B };
auto type = FooType::B;
const std::map<std::string, FooType> foo_map{
{"a", FooType::A},
{"b", FooType::B},
};
app.add_option("-f,--foo", type, "FooType")
->transform(CLI::CheckedTransformer(foo_map, CLI::ignore_case))
->default_val(FooType::A)
->force_callback();
run();
CHECK(type == FooType::A);
}
// from jzakrzewski Issue #330
TEST_CASE_METHOD(TApp, "EnumCheckedDefaultTransform", "[transform]") {
enum class existing : std::int16_t { abort, overwrite, remove };