Commit 55bfe49aa0faffbbdcb9df294011171ea055f9fd
1 parent
003ba325
options test
Showing
4 changed files
with
77 additions
and
11 deletions
CMakeLists.txt
| ... | ... | @@ -19,9 +19,9 @@ |
| 19 | 19 | # THE SOFTWARE. |
| 20 | 20 | |
| 21 | 21 | IF(BIICODE) |
| 22 | - include(biicode/cmake/tools) | |
| 23 | - ADD_BIICODE_TARGETS() | |
| 24 | - ACTIVATE_CPP11(INTERFACE ${BII_BLOCK_TARGET}) | |
| 22 | + include(biicode/cmake/tools) | |
| 23 | + ADD_BIICODE_TARGETS() | |
| 24 | + ACTIVATE_CPP11(INTERFACE ${BII_BLOCK_TARGET}) | |
| 25 | 25 | RETURN() |
| 26 | 26 | |
| 27 | 27 | ENDIF() |
| ... | ... | @@ -34,6 +34,7 @@ enable_testing() |
| 34 | 34 | set(VERSION "0.0.1") |
| 35 | 35 | |
| 36 | 36 | option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON) |
| 37 | +option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" OFF) | |
| 37 | 38 | |
| 38 | 39 | set(CXXOPTS_LINKER_LIBRARIES "") |
| 39 | 40 | ... | ... |
src/CMakeLists.txt
| ... | ... | @@ -19,13 +19,13 @@ |
| 19 | 19 | # THE SOFTWARE. |
| 20 | 20 | |
| 21 | 21 | if(CXXOPTS_BUILD_EXAMPLES) |
| 22 | - add_executable(example example.cpp cxxopts.hpp) | |
| 22 | + add_executable(example example.cpp cxxopts.hpp) | |
| 23 | 23 | |
| 24 | - if (MSVC) | |
| 25 | - target_compile_options(example PUBLIC /W2) | |
| 26 | - elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") | |
| 27 | - target_compile_options(example PUBLIC -std=c++11 -Wall) | |
| 28 | - endif() | |
| 24 | + if (MSVC) | |
| 25 | + target_compile_options(example PUBLIC /W2) | |
| 26 | + elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") | |
| 27 | + target_compile_options(example PUBLIC -std=c++11 -Wall) | |
| 28 | + endif() | |
| 29 | 29 | endif() |
| 30 | 30 | |
| 31 | 31 | install(FILES cxxopts.hpp DESTINATION include) | ... | ... |
test/CMakeLists.txt
| 1 | -add_executable(options_test options.cpp) | |
| 1 | +if (CXXOPTS_BUILD_TESTS) | |
| 2 | + add_executable(options_test options.cpp) | |
| 3 | + | |
| 4 | + if (MSVC) | |
| 5 | + target_compile_options(options_test PUBLIC /W2) | |
| 6 | + elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") | |
| 7 | + target_compile_options(options_test PUBLIC -std=c++11 -Wall) | |
| 8 | + endif() | |
| 9 | + | |
| 10 | + add_test(options options_test) | |
| 11 | +endif() | |
| 2 | 12 | |
| 3 | -add_test(options options_test) | ... | ... |
test/options.cpp
| 1 | 1 | #define CATCH_CONFIG_MAIN |
| 2 | 2 | #include "catch.hpp" |
| 3 | 3 | |
| 4 | +#include "../src/cxxopts.hpp" | |
| 5 | + | |
| 6 | +class Argv { | |
| 7 | + public: | |
| 8 | + | |
| 9 | + Argv(int n, const char** argv) | |
| 10 | + : m_argv(new char*[n]) | |
| 11 | + { | |
| 12 | + for (int i = 0; i != n; ++i) { | |
| 13 | + auto len = strlen(argv[i]) + 1; | |
| 14 | + auto ptr = std::unique_ptr<char[]>(new char[len]); | |
| 15 | + | |
| 16 | + strcpy(ptr.get(), argv[i]); | |
| 17 | + m_args.push_back(std::move(ptr)); | |
| 18 | + m_argv.get()[i] = m_args.back().get(); | |
| 19 | + } | |
| 20 | + } | |
| 21 | + | |
| 22 | + char** argv() const { | |
| 23 | + return m_argv.get(); | |
| 24 | + } | |
| 25 | + | |
| 26 | + private: | |
| 27 | + | |
| 28 | + std::vector<std::unique_ptr<char[]>> m_args; | |
| 29 | + std::unique_ptr<char*[]> m_argv; | |
| 30 | +}; | |
| 31 | + | |
| 4 | 32 | TEST_CASE("Basic options", "[options]") |
| 5 | 33 | { |
| 34 | + | |
| 35 | + cxxopts::Options options("tester", " - test basic options"); | |
| 36 | + | |
| 37 | + options.add_options() | |
| 38 | + ("long", "a long option") | |
| 39 | + ("s,short", "a short option") | |
| 40 | + ("value", "an option with a value", cxxopts::value<std::string>()) | |
| 41 | + ("a,av", "a short option with a value"); | |
| 42 | + | |
| 43 | + const char* args[] = { | |
| 44 | + "tester", | |
| 45 | + "--long", | |
| 46 | + "-s", | |
| 47 | + "--value", | |
| 48 | + "value", | |
| 49 | + "-a", | |
| 50 | + "b" | |
| 51 | + }; | |
| 52 | + | |
| 53 | + int argc = sizeof(args) / sizeof(args[0]); | |
| 54 | + | |
| 55 | + Argv argv(argc, args); | |
| 56 | + | |
| 57 | + char** actual_argv = argv.argv(); | |
| 58 | + | |
| 59 | + options.parse(argc, actual_argv); | |
| 60 | + | |
| 61 | + CHECK(options.count("long") == 1); | |
| 6 | 62 | } | ... | ... |