|
| 1 | +/* |
| 2 | + Author: Diffblue Ltd. |
| 3 | +*/ |
| 4 | + |
| 5 | +/// \file |
| 6 | +/// split_string Unit Tests |
| 7 | + |
| 8 | +#include <testing-utils/catch.hpp> |
| 9 | +#include <util/string_utils.h> |
| 10 | + |
| 11 | +SCENARIO("split_string", "[core][utils][string_utils][split_string]") |
| 12 | +{ |
| 13 | + GIVEN("A non whitespace delimited string with two elements") |
| 14 | + { |
| 15 | + const char delimiter = ','; |
| 16 | + const std::string string = " a,, x , ,"; |
| 17 | + |
| 18 | + WHEN("Not stripping, not removing empty") |
| 19 | + { |
| 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 | + } |
| 30 | + } |
| 31 | + WHEN("Stripping, not removing empty") |
| 32 | + { |
| 33 | + std::vector<std::string> result; |
| 34 | + split_string(string, delimiter, result, true, false); |
| 35 | + |
| 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") |
| 45 | + { |
| 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 | + } |
| 56 | + } |
| 57 | + WHEN("Stripping and removing empty") |
| 58 | + { |
| 59 | + std::vector<std::string> result; |
| 60 | + split_string(string, delimiter, result, true, true); |
| 61 | + |
| 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 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +SCENARIO("split_string into two", "[core][utils][string_utils][split_string]") |
| 74 | +{ |
| 75 | + GIVEN("A string with one separator") |
| 76 | + { |
| 77 | + const char separator = ':'; |
| 78 | + std::string string = "a:b"; |
| 79 | + |
| 80 | + WHEN("Splitting into two strings") |
| 81 | + { |
| 82 | + std::string s1; |
| 83 | + std::string s2; |
| 84 | + split_string(string, separator, s1, s2, false); |
| 85 | + THEN("The string should be split") |
| 86 | + { |
| 87 | + REQUIRE(s1 == "a"); |
| 88 | + REQUIRE(s2 == "b"); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments