From 026a53fbe1860509b7717d39b81c06e4421bec1d Mon Sep 17 00:00:00 2001 From: Chen Date: Fri, 3 Jul 2020 03:08:14 +0800 Subject: [PATCH] Parse colon in plain scalar correctly when in a flow collection Fixes #740. --- src/exp.h | 2 +- test/integration/load_node_test.cpp | 46 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/exp.h b/src/exp.h index 33c4b7b34..301449e8f 100644 --- a/src/exp.h +++ b/src/exp.h @@ -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() { diff --git a/test/integration/load_node_test.cpp b/test/integration/load_node_test.cpp index b28cd3736..8f3bece7a 100644 --- a/test/integration/load_node_test.cpp +++ b/test/integration/load_node_test.cpp @@ -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 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 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());