mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Officially added first unit test with catch
This commit is contained in:
parent
717e6aafdb
commit
057b1eaa52
@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
@ -201,6 +202,10 @@ class ArgumentParser {
|
||||
return T();
|
||||
}
|
||||
|
||||
std::map<std::string, std::shared_ptr<Argument>> get_arguments() const {
|
||||
return mArgumentMap;
|
||||
}
|
||||
|
||||
private:
|
||||
Argument& add_argument_internal(std::shared_ptr<Argument> aArgument) {
|
||||
return *aArgument;
|
||||
|
@ -17,7 +17,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
|
||||
"../src/argparse.hpp"
|
||||
)
|
||||
ADD_EXECUTABLE(ARGPARSE ${ARGPARSE_TEST_SOURCES})
|
||||
INCLUDE_DIRECTORIES("../src")
|
||||
INCLUDE_DIRECTORIES("../src" ".")
|
||||
set_target_properties(ARGPARSE PROPERTIES OUTPUT_NAME tests)
|
||||
set_property(TARGET ARGPARSE PROPERTY CXX_STANDARD 17)
|
||||
|
||||
|
@ -1,39 +1,4 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include <iostream>
|
||||
#include <argparse.hpp>
|
||||
using namespace argparse;
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
ArgumentParser program("test");
|
||||
|
||||
program.add_argument("--config")
|
||||
.help("Path to input file")
|
||||
.default_value([]() { return std::string{"config.yml"}; });
|
||||
|
||||
program.add_argument("-n", "--num_iterations")
|
||||
.help("Number of iterations")
|
||||
.action([](const std::string& value) { return std::stoi(value); });
|
||||
|
||||
program.add_argument("-v", "--verbose", "VERBOSE")
|
||||
.nargs(0)
|
||||
.default_value([]() { return false; });
|
||||
|
||||
program.add_argument("--test_inputs")
|
||||
.nargs(3)
|
||||
.default_value([]() { return std::vector<int>{ 1, 2, 3 }; })
|
||||
.action([](const std::string& value) { return std::stoi(value); });
|
||||
|
||||
program.parse_args(argc, argv);
|
||||
|
||||
auto config_file = program.get<std::string>("--config");
|
||||
auto num_iters = program.get<int>("-n");
|
||||
auto verbose = program.get<bool>("-v");
|
||||
auto test_inputs = program.get<std::vector<int>>("--test_inputs");
|
||||
|
||||
std::cout << config_file << std::endl;
|
||||
std::cout << num_iters << std::endl;
|
||||
std::cout << verbose << std::endl;
|
||||
for (auto& input : test_inputs)
|
||||
std::cout << input << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#include <test_add_argument.hpp>
|
16
tests/test_add_argument.hpp
Normal file
16
tests/test_add_argument.hpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include <catch.hpp>
|
||||
#include <argparse.hpp>
|
||||
|
||||
TEST_CASE("Add a simple argument", "[add_argument]") {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--foo");
|
||||
|
||||
auto arguments = program.get_arguments();
|
||||
REQUIRE(arguments.size() == 1);
|
||||
REQUIRE(arguments["--foo"] != nullptr);
|
||||
REQUIRE(arguments["--foo"]->mNames == std::vector<std::string>{"--foo"});
|
||||
REQUIRE(arguments["--foo"]->mHelp == "");
|
||||
REQUIRE(arguments["--foo"]->mNumArgs == 1);
|
||||
REQUIRE(arguments["--foo"]->mRawValues.size() == 0);
|
||||
REQUIRE(arguments["--foo"]->mValues.size() == 0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user