-
Notifications
You must be signed in to change notification settings - Fork 273
Enhance split_string to support splitting on white space [TG-2922] #2071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thk123
merged 6 commits into
diffblue:develop
from
thk123:refactor/split-string-unit-tests
Apr 19, 2018
+265
−79
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e87edbf
Removing wrong elements from the make file
252c24c
Migrate old string utils unit tests
07009d4
Refactored test to run all combinations
145f474
Adding tests for empty string edge cases
a385d9b
Allowed split_string to be used on whitespace if not also trying to s…
fc8ba88
Revert to aborting precondition for function inputs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,6 @@ set_target_properties( | |
mmcc | ||
pointer-analysis | ||
solvers | ||
string_utils | ||
test-bigint | ||
testing-utils | ||
unit | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# Unit test binaries | ||
miniBDD | ||
sharing_node | ||
string_utils | ||
unit_tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
Author: Diffblue Ltd. | ||
*/ | ||
|
||
/// \file | ||
/// split_string Unit Tests | ||
|
||
#include <testing-utils/catch.hpp> | ||
#include <util/string_utils.h> | ||
|
||
struct expected_resultst | ||
{ | ||
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") | ||
{ | ||
std::vector<std::string> result; | ||
split_string(string, delimiter, result, false, false); | ||
|
||
THEN("Should get expected vector") | ||
{ | ||
REQUIRE_THAT( | ||
result, | ||
// NOLINTNEXTLINE(whitespace/braces) | ||
Catch::Matchers::Vector::EqualsMatcher<std::string>{ | ||
expected_results.no_strip_no_remove}); | ||
} | ||
} | ||
WHEN("Not stripping, removing empty") | ||
{ | ||
std::vector<std::string> result; | ||
split_string(string, delimiter, result, false, true); | ||
|
||
THEN("Should get expected vector") | ||
{ | ||
REQUIRE_THAT( | ||
result, | ||
// NOLINTNEXTLINE(whitespace/braces) | ||
Catch::Matchers::Vector::EqualsMatcher<std::string>{ | ||
expected_results.no_strip_remove_empty}); | ||
} | ||
} | ||
WHEN("Stripping, not removing empty") | ||
{ | ||
std::vector<std::string> result; | ||
split_string(string, delimiter, result, true, false); | ||
|
||
THEN("Should get expected vector") | ||
{ | ||
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 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); | ||
} | ||
GIVEN("An empty string") | ||
{ | ||
std::string string = ""; | ||
WHEN("Splitting it") | ||
{ | ||
expected_resultst expected_results; | ||
expected_results.no_strip_no_remove = {""}; | ||
expected_results.strip_no_remove = {""}; | ||
|
||
// TODO(tkiley): This is probably wrong, since I'd expect removing empty | ||
// TODO(tkiley): elements to return an empty vector here. | ||
expected_results.no_strip_remove_empty = {""}; | ||
expected_results.strip_remove_empty = {""}; | ||
|
||
run_on_all_variants(string, ',', expected_results); | ||
} | ||
} | ||
GIVEN("A whitespace only string") | ||
{ | ||
std::string string = " "; | ||
WHEN("Splitting it") | ||
{ | ||
expected_resultst expected_results; | ||
expected_results.no_strip_no_remove = {" "}; | ||
expected_results.strip_no_remove = {""}; | ||
expected_results.no_strip_remove_empty = {" "}; | ||
// TODO(tkiley): This is probably wrong, since I'd expect removing empty | ||
// TODO(tkiley): elements to return an empty vector here. | ||
expected_results.strip_remove_empty = {""}; | ||
|
||
run_on_all_variants(string, ',', expected_results); | ||
} | ||
} | ||
} | ||
|
||
SCENARIO("split_string into two", "[core][utils][string_utils][split_string]") | ||
{ | ||
GIVEN("A string with one separator") | ||
{ | ||
const char separator = ':'; | ||
std::string string = "a:b"; | ||
|
||
WHEN("Splitting into two strings") | ||
{ | ||
std::string s1; | ||
std::string s2; | ||
split_string(string, separator, s1, s2, false); | ||
THEN("The string should be split") | ||
{ | ||
REQUIRE(s1 == "a"); | ||
REQUIRE(s2 == "b"); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Author: Diffblue Ltd. | ||
*/ | ||
|
||
/// \file | ||
/// strip_string Unit Tests | ||
|
||
#include <testing-utils/catch.hpp> | ||
#include <util/string_utils.h> | ||
|
||
SCENARIO("strip_string", "[core][utils][string_utils][strip_string]") | ||
{ | ||
GIVEN("A string with some whitespace in it") | ||
{ | ||
std::string string = " x y "; | ||
WHEN("Using strip_string") | ||
{ | ||
std::string result = strip_string(string); | ||
THEN( | ||
"Whitespace at either end should be removed, but not internal " | ||
"whitespace") | ||
{ | ||
REQUIRE(result == "x y"); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, we could change this to return an empty vector. I've looked at the places where
split_string()
is currently used, and they would also work when returning an empty vector (except an assertionassert(!result.empty())
would need to be removed inutil/unwind.cpp
). Also once changed, the current usages ofsplit_string()
can be slightly simplified as some of them have a check for this case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent. Since this PR is already a pre-req for another I will put together a separate PR to address this.