|
8 | 8 | #include <testing-utils/catch.hpp> |
9 | 9 | #include <util/string_utils.h> |
10 | 10 |
|
11 | | -SCENARIO("split_string", "[core][utils][string_utils][split_string]") |
| 11 | +struct expected_resultst |
12 | 12 | { |
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") |
14 | 32 | { |
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); |
17 | 35 |
|
18 | | - WHEN("Not stripping, not removing empty") |
| 36 | + THEN("Should get expected vector") |
19 | 37 | { |
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}); |
30 | 43 | } |
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); |
35 | 49 |
|
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") |
45 | 51 | { |
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}); |
56 | 57 | } |
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") |
58 | 65 | { |
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); |
61 | 77 |
|
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}); |
69 | 85 | } |
70 | 86 | } |
71 | 87 | } |
72 | 88 |
|
| 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 | + |
73 | 106 | SCENARIO("split_string into two", "[core][utils][string_utils][split_string]") |
74 | 107 | { |
75 | 108 | GIVEN("A string with one separator") |
|
0 commit comments