mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-30 12:43:52 +00:00
Nicer adding of tests
This commit is contained in:
parent
3eac26e2e1
commit
4b0f6dbfd6
37
cmake/AddGoogletest.cmake
Normal file
37
cmake/AddGoogletest.cmake
Normal file
@ -0,0 +1,37 @@
|
||||
#
|
||||
#
|
||||
# Downloads GTest and provides a helper macro to add tests. Add make check, as well, which
|
||||
# gives output on failed tests without having to set an environment variable.
|
||||
#
|
||||
#
|
||||
|
||||
include(DownloadProject)
|
||||
download_project(PROJ googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG release-1.8.0
|
||||
UPDATE_DISCONNECTED 1
|
||||
QUIET
|
||||
)
|
||||
|
||||
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
|
||||
|
||||
if (CMAKE_CONFIGURATION_TYPES)
|
||||
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
|
||||
--force-new-ctest-process --output-on-failure
|
||||
--build-config "$<CONFIGURATION>")
|
||||
else()
|
||||
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
|
||||
--force-new-ctest-process --output-on-failure)
|
||||
endif()
|
||||
|
||||
include_directories(${gtest_SOURCE_DIR}/include)
|
||||
|
||||
# More modern way to do the last line, less messy but needs newish CMake:
|
||||
# target_include_directories(gtest INTERFACE ${gtest_SOURCE_DIR}/include)
|
||||
|
||||
# Target must already exist
|
||||
macro(add_gtest TESTNAME)
|
||||
target_link_libraries(${TESTNAME} gtest gtest_main)
|
||||
add_test(${TESTNAME} ${TESTNAME})
|
||||
endmacro()
|
||||
|
@ -158,6 +158,10 @@ public:
|
||||
std::tie(sname, lname) = split(name);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
results.clear();
|
||||
}
|
||||
|
||||
bool required() const {
|
||||
return opts.required;
|
||||
}
|
||||
@ -320,6 +324,20 @@ protected:
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/// Reset the parsed data
|
||||
void reset() {
|
||||
|
||||
parsed = false;
|
||||
subcommand = nullptr;
|
||||
|
||||
for(Option& opt : options) {
|
||||
opt.clear();
|
||||
}
|
||||
for(App* app : subcommands) {
|
||||
app->reset();
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new program. Pass in the same arguments as main(), along with a help string.
|
||||
App(std::string prog_discription="")
|
||||
|
@ -125,3 +125,70 @@ TEST_F(TApp, LotsOfFlags) {
|
||||
EXPECT_EQ(1, app.count("b"));
|
||||
EXPECT_EQ(1, app.count("A"));
|
||||
}
|
||||
|
||||
TEST_F(TApp, ShortOpts) {
|
||||
|
||||
unsigned long long funnyint;
|
||||
std::string someopt;
|
||||
app.add_flag("z", funnyint);
|
||||
app.add_option("y", someopt);
|
||||
|
||||
args = {"-zzyzyz",};
|
||||
|
||||
run();
|
||||
|
||||
EXPECT_EQ(2, app.count("z"));
|
||||
EXPECT_EQ(1, app.count("y"));
|
||||
EXPECT_EQ((unsigned long long) 2, funnyint);
|
||||
EXPECT_EQ("zyz", someopt);
|
||||
}
|
||||
|
||||
TEST_F(TApp, Reset) {
|
||||
|
||||
app.add_flag("simple");
|
||||
double doub;
|
||||
app.add_option("d,double", doub);
|
||||
|
||||
args = {"--simple", "--double", "1.2"};
|
||||
|
||||
run();
|
||||
|
||||
EXPECT_EQ(1, app.count("simple"));
|
||||
EXPECT_EQ(1, app.count("d"));
|
||||
EXPECT_FLOAT_EQ(1.2, doub);
|
||||
|
||||
app.reset();
|
||||
|
||||
EXPECT_EQ(0, app.count("simple"));
|
||||
EXPECT_EQ(0, app.count("d"));
|
||||
|
||||
run();
|
||||
|
||||
EXPECT_EQ(1, app.count("simple"));
|
||||
EXPECT_EQ(1, app.count("d"));
|
||||
EXPECT_FLOAT_EQ(1.2, doub);
|
||||
|
||||
}
|
||||
|
||||
struct TSubcom : public TApp {
|
||||
};
|
||||
|
||||
TEST_F(TSubcom, Basic) {
|
||||
auto sub1 = app.add_subcommand("sub1");
|
||||
auto sub2 = app.add_subcommand("sub2");
|
||||
|
||||
run();
|
||||
EXPECT_EQ(nullptr, app.get_subcommand());
|
||||
|
||||
app.reset();
|
||||
args = {"sub1"};
|
||||
run();
|
||||
EXPECT_EQ(sub1, app.get_subcommand());
|
||||
|
||||
app.reset();
|
||||
EXPECT_EQ(nullptr, app.get_subcommand());
|
||||
|
||||
args = {"sub2"};
|
||||
run();
|
||||
EXPECT_EQ(sub2, app.get_subcommand());
|
||||
}
|
||||
|
@ -1,30 +1,7 @@
|
||||
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
|
||||
|
||||
include(DownloadProject)
|
||||
download_project(PROJ googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG release-1.8.0
|
||||
UPDATE_DISCONNECTED 1
|
||||
QUIET
|
||||
)
|
||||
|
||||
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
|
||||
|
||||
if (CMAKE_CONFIGURATION_TYPES)
|
||||
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
|
||||
--force-new-ctest-process --output-on-failure
|
||||
--build-config "$<CONFIGURATION>")
|
||||
else()
|
||||
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
|
||||
--force-new-ctest-process --output-on-failure)
|
||||
endif()
|
||||
|
||||
include_directories(${gtest_SOURCE_DIR}/include)
|
||||
include(AddGoogletest)
|
||||
|
||||
add_executable(CLITest CLITest.cpp ${headers})
|
||||
target_link_libraries(CLITest gtest gtest_main)
|
||||
add_test(CLITest CLITest)
|
||||
add_gtest(CLITest)
|
||||
|
||||
add_executable(SmallTest SmallTest.cpp ${headers})
|
||||
target_link_libraries(SmallTest gtest gtest_main)
|
||||
add_test(SmallTest SmallTest)
|
||||
add_gtest(SmallTest)
|
||||
|
Loading…
x
Reference in New Issue
Block a user