From 4ede4292648f1319cf3a15e3b2d09ff97531d43c Mon Sep 17 00:00:00 2001 From: Rafal Bedzkowski Date: Mon, 2 Aug 2021 17:21:46 +0200 Subject: [PATCH 1/2] Const-correct ArgumentParser --- include/argparse/argparse.hpp | 2 +- test/test_value_semantics.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index ad8d8bea..c0ab731b 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -925,7 +925,7 @@ class ArgumentParser { * @throws std::bad_any_cast if the option is not of type T */ template - auto present(std::string_view aArgumentName) -> std::optional { + auto present(std::string_view aArgumentName) const -> std::optional { return (*this)[aArgumentName].present(); } diff --git a/test/test_value_semantics.cpp b/test/test_value_semantics.cpp index 2cc0f887..72958cc0 100644 --- a/test/test_value_semantics.cpp +++ b/test/test_value_semantics.cpp @@ -93,4 +93,27 @@ TEST_CASE("ArgumentParser is CopyConstructible and CopyAssignable" * } } } + + TEST_CASE("ArgumentParser is const-correct after construction and parsing" * + test_suite("value_semantics")) { + GIVEN("a parser") { + argparse::ArgumentParser parser("test"); + parser.add_argument("--foo", "-f").help("I am foo"); + parser.add_description("A description"); + parser.add_epilog("An epilog"); + + WHEN("becomes const-qualified") { + parser.parse_args({"./main", "--foo", "baz"}); + const auto const_parser = std::move(parser); + + THEN("only const methods are accessible") { + REQUIRE(const_parser.help().str().size() > 0); + REQUIRE(const_parser.present("--foo")); + REQUIRE(const_parser.is_used("-f")); + REQUIRE(const_parser.get("-f") == "baz"); + REQUIRE(const_parser["-f"] == std::string("baz")); + } + } + } + } } From 5841bca8944aa3751733379f620a3728964bbf18 Mon Sep 17 00:00:00 2001 From: Rafal Bedzkowski Date: Tue, 3 Aug 2021 22:12:05 +0200 Subject: [PATCH 2/2] Introduce separate const-correctness test --- test/CMakeLists.txt | 1 + test/test_const_correct.cpp | 27 +++++++++++++++++++++++++++ test/test_value_semantics.cpp | 23 ----------------------- 3 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 test/test_const_correct.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c981f893..b1e00bef 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -28,6 +28,7 @@ file(GLOB ARGPARSE_TEST_SOURCES test_append.cpp test_compound_arguments.cpp test_container_arguments.cpp + test_const_correct.cpp test_help.cpp test_invalid_arguments.cpp test_is_used.cpp diff --git a/test/test_const_correct.cpp b/test/test_const_correct.cpp new file mode 100644 index 00000000..1acfdd40 --- /dev/null +++ b/test/test_const_correct.cpp @@ -0,0 +1,27 @@ +#include +#include + +using doctest::test_suite; + +TEST_CASE("ArgumentParser is const-correct after construction and parsing" * + test_suite("value_semantics")) { + GIVEN("a parser") { + argparse::ArgumentParser parser("test"); + parser.add_argument("--foo", "-f").help("I am foo"); + parser.add_description("A description"); + parser.add_epilog("An epilog"); + + WHEN("becomes const-qualified") { + parser.parse_args({"./main", "--foo", "baz"}); + const auto const_parser = std::move(parser); + + THEN("only const methods are accessible") { + REQUIRE(const_parser.help().str().size() > 0); + REQUIRE(const_parser.present("--foo")); + REQUIRE(const_parser.is_used("-f")); + REQUIRE(const_parser.get("-f") == "baz"); + REQUIRE(const_parser["-f"] == std::string("baz")); + } + } + } +} diff --git a/test/test_value_semantics.cpp b/test/test_value_semantics.cpp index 72958cc0..2cc0f887 100644 --- a/test/test_value_semantics.cpp +++ b/test/test_value_semantics.cpp @@ -93,27 +93,4 @@ TEST_CASE("ArgumentParser is CopyConstructible and CopyAssignable" * } } } - - TEST_CASE("ArgumentParser is const-correct after construction and parsing" * - test_suite("value_semantics")) { - GIVEN("a parser") { - argparse::ArgumentParser parser("test"); - parser.add_argument("--foo", "-f").help("I am foo"); - parser.add_description("A description"); - parser.add_epilog("An epilog"); - - WHEN("becomes const-qualified") { - parser.parse_args({"./main", "--foo", "baz"}); - const auto const_parser = std::move(parser); - - THEN("only const methods are accessible") { - REQUIRE(const_parser.help().str().size() > 0); - REQUIRE(const_parser.present("--foo")); - REQUIRE(const_parser.is_used("-f")); - REQUIRE(const_parser.get("-f") == "baz"); - REQUIRE(const_parser["-f"] == std::string("baz")); - } - } - } - } }