From da5e8ee4a982d889e55ffd1b19c2b48c4aea198e Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 5 Jun 2017 09:10:04 -0400 Subject: [PATCH] Adding a few more tests --- include/CLI/App.hpp | 2 +- tests/AppTest.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index a58cae06..b9dc4f63 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -281,7 +281,7 @@ class App { variable.emplace_back(); retval &= detail::lexical_cast(a, variable.back()); } - return variable.size() > 0 && retval; + return (!variable.empty()) && retval; }; Option *opt = add_option(name, fun, description, false); diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 779e164e..5990e99b 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -420,6 +420,47 @@ TEST_F(TApp, InSet) { EXPECT_THROW(run(), CLI::ConversionError); } +TEST_F(TApp, InSetWithDefault) { + + std::string choice = "one"; + app.add_set("-q,--quick", choice, {"one", "two", "three"}, "", true); + + run(); + EXPECT_EQ("one", choice); + app.reset(); + + args = {"--quick", "two"}; + + run(); + EXPECT_EQ("two", choice); + + app.reset(); + + args = {"--quick", "four"}; + EXPECT_THROW(run(), CLI::ConversionError); +} + + +TEST_F(TApp, InCaselessSetWithDefault) { + + std::string choice = "one"; + app.add_set_ignore_case("-q,--quick", choice, {"one", "two", "three"}, "", true); + + run(); + EXPECT_EQ("one", choice); + app.reset(); + + args = {"--quick", "tWo"}; + + run(); + EXPECT_EQ("two", choice); + + app.reset(); + + args = {"--quick", "four"}; + EXPECT_THROW(run(), CLI::ConversionError); +} + TEST_F(TApp, InIntSet) { int choice; @@ -491,6 +532,20 @@ TEST_F(TApp, VectorFixedString) { EXPECT_EQ(answer, strvec); } + +TEST_F(TApp, VectorDefaultedFixedString) { + std::vector strvec{"one"}; + std::vector answer{"mystring", "mystring2", "mystring3"}; + + CLI::Option *opt = app.add_option("-s,--string", strvec, "", true)->expected(3); + EXPECT_EQ(3, opt->get_expected()); + + args = {"--string", "mystring", "mystring2", "mystring3"}; + run(); + EXPECT_EQ((size_t)3, app.count("--string")); + EXPECT_EQ(answer, strvec); +} + TEST_F(TApp, VectorUnlimString) { std::vector strvec; std::vector answer{"mystring", "mystring2", "mystring3"};