fix issue743: handle the empty content of flow sep/map correctly during emitting.

This commit is contained in:
dota17 2020-07-11 17:13:30 +08:00
parent 370aceeaf8
commit 2f3938e9fe
2 changed files with 76 additions and 2 deletions

View File

@ -205,6 +205,7 @@ void Emitter::EmitBeginSeq() {
void Emitter::EmitEndSeq() {
if (!good())
return;
FlowType::value originalType = m_pState->CurGroupFlowType();
if (m_pState->CurGroupChildCount() == 0)
m_pState->ForceFlow();
@ -213,8 +214,12 @@ void Emitter::EmitEndSeq() {
if (m_stream.comment())
m_stream << "\n";
m_stream << IndentTo(m_pState->CurIndent());
if (m_pState->CurGroupChildCount() == 0)
if (originalType == FlowType::Block) {
m_stream << "[";
} else {
if (m_pState->CurGroupChildCount() == 0 && !m_pState->HasBegunNode())
m_stream << "[";
}
m_stream << "]";
}
@ -235,6 +240,7 @@ void Emitter::EmitBeginMap() {
void Emitter::EmitEndMap() {
if (!good())
return;
FlowType::value originalType = m_pState->CurGroupFlowType();
if (m_pState->CurGroupChildCount() == 0)
m_pState->ForceFlow();
@ -243,8 +249,12 @@ void Emitter::EmitEndMap() {
if (m_stream.comment())
m_stream << "\n";
m_stream << IndentTo(m_pState->CurIndent());
if (m_pState->CurGroupChildCount() == 0)
if (originalType == FlowType::Block) {
m_stream << "{";
} else {
if (m_pState->CurGroupChildCount() == 0 && !m_pState->HasBegunNode())
m_stream << "{";
}
m_stream << "}";
}

View File

@ -138,6 +138,70 @@ TEST_F(EmitterTest, EmptyFlowSeq) {
ExpectEmit("[]");
}
TEST_F(EmitterTest, EmptyBlockSeqWithBegunContent) {
out << BeginSeq;
out << BeginSeq << Comment("comment") << EndSeq;
out << BeginSeq << Newline << EndSeq;
out << BeginSeq << Anchor("test") << EndSeq;
out << BeginSeq << VerbatimTag("foo") << EndSeq;
out << EndSeq;
ExpectEmit(R"(-
# comment
[]
-
[]
-
- &test[]
-
- !<foo>[])");
}
TEST_F(EmitterTest, EmptyBlockMapWithBegunContent) {
out << BeginSeq;
out << BeginMap << Comment("comment") << EndMap;
out << BeginMap << Newline << EndMap;
out << BeginMap << Anchor("test") << EndMap;
out << BeginMap << VerbatimTag("foo") << EndMap;
out << EndSeq;
ExpectEmit(R"(- # comment
{}
-
{}
- &test{}
- !<foo>{})");
}
TEST_F(EmitterTest, EmptyFlowSeqWithBegunContent) {
out << Flow;
out << BeginSeq;
out << BeginSeq << Comment("comment") << EndSeq;
out << BeginSeq << Newline << EndSeq;
out << BeginSeq << Anchor("test") << EndSeq;
out << BeginSeq << VerbatimTag("foo") << EndSeq;
out << EndSeq;
ExpectEmit(R"([[ # comment
], [
], [&test], [!<foo>]])");
}
TEST_F(EmitterTest, EmptyFlowMapWithBegunContent) {
out << Flow;
out << BeginSeq;
out << BeginMap << Comment("comment") << EndMap;
out << BeginMap << Newline << EndMap;
out << BeginMap << Anchor("test") << EndMap;
out << BeginMap << VerbatimTag("foo") << EndMap;
out << EndSeq;
ExpectEmit(R"([{ # comment
}, {
}, {&test}, {!<foo>}])");
}
TEST_F(EmitterTest, NestedBlockSeq) {
out << BeginSeq;
out << "item 1";