diff options
author | Jarryd Beck <jarro.2783@gmail.com> | 2017-11-15 10:04:20 +0300 |
---|---|---|
committer | Jarryd Beck <jarro.2783@gmail.com> | 2017-11-15 10:04:20 +0300 |
commit | 8893afe13cc47dd0be4f25b5ae491e652c146098 (patch) | |
tree | 6c2c56bf4940eee0aec1ce397d9225a4f6756241 /test | |
parent | d7b930846cdccfc8bcecc4d7150ddcbadffac360 (diff) | |
parent | 70b9230639cad36c54d19aae37ae890e44939cdf (diff) |
Merge branch '2_0'v2.0.0
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | test/link_a.cpp | 6 | ||||
-rw-r--r-- | test/link_b.cpp | 1 | ||||
-rw-r--r-- | test/options.cpp | 174 |
4 files changed, 128 insertions, 56 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0920e90..1969545 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -29,4 +29,7 @@ if (CXXOPTS_BUILD_TESTS) "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" ) + + add_executable(link_test link_a.cpp link_b.cpp) + target_link_libraries(link_test cxxopts) endif() diff --git a/test/link_a.cpp b/test/link_a.cpp new file mode 100644 index 0000000..3611692 --- /dev/null +++ b/test/link_a.cpp @@ -0,0 +1,6 @@ +#include "cxxopts.hpp" + +int main(int, char**) +{ + return 0; +} diff --git a/test/link_b.cpp b/test/link_b.cpp new file mode 100644 index 0000000..e48e22a --- /dev/null +++ b/test/link_b.cpp @@ -0,0 +1 @@ +#include <cxxopts.hpp> diff --git a/test/options.cpp b/test/options.cpp index d428f9a..afd7292 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -71,17 +71,27 @@ TEST_CASE("Basic options", "[options]") char** actual_argv = argv.argv(); auto argc = argv.argc(); - options.parse(argc, actual_argv); - - CHECK(options.count("long") == 1); - CHECK(options.count("s") == 1); - CHECK(options.count("value") == 1); - CHECK(options.count("a") == 1); - CHECK(options["value"].as<std::string>() == "value"); - CHECK(options["a"].as<std::string>() == "b"); - CHECK(options.count("6") == 1); - CHECK(options.count("p") == 2); - CHECK(options.count("space") == 2); + auto result = options.parse(argc, actual_argv); + + CHECK(result.count("long") == 1); + CHECK(result.count("s") == 1); + CHECK(result.count("value") == 1); + CHECK(result.count("a") == 1); + CHECK(result["value"].as<std::string>() == "value"); + CHECK(result["a"].as<std::string>() == "b"); + CHECK(result.count("6") == 1); + CHECK(result.count("p") == 2); + CHECK(result.count("space") == 2); + + auto& arguments = result.arguments(); + REQUIRE(arguments.size() == 7); + CHECK(arguments[0].key() == "long"); + CHECK(arguments[0].value() == "true"); + CHECK(arguments[0].as<bool>() == true); + + CHECK(arguments[1].key() == "short"); + CHECK(arguments[2].key() == "value"); + CHECK(arguments[3].key() == "av"); } TEST_CASE("Short options", "[options]") @@ -96,36 +106,15 @@ TEST_CASE("Short options", "[options]") auto actual_argv = argv.argv(); auto argc = argv.argc(); - options.parse(argc, actual_argv); + auto result = options.parse(argc, actual_argv); - CHECK(options.count("a") == 1); - CHECK(options["a"].as<std::string>() == "value"); + CHECK(result.count("a") == 1); + CHECK(result["a"].as<std::string>() == "value"); REQUIRE_THROWS_AS(options.add_options()("", "nothing option"), cxxopts::invalid_option_format_error); } -TEST_CASE("Required arguments", "[options]") -{ - cxxopts::Options options("required", " - test required options"); - options.add_options() - ("one", "one option") - ("two", "second option") - ; - - Argv argv({ - "required", - "--one" - }); - - auto aargv = argv.argv(); - auto argc = argv.argc(); - - options.parse(argc, aargv); - REQUIRE_THROWS_AS(cxxopts::check_required(options, {"two"}), - cxxopts::option_required_exception); -} - TEST_CASE("No positional", "[positional]") { cxxopts::Options options("test_no_positional", @@ -135,7 +124,7 @@ TEST_CASE("No positional", "[positional]") char** argv = av.argv(); auto argc = av.argc(); - options.parse(argc, argv); + auto result = options.parse(argc, argv); REQUIRE(argc == 4); CHECK(strcmp(argv[1], "a") == 0); @@ -158,7 +147,7 @@ TEST_CASE("All positional", "[positional]") options.parse_positional("positional"); - options.parse(argc, argv); + auto result = options.parse(argc, argv); REQUIRE(argc == 1); REQUIRE(positional.size() == 3); @@ -186,14 +175,14 @@ TEST_CASE("Some positional explicit", "[positional]") char** argv = av.argv(); auto argc = av.argc(); - options.parse(argc, argv); + auto result = options.parse(argc, argv); CHECK(argc == 1); - CHECK(options.count("output")); - CHECK(options["input"].as<std::string>() == "b"); - CHECK(options["output"].as<std::string>() == "a"); + CHECK(result.count("output")); + CHECK(result["input"].as<std::string>() == "b"); + CHECK(result["output"].as<std::string>() == "a"); - auto& positional = options["positional"].as<std::vector<std::string>>(); + auto& positional = result["positional"].as<std::vector<std::string>>(); REQUIRE(positional.size() == 2); CHECK(positional[0] == "c"); @@ -234,10 +223,58 @@ TEST_CASE("Empty with implicit value", "[implicit]") char** argv = av.argv(); auto argc = av.argc(); - options.parse(argc, argv); + auto result = options.parse(argc, argv); - REQUIRE(options.count("implicit") == 1); - REQUIRE(options["implicit"].as<std::string>() == ""); + REQUIRE(result.count("implicit") == 1); + REQUIRE(result["implicit"].as<std::string>() == ""); +} + +TEST_CASE("Default values", "[default]") +{ + cxxopts::Options options("defaults", "has defaults"); + options.add_options() + ("default", "Has implicit", cxxopts::value<int>() + ->default_value("42")); + + SECTION("Sets defaults") { + Argv av({"implicit"}); + + char** argv = av.argv(); + auto argc = av.argc(); + + auto result = options.parse(argc, argv); + CHECK(result.count("default") == 1); + CHECK(result["default"].as<int>() == 42); + } + + SECTION("When values provided") { + Argv av({"implicit", "--default", "5"}); + + char** argv = av.argv(); + auto argc = av.argc(); + + auto result = options.parse(argc, argv); + CHECK(result.count("default") == 1); + CHECK(result["default"].as<int>() == 5); + } +} + +TEST_CASE("Parse into a reference", "[reference]") +{ + int value = 0; + + cxxopts::Options options("into_reference", "parses into a reference"); + options.add_options() + ("ref", "A reference", cxxopts::value(value)); + + Argv av({"into_reference", "--ref", "42"}); + + auto argv = av.argv(); + auto argc = av.argc(); + + auto result = options.parse(argc, argv); + CHECK(result.count("ref") == 1); + CHECK(value == 42); } TEST_CASE("Integers", "[options]") @@ -252,11 +289,12 @@ TEST_CASE("Integers", "[options]") auto argc = av.argc(); options.parse_positional("positional"); - options.parse(argc, argv); + auto result = options.parse(argc, argv); - REQUIRE(options.count("positional") == 7); + REQUIRE(result.count("positional") == 7); - auto& positional = options["positional"].as<std::vector<int>>(); + auto& positional = result["positional"].as<std::vector<int>>(); + REQUIRE(positional.size() == 7); CHECK(positional[0] == 5); CHECK(positional[1] == 6); CHECK(positional[2] == -6); @@ -295,11 +333,11 @@ TEST_CASE("Integer bounds", "[integer]") auto argc = av.argc(); options.parse_positional("positional"); - options.parse(argc, argv); + auto result = options.parse(argc, argv); - REQUIRE(options.count("positional") == 5); + REQUIRE(result.count("positional") == 5); - auto& positional = options["positional"].as<std::vector<int8_t>>(); + auto& positional = result["positional"].as<std::vector<int8_t>>(); CHECK(positional[0] == 127); CHECK(positional[1] == -128); CHECK(positional[2] == 0x7f); @@ -351,14 +389,14 @@ TEST_CASE("Floats", "[options]") auto argc = av.argc(); options.parse_positional("positional"); - options.parse(argc, argv); + auto result = options.parse(argc, argv); - REQUIRE(options.count("double") == 1); - REQUIRE(options.count("positional") == 4); + REQUIRE(result.count("double") == 1); + REQUIRE(result.count("positional") == 4); - CHECK(options["double"].as<double>() == 0.5); + CHECK(result["double"].as<double>() == 0.5); - auto& positional = options["positional"].as<std::vector<float>>(); + auto& positional = result["positional"].as<std::vector<float>>(); CHECK(positional[0] == 4); CHECK(positional[1] == -4); CHECK(positional[2] == 1.5e6); @@ -378,3 +416,27 @@ TEST_CASE("Invalid integers", "[integer]") { options.parse_positional("positional"); CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type); } + +TEST_CASE("Booleans", "[boolean]") { + cxxopts::Options options("parses_floats", "parses floats correctly"); + options.add_options() + ("bool", "A Boolean", cxxopts::value<bool>()) + ("debug", "Debugging", cxxopts::value<bool>()) + ("timing", "Timing", cxxopts::value<bool>()) + ; + + Argv av({"booleans", "--bool=false", "--debug", "true", "--timing"}); + + char** argv = av.argv(); + auto argc = av.argc(); + + auto result = options.parse(argc, argv); + + REQUIRE(result.count("bool") == 1); + REQUIRE(result.count("debug") == 1); + REQUIRE(result.count("timing") == 1); + + CHECK(result["bool"].as<bool>() == false); + CHECK(result["debug"].as<bool>() == true); + CHECK(result["timing"].as<bool>() == true); +} |