Skip to content

Commit

Permalink
wip: argument_name refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectraL519 committed Mar 29, 2024
1 parent 24137db commit 2415be8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 96 deletions.
33 changes: 15 additions & 18 deletions include/ap/argument_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,17 @@ struct argument_name {

/**
* @brief Primary name constructor.
* @param name The primary name of the argument.
* @param primary The primary name of the argument.
*/
explicit argument_name(std::string_view name) : name(name) {}
explicit argument_name(std::string_view primary) : primary(primary) {}

/**
* @brief Primary and secondary name constructor.
* @param name The primary name of the argument.
* @param short_name The secondary (short) name of the argument.
* @param primary The primary name of the argument.
* @param secondary The secondary (short) name of the argument.
*/
explicit argument_name(std::string_view name, std::string_view short_name)
: name(name), short_name(short_name) {}
explicit argument_name(std::string_view primary, std::string_view secondary)
: primary(primary), secondary(secondary) {}

/// @brief Class destructor.
~argument_name() = default;
Expand All @@ -350,23 +350,23 @@ struct argument_name {
* @return Equality of argument names.
*/
bool operator==(const argument_name& other) const noexcept {
return this->name == other.name;
return this->primary == other.primary;
}

/**
* @brief Equality comparison operator for string variables representing argument names.
* @param name The string view to compare with.
* @return Equality of names comparison (either full or short name).
* @return Equality of names comparison (either primary or secondary name).
*/
bool operator==(std::string_view name) const noexcept {
return name == this->name or (this->short_name and name == this->short_name.value());
return name == this->primary or (this->secondary and name == this->secondary.value());
}

/// @brief Get a string representation of the argument_name.
[[nodiscard]] std::string str() const noexcept {
return this->short_name
? ("[" + this->name + "," + this->short_name.value() + "]")
: ("[" + this->name + "]");
return this->secondary
? ("[" + this->primary + "," + this->secondary.value() + "]")
: ("[" + this->primary + "]");
}

/**
Expand All @@ -380,11 +380,8 @@ struct argument_name {
return os;
}

// TODO: rename
// * name -> primary
// * short_name -> secondary
const std::string name; ///< The primary name of the argument.
const std::optional<std::string> short_name; ///< The optional short name of the argument.
const std::string primary; ///< The primary name of the argument.
const std::optional<std::string> secondary; ///< The optional (short) name of the argument.
};

/// @brief Argument class interface
Expand Down Expand Up @@ -792,7 +789,7 @@ class positional_argument : public detail::argument_interface {

/// @return Reference to the vector of parsed values for the positional argument.
[[nodiscard]] const std::vector<std::any>& values() const override {
throw std::logic_error("Positional argument " + this->_name.name + "has only 1 value.");
throw std::logic_error("Positional argument " + this->_name.primary + "has only 1 value.");
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/include/argument_parser_test_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ struct argument_parser_test_fixture {
void add_arguments(ap::argument_parser& parser, std::size_t num_args, std::size_t args_split) const {
for (std::size_t i = 0; i < args_split; i++) { // positional args
const auto arg_name = prepare_arg_name(i);
parser.add_positional_argument(arg_name.name, arg_name.short_name.value());
parser.add_positional_argument(arg_name.primary, arg_name.secondary.value());
}

for (std::size_t i = args_split; i < num_args; i++) { // optional args
const auto arg_name = prepare_arg_name(i);
parser.add_optional_argument(arg_name.name, arg_name.short_name.value());
parser.add_optional_argument(arg_name.primary, arg_name.secondary.value());
}
}

Expand All @@ -105,7 +105,7 @@ struct argument_parser_test_fixture {
}
for (std::size_t i = args_split; i < num_args; i++) { // optional args
cmd_args.push_back(cmd_argument{
cmd_argument::type_discriminator::flag, prepare_arg_name(i).name });
cmd_argument::type_discriminator::flag, prepare_arg_name(i).primary });
cmd_args.push_back(cmd_argument{ cmd_argument::type_discriminator::value, prepare_arg_value(i) });
}

Expand Down
58 changes: 28 additions & 30 deletions test/source/test_argument_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,105 +12,103 @@ using namespace ap::argument::detail;

namespace {

constexpr std::string_view name = "test";
constexpr std::string_view short_name = "t";
constexpr std::string_view primary_name = "test";
constexpr std::string_view secondary_name = "t";

constexpr std::string_view other_name = "other";
constexpr std::string_view other_short_name = "o";
constexpr std::string_view other_primary_name = "other";
constexpr std::string_view other_secondary_name = "o";

argument_name default_argument_name_long_name() {
return argument_name(name);
return argument_name(primary_name);
}

argument_name default_argument_name_both_names() {
return argument_name(name, short_name);
return argument_name(primary_name, secondary_name);
}

} // namespace

TEST_SUITE_BEGIN("test_argument_name");

TEST_CASE("argument_name.name member should be correctly "
TEST_CASE("argument_name.primary member should be correctly "
"initialized") {
const auto arg_name = default_argument_name_long_name();

REQUIRE_EQ(arg_name.name, name);
REQUIRE_EQ(arg_name.primary, primary_name);
}

TEST_CASE("argument_name members should be correctly "
"initialized") {
const auto arg_name = default_argument_name_both_names();

REQUIRE_EQ(arg_name.name, name);
REQUIRE_EQ(arg_name.primary, primary_name);

REQUIRE(arg_name.short_name);
REQUIRE_EQ(arg_name.short_name.value(), short_name);
REQUIRE(arg_name.secondary);
REQUIRE_EQ(arg_name.secondary.value(), secondary_name);
}

TEST_CASE("argument_name::operator==(argument_name) should "
"return true if "
"long names are equal") {
"return true if primary names are equal") {
const auto arg_name_a = default_argument_name_long_name();
const auto arg_name_b = default_argument_name_both_names();

REQUIRE(arg_name_a == arg_name_b);
}

TEST_CASE("argument_name::operator==(argument_name) should "
"return false if "
"long names are not equal") {
"return false if primary names are not equal") {
const auto arg_name_a = default_argument_name_long_name();
const auto arg_name_b = argument_name(other_name, other_short_name);
const auto arg_name_b = argument_name(other_primary_name, other_secondary_name);

REQUIRE_FALSE(arg_name_a == arg_name_b);
}

TEST_CASE("argument_name::operator==(string_view) should "
"return true if at "
"least one name matches") {
SUBCASE("argument_name with long name only") {
"least one primary_name matches") {
SUBCASE("argument_name with long primary_name only") {
const auto arg_name = default_argument_name_long_name();

REQUIRE(arg_name == name);
REQUIRE(arg_name == primary_name);
}

SUBCASE("argument_name with both names") {
const auto arg_name = default_argument_name_both_names();

REQUIRE(arg_name == name);
REQUIRE(arg_name == short_name);
REQUIRE(arg_name == primary_name);
REQUIRE(arg_name == secondary_name);
}
}

TEST_CASE("argument_name::operator==(string_view) should "
"return false if no "
"name matches") {
SUBCASE("argument_name with long name only") {
"primary_name matches") {
SUBCASE("argument_name with long primary_name only") {
const auto arg_name = default_argument_name_long_name();

REQUIRE_FALSE(arg_name == other_name);
REQUIRE_FALSE(arg_name == other_short_name);
REQUIRE_FALSE(arg_name == other_primary_name);
REQUIRE_FALSE(arg_name == other_secondary_name);
}

SUBCASE("argument_name with both names") {
const auto arg_name = default_argument_name_both_names();

REQUIRE_FALSE(arg_name == other_name);
REQUIRE_FALSE(arg_name == other_short_name);
REQUIRE_FALSE(arg_name == other_primary_name);
REQUIRE_FALSE(arg_name == other_secondary_name);
}
}

TEST_CASE("operator<< should push correct data to the output stream") {
std::stringstream ss, expected_ss;

SUBCASE("argument_name with long name only") {
SUBCASE("argument_name with long primary_name only") {
ss << default_argument_name_long_name();
expected_ss << "[" << name << "]";
expected_ss << "[" << primary_name << "]";
}

SUBCASE("argument_name with both names") {
ss << default_argument_name_both_names();
expected_ss << "[" << name << "," << short_name << "]";
expected_ss << "[" << primary_name << "," << secondary_name << "]";
}

CAPTURE(ss);
Expand Down
12 changes: 6 additions & 6 deletions test/source/test_argument_parser_add_argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ TEST_CASE_FIXTURE(
REQUIRE_NOTHROW(sut.add_positional_argument(other_name, other_short_name));
}

SUBCASE("adding argument with a previously used long name") {
SUBCASE("adding argument with a previously used primary name") {
REQUIRE_THROWS_AS(sut.add_positional_argument(name, other_short_name), ap::error::argument_name_used_error);
}

SUBCASE("adding argument with a previously used short name") {
SUBCASE("adding argument with a previously used secondary name") {
REQUIRE_THROWS_AS(sut.add_positional_argument(other_name, short_name), ap::error::argument_name_used_error);
}
}
Expand All @@ -90,11 +90,11 @@ TEST_CASE_FIXTURE(
REQUIRE_NOTHROW(sut.add_optional_argument(other_name, other_short_name));
}

SUBCASE("adding argument with a previously used long name") {
SUBCASE("adding argument with a previously used primary name") {
REQUIRE_THROWS_AS(sut.add_optional_argument(name, other_short_name), ap::error::argument_name_used_error);
}

SUBCASE("adding argument with a previously used short name") {
SUBCASE("adding argument with a previously used secondary name") {
REQUIRE_THROWS_AS(sut.add_optional_argument(other_name, short_name), ap::error::argument_name_used_error);
}
}
Expand Down Expand Up @@ -130,11 +130,11 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "add_flag should throw only when
REQUIRE_NOTHROW(sut.add_flag(other_name, other_short_name));
}

SUBCASE("adding argument with a previously used long name") {
SUBCASE("adding argument with a previously used primary name") {
REQUIRE_THROWS_AS(sut.add_flag(name, other_short_name), ap::error::argument_name_used_error);
}

SUBCASE("adding argument with a previously used short name") {
SUBCASE("adding argument with a previously used secondary name") {
REQUIRE_THROWS_AS(sut.add_flag(other_name, short_name), ap::error::argument_name_used_error);
}
}
Expand Down
Loading

0 comments on commit 2415be8

Please sign in to comment.