From 1b2af21a2db77b9ccf0b9432b38df6936b7068b1 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 26 Jan 2017 17:09:56 -0500 Subject: [PATCH] Added more tests, some fixes --- include/CLI.hpp | 5 ++- tests/CLItest.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++- tests/CMakeLists.txt | 9 ++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/include/CLI.hpp b/include/CLI.hpp index e89f244b..c34d2eaa 100644 --- a/include/CLI.hpp +++ b/include/CLI.hpp @@ -289,7 +289,10 @@ lexical_cast(std::string input, T& output) { // String and similar template -bool lexical_cast(std::string input, T& output) { +typename std::enable_if< + !std::is_floating_point::value + && !std::is_integral::value, bool>::type +lexical_cast(std::string input, T& output) { logit("Direct lexical cast: " + input); output = input; return true; diff --git a/tests/CLItest.cpp b/tests/CLItest.cpp index 86209eea..b7ff36dd 100644 --- a/tests/CLItest.cpp +++ b/tests/CLItest.cpp @@ -36,9 +36,79 @@ struct TApp : public ::testing::Test { }; -TEST_F(TApp, AFewArgs) { +TEST_F(TApp, OneFlagShort) { app.add_flag("c,count"); args = {"-c"}; run(); + EXPECT_EQ(1, app.count("c")); EXPECT_EQ(1, app.count("count")); } + +TEST_F(TApp, OneFlagLong) { + app.add_flag("c,count"); + args = {"--count"}; + run(); + EXPECT_EQ(1, app.count("c")); + EXPECT_EQ(1, app.count("count")); +} + +TEST_F(TApp, OneFlagRef) { + int ref; + app.add_flag("c,count", ref); + args = {"--count"}; + run(); + EXPECT_EQ(1, app.count("c")); + EXPECT_EQ(1, app.count("count")); + EXPECT_EQ(1, ref); +} + +TEST_F(TApp, OneString) { + std::string str; + app.add_option("s,string", str); + args = {"--string", "mystring"}; + run(); + EXPECT_EQ(1, app.count("s")); + EXPECT_EQ(1, app.count("string")); + EXPECT_EQ(str, "mystring"); +} + + +TEST_F(TApp, TogetherInt) { + int i; + app.add_option("i,int", i); + args = {"-i4"}; + run(); + EXPECT_EQ(1, app.count("int")); + EXPECT_EQ(1, app.count("i")); + EXPECT_EQ(i, 4); +} + +TEST_F(TApp, SepInt) { + int i; + app.add_option("i,int", i); + args = {"-i","4"}; + run(); + EXPECT_EQ(1, app.count("int")); + EXPECT_EQ(1, app.count("i")); + EXPECT_EQ(i, 4); +} + +TEST_F(TApp, OneStringAgain) { + std::string str; + app.add_option("s,string", str); + args = {"--string", "mystring"}; + run(); + EXPECT_EQ(1, app.count("s")); + EXPECT_EQ(1, app.count("string")); + EXPECT_EQ(str, "mystring"); +} + + +TEST_F(TApp, DefaultStringAgain) { + std::string str = "previous"; + app.add_option("s,string", str); + run(); + EXPECT_EQ(0, app.count("s")); + EXPECT_EQ(0, app.count("string")); + EXPECT_EQ(str, "previous"); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bf8494b7..f447160e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,15 @@ cmake_minimum_required(VERSION 2.8 FATAL_ERROR) add_subdirectory(googletest) +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) add_executable(CLITest CLITest.cpp ${headers})