From 97993666abf808a918fea4f4906bf8eb366d5294 Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Thu, 22 Jul 2021 06:45:44 -0700 Subject: [PATCH] Add tests for ArgumentParser::get These test the API shown in README.md, rather than the Argument::get function that does most of the work. Signed-off-by: Sean Robinson --- test/CMakeLists.txt | 1 + test/test_get.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 test/test_get.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e601965..e8f7d89 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -30,6 +30,7 @@ file(GLOB ARGPARSE_TEST_SOURCES test_compound_arguments.cpp test_container_arguments.cpp test_const_correct.cpp + test_get.cpp test_help.cpp test_invalid_arguments.cpp test_is_used.cpp diff --git a/test/test_get.cpp b/test/test_get.cpp new file mode 100644 index 0000000..d54e0f3 --- /dev/null +++ b/test/test_get.cpp @@ -0,0 +1,29 @@ +#include +#include + +using doctest::test_suite; + +TEST_CASE("Getting a simple argument" * test_suite("ArgumentParser::get")) { + argparse::ArgumentParser program("test"); + program.add_argument("-s", "--stuff"); + REQUIRE_NOTHROW(program.parse_args({ "test", "-s", "./src" })); + REQUIRE(program.get("--stuff") == "./src"); +} + +TEST_CASE("Missing argument" * test_suite("ArgumentParser::get")) { + argparse::ArgumentParser program("test"); + program.add_argument("-s", "--stuff"); + REQUIRE_NOTHROW(program.parse_args({ "test" })); + REQUIRE_THROWS_WITH_AS(program.get("--stuff"), + "No value provided", + std::logic_error); +} + +TEST_CASE("Implicit argument" * test_suite("ArgumentParser::get")) { + argparse::ArgumentParser program("test"); + program.add_argument("-s", "--stuff").nargs(1); + REQUIRE_NOTHROW(program.parse_args({ "test" })); + REQUIRE_THROWS_WITH_AS(program.get("--stuff"), + "No value provided", + std::logic_error); +}