Add more unit tests for split string function #4933

This commit is contained in:
Jerry (Xinyu Hou) 2015-10-29 13:00:03 -07:00
parent 1b73b8875b
commit d429988e73
2 changed files with 40 additions and 6 deletions

View File

@ -232,7 +232,9 @@ splitString(String string, const char c)
size_t head = 0; size_t head = 0;
size_t separator = string.find(c); size_t separator = string.find(c);
while (separator != String::npos) { while (separator != String::npos) {
if (head!=separator) {
results.push_back(string.substr(head, separator - head)); results.push_back(string.substr(head, separator - head));
}
head = separator + 1; head = separator + 1;
separator = string.find(c, head); separator = string.find(c, head);
} }

View File

@ -101,7 +101,7 @@ TEST(StringTests, stringToUint)
EXPECT_EQ(123, value); EXPECT_EQ(123, value);
} }
TEST(StringTests, splitString1) TEST(StringTests, splitString_twoSeparator_returnThreeParts)
{ {
String string = "stub1:stub2:stub3"; String string = "stub1:stub2:stub3";
@ -113,7 +113,28 @@ TEST(StringTests, splitString1)
EXPECT_EQ("stub3", results[2]); EXPECT_EQ("stub3", results[2]);
} }
TEST(StringTests, splitString2) TEST(StringTests, splitString_oneSeparator_returnTwoParts)
{
String string = "stub1:stub2";
std::vector<String> results = string::splitString(string, ':');
EXPECT_EQ(2, results.size());
EXPECT_EQ("stub1", results[0]);
EXPECT_EQ("stub2", results[1]);
}
TEST(StringTests, splitString_noSeparator_returnOriginalString)
{
String string = "stub1";
std::vector<String> results = string::splitString(string, ':');
EXPECT_EQ(1, results.size());
EXPECT_EQ("stub1", results[0]);
}
TEST(StringTests, splitString_tailSeparator_returnTwoParts)
{ {
String string = "stub1:stub2:"; String string = "stub1:stub2:";
@ -124,13 +145,24 @@ TEST(StringTests, splitString2)
EXPECT_EQ("stub2", results[1]); EXPECT_EQ("stub2", results[1]);
} }
TEST(StringTests, splitString3) TEST(StringTests, splitString_headSeparator_returnTwoParts)
{ {
String string = "stub1"; String string = ":stub1:stub2";
std::vector<String> results = string::splitString(string, ':'); std::vector<String> results = string::splitString(string, ':');
EXPECT_EQ(1, results.size()); EXPECT_EQ(2, results.size());
EXPECT_EQ("stub1", results[0]); EXPECT_EQ("stub1", results[0]);
EXPECT_EQ("stub2", results[1]);
} }
TEST(StringTests, splitString_headAndTailSeparators_returnTwoParts)
{
String string = ":stub1:stub2:";
std::vector<String> results = string::splitString(string, ':');
EXPECT_EQ(2, results.size());
EXPECT_EQ("stub1", results[0]);
EXPECT_EQ("stub2", results[1]);
}