From 4b0f6dbfd64336d064049f8c663c0f510508cb21 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Fri, 27 Jan 2017 10:26:52 -0500 Subject: [PATCH] Nicer adding of tests --- cmake/AddGoogletest.cmake | 37 +++++++++++++++++++++ include/CLI.hpp | 18 +++++++++++ tests/CLItest.cpp | 67 +++++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 29 ++--------------- 4 files changed, 125 insertions(+), 26 deletions(-) create mode 100644 cmake/AddGoogletest.cmake diff --git a/cmake/AddGoogletest.cmake b/cmake/AddGoogletest.cmake new file mode 100644 index 00000000..6119c3b0 --- /dev/null +++ b/cmake/AddGoogletest.cmake @@ -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 "$") +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() + diff --git a/include/CLI.hpp b/include/CLI.hpp index c34d2eaa..d4038dba 100644 --- a/include/CLI.hpp +++ b/include/CLI.hpp @@ -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="") diff --git a/tests/CLItest.cpp b/tests/CLItest.cpp index bdd92ac0..e0ea3fe3 100644 --- a/tests/CLItest.cpp +++ b/tests/CLItest.cpp @@ -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()); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f220953e..5bf3d0c2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 "$") -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)