fix issue740: parse colon in plain scalar correctly

This commit is contained in:
dota17 2020-07-01 11:10:15 +08:00
parent 97d1ae547c
commit a62add807c
3 changed files with 55 additions and 12 deletions

View File

@ -164,8 +164,7 @@ inline const RegEx& EndScalar() {
}
inline const RegEx& EndScalarInFlow() {
static const RegEx e =
(RegEx(':') + (BlankOrBreak() | RegEx() | RegEx(",]}", REGEX_OR))) |
RegEx(",?[]{}", REGEX_OR);
(RegEx(':') + (BlankOrBreak() | RegEx())) | RegEx(",?[]{}", REGEX_OR);
return e;
}

View File

@ -246,13 +246,13 @@ struct ParserExceptionTestCase {
TEST(NodeTest, IncompleteJson) {
std::vector<ParserExceptionTestCase> tests = {
{"JSON map without value", "{\"access\"", ErrorMsg::END_OF_MAP_FLOW},
{"JSON map with colon but no value", "{\"access\":",
ErrorMsg::END_OF_MAP_FLOW},
{"JSON map with unclosed value quote", "{\"access\":\"",
ErrorMsg::END_OF_MAP_FLOW},
{"JSON map without end brace", "{\"access\":\"abc\"",
ErrorMsg::END_OF_MAP_FLOW},
{"JSON map without value", "{\"access\"", ErrorMsg::END_OF_MAP_FLOW},
{"JSON map with colon but no value", "{\"access\":",
ErrorMsg::END_OF_MAP_FLOW},
{"JSON map with unclosed value quote", "{\"access\":\"",
ErrorMsg::END_OF_MAP_FLOW},
{"JSON map without end brace", "{\"access\":\"abc\"",
ErrorMsg::END_OF_MAP_FLOW},
};
for (const ParserExceptionTestCase& test : tests) {
try {
@ -265,6 +265,50 @@ TEST(NodeTest, IncompleteJson) {
}
}
struct SingleNodeTestCase {
std::string input;
NodeType::value nodeType;
int nodeSize;
std::string expected_content;
};
TEST(NodeTest, SpecialFlow) {
std::vector<SingleNodeTestCase> tests = {
{"[,]", NodeType::Sequence, 1, "[~]"},
{"[a:]", NodeType::Sequence, 1, "[\"a:\"]"},
{"[:a]", NodeType::Sequence, 1, "[:a]"},
{"[:]", NodeType::Sequence, 1, "[\":\"]"},
{"{:}", NodeType::Map, 1, "{~: ~}"},
{"{,}", NodeType::Map, 1, "{~: ~}"},
{"{a:}", NodeType::Map, 1, "{\"a:\": ~}"},
{"{:a}", NodeType::Map, 1, "{:a: ~}"},
};
for (const SingleNodeTestCase& test : tests) {
Node node = Load(test.input);
Emitter emitter;
emitter << node;
EXPECT_EQ(test.nodeType, node.Type());
EXPECT_EQ(test.nodeSize, node.size());
EXPECT_EQ(test.expected_content, std::string(emitter.c_str()));
}
}
TEST(NodeTest, IncorrectFlow) {
std::vector<ParserExceptionTestCase> tests = {
{"Incorrect yaml: \"{:]\"", "{:]", ErrorMsg::FLOW_END},
{"Incorrect yaml: \"[:}\"", "[:}", ErrorMsg::FLOW_END},
};
for (const ParserExceptionTestCase test : tests) {
try {
Load(test.input);
FAIL() << "Expected exception " << test.expected_exception << " for "
<< test.name << ", input: " << test.input;
} catch (const ParserException& e) {
EXPECT_EQ(test.expected_exception, e.msg);
}
}
}
TEST(NodeTest, LoadTildeAsNull) {
Node node = Load("~");
ASSERT_TRUE(node.IsNull());

View File

@ -552,7 +552,7 @@ const char *ex7_2 =
const char *ex7_3 =
"{\n"
" ? foo :,\n"
" ? foo : ,\n"
" : bar,\n"
"}\n";
@ -641,8 +641,8 @@ const char *ex7_17 =
"{\n"
"unquoted : \"separate\",\n"
"http://foo.com,\n"
"omitted value:,\n"
": omitted key,\n"
"omitted value: ,\n"
" : omitted key,\n"
"}";
const char *ex7_18 =