Commit 7ba1e84b105a3d74ee83f5328120ba8c21ec98dc

Authored by Henry Fredrick Schreiner
1 parent 3061782d

Adding exists check, verify nothrow

include/CLI.hpp
... ... @@ -115,11 +115,8 @@ bool _ExistingDirectory(std::string filename) {
115 115 }
116 116  
117 117 bool _NonexistentPath(std::string filename) {
118   - std::cout << "Validating: " << filename << std::endl;
119 118 struct stat buffer;
120   - bool out = stat(filename.c_str(), &buffer) != 0;
121   - std::cout << (out ? "Passed" : "Failed") << std::endl;
122   - return out;
  119 + return stat(filename.c_str(), &buffer) != 0;
123 120 }
124 121  
125 122 struct Error : public std::runtime_error {
... ...
tests/CLITest.cpp
... ... @@ -39,7 +39,7 @@ struct TApp : public ::testing::Test {
39 39 TEST_F(TApp, OneFlagShort) {
40 40 app.add_flag("c,count");
41 41 args = {"-c"};
42   - run();
  42 + EXPECT_NO_THROW(run());
43 43 EXPECT_EQ(1, app.count("c"));
44 44 EXPECT_EQ(1, app.count("count"));
45 45 }
... ... @@ -47,7 +47,7 @@ TEST_F(TApp, OneFlagShort) {
47 47 TEST_F(TApp, OneFlagLong) {
48 48 app.add_flag("c,count");
49 49 args = {"--count"};
50   - run();
  50 + EXPECT_NO_THROW(run());
51 51 EXPECT_EQ(1, app.count("c"));
52 52 EXPECT_EQ(1, app.count("count"));
53 53 }
... ... @@ -56,7 +56,7 @@ TEST_F(TApp, OneFlagRef) {
56 56 int ref;
57 57 app.add_flag("c,count", ref);
58 58 args = {"--count"};
59   - run();
  59 + EXPECT_NO_THROW(run());
60 60 EXPECT_EQ(1, app.count("c"));
61 61 EXPECT_EQ(1, app.count("count"));
62 62 EXPECT_EQ(1, ref);
... ... @@ -66,7 +66,7 @@ TEST_F(TApp, OneString) {
66 66 std::string str;
67 67 app.add_option("s,string", str);
68 68 args = {"--string", "mystring"};
69   - run();
  69 + EXPECT_NO_THROW(run());
70 70 EXPECT_EQ(1, app.count("s"));
71 71 EXPECT_EQ(1, app.count("string"));
72 72 EXPECT_EQ(str, "mystring");
... ... @@ -77,7 +77,7 @@ TEST_F(TApp, TogetherInt) {
77 77 int i;
78 78 app.add_option("i,int", i);
79 79 args = {"-i4"};
80   - run();
  80 + EXPECT_NO_THROW(run());
81 81 EXPECT_EQ(1, app.count("int"));
82 82 EXPECT_EQ(1, app.count("i"));
83 83 EXPECT_EQ(i, 4);
... ... @@ -87,7 +87,7 @@ TEST_F(TApp, SepInt) {
87 87 int i;
88 88 app.add_option("i,int", i);
89 89 args = {"-i","4"};
90   - run();
  90 + EXPECT_NO_THROW(run());
91 91 EXPECT_EQ(1, app.count("int"));
92 92 EXPECT_EQ(1, app.count("i"));
93 93 EXPECT_EQ(i, 4);
... ... @@ -97,7 +97,7 @@ TEST_F(TApp, OneStringAgain) {
97 97 std::string str;
98 98 app.add_option("s,string", str);
99 99 args = {"--string", "mystring"};
100   - run();
  100 + EXPECT_NO_THROW(run());
101 101 EXPECT_EQ(1, app.count("s"));
102 102 EXPECT_EQ(1, app.count("string"));
103 103 EXPECT_EQ(str, "mystring");
... ... @@ -107,7 +107,7 @@ TEST_F(TApp, OneStringAgain) {
107 107 TEST_F(TApp, DefaultStringAgain) {
108 108 std::string str = "previous";
109 109 app.add_option("s,string", str);
110   - run();
  110 + EXPECT_NO_THROW(run());
111 111 EXPECT_EQ(0, app.count("s"));
112 112 EXPECT_EQ(0, app.count("string"));
113 113 EXPECT_EQ(str, "previous");
... ... @@ -120,7 +120,7 @@ TEST_F(TApp, LotsOfFlags) {
120 120 app.add_flag("b");
121 121  
122 122 args = {"-a","-b","-aA"};
123   - run();
  123 + EXPECT_NO_THROW(run());
124 124 EXPECT_EQ(2, app.count("a"));
125 125 EXPECT_EQ(1, app.count("b"));
126 126 EXPECT_EQ(1, app.count("A"));
... ... @@ -135,7 +135,7 @@ TEST_F(TApp, ShortOpts) {
135 135  
136 136 args = {"-zzyzyz",};
137 137  
138   - run();
  138 + EXPECT_NO_THROW(run());
139 139  
140 140 EXPECT_EQ(2, app.count("z"));
141 141 EXPECT_EQ(1, app.count("y"));
... ... @@ -152,7 +152,7 @@ TEST_F(TApp, Positionals) {
152 152  
153 153 args = {"thing1","thing2"};
154 154  
155   - run();
  155 + EXPECT_NO_THROW(run());
156 156  
157 157 EXPECT_EQ(1, app.count("posit1"));
158 158 EXPECT_EQ(1, app.count("posit2"));
... ... @@ -169,7 +169,7 @@ TEST_F(TApp, MixedPositionals) {
169 169  
170 170 args = {"--posit2","thing2","7"};
171 171  
172   - run();
  172 + EXPECT_NO_THROW(run());
173 173  
174 174 EXPECT_EQ(1, app.count("posit2"));
175 175 EXPECT_EQ(1, app.count("posit1"));
... ... @@ -185,7 +185,7 @@ TEST_F(TApp, Reset) {
185 185  
186 186 args = {"--simple", "--double", "1.2"};
187 187  
188   - run();
  188 + EXPECT_NO_THROW(run());
189 189  
190 190 EXPECT_EQ(1, app.count("simple"));
191 191 EXPECT_EQ(1, app.count("d"));
... ... @@ -196,7 +196,7 @@ TEST_F(TApp, Reset) {
196 196 EXPECT_EQ(0, app.count("simple"));
197 197 EXPECT_EQ(0, app.count("d"));
198 198  
199   - run();
  199 + EXPECT_NO_THROW(run());
200 200  
201 201 EXPECT_EQ(1, app.count("simple"));
202 202 EXPECT_EQ(1, app.count("d"));
... ... @@ -213,7 +213,7 @@ TEST_F(TApp, FileNotExists) {
213 213 app.add_option("file", filename, "", CLI::NonexistentPath);
214 214 args = {"--file", myfile};
215 215  
216   - run();
  216 + EXPECT_NO_THROW(run());
217 217 EXPECT_EQ(myfile, filename);
218 218  
219 219 app.reset();
... ... @@ -227,23 +227,44 @@ TEST_F(TApp, FileNotExists) {
227 227 EXPECT_FALSE(CLI::_ExistingFile(myfile));
228 228 }
229 229  
  230 +TEST_F(TApp, FileExists) {
  231 + std::string myfile{"TestNonFileNotUsed.txt"};
  232 + EXPECT_FALSE(CLI::_ExistingFile(myfile));
  233 +
  234 + std::string filename = "Failed";
  235 + app.add_option("file", filename, "", CLI::ExistingFile);
  236 + args = {"--file", myfile};
  237 +
  238 + EXPECT_THROW(run(), CLI::ParseError);
  239 + EXPECT_EQ("Failed", filename);
  240 +
  241 + app.reset();
  242 +
  243 + bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
  244 + EXPECT_TRUE(ok);
  245 + EXPECT_NO_THROW(run());
  246 + EXPECT_EQ(myfile, filename);
  247 +
  248 + std::remove(myfile.c_str());
  249 + EXPECT_FALSE(CLI::_ExistingFile(myfile));
  250 +}
230 251 TEST_F(TApp, Basic) {
231 252 auto sub1 = app.add_subcommand("sub1");
232 253 auto sub2 = app.add_subcommand("sub2");
233 254  
234   - run();
  255 + EXPECT_NO_THROW(run());
235 256 EXPECT_EQ(nullptr, app.get_subcommand());
236 257  
237 258 app.reset();
238 259 args = {"sub1"};
239   - run();
  260 + EXPECT_NO_THROW(run());
240 261 EXPECT_EQ(sub1, app.get_subcommand());
241 262  
242 263 app.reset();
243 264 EXPECT_EQ(nullptr, app.get_subcommand());
244 265  
245 266 args = {"sub2"};
246   - run();
  267 + EXPECT_NO_THROW(run());
247 268 EXPECT_EQ(sub2, app.get_subcommand());
248 269 }
249 270  
... ... @@ -270,7 +291,7 @@ struct SubcommandProgram : public TApp {
270 291 TEST_F(SubcommandProgram, Working) {
271 292 args = {"-d", "start", "-ffilename"};
272 293  
273   - run();
  294 + EXPECT_NO_THROW(run());
274 295  
275 296 EXPECT_EQ(1, dummy);
276 297 EXPECT_EQ(start, app.get_subcommand());
... ...