From 55bfe49aa0faffbbdcb9df294011171ea055f9fd Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Fri, 26 Aug 2016 08:22:04 +1000 Subject: [PATCH] options test --- CMakeLists.txt | 7 ++++--- src/CMakeLists.txt | 12 ++++++------ test/CMakeLists.txt | 13 +++++++++++-- test/options.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fffb795..08ef64f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,9 +19,9 @@ # THE SOFTWARE. IF(BIICODE) - include(biicode/cmake/tools) - ADD_BIICODE_TARGETS() - ACTIVATE_CPP11(INTERFACE ${BII_BLOCK_TARGET}) + include(biicode/cmake/tools) + ADD_BIICODE_TARGETS() + ACTIVATE_CPP11(INTERFACE ${BII_BLOCK_TARGET}) RETURN() ENDIF() @@ -34,6 +34,7 @@ enable_testing() set(VERSION "0.0.1") option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON) +option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" OFF) set(CXXOPTS_LINKER_LIBRARIES "") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b750ef5..dfd768d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,13 +19,13 @@ # THE SOFTWARE. if(CXXOPTS_BUILD_EXAMPLES) - add_executable(example example.cpp cxxopts.hpp) + add_executable(example example.cpp cxxopts.hpp) - if (MSVC) - target_compile_options(example PUBLIC /W2) - elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") - target_compile_options(example PUBLIC -std=c++11 -Wall) - endif() + if (MSVC) + target_compile_options(example PUBLIC /W2) + elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") + target_compile_options(example PUBLIC -std=c++11 -Wall) + endif() endif() install(FILES cxxopts.hpp DESTINATION include) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d2a0eff..686e1f6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,12 @@ -add_executable(options_test options.cpp) +if (CXXOPTS_BUILD_TESTS) + add_executable(options_test options.cpp) + + if (MSVC) + target_compile_options(options_test PUBLIC /W2) + elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") + target_compile_options(options_test PUBLIC -std=c++11 -Wall) + endif() + + add_test(options options_test) +endif() -add_test(options options_test) diff --git a/test/options.cpp b/test/options.cpp index 35f38ff..ec9be10 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -1,6 +1,62 @@ #define CATCH_CONFIG_MAIN #include "catch.hpp" +#include "../src/cxxopts.hpp" + +class Argv { + public: + + Argv(int n, const char** argv) + : m_argv(new char*[n]) + { + for (int i = 0; i != n; ++i) { + auto len = strlen(argv[i]) + 1; + auto ptr = std::unique_ptr(new char[len]); + + strcpy(ptr.get(), argv[i]); + m_args.push_back(std::move(ptr)); + m_argv.get()[i] = m_args.back().get(); + } + } + + char** argv() const { + return m_argv.get(); + } + + private: + + std::vector> m_args; + std::unique_ptr m_argv; +}; + TEST_CASE("Basic options", "[options]") { + + cxxopts::Options options("tester", " - test basic options"); + + options.add_options() + ("long", "a long option") + ("s,short", "a short option") + ("value", "an option with a value", cxxopts::value()) + ("a,av", "a short option with a value"); + + const char* args[] = { + "tester", + "--long", + "-s", + "--value", + "value", + "-a", + "b" + }; + + int argc = sizeof(args) / sizeof(args[0]); + + Argv argv(argc, args); + + char** actual_argv = argv.argv(); + + options.parse(argc, actual_argv); + + CHECK(options.count("long") == 1); } -- libgit2 0.21.4