diff --git a/src/emitter.cpp b/src/emitter.cpp index cf093e0..33e1466 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -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 << "}"; } diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index 4cae36f..6ab3e8c 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -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[] +- + - ![])"); +} + +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{} +- !{})"); +} + +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], [!]])"); +} + +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}, {!}])"); +} + TEST_F(EmitterTest, NestedBlockSeq) { out << BeginSeq; out << "item 1";