Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Unified Diff: base/strings/string_split_unittest.cc

Issue 1169393003: Add new SplitString backend. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/strings/string_split.cc ('k') | base/strings/string_util.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/strings/string_split_unittest.cc
diff --git a/base/strings/string_split_unittest.cc b/base/strings/string_split_unittest.cc
index 32bbe28b65f2f160c837d9d9b11e31e08b19c4c3..c745ab58f08e97d1d0cf72a9d1854ad39f220855 100644
--- a/base/strings/string_split_unittest.cc
+++ b/base/strings/string_split_unittest.cc
@@ -169,7 +169,81 @@ TEST(SplitStringUsingSubstrTest, EmptyString) {
EXPECT_THAT(results, ElementsAre(""));
}
-TEST(StringUtilTest, SplitString) {
+TEST(StringUtilTest, SplitString_Basics) {
+ std::vector<std::string> r;
+
+ r = SplitString(std::string(), ",:;", KEEP_WHITESPACE, SPLIT_WANT_ALL);
+ EXPECT_TRUE(r.empty());
+
+ // Empty separator list
+ r = SplitString("hello, world", "", KEEP_WHITESPACE, SPLIT_WANT_ALL);
+ ASSERT_EQ(1u, r.size());
+ EXPECT_EQ("hello, world", r[0]);
+
+ // Should split on any of the separators.
+ r = SplitString("::,,;;", ",:;", KEEP_WHITESPACE, SPLIT_WANT_ALL);
+ ASSERT_EQ(7u, r.size());
+ for (auto str : r)
+ ASSERT_TRUE(str.empty());
+
+ r = SplitString("red, green; blue:", ",:;", TRIM_WHITESPACE,
+ SPLIT_WANT_NONEMPTY);
+ ASSERT_EQ(3u, r.size());
+ EXPECT_EQ("red", r[0]);
+ EXPECT_EQ("green", r[1]);
+ EXPECT_EQ("blue", r[2]);
+
+ // Want to split a string along whitespace sequences.
+ r = SplitString(" red green \tblue\n", " \t\n", TRIM_WHITESPACE,
+ SPLIT_WANT_NONEMPTY);
+ ASSERT_EQ(3u, r.size());
+ EXPECT_EQ("red", r[0]);
+ EXPECT_EQ("green", r[1]);
+ EXPECT_EQ("blue", r[2]);
+
+ // Weird case of splitting on spaces but not trimming.
+ r = SplitString(" red ", " ", TRIM_WHITESPACE, SPLIT_WANT_ALL);
+ ASSERT_EQ(3u, r.size());
+ EXPECT_EQ("", r[0]); // Before the first space.
+ EXPECT_EQ("red", r[1]);
+ EXPECT_EQ("", r[2]); // After the last space.
+}
+
+TEST(StringUtilTest, SplitString_WhitespaceAndResultType) {
+ std::vector<std::string> r;
+
+ // Empty input handling.
+ r = SplitString(std::string(), ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
+ EXPECT_TRUE(r.empty());
+ r = SplitString(std::string(), ",", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
+ EXPECT_TRUE(r.empty());
+
+ // Input string is space and we're trimming.
+ r = SplitString(" ", ",", TRIM_WHITESPACE, SPLIT_WANT_ALL);
+ ASSERT_EQ(1u, r.size());
+ EXPECT_EQ("", r[0]);
+ r = SplitString(" ", ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
+ EXPECT_TRUE(r.empty());
+
+ // Test all 4 combinations of flags on ", ,".
+ r = SplitString(", ,", ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
+ ASSERT_EQ(3u, r.size());
+ EXPECT_EQ("", r[0]);
+ EXPECT_EQ(" ", r[1]);
+ EXPECT_EQ("", r[2]);
+ r = SplitString(", ,", ",", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
+ ASSERT_EQ(1u, r.size());
+ ASSERT_EQ(" ", r[0]);
+ r = SplitString(", ,", ",", TRIM_WHITESPACE, SPLIT_WANT_ALL);
+ ASSERT_EQ(3u, r.size());
+ EXPECT_EQ("", r[0]);
+ EXPECT_EQ("", r[1]);
+ EXPECT_EQ("", r[2]);
+ r = SplitString(", ,", ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
+ ASSERT_TRUE(r.empty());
+}
+
+TEST(StringUtilTest, SplitString_Legacy) {
std::vector<std::wstring> r;
SplitString(std::wstring(), L',', &r);
@@ -197,6 +271,13 @@ TEST(StringUtilTest, SplitString) {
EXPECT_EQ(r[2], L"c");
r.clear();
+ SplitString(L"a, ,c", L',', &r);
+ ASSERT_EQ(3U, r.size());
+ EXPECT_EQ(r[0], L"a");
+ EXPECT_EQ(r[1], L"");
+ EXPECT_EQ(r[2], L"c");
+ r.clear();
+
SplitString(L" ", L'*', &r);
EXPECT_EQ(0U, r.size());
r.clear();
« no previous file with comments | « base/strings/string_split.cc ('k') | base/strings/string_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698