Commit b60143ea authored by zhujiashun's avatar zhujiashun

support_kv_pair_splitter: make tests more robust & refine name

parent 55f18449
......@@ -169,10 +169,10 @@ private:
// This class can also handle some exceptional cases.
// 1. consecutive key_value_pair_delimiter are omitted, for example,
// suppose key_value_delimiter is '=' and key_value_pair_delimiter
// is '&', then k1=v1&&&k2=v2 is normalized to k1=k2&k2=v2.
// 2. key or value can be empty or both can be empty
// is '&', then 'k1=v1&&&k2=v2' is normalized to 'k1=k2&k2=v2'.
// 2. key or value can be empty or both can be empty.
// 3. consecutive key_value_delimiter are not omitted, for example,
// suppose input is k1===v2 and key_value_delimiter is '=', then
// suppose input is 'k1===v2' and key_value_delimiter is '=', then
// key() returns 'k1', value() returns '==v2'.
class KeyValuePairsSplitter {
public:
......@@ -181,8 +181,8 @@ public:
char key_value_delimiter,
char key_value_pair_delimiter)
: _sp(str_begin, str_end, key_value_pair_delimiter)
, _deli_pos(StringPiece::npos)
, _key_value_delimiter(key_value_delimiter) {
, _delim_pos(StringPiece::npos)
, _key_value_delim(key_value_delimiter) {
UpdateDelimiterPosition();
}
......@@ -199,11 +199,11 @@ public:
key_value_delimiter, key_value_pair_delimiter) {}
inline StringPiece key() {
return key_and_value().substr(0, _deli_pos);
return key_and_value().substr(0, _delim_pos);
}
inline StringPiece value() {
return key_and_value().substr(_deli_pos + 1);
return key_and_value().substr(_delim_pos + 1);
}
// Get the current value of key and value
......@@ -232,8 +232,8 @@ private:
private:
StringSplitter _sp;
StringPiece::size_type _deli_pos;
const char _key_value_delimiter;
StringPiece::size_type _delim_pos;
const char _key_value_delim;
};
} // namespace butil
......
......@@ -318,9 +318,9 @@ int StringMultiSplitter::to_double(double* pv) const {
void KeyValuePairsSplitter::UpdateDelimiterPosition() {
const StringPiece key_value_pair(key_and_value());
_deli_pos = key_value_pair.find(_key_value_delimiter);
if (_deli_pos == StringPiece::npos) {
_deli_pos = key_value_pair.length();
_delim_pos = key_value_pair.find(_key_value_delim);
if (_delim_pos == StringPiece::npos) {
_delim_pos = key_value_pair.length();
}
}
......
......@@ -338,9 +338,20 @@ TEST_F(StringSplitterTest, split_limit_len) {
}
TEST_F(StringSplitterTest, key_value_pairs_splitter_sanity) {
std::string kvstr = "key1=value1&&&key2=value2&key3=value3";
{
butil::KeyValuePairsSplitter splitter(kvstr, '=', '&');
std::string kvstr = "key1=value1&&&key2=value2&key3=value3&===&key4=&=&=value5";
for (int i = 0 ; i < 3; ++i) {
// Test three constructors
butil::KeyValuePairsSplitter* psplitter = NULL;
if (i == 0) {
psplitter = new butil::KeyValuePairsSplitter(kvstr, '=', '&');
} else if (i == 1) {
psplitter = new butil::KeyValuePairsSplitter(
kvstr.data(), kvstr.data() + kvstr.size(), '=', '&');
} else if (i == 2) {
psplitter = new butil::KeyValuePairsSplitter(kvstr.c_str(), '=', '&');
}
butil::KeyValuePairsSplitter& splitter = *psplitter;
ASSERT_TRUE(splitter);
ASSERT_EQ(splitter.key(), "key1");
ASSERT_EQ(splitter.value(), "value1");
......@@ -353,39 +364,25 @@ TEST_F(StringSplitterTest, key_value_pairs_splitter_sanity) {
ASSERT_EQ(splitter.key(), "key3");
ASSERT_EQ(splitter.value(), "value3");
++splitter;
ASSERT_FALSE(splitter);
}
{
butil::KeyValuePairsSplitter splitter(kvstr.data(), kvstr.data() + kvstr.size(), '=', '&');
ASSERT_TRUE(splitter);
ASSERT_EQ(splitter.key(), "key1");
ASSERT_EQ(splitter.value(), "value1");
++splitter;
ASSERT_TRUE(splitter);
ASSERT_EQ(splitter.key(), "key2");
ASSERT_EQ(splitter.value(), "value2");
++splitter;
ASSERT_TRUE(splitter);
ASSERT_EQ(splitter.key(), "key3");
ASSERT_EQ(splitter.value(), "value3");
ASSERT_EQ(splitter.key(), "");
ASSERT_EQ(splitter.value(), "==");
++splitter;
ASSERT_FALSE(splitter);
}
{
butil::KeyValuePairsSplitter splitter(kvstr.c_str(), '=', '&');
ASSERT_TRUE(splitter);
ASSERT_EQ(splitter.key(), "key1");
ASSERT_EQ(splitter.value(), "value1");
ASSERT_EQ(splitter.key(), "key4");
ASSERT_EQ(splitter.value(), "");
++splitter;
ASSERT_TRUE(splitter);
ASSERT_EQ(splitter.key(), "key2");
ASSERT_EQ(splitter.value(), "value2");
ASSERT_EQ(splitter.key(), "");
ASSERT_EQ(splitter.value(), "");
++splitter;
ASSERT_TRUE(splitter);
ASSERT_EQ(splitter.key(), "key3");
ASSERT_EQ(splitter.value(), "value3");
ASSERT_EQ(splitter.key(), "");
ASSERT_EQ(splitter.value(), "value5");
++splitter;
ASSERT_FALSE(splitter);
delete psplitter;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment