From 5c7653cb9e4948be1f2a7c40b971f73cee4b0ab2 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Sat, 30 Mar 2019 16:45:44 -0400 Subject: [PATCH] More unit tests. Added overloaded parse_args method for testing purposes --- src/argparse.hpp | 10 ++++- tests/main.cpp | 3 +- tests/test_add_argument.hpp | 1 + tests/test_parse_args.hpp | 85 +++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 tests/test_parse_args.hpp diff --git a/src/argparse.hpp b/src/argparse.hpp index 1d68ede..acf9b64 100644 --- a/src/argparse.hpp +++ b/src/argparse.hpp @@ -5,7 +5,7 @@ #include #include #include -#include // C++0x +#include namespace argparse { @@ -147,6 +147,14 @@ class ArgumentParser { return *tArgument; } + void parse_args(const std::vector& aArguments) { + std::vector argv; + for (const auto& arg : aArguments) + argv.push_back((char*)arg.data()); + argv.push_back(nullptr); + return parse_args(argv.size() - 1, argv.data()); + } + void parse_args(int argc, char * argv[]) { for (int i = 1; i < argc; i++) { auto tCurrentArgument = argv[i]; diff --git a/tests/main.cpp b/tests/main.cpp index c866956..1858f58 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,4 +1,5 @@ #define CATCH_CONFIG_MAIN #include #include -#include \ No newline at end of file +#include +#include diff --git a/tests/test_add_argument.hpp b/tests/test_add_argument.hpp index a0b8f6f..da3923b 100644 --- a/tests/test_add_argument.hpp +++ b/tests/test_add_argument.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include diff --git a/tests/test_parse_args.hpp b/tests/test_parse_args.hpp new file mode 100644 index 0000000..91130d2 --- /dev/null +++ b/tests/test_parse_args.hpp @@ -0,0 +1,85 @@ +#pragma once +#include +#include + +TEST_CASE("Parse a string argument with value", "[parse_args]") { + argparse::ArgumentParser program("test"); + program.add_argument("--config"); + program.parse_args({ "test", "--config", "config.yml"}); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--config") == "config.yml"); +} + +TEST_CASE("Parse a string argument with default value", "[parse_args]") { + argparse::ArgumentParser program("test"); + program.add_argument("--config") + .default_value([]() { return std::string("foo.yml"); }); + program.parse_args({ "test", "--config" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--config") == "foo.yml"); +} + +TEST_CASE("Parse an int argument with value", "[parse_args]") { + argparse::ArgumentParser program("test"); + program.add_argument("--count") + .action([](const std::string& value) { return std::stoi(value); }); + program.parse_args({ "test", "--count", "5" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--count") == 5); +} + +TEST_CASE("Parse an int argument with default value", "[parse_args]") { + argparse::ArgumentParser program("test"); + program.add_argument("--count") + .default_value([]() { return 2; }) + .action([](const std::string& value) { return std::stoi(value); }); + program.parse_args({ "test", "--count" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--count") == 2); +} + +TEST_CASE("Parse a float argument with value", "[parse_args]") { + argparse::ArgumentParser program("test"); + program.add_argument("--ratio") + .action([](const std::string& value) { return std::stof(value); }); + program.parse_args({ "test", "--ratio", "5.6645" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--ratio") == 5.6645f); +} + +TEST_CASE("Parse a float argument with default value", "[parse_args]") { + argparse::ArgumentParser program("test"); + program.add_argument("--ratio") + .default_value([]() { return 3.14f; }) + .action([](const std::string& value) { return std::stof(value); }); + program.parse_args({ "test", "--ratio" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--ratio") == 3.14f); +} + +TEST_CASE("Parse a double argument with value", "[parse_args]") { + argparse::ArgumentParser program("test"); + program.add_argument("--ratio") + .action([](const std::string& value) { return std::stod(value); }); + program.parse_args({ "test", "--ratio", "5.6645" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--ratio") == 5.6645); +} + +TEST_CASE("Parse a double argument with default value", "[parse_args]") { + argparse::ArgumentParser program("test"); + program.add_argument("--ratio") + .default_value([]() { return 3.14; }) + .action([](const std::string& value) { return std::stod(value); }); + program.parse_args({ "test", "--ratio" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + REQUIRE(program.get("--ratio") == 3.14); +} \ No newline at end of file