From 58655472f726e04f66981396ed91cb3cfe8be9ee Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Wed, 8 Mar 2017 09:34:08 -0500 Subject: [PATCH] IncorrectConstruction --- include/CLI/Option.hpp | 4 +++- tests/CreationTest.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index 51b9588e..d3cf7594 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -154,7 +154,9 @@ public: Option* expected(int value) { if(value == 0) throw IncorrectConstruction("Cannot set 0 expected, use a flag instead"); - if(!allow_vector_ && value != 1) + else if(expected_ == 0) + throw IncorrectConstruction("Cannot make a flag take arguments!"); + else if(!allow_vector_ && value != 1) throw IncorrectConstruction("You can only change the Expected arguments for vectors"); expected_ = value; return this; diff --git a/tests/CreationTest.cpp b/tests/CreationTest.cpp index fcf8155b..36de203d 100644 --- a/tests/CreationTest.cpp +++ b/tests/CreationTest.cpp @@ -74,3 +74,50 @@ TEST_F(TApp, MultipleSubcomNoMatchingInplace2) { EXPECT_NO_THROW(first->ignore_case()); EXPECT_NO_THROW(second->ignore_case()); } + +TEST_F(TApp, IncorrectConstructionFlagPositional1) { + EXPECT_THROW(app.add_flag("cat"), CLI::IncorrectConstruction); +} + +TEST_F(TApp, IncorrectConstructionFlagPositional2) { + int x; + EXPECT_THROW(app.add_flag("cat", x), CLI::IncorrectConstruction); +} + +TEST_F(TApp, IncorrectConstructionFlagPositional3) { + bool x; + EXPECT_THROW(app.add_flag("cat", x), CLI::IncorrectConstruction); +} + +TEST_F(TApp, IncorrectConstructionFlagExpected) { + auto cat = app.add_flag("--cat"); + EXPECT_THROW(cat->expected(1), CLI::IncorrectConstruction); +} + +TEST_F(TApp, IncorrectConstructionOptionAsFlag) { + int x; + auto cat = app.add_option("--cat", x); + EXPECT_THROW(cat->expected(0), CLI::IncorrectConstruction); +} + +TEST_F(TApp, IncorrectConstructionOptionAsVector) { + int x; + auto cat = app.add_option("--cat", x); + EXPECT_THROW(cat->expected(2), CLI::IncorrectConstruction); +} + +TEST_F(TApp, IncorrectConstructionVectorAsFlag) { + std::vector x; + auto cat = app.add_option("--cat", x); + EXPECT_THROW(cat->expected(0), CLI::IncorrectConstruction); +} + +TEST_F(TApp, IncorrectConstructionRequiresCannotFind) { + auto cat = app.add_flag("--cat"); + EXPECT_THROW(cat->requires("--nothing"),CLI::IncorrectConstruction); +} + +TEST_F(TApp, IncorrectConstructionExcludesCannotFind) { + auto cat = app.add_flag("--cat"); + EXPECT_THROW(cat->excludes("--nothing"),CLI::IncorrectConstruction); +}