Skip to content

Commit

Permalink
Refactored test to run all combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
thk123 committed Apr 16, 2018
1 parent 252c24c commit 07009d4
Showing 1 changed file with 81 additions and 48 deletions.
129 changes: 81 additions & 48 deletions unit/util/string_utils/split_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,101 @@
#include <testing-utils/catch.hpp>
#include <util/string_utils.h>

SCENARIO("split_string", "[core][utils][string_utils][split_string]")
struct expected_resultst
{
GIVEN("A non whitespace delimited string with two elements")
std::vector<std::string> no_strip_no_remove;
std::vector<std::string> strip_no_remove;
std::vector<std::string> no_strip_remove_empty;
std::vector<std::string> strip_remove_empty;
};

/// For a given string and delimiter, use the split_string function with all
/// the possible combinations of stripping whitespace and removing empty
/// elements.
/// \param string: The string to split
/// \param delimiter: The delimter to split on
/// \param expected_results: The results expected for each of the versions of
/// the method
void run_on_all_variants(
std::string string,
char delimiter,
const expected_resultst &expected_results)
{
WHEN("Not stripping, not removing empty")
{
const char delimiter = ',';
const std::string string = " a,, x , ,";
std::vector<std::string> result;
split_string(string, delimiter, result, false, false);

WHEN("Not stripping, not removing empty")
THEN("Should get expected vector")
{
std::vector<std::string> result;
split_string(string, delimiter, result, false, false);

THEN("All the elements should remain")
{
std::vector<std::string> expected_result = {" a", "", " x ", " ", ""};
REQUIRE_THAT(
result,
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
}
REQUIRE_THAT(
result,
// NOLINTNEXTLINE(whitespace/braces)
Catch::Matchers::Vector::EqualsMatcher<std::string>{
expected_results.no_strip_no_remove});
}
WHEN("Stripping, not removing empty")
{
std::vector<std::string> result;
split_string(string, delimiter, result, true, false);
}
WHEN("Not stripping, removing empty")
{
std::vector<std::string> result;
split_string(string, delimiter, result, false, true);

THEN("All whitespace borders should be removed but all elements remain")
{
std::vector<std::string> expected_result = {"a", "", "x", "", ""};
REQUIRE_THAT(
result,
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
}
}
WHEN("Not stripping, removing empty")
THEN("Should get expected vector")
{
std::vector<std::string> result;
split_string(string, delimiter, result, false, true);

THEN("All empty elements should be removed")
{
std::vector<std::string> expected_result = {" a", " x ", " "};
REQUIRE_THAT(
result,
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
}
REQUIRE_THAT(
result,
// NOLINTNEXTLINE(whitespace/braces)
Catch::Matchers::Vector::EqualsMatcher<std::string>{
expected_results.no_strip_remove_empty});
}
WHEN("Stripping and removing empty")
}
WHEN("Stripping, not removing empty")
{
std::vector<std::string> result;
split_string(string, delimiter, result, true, false);

THEN("Should get expected vector")
{
std::vector<std::string> result;
split_string(string, delimiter, result, true, true);
REQUIRE_THAT(
result,
// NOLINTNEXTLINE(whitespace/braces)
Catch::Matchers::Vector::EqualsMatcher<std::string>{
expected_results.strip_no_remove});
}
}
WHEN("Stripping and removing empty")
{
std::vector<std::string> result;
split_string(string, delimiter, result, true, true);

THEN("Should get the two parts in the vector")
{
std::vector<std::string> expected_result = {"a", "x"};
REQUIRE_THAT(
result,
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
}
THEN("Should get expected vector")
{
REQUIRE_THAT(
result,
// NOLINTNEXTLINE(whitespace/braces)
Catch::Matchers::Vector::EqualsMatcher<std::string>{
expected_results.strip_remove_empty});
}
}
}

SCENARIO("split_string", "[core][utils][string_utils][split_string]")
{
GIVEN("A non whitespace delimited string with two elements")
{
const char delimiter = ',';
const std::string string = " a,, x , ,";

expected_resultst expected_results;
expected_results.no_strip_no_remove = {" a", "", " x ", " ", ""};
expected_results.strip_no_remove = {"a", "", "x", "", ""};
expected_results.no_strip_remove_empty = {" a", " x ", " "};
expected_results.strip_remove_empty = {"a", "x"};

run_on_all_variants(string, delimiter, expected_results);
}
}

SCENARIO("split_string into two", "[core][utils][string_utils][split_string]")
{
GIVEN("A string with one separator")
Expand Down

0 comments on commit 07009d4

Please sign in to comment.