Skip to content

Commit f8d2987

Browse files
committed
added tests
1 parent d3fbde3 commit f8d2987

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

include/ap/argument_parser.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ class argument_parser {
497497
arg_name, "An argument name cannot be empty."
498498
);
499499

500-
// TODO: add tests
501500
if (detail::contains_whitespaces(arg_name))
502501
throw invalid_configuration::invalid_argument_name(
503502
arg_name, "An argument name cannot contain whitespaces."

include/ap/detail/str_utility.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ template <c_writable T>
2929
return oss.str();
3030
}
3131

32-
// TODO: add tests
3332
/// @brief Checks whether a string contains any whitespace characters.
3433
[[nodiscard]] inline bool contains_whitespaces(std::string_view str) noexcept {
3534
return std::ranges::any_of(str, [](unsigned char c) { return std::isspace(c); });

tests/source/test_argument_parser_add_argument.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct test_argument_parser_add_argument : public argument_parser_test_fixture {
2121
const std::string_view secondary_name_2 = "s2";
2222

2323
const std::string_view invalid_name_empty = "";
24+
const std::string_view invalid_name_whitespace = "invalid name";
2425
const std::string_view invalid_name_flag_prefix = "-invalid";
2526
const std::string_view invalid_name_digit = "1invalid";
2627
};
@@ -37,6 +38,11 @@ TEST_CASE_FIXTURE(
3738
reason = "An argument name cannot be empty.";
3839
}
3940

41+
SUBCASE("The name contains whitespace characters") {
42+
primary_name = invalid_name_whitespace;
43+
reason = "An argument name cannot contain whitespaces.";
44+
}
45+
4046
SUBCASE("The name begins with the flag prefix character") {
4147
primary_name = invalid_name_flag_prefix;
4248
reason = "An argument name cannot begin with a flag prefix character (-).";
@@ -75,6 +81,11 @@ TEST_CASE_FIXTURE(
7581
reason = "An argument name cannot be empty.";
7682
}
7783

84+
SUBCASE("The name contains whitespace characters") {
85+
primary_name = invalid_name_whitespace;
86+
reason = "An argument name cannot contain whitespaces.";
87+
}
88+
7889
SUBCASE("The name begins with the flag prefix character") {
7990
primary_name = invalid_name_flag_prefix;
8091
reason = "An argument name cannot begin with a flag prefix character (-).";
@@ -113,6 +124,11 @@ TEST_CASE_FIXTURE(
113124
reason = "An argument name cannot be empty.";
114125
}
115126

127+
SUBCASE("The name contains whitespace characters") {
128+
secondary_name = invalid_name_whitespace;
129+
reason = "An argument name cannot contain whitespaces.";
130+
}
131+
116132
SUBCASE("The name begins with the flag prefix character") {
117133
secondary_name = invalid_name_flag_prefix;
118134
reason = "An argument name cannot begin with a flag prefix character (-).";
@@ -141,8 +157,7 @@ TEST_CASE_FIXTURE(
141157

142158
TEST_CASE_FIXTURE(
143159
test_argument_parser_add_argument,
144-
"add_positional_argument should throw only when adding an"
145-
"argument with a previously used name"
160+
"add_positional_argument should throw when adding an argument with a previously used name"
146161
) {
147162
sut.add_positional_argument(primary_name_1, secondary_name_1);
148163

@@ -169,8 +184,7 @@ TEST_CASE_FIXTURE(
169184

170185
TEST_CASE_FIXTURE(
171186
test_argument_parser_add_argument,
172-
"add_optional_argument should throw only when adding an"
173-
"argument with a previously used name"
187+
"add_optional_argument should throw when adding an argument with a previously used name"
174188
) {
175189
sut.add_optional_argument(primary_name_1, secondary_name_1);
176190

@@ -226,7 +240,7 @@ TEST_CASE_FIXTURE(
226240

227241
TEST_CASE_FIXTURE(
228242
test_argument_parser_add_argument,
229-
"add_flag should throw only when adding and argument with a previously used name"
243+
"add_flag should throw when adding and argument with a previously used name"
230244
) {
231245
sut.add_flag(primary_name_1, secondary_name_1);
232246

tests/source/test_str_utility.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <ap/detail/str_utility.hpp>
66

7+
#include <array>
8+
#include <format>
79
#include <vector>
810

911
using namespace ap::detail;
@@ -12,10 +14,39 @@ namespace {
1214

1315
constexpr std::string_view delimiter = ",";
1416

17+
struct dummy_writable {
18+
int x;
19+
int y;
20+
};
21+
22+
std::ostream& operator<<(std::ostream& os, const dummy_writable& dw) {
23+
os << dw.x << "," << dw.y;
24+
return os;
25+
}
26+
1527
} // namespace
1628

1729
TEST_SUITE_BEGIN("test_str_utility");
1830

31+
TEST_CASE("as_string should convert the given writable object to string") {
32+
int value = 5;
33+
CHECK_EQ(as_string(value), std::to_string(value));
34+
35+
dummy_writable dw{3, 14};
36+
std::ostringstream dw_oss;
37+
dw_oss << dw;
38+
CHECK_EQ(as_string(dw), dw_oss.str());
39+
}
40+
41+
TEST_CASE("contains_whitespace should return true for strings containing at least one whitespace "
42+
"characted") {
43+
CHECK_FALSE(contains_whitespaces("str-without-whitespaces"));
44+
45+
constexpr std::array<char, 6> whitespace_chars = {' ', '\t', '\n', '\v', '\f', '\r'};
46+
for (char c : whitespace_chars)
47+
CHECK(contains_whitespaces(std::format("begin{}end", c)));
48+
}
49+
1950
TEST_CASE("join should return an empty string for an empty range") {
2051
std::vector<int> range{};
2152
CHECK_EQ(join(range, delimiter), "");

0 commit comments

Comments
 (0)