Commit 83eb7eb84bb5c36322262777ae9faad0604a7fdf

Authored by Henry Fredrick Schreiner
1 parent f4bf6d72

Splitting up tests

tests/CLITest.cpp renamed to tests/AppTest.cpp
1 -#ifdef CLI_SINGLE_FILE  
2 -#include "CLI11.hpp"  
3 -#else  
4 -#include "CLI/CLI.hpp"  
5 -#endif  
6 -  
7 -#include "gtest/gtest.h"  
8 -#include <fstream>  
9 -  
10 -typedef std::vector<std::string> input_t;  
11 -  
12 -TEST(Basic, Empty) {  
13 -  
14 - {  
15 - CLI::App app;  
16 - input_t simpleput;  
17 - app.parse(simpleput);  
18 - }  
19 - {  
20 - CLI::App app;  
21 - input_t spare = {"spare"};  
22 - EXPECT_THROW(app.parse(spare), CLI::PositionalError);  
23 - }  
24 - {  
25 - CLI::App app;  
26 - input_t simpleput;  
27 - app.parse(simpleput);  
28 - }  
29 -}  
30 -  
31 -struct TApp : public ::testing::Test {  
32 - CLI::App app{"My Test Program"};  
33 - input_t args;  
34 -  
35 - void run() {  
36 - input_t newargs = args;  
37 - std::reverse(std::begin(newargs), std::end(newargs));  
38 - app.parse(newargs);  
39 - }  
40 -  
41 -}; 1 +#include "app_helper.hpp"
42 2
43 TEST_F(TApp, OneFlagShort) { 3 TEST_F(TApp, OneFlagShort) {
44 app.add_flag("-c,--count"); 4 app.add_flag("-c,--count");
@@ -393,148 +353,6 @@ TEST_F(TApp, VectorFancyOpts) { @@ -393,148 +353,6 @@ TEST_F(TApp, VectorFancyOpts) {
393 EXPECT_THROW(run(), CLI::ParseError); 353 EXPECT_THROW(run(), CLI::ParseError);
394 } 354 }
395 355
396 -struct TIni : public TApp {  
397 -  
398 - std::ofstream f{"IniParseSimple.ini"};  
399 -  
400 - void run() {  
401 - f.close();  
402 - TApp::run();  
403 - }  
404 -  
405 - ~TIni() {  
406 - f.close();  
407 - std::remove("IniParseSimple.ini");  
408 - }  
409 -  
410 -};  
411 -  
412 -  
413 -  
414 -TEST_F(TIni, IniParseSimple) {  
415 -  
416 - int x;  
417 - std::string y;  
418 -  
419 - app.add_option("--something", x);  
420 - app.add_option("--else", y);  
421 -  
422 - app.add_config("--config","", "", true);  
423 -  
424 - args = {"--config=IniParseSimple.ini"};  
425 -  
426 -  
427 - ASSERT_TRUE(f.good());  
428 -  
429 - f << "[default]" << std::endl;  
430 - f << "" << std::endl;  
431 - f << "something=7" << std::endl;  
432 - f << "else=seven" << std::endl;  
433 -  
434 - //EXPECT_NO_THROW  
435 - (run());  
436 -  
437 - EXPECT_EQ(7, x);  
438 - EXPECT_EQ("seven", y);  
439 -}  
440 -  
441 -  
442 -TEST(Ini, IniDoubleAdd) {  
443 -  
444 - CLI::App app;  
445 -  
446 - app.add_config("--first");  
447 - app.add_config("--second");  
448 -  
449 - EXPECT_NO_THROW(app.count("--second"));  
450 - EXPECT_THROW(app.count("--first"), CLI::OptionNotFound);  
451 -  
452 -}  
453 -TEST_F(TApp, BasicSubcommands) {  
454 - auto sub1 = app.add_subcommand("sub1");  
455 - auto sub2 = app.add_subcommand("sub2");  
456 -  
457 - EXPECT_NO_THROW(run());  
458 - EXPECT_EQ(nullptr, app.get_subcommand());  
459 -  
460 - app.reset();  
461 - args = {"sub1"};  
462 - EXPECT_NO_THROW(run());  
463 - EXPECT_EQ(sub1, app.get_subcommand());  
464 -  
465 - app.reset();  
466 - EXPECT_EQ(nullptr, app.get_subcommand());  
467 -  
468 - args = {"sub2"};  
469 - EXPECT_NO_THROW(run());  
470 - EXPECT_EQ(sub2, app.get_subcommand());  
471 -}  
472 -  
473 -  
474 -TEST_F(TApp, Callbacks) {  
475 - auto sub1 = app.add_subcommand("sub1");  
476 - sub1->set_callback([](){  
477 - throw CLI::Success();  
478 - });  
479 - auto sub2 = app.add_subcommand("sub2");  
480 - bool val = false;  
481 - sub2->set_callback([&val](){  
482 - val = true;  
483 - });  
484 -  
485 - app.reset();  
486 - args = {"sub2"};  
487 - EXPECT_FALSE(val);  
488 - EXPECT_NO_THROW(run());  
489 - EXPECT_TRUE(val);  
490 -  
491 -}  
492 -  
493 -// TODO: Add directory test  
494 -  
495 -  
496 -  
497 -struct SubcommandProgram : public TApp {  
498 -  
499 - CLI::App* start;  
500 - CLI::App* stop;  
501 -  
502 - int dummy;  
503 - std::string file;  
504 - int count;  
505 -  
506 - SubcommandProgram() {  
507 - start = app.add_subcommand("start", "Start prog");  
508 - stop = app.add_subcommand("stop", "Stop prog");  
509 -  
510 - app.add_flag("-d", dummy, "My dummy var");  
511 - start->add_option("-f,--file", file, "File name");  
512 - stop->add_flag("-c,--count", count, "Some flag opt");  
513 - }  
514 -};  
515 -  
516 -TEST_F(SubcommandProgram, Working) {  
517 - args = {"-d", "start", "-ffilename"};  
518 -  
519 - EXPECT_NO_THROW(run());  
520 -  
521 - EXPECT_EQ(1, dummy);  
522 - EXPECT_EQ(start, app.get_subcommand());  
523 - EXPECT_EQ("filename", file);  
524 -}  
525 -  
526 -  
527 -TEST_F(SubcommandProgram, Spare) {  
528 - args = {"extra", "-d", "start", "-ffilename"};  
529 -  
530 - EXPECT_THROW(run(), CLI::PositionalError);  
531 -}  
532 -  
533 -TEST_F(SubcommandProgram, SpareSub) {  
534 - args = {"-d", "start", "spare", "-ffilename"};  
535 -  
536 - EXPECT_THROW(run(), CLI::PositionalError);  
537 -}  
538 356
539 357
540 // TODO: add tests for requires, excludes, envname 358 // TODO: add tests for requires, excludes, envname
tests/CMakeLists.txt
1 include(AddGoogletest) 1 include(AddGoogletest)
2 2
3 -set(CLI_TESTS SmallTest IniTest CLITest HelpTest) 3 +set(CLI_TESTS
  4 + HelpersTest
  5 + IniTest
  6 + SimpleTest
  7 + AppTest
  8 + SubcommandTest
  9 + HelpTest)
  10 +
  11 +# Only affects current directory, so safe
  12 +include_directories(${CMAKE_CURRENT_SOURCE_DIR})
4 13
5 foreach(T ${CLI_TESTS}) 14 foreach(T ${CLI_TESTS})
6 15
tests/SmallTest.cpp renamed to tests/HelpersTest.cpp
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 #else 3 #else
4 #include "CLI/CLI.hpp" 4 #include "CLI/CLI.hpp"
5 #endif 5 #endif
  6 +
6 #include "gtest/gtest.h" 7 #include "gtest/gtest.h"
7 #include <cstdio> 8 #include <cstdio>
8 #include <fstream> 9 #include <fstream>
tests/SimpleTest.cpp 0 → 100644
  1 +#ifdef CLI_SINGLE_FILE
  2 +#include "CLI11.hpp"
  3 +#else
  4 +#include "CLI/CLI.hpp"
  5 +#endif
  6 +
  7 +#include "gtest/gtest.h"
  8 +
  9 +typedef std::vector<std::string> input_t;
  10 +
  11 +TEST(Basic, Empty) {
  12 +
  13 + {
  14 + CLI::App app;
  15 + input_t simpleput;
  16 + app.parse(simpleput);
  17 + }
  18 + {
  19 + CLI::App app;
  20 + input_t spare = {"spare"};
  21 + EXPECT_THROW(app.parse(spare), CLI::PositionalError);
  22 + }
  23 + {
  24 + CLI::App app;
  25 + input_t simpleput;
  26 + app.parse(simpleput);
  27 + }
  28 +}
  29 +
  30 +
tests/SubcommandTest.cpp 0 → 100644
  1 +#include "app_helper.hpp"
  2 +
  3 +TEST_F(TApp, BasicSubcommands) {
  4 + auto sub1 = app.add_subcommand("sub1");
  5 + auto sub2 = app.add_subcommand("sub2");
  6 +
  7 + EXPECT_NO_THROW(run());
  8 + EXPECT_EQ(nullptr, app.get_subcommand());
  9 +
  10 + app.reset();
  11 + args = {"sub1"};
  12 + EXPECT_NO_THROW(run());
  13 + EXPECT_EQ(sub1, app.get_subcommand());
  14 +
  15 + app.reset();
  16 + EXPECT_EQ(nullptr, app.get_subcommand());
  17 +
  18 + args = {"sub2"};
  19 + EXPECT_NO_THROW(run());
  20 + EXPECT_EQ(sub2, app.get_subcommand());
  21 +}
  22 +
  23 +
  24 +TEST_F(TApp, Callbacks) {
  25 + auto sub1 = app.add_subcommand("sub1");
  26 + sub1->set_callback([](){
  27 + throw CLI::Success();
  28 + });
  29 + auto sub2 = app.add_subcommand("sub2");
  30 + bool val = false;
  31 + sub2->set_callback([&val](){
  32 + val = true;
  33 + });
  34 +
  35 + app.reset();
  36 + args = {"sub2"};
  37 + EXPECT_FALSE(val);
  38 + EXPECT_NO_THROW(run());
  39 + EXPECT_TRUE(val);
  40 +
  41 +}
  42 +
  43 +// TODO: Add directory test
  44 +
  45 +
  46 +
  47 +struct SubcommandProgram : public TApp {
  48 +
  49 + CLI::App* start;
  50 + CLI::App* stop;
  51 +
  52 + int dummy;
  53 + std::string file;
  54 + int count;
  55 +
  56 + SubcommandProgram() {
  57 + start = app.add_subcommand("start", "Start prog");
  58 + stop = app.add_subcommand("stop", "Stop prog");
  59 +
  60 + app.add_flag("-d", dummy, "My dummy var");
  61 + start->add_option("-f,--file", file, "File name");
  62 + stop->add_flag("-c,--count", count, "Some flag opt");
  63 + }
  64 +};
  65 +
  66 +TEST_F(SubcommandProgram, Working) {
  67 + args = {"-d", "start", "-ffilename"};
  68 +
  69 + EXPECT_NO_THROW(run());
  70 +
  71 + EXPECT_EQ(1, dummy);
  72 + EXPECT_EQ(start, app.get_subcommand());
  73 + EXPECT_EQ("filename", file);
  74 +}
  75 +
  76 +
  77 +TEST_F(SubcommandProgram, Spare) {
  78 + args = {"extra", "-d", "start", "-ffilename"};
  79 +
  80 + EXPECT_THROW(run(), CLI::PositionalError);
  81 +}
  82 +
  83 +TEST_F(SubcommandProgram, SpareSub) {
  84 + args = {"-d", "start", "spare", "-ffilename"};
  85 +
  86 + EXPECT_THROW(run(), CLI::PositionalError);
  87 +}
  88 +
  89 +
tests/app_helper.hpp 0 → 100644
  1 +#pragma once
  2 +
  3 +#ifdef CLI_SINGLE_FILE
  4 +#include "CLI11.hpp"
  5 +#else
  6 +#include "CLI/CLI.hpp"
  7 +#endif
  8 +
  9 +#include "gtest/gtest.h"
  10 +#include <iostream>
  11 +
  12 +typedef std::vector<std::string> input_t;
  13 +
  14 +struct TApp : public ::testing::Test {
  15 + CLI::App app{"My Test Program"};
  16 + input_t args;
  17 +
  18 + void run() {
  19 + input_t newargs = args;
  20 + std::reverse(std::begin(newargs), std::end(newargs));
  21 + app.parse(newargs);
  22 + }
  23 +
  24 +};
  25 +
  26 +
  27 +