options test
This commit is contained in:
parent
003ba3253e
commit
55bfe49aa0
@ -34,6 +34,7 @@ enable_testing()
|
|||||||
set(VERSION "0.0.1")
|
set(VERSION "0.0.1")
|
||||||
|
|
||||||
option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON)
|
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 "")
|
set(CXXOPTS_LINKER_LIBRARIES "")
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,12 @@
|
|||||||
|
if (CXXOPTS_BUILD_TESTS)
|
||||||
add_executable(options_test options.cpp)
|
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)
|
add_test(options options_test)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,62 @@
|
|||||||
#define CATCH_CONFIG_MAIN
|
#define CATCH_CONFIG_MAIN
|
||||||
#include "catch.hpp"
|
#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<char[]>(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<std::unique_ptr<char[]>> m_args;
|
||||||
|
std::unique_ptr<char*[]> m_argv;
|
||||||
|
};
|
||||||
|
|
||||||
TEST_CASE("Basic options", "[options]")
|
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<std::string>())
|
||||||
|
("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);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user