mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-07 15:33:51 +00:00
Splitting up tests
This commit is contained in:
parent
f4bf6d7226
commit
83eb7eb84b
@ -1,44 +1,4 @@
|
||||
#ifdef CLI_SINGLE_FILE
|
||||
#include "CLI11.hpp"
|
||||
#else
|
||||
#include "CLI/CLI.hpp"
|
||||
#endif
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include <fstream>
|
||||
|
||||
typedef std::vector<std::string> input_t;
|
||||
|
||||
TEST(Basic, Empty) {
|
||||
|
||||
{
|
||||
CLI::App app;
|
||||
input_t simpleput;
|
||||
app.parse(simpleput);
|
||||
}
|
||||
{
|
||||
CLI::App app;
|
||||
input_t spare = {"spare"};
|
||||
EXPECT_THROW(app.parse(spare), CLI::PositionalError);
|
||||
}
|
||||
{
|
||||
CLI::App app;
|
||||
input_t simpleput;
|
||||
app.parse(simpleput);
|
||||
}
|
||||
}
|
||||
|
||||
struct TApp : public ::testing::Test {
|
||||
CLI::App app{"My Test Program"};
|
||||
input_t args;
|
||||
|
||||
void run() {
|
||||
input_t newargs = args;
|
||||
std::reverse(std::begin(newargs), std::end(newargs));
|
||||
app.parse(newargs);
|
||||
}
|
||||
|
||||
};
|
||||
#include "app_helper.hpp"
|
||||
|
||||
TEST_F(TApp, OneFlagShort) {
|
||||
app.add_flag("-c,--count");
|
||||
@ -393,148 +353,6 @@ TEST_F(TApp, VectorFancyOpts) {
|
||||
EXPECT_THROW(run(), CLI::ParseError);
|
||||
}
|
||||
|
||||
struct TIni : public TApp {
|
||||
|
||||
std::ofstream f{"IniParseSimple.ini"};
|
||||
|
||||
void run() {
|
||||
f.close();
|
||||
TApp::run();
|
||||
}
|
||||
|
||||
~TIni() {
|
||||
f.close();
|
||||
std::remove("IniParseSimple.ini");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
TEST_F(TIni, IniParseSimple) {
|
||||
|
||||
int x;
|
||||
std::string y;
|
||||
|
||||
app.add_option("--something", x);
|
||||
app.add_option("--else", y);
|
||||
|
||||
app.add_config("--config","", "", true);
|
||||
|
||||
args = {"--config=IniParseSimple.ini"};
|
||||
|
||||
|
||||
ASSERT_TRUE(f.good());
|
||||
|
||||
f << "[default]" << std::endl;
|
||||
f << "" << std::endl;
|
||||
f << "something=7" << std::endl;
|
||||
f << "else=seven" << std::endl;
|
||||
|
||||
//EXPECT_NO_THROW
|
||||
(run());
|
||||
|
||||
EXPECT_EQ(7, x);
|
||||
EXPECT_EQ("seven", y);
|
||||
}
|
||||
|
||||
|
||||
TEST(Ini, IniDoubleAdd) {
|
||||
|
||||
CLI::App app;
|
||||
|
||||
app.add_config("--first");
|
||||
app.add_config("--second");
|
||||
|
||||
EXPECT_NO_THROW(app.count("--second"));
|
||||
EXPECT_THROW(app.count("--first"), CLI::OptionNotFound);
|
||||
|
||||
}
|
||||
TEST_F(TApp, BasicSubcommands) {
|
||||
auto sub1 = app.add_subcommand("sub1");
|
||||
auto sub2 = app.add_subcommand("sub2");
|
||||
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_EQ(nullptr, app.get_subcommand());
|
||||
|
||||
app.reset();
|
||||
args = {"sub1"};
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_EQ(sub1, app.get_subcommand());
|
||||
|
||||
app.reset();
|
||||
EXPECT_EQ(nullptr, app.get_subcommand());
|
||||
|
||||
args = {"sub2"};
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_EQ(sub2, app.get_subcommand());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TApp, Callbacks) {
|
||||
auto sub1 = app.add_subcommand("sub1");
|
||||
sub1->set_callback([](){
|
||||
throw CLI::Success();
|
||||
});
|
||||
auto sub2 = app.add_subcommand("sub2");
|
||||
bool val = false;
|
||||
sub2->set_callback([&val](){
|
||||
val = true;
|
||||
});
|
||||
|
||||
app.reset();
|
||||
args = {"sub2"};
|
||||
EXPECT_FALSE(val);
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_TRUE(val);
|
||||
|
||||
}
|
||||
|
||||
// TODO: Add directory test
|
||||
|
||||
|
||||
|
||||
struct SubcommandProgram : public TApp {
|
||||
|
||||
CLI::App* start;
|
||||
CLI::App* stop;
|
||||
|
||||
int dummy;
|
||||
std::string file;
|
||||
int count;
|
||||
|
||||
SubcommandProgram() {
|
||||
start = app.add_subcommand("start", "Start prog");
|
||||
stop = app.add_subcommand("stop", "Stop prog");
|
||||
|
||||
app.add_flag("-d", dummy, "My dummy var");
|
||||
start->add_option("-f,--file", file, "File name");
|
||||
stop->add_flag("-c,--count", count, "Some flag opt");
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(SubcommandProgram, Working) {
|
||||
args = {"-d", "start", "-ffilename"};
|
||||
|
||||
EXPECT_NO_THROW(run());
|
||||
|
||||
EXPECT_EQ(1, dummy);
|
||||
EXPECT_EQ(start, app.get_subcommand());
|
||||
EXPECT_EQ("filename", file);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(SubcommandProgram, Spare) {
|
||||
args = {"extra", "-d", "start", "-ffilename"};
|
||||
|
||||
EXPECT_THROW(run(), CLI::PositionalError);
|
||||
}
|
||||
|
||||
TEST_F(SubcommandProgram, SpareSub) {
|
||||
args = {"-d", "start", "spare", "-ffilename"};
|
||||
|
||||
EXPECT_THROW(run(), CLI::PositionalError);
|
||||
}
|
||||
|
||||
|
||||
// TODO: add tests for requires, excludes, envname
|
@ -1,6 +1,15 @@
|
||||
include(AddGoogletest)
|
||||
|
||||
set(CLI_TESTS SmallTest IniTest CLITest HelpTest)
|
||||
set(CLI_TESTS
|
||||
HelpersTest
|
||||
IniTest
|
||||
SimpleTest
|
||||
AppTest
|
||||
SubcommandTest
|
||||
HelpTest)
|
||||
|
||||
# Only affects current directory, so safe
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
foreach(T ${CLI_TESTS})
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
#else
|
||||
#include "CLI/CLI.hpp"
|
||||
#endif
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
30
tests/SimpleTest.cpp
Normal file
30
tests/SimpleTest.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#ifdef CLI_SINGLE_FILE
|
||||
#include "CLI11.hpp"
|
||||
#else
|
||||
#include "CLI/CLI.hpp"
|
||||
#endif
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
typedef std::vector<std::string> input_t;
|
||||
|
||||
TEST(Basic, Empty) {
|
||||
|
||||
{
|
||||
CLI::App app;
|
||||
input_t simpleput;
|
||||
app.parse(simpleput);
|
||||
}
|
||||
{
|
||||
CLI::App app;
|
||||
input_t spare = {"spare"};
|
||||
EXPECT_THROW(app.parse(spare), CLI::PositionalError);
|
||||
}
|
||||
{
|
||||
CLI::App app;
|
||||
input_t simpleput;
|
||||
app.parse(simpleput);
|
||||
}
|
||||
}
|
||||
|
||||
|
89
tests/SubcommandTest.cpp
Normal file
89
tests/SubcommandTest.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
#include "app_helper.hpp"
|
||||
|
||||
TEST_F(TApp, BasicSubcommands) {
|
||||
auto sub1 = app.add_subcommand("sub1");
|
||||
auto sub2 = app.add_subcommand("sub2");
|
||||
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_EQ(nullptr, app.get_subcommand());
|
||||
|
||||
app.reset();
|
||||
args = {"sub1"};
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_EQ(sub1, app.get_subcommand());
|
||||
|
||||
app.reset();
|
||||
EXPECT_EQ(nullptr, app.get_subcommand());
|
||||
|
||||
args = {"sub2"};
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_EQ(sub2, app.get_subcommand());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TApp, Callbacks) {
|
||||
auto sub1 = app.add_subcommand("sub1");
|
||||
sub1->set_callback([](){
|
||||
throw CLI::Success();
|
||||
});
|
||||
auto sub2 = app.add_subcommand("sub2");
|
||||
bool val = false;
|
||||
sub2->set_callback([&val](){
|
||||
val = true;
|
||||
});
|
||||
|
||||
app.reset();
|
||||
args = {"sub2"};
|
||||
EXPECT_FALSE(val);
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_TRUE(val);
|
||||
|
||||
}
|
||||
|
||||
// TODO: Add directory test
|
||||
|
||||
|
||||
|
||||
struct SubcommandProgram : public TApp {
|
||||
|
||||
CLI::App* start;
|
||||
CLI::App* stop;
|
||||
|
||||
int dummy;
|
||||
std::string file;
|
||||
int count;
|
||||
|
||||
SubcommandProgram() {
|
||||
start = app.add_subcommand("start", "Start prog");
|
||||
stop = app.add_subcommand("stop", "Stop prog");
|
||||
|
||||
app.add_flag("-d", dummy, "My dummy var");
|
||||
start->add_option("-f,--file", file, "File name");
|
||||
stop->add_flag("-c,--count", count, "Some flag opt");
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(SubcommandProgram, Working) {
|
||||
args = {"-d", "start", "-ffilename"};
|
||||
|
||||
EXPECT_NO_THROW(run());
|
||||
|
||||
EXPECT_EQ(1, dummy);
|
||||
EXPECT_EQ(start, app.get_subcommand());
|
||||
EXPECT_EQ("filename", file);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(SubcommandProgram, Spare) {
|
||||
args = {"extra", "-d", "start", "-ffilename"};
|
||||
|
||||
EXPECT_THROW(run(), CLI::PositionalError);
|
||||
}
|
||||
|
||||
TEST_F(SubcommandProgram, SpareSub) {
|
||||
args = {"-d", "start", "spare", "-ffilename"};
|
||||
|
||||
EXPECT_THROW(run(), CLI::PositionalError);
|
||||
}
|
||||
|
||||
|
27
tests/app_helper.hpp
Normal file
27
tests/app_helper.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef CLI_SINGLE_FILE
|
||||
#include "CLI11.hpp"
|
||||
#else
|
||||
#include "CLI/CLI.hpp"
|
||||
#endif
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include <iostream>
|
||||
|
||||
typedef std::vector<std::string> input_t;
|
||||
|
||||
struct TApp : public ::testing::Test {
|
||||
CLI::App app{"My Test Program"};
|
||||
input_t args;
|
||||
|
||||
void run() {
|
||||
input_t newargs = args;
|
||||
std::reverse(std::begin(newargs), std::end(newargs));
|
||||
app.parse(newargs);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user