Skip to content

Commit 026a53f

Browse files
authored
Parse colon in plain scalar correctly when in a flow collection
Fixes jbeder#740.
1 parent 1c2e767 commit 026a53f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/exp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ inline const RegEx& Value() {
110110
return e;
111111
}
112112
inline const RegEx& ValueInFlow() {
113-
static const RegEx e = RegEx(':') + (BlankOrBreak() | RegEx(",}", REGEX_OR));
113+
static const RegEx e = RegEx(':') + (BlankOrBreak() | RegEx(",]}", REGEX_OR));
114114
return e;
115115
}
116116
inline const RegEx& ValueInJSONFlow() {

test/integration/load_node_test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,52 @@ TEST(NodeTest, IncompleteJson) {
265265
}
266266
}
267267

268+
struct SingleNodeTestCase {
269+
std::string input;
270+
NodeType::value nodeType;
271+
int nodeSize;
272+
std::string expected_content;
273+
};
274+
275+
TEST(NodeTest, SpecialFlow) {
276+
std::vector<SingleNodeTestCase> tests = {
277+
{"[:]", NodeType::Sequence, 1, "[{~: ~}]"},
278+
{"[a:]", NodeType::Sequence, 1, "[{a: ~}]"},
279+
{"[:a]", NodeType::Sequence, 1, "[:a]"},
280+
{"[,]", NodeType::Sequence, 1, "[~]"},
281+
{"[a:,]", NodeType::Sequence, 1, "[{a: ~}]"},
282+
{"{:}", NodeType::Map, 1, "{~: ~}"},
283+
{"{a:}", NodeType::Map, 1, "{a: ~}"},
284+
{"{:a}", NodeType::Map, 1, "{:a: ~}"},
285+
{"{,}", NodeType::Map, 1, "{~: ~}"},
286+
{"{a:,}", NodeType::Map, 1, "{a: ~}"},
287+
};
288+
for (const SingleNodeTestCase& test : tests) {
289+
Node node = Load(test.input);
290+
Emitter emitter;
291+
emitter << node;
292+
EXPECT_EQ(test.nodeType, node.Type());
293+
EXPECT_EQ(test.nodeSize, node.size());
294+
EXPECT_EQ(test.expected_content, std::string(emitter.c_str()));
295+
}
296+
}
297+
298+
TEST(NodeTest, IncorrectFlow) {
299+
std::vector<ParserExceptionTestCase> tests = {
300+
{"Incorrect yaml: \"{:]\"", "{:]", ErrorMsg::FLOW_END},
301+
{"Incorrect yaml: \"[:}\"", "[:}", ErrorMsg::FLOW_END},
302+
};
303+
for (const ParserExceptionTestCase test : tests) {
304+
try {
305+
Load(test.input);
306+
FAIL() << "Expected exception " << test.expected_exception << " for "
307+
<< test.name << ", input: " << test.input;
308+
} catch (const ParserException& e) {
309+
EXPECT_EQ(test.expected_exception, e.msg);
310+
}
311+
}
312+
}
313+
268314
TEST(NodeTest, LoadTildeAsNull) {
269315
Node node = Load("~");
270316
ASSERT_TRUE(node.IsNull());

0 commit comments

Comments
 (0)