Skip to content

Commit

Permalink
Parse colon in plain scalar correctly when in a flow collection
Browse files Browse the repository at this point in the history
Fixes #740.
  • Loading branch information
dota17 authored Jul 2, 2020
1 parent 1c2e767 commit 026a53f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/exp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
46 changes: 46 additions & 0 deletions test/integration/load_node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 026a53f

Please sign in to comment.