Parse colon in plain scalar correctly when in a flow collection

Fixes #740.
This commit is contained in:
Chen 2020-07-03 03:08:14 +08:00 committed by GitHub
parent 1c2e767347
commit 026a53fbe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -110,7 +110,7 @@ inline const RegEx& Value() {
return e;
}
inline const RegEx& ValueInFlow() {
static const RegEx e = RegEx(':') + (BlankOrBreak() | RegEx(",}", REGEX_OR));
static const RegEx e = RegEx(':') + (BlankOrBreak() | RegEx(",]}", REGEX_OR));
return e;
}
inline const RegEx& ValueInJSONFlow() {

View File

@ -265,6 +265,52 @@ 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, "[~]"},
{"[a:,]", NodeType::Sequence, 1, "[{a: ~}]"},
{"{:}", NodeType::Map, 1, "{~: ~}"},
{"{a:}", NodeType::Map, 1, "{a: ~}"},
{"{:a}", NodeType::Map, 1, "{:a: ~}"},
{"{,}", NodeType::Map, 1, "{~: ~}"},
{"{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());