Commit 58655472f726e04f66981396ed91cb3cfe8be9ee
1 parent
2af5577b
IncorrectConstruction
Showing
2 changed files
with
50 additions
and
1 deletions
include/CLI/Option.hpp
| ... | ... | @@ -154,7 +154,9 @@ public: |
| 154 | 154 | Option* expected(int value) { |
| 155 | 155 | if(value == 0) |
| 156 | 156 | throw IncorrectConstruction("Cannot set 0 expected, use a flag instead"); |
| 157 | - if(!allow_vector_ && value != 1) | |
| 157 | + else if(expected_ == 0) | |
| 158 | + throw IncorrectConstruction("Cannot make a flag take arguments!"); | |
| 159 | + else if(!allow_vector_ && value != 1) | |
| 158 | 160 | throw IncorrectConstruction("You can only change the Expected arguments for vectors"); |
| 159 | 161 | expected_ = value; |
| 160 | 162 | return this; | ... | ... |
tests/CreationTest.cpp
| ... | ... | @@ -74,3 +74,50 @@ TEST_F(TApp, MultipleSubcomNoMatchingInplace2) { |
| 74 | 74 | EXPECT_NO_THROW(first->ignore_case()); |
| 75 | 75 | EXPECT_NO_THROW(second->ignore_case()); |
| 76 | 76 | } |
| 77 | + | |
| 78 | +TEST_F(TApp, IncorrectConstructionFlagPositional1) { | |
| 79 | + EXPECT_THROW(app.add_flag("cat"), CLI::IncorrectConstruction); | |
| 80 | +} | |
| 81 | + | |
| 82 | +TEST_F(TApp, IncorrectConstructionFlagPositional2) { | |
| 83 | + int x; | |
| 84 | + EXPECT_THROW(app.add_flag("cat", x), CLI::IncorrectConstruction); | |
| 85 | +} | |
| 86 | + | |
| 87 | +TEST_F(TApp, IncorrectConstructionFlagPositional3) { | |
| 88 | + bool x; | |
| 89 | + EXPECT_THROW(app.add_flag("cat", x), CLI::IncorrectConstruction); | |
| 90 | +} | |
| 91 | + | |
| 92 | +TEST_F(TApp, IncorrectConstructionFlagExpected) { | |
| 93 | + auto cat = app.add_flag("--cat"); | |
| 94 | + EXPECT_THROW(cat->expected(1), CLI::IncorrectConstruction); | |
| 95 | +} | |
| 96 | + | |
| 97 | +TEST_F(TApp, IncorrectConstructionOptionAsFlag) { | |
| 98 | + int x; | |
| 99 | + auto cat = app.add_option("--cat", x); | |
| 100 | + EXPECT_THROW(cat->expected(0), CLI::IncorrectConstruction); | |
| 101 | +} | |
| 102 | + | |
| 103 | +TEST_F(TApp, IncorrectConstructionOptionAsVector) { | |
| 104 | + int x; | |
| 105 | + auto cat = app.add_option("--cat", x); | |
| 106 | + EXPECT_THROW(cat->expected(2), CLI::IncorrectConstruction); | |
| 107 | +} | |
| 108 | + | |
| 109 | +TEST_F(TApp, IncorrectConstructionVectorAsFlag) { | |
| 110 | + std::vector<int> x; | |
| 111 | + auto cat = app.add_option("--cat", x); | |
| 112 | + EXPECT_THROW(cat->expected(0), CLI::IncorrectConstruction); | |
| 113 | +} | |
| 114 | + | |
| 115 | +TEST_F(TApp, IncorrectConstructionRequiresCannotFind) { | |
| 116 | + auto cat = app.add_flag("--cat"); | |
| 117 | + EXPECT_THROW(cat->requires("--nothing"),CLI::IncorrectConstruction); | |
| 118 | +} | |
| 119 | + | |
| 120 | +TEST_F(TApp, IncorrectConstructionExcludesCannotFind) { | |
| 121 | + auto cat = app.add_flag("--cat"); | |
| 122 | + EXPECT_THROW(cat->excludes("--nothing"),CLI::IncorrectConstruction); | |
| 123 | +} | ... | ... |