From b730100b2962e8252d76204871e80d9f40d73782 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Sat, 30 Mar 2019 17:26:25 -0400 Subject: [PATCH] Unit test to parse and construct a vector of objects of some class --- tests/test_parse_args.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_parse_args.hpp b/tests/test_parse_args.hpp index 2955aa5..504e4aa 100644 --- a/tests/test_parse_args.hpp +++ b/tests/test_parse_args.hpp @@ -167,4 +167,28 @@ TEST_CASE("Parse a vector of character arguments", "[parse_args]") { REQUIRE(vector[2] == 'c'); REQUIRE(vector[3] == 'd'); REQUIRE(vector[4] == 'e'); +} + +TEST_CASE("Parse a vector of string arguments and construct objects", "[parse_args]") { + + class Foo { + public: + Foo(const std::string& value) : value(value) {} + std::string value; + }; + + argparse::ArgumentParser program("test"); + program.add_argument("--vector") + .nargs(5) + .action([](const std::string& value) { return Foo(value); }); + program.parse_args({ "test", "--vector", "abc", "def", "ghi", "jkl", "mno" }); + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 1); + auto vector = program.get>("--vector"); + REQUIRE(vector.size() == 5); + REQUIRE(vector[0].value == Foo("abc").value); + REQUIRE(vector[1].value == Foo("def").value); + REQUIRE(vector[2].value == Foo("ghi").value); + REQUIRE(vector[3].value == Foo("jkl").value); + REQUIRE(vector[4].value == Foo("mno").value); } \ No newline at end of file