mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
More unit tests. Added overloaded parse_args method for testing purposes
This commit is contained in:
parent
72c11da1be
commit
5c7653cb9e
@ -5,7 +5,7 @@
|
||||
#include <functional>
|
||||
#include <any>
|
||||
#include <memory>
|
||||
#include <type_traits> // C++0x
|
||||
#include <type_traits>
|
||||
|
||||
namespace argparse {
|
||||
|
||||
@ -147,6 +147,14 @@ class ArgumentParser {
|
||||
return *tArgument;
|
||||
}
|
||||
|
||||
void parse_args(const std::vector<std::string>& aArguments) {
|
||||
std::vector<char*> 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];
|
||||
|
@ -2,3 +2,4 @@
|
||||
#include <iostream>
|
||||
#include <argparse.hpp>
|
||||
#include <test_add_argument.hpp>
|
||||
#include <test_parse_args.hpp>
|
||||
|
@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
#include <catch.hpp>
|
||||
#include <argparse.hpp>
|
||||
|
||||
|
85
tests/test_parse_args.hpp
Normal file
85
tests/test_parse_args.hpp
Normal file
@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
#include <catch.hpp>
|
||||
#include <argparse.hpp>
|
||||
|
||||
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<int>("--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<int>("--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<float>("--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<float>("--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<double>("--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<double>("--ratio") == 3.14);
|
||||
}
|
Loading…
Reference in New Issue
Block a user