Skip to content

Commit 07009d4

Browse files
author
thk123
committed
Refactored test to run all combinations
1 parent 252c24c commit 07009d4

File tree

1 file changed

+81
-48
lines changed

1 file changed

+81
-48
lines changed

unit/util/string_utils/split_string.cpp

Lines changed: 81 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,101 @@
88
#include <testing-utils/catch.hpp>
99
#include <util/string_utils.h>
1010

11-
SCENARIO("split_string", "[core][utils][string_utils][split_string]")
11+
struct expected_resultst
1212
{
13-
GIVEN("A non whitespace delimited string with two elements")
13+
std::vector<std::string> no_strip_no_remove;
14+
std::vector<std::string> strip_no_remove;
15+
std::vector<std::string> no_strip_remove_empty;
16+
std::vector<std::string> strip_remove_empty;
17+
};
18+
19+
/// For a given string and delimiter, use the split_string function with all
20+
/// the possible combinations of stripping whitespace and removing empty
21+
/// elements.
22+
/// \param string: The string to split
23+
/// \param delimiter: The delimter to split on
24+
/// \param expected_results: The results expected for each of the versions of
25+
/// the method
26+
void run_on_all_variants(
27+
std::string string,
28+
char delimiter,
29+
const expected_resultst &expected_results)
30+
{
31+
WHEN("Not stripping, not removing empty")
1432
{
15-
const char delimiter = ',';
16-
const std::string string = " a,, x , ,";
33+
std::vector<std::string> result;
34+
split_string(string, delimiter, result, false, false);
1735

18-
WHEN("Not stripping, not removing empty")
36+
THEN("Should get expected vector")
1937
{
20-
std::vector<std::string> result;
21-
split_string(string, delimiter, result, false, false);
22-
23-
THEN("All the elements should remain")
24-
{
25-
std::vector<std::string> expected_result = {" a", "", " x ", " ", ""};
26-
REQUIRE_THAT(
27-
result,
28-
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
29-
}
38+
REQUIRE_THAT(
39+
result,
40+
// NOLINTNEXTLINE(whitespace/braces)
41+
Catch::Matchers::Vector::EqualsMatcher<std::string>{
42+
expected_results.no_strip_no_remove});
3043
}
31-
WHEN("Stripping, not removing empty")
32-
{
33-
std::vector<std::string> result;
34-
split_string(string, delimiter, result, true, false);
44+
}
45+
WHEN("Not stripping, removing empty")
46+
{
47+
std::vector<std::string> result;
48+
split_string(string, delimiter, result, false, true);
3549

36-
THEN("All whitespace borders should be removed but all elements remain")
37-
{
38-
std::vector<std::string> expected_result = {"a", "", "x", "", ""};
39-
REQUIRE_THAT(
40-
result,
41-
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
42-
}
43-
}
44-
WHEN("Not stripping, removing empty")
50+
THEN("Should get expected vector")
4551
{
46-
std::vector<std::string> result;
47-
split_string(string, delimiter, result, false, true);
48-
49-
THEN("All empty elements should be removed")
50-
{
51-
std::vector<std::string> expected_result = {" a", " x ", " "};
52-
REQUIRE_THAT(
53-
result,
54-
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
55-
}
52+
REQUIRE_THAT(
53+
result,
54+
// NOLINTNEXTLINE(whitespace/braces)
55+
Catch::Matchers::Vector::EqualsMatcher<std::string>{
56+
expected_results.no_strip_remove_empty});
5657
}
57-
WHEN("Stripping and removing empty")
58+
}
59+
WHEN("Stripping, not removing empty")
60+
{
61+
std::vector<std::string> result;
62+
split_string(string, delimiter, result, true, false);
63+
64+
THEN("Should get expected vector")
5865
{
59-
std::vector<std::string> result;
60-
split_string(string, delimiter, result, true, true);
66+
REQUIRE_THAT(
67+
result,
68+
// NOLINTNEXTLINE(whitespace/braces)
69+
Catch::Matchers::Vector::EqualsMatcher<std::string>{
70+
expected_results.strip_no_remove});
71+
}
72+
}
73+
WHEN("Stripping and removing empty")
74+
{
75+
std::vector<std::string> result;
76+
split_string(string, delimiter, result, true, true);
6177

62-
THEN("Should get the two parts in the vector")
63-
{
64-
std::vector<std::string> expected_result = {"a", "x"};
65-
REQUIRE_THAT(
66-
result,
67-
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
68-
}
78+
THEN("Should get expected vector")
79+
{
80+
REQUIRE_THAT(
81+
result,
82+
// NOLINTNEXTLINE(whitespace/braces)
83+
Catch::Matchers::Vector::EqualsMatcher<std::string>{
84+
expected_results.strip_remove_empty});
6985
}
7086
}
7187
}
7288

89+
SCENARIO("split_string", "[core][utils][string_utils][split_string]")
90+
{
91+
GIVEN("A non whitespace delimited string with two elements")
92+
{
93+
const char delimiter = ',';
94+
const std::string string = " a,, x , ,";
95+
96+
expected_resultst expected_results;
97+
expected_results.no_strip_no_remove = {" a", "", " x ", " ", ""};
98+
expected_results.strip_no_remove = {"a", "", "x", "", ""};
99+
expected_results.no_strip_remove_empty = {" a", " x ", " "};
100+
expected_results.strip_remove_empty = {"a", "x"};
101+
102+
run_on_all_variants(string, delimiter, expected_results);
103+
}
104+
}
105+
73106
SCENARIO("split_string into two", "[core][utils][string_utils][split_string]")
74107
{
75108
GIVEN("A string with one separator")

0 commit comments

Comments
 (0)