From 463aded16422ebb04e16fa319d12fa34fb484533 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Sun, 31 Mar 2019 13:01:09 -0400 Subject: [PATCH] Cleaned up unit tests. Added test to implement choices inside .action(...) --- tests/main.cpp | 4 +- tests/test_actions.hpp | 21 +++++++ ...values.hpp => test_compound_arguments.hpp} | 56 ++----------------- tests/test_optional_arguments.hpp | 49 ++++++++++++++++ tests/test_positional_arguments.hpp | 6 +- 5 files changed, 81 insertions(+), 55 deletions(-) create mode 100644 tests/test_actions.hpp rename tests/{test_implicit_values.hpp => test_compound_arguments.hpp} (66%) create mode 100644 tests/test_optional_arguments.hpp diff --git a/tests/main.cpp b/tests/main.cpp index fe5a6d9..48b27de 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -4,4 +4,6 @@ #include #include #include -#include \ No newline at end of file +#include +#include +#include \ No newline at end of file diff --git a/tests/test_actions.hpp b/tests/test_actions.hpp new file mode 100644 index 0000000..9e7bfd8 --- /dev/null +++ b/tests/test_actions.hpp @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +TEST_CASE("Users can use defaul value inside actions", "[actions]") { + argparse::ArgumentParser program("test"); + program.add_argument("input") + .default_value("bar") + .action([=](const std::string& value) { + static const std::vector choices = { "foo", "bar", "baz" }; + if (std::find(choices.begin(), choices.end(), value) != choices.end()) { + return value; + } + return std::string{ "bar" }; + }); + + program.parse_args({ "test", "fez" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("input") == "bar"); +} \ No newline at end of file diff --git a/tests/test_implicit_values.hpp b/tests/test_compound_arguments.hpp similarity index 66% rename from tests/test_implicit_values.hpp rename to tests/test_compound_arguments.hpp index dffea11..7068582 100644 --- a/tests/test_implicit_values.hpp +++ b/tests/test_compound_arguments.hpp @@ -2,53 +2,7 @@ #include #include -TEST_CASE("Parse toggle arguments with default value", "[parse_args]") { - argparse::ArgumentParser program("test"); - program.add_argument("--verbose", "-v") - .default_value(false) - .implicit_value(true); - - program.parse_args({ "./test.exe" }); - auto arguments = program.get_arguments(); - REQUIRE(arguments.size() == 2); - REQUIRE(program.get("--verbose") == false); -} - -TEST_CASE("Parse toggle arguments with implicit value", "[parse_args]") { - argparse::ArgumentParser program("test"); - program.add_argument("--verbose") - .default_value(false) - .implicit_value(true); - - program.parse_args({ "./test.exe", "--verbose" }); - auto arguments = program.get_arguments(); - REQUIRE(arguments.size() == 1); - REQUIRE(program.get("--verbose") == true); -} - -TEST_CASE("Parse multiple toggle arguments with implicit values", "[parse_args]") { - argparse::ArgumentParser program("test"); - program.add_argument("-a") - .default_value(false) - .implicit_value(true); - - program.add_argument("-u") - .default_value(false) - .implicit_value(true); - - program.add_argument("-x") - .default_value(false) - .implicit_value(true); - - program.parse_args({ "./test.exe", "-a", "-x" }); - auto arguments = program.get_arguments(); - REQUIRE(arguments.size() == 3); - REQUIRE(program.get("-a") == true); - REQUIRE(program.get("-u") == false); - REQUIRE(program.get("-x") == true); -} - -TEST_CASE("Parse compound toggle arguments with implicit values", "[parse_args]") { +TEST_CASE("Parse compound toggle arguments with implicit values", "[compound_arguments]") { argparse::ArgumentParser program("test"); program.add_argument("-a") .default_value(false) @@ -70,7 +24,7 @@ TEST_CASE("Parse compound toggle arguments with implicit values", "[parse_args]" REQUIRE(program.get("-x") == true); } -TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[parse_args]") { +TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[compound_arguments]") { argparse::ArgumentParser program("test"); program.add_argument("-a") .default_value(false) @@ -87,8 +41,8 @@ TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[pa program.add_argument("--input_files") .nargs(3); - program.parse_args({ "./test.exe", "-abc", "3.14", "2.718", "--input_files", - "a.txt", "b.txt", "c.txt"}); + program.parse_args({ "./test.exe", "-abc", "3.14", "2.718", "--input_files", + "a.txt", "b.txt", "c.txt" }); auto arguments = program.get_arguments(); REQUIRE(arguments.size() == 4); REQUIRE(program.get("-a") == true); @@ -104,7 +58,7 @@ TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[pa REQUIRE(input_files[2] == "c.txt"); } -TEST_CASE("Parse compound toggle arguments with implicit values and nargs and other positional arguments", "[parse_args]") { +TEST_CASE("Parse compound toggle arguments with implicit values and nargs and other positional arguments", "[compound_arguments]") { argparse::ArgumentParser program("test"); program.add_argument("numbers") diff --git a/tests/test_optional_arguments.hpp b/tests/test_optional_arguments.hpp new file mode 100644 index 0000000..edd1b64 --- /dev/null +++ b/tests/test_optional_arguments.hpp @@ -0,0 +1,49 @@ +#pragma once +#include +#include + +TEST_CASE("Parse toggle arguments with default value", "[optional_arguments]") { + argparse::ArgumentParser program("test"); + program.add_argument("--verbose", "-v") + .default_value(false) + .implicit_value(true); + + program.parse_args({ "./test.exe" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 2); + REQUIRE(program.get("--verbose") == false); +} + +TEST_CASE("Parse toggle arguments with implicit value", "[optional_arguments]") { + argparse::ArgumentParser program("test"); + program.add_argument("--verbose") + .default_value(false) + .implicit_value(true); + + program.parse_args({ "./test.exe", "--verbose" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--verbose") == true); +} + +TEST_CASE("Parse multiple toggle arguments with implicit values", "[optional_arguments]") { + argparse::ArgumentParser program("test"); + program.add_argument("-a") + .default_value(false) + .implicit_value(true); + + program.add_argument("-u") + .default_value(false) + .implicit_value(true); + + program.add_argument("-x") + .default_value(false) + .implicit_value(true); + + program.parse_args({ "./test.exe", "-a", "-x" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 3); + REQUIRE(program.get("-a") == true); + REQUIRE(program.get("-u") == false); + REQUIRE(program.get("-x") == true); +} \ No newline at end of file diff --git a/tests/test_positional_arguments.hpp b/tests/test_positional_arguments.hpp index 6b110b6..a5164ac 100644 --- a/tests/test_positional_arguments.hpp +++ b/tests/test_positional_arguments.hpp @@ -2,7 +2,7 @@ #include #include -TEST_CASE("Parse positional arguments", "[parse_args]") { +TEST_CASE("Parse positional arguments", "[positional_arguments]") { argparse::ArgumentParser program("test"); program.add_argument("input"); program.add_argument("output"); @@ -13,7 +13,7 @@ TEST_CASE("Parse positional arguments", "[parse_args]") { REQUIRE(program.get("output") == "thrust_profile.csv"); } -TEST_CASE("Parse positional arguments with fixed nargs", "[parse_args]") { +TEST_CASE("Parse positional arguments with fixed nargs", "[positional_arguments]") { argparse::ArgumentParser program("test"); program.add_argument("input"); program.add_argument("output").nargs(2); @@ -27,7 +27,7 @@ TEST_CASE("Parse positional arguments with fixed nargs", "[parse_args]") { REQUIRE(outputs[1] == "output.mesh"); } -TEST_CASE("Parse positional arguments with optional arguments", "[parse_args]") { +TEST_CASE("Parse positional arguments with optional arguments", "[positional_arguments]") { argparse::ArgumentParser program("test"); program.add_argument("input"); program.add_argument("output").nargs(2);