Skip to content

Commit 1c9abc8

Browse files
authored
fix issue743: handle the empty content of flow sep/map correctly during emitting. (jbeder#921)
* fix issue743: handle the empty content of flow sep/map correctly during emitting. * handle the empty Tag/Anchor properly. * delete comment
1 parent 11917ba commit 1c9abc8

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/emitter.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ void Emitter::EmitBeginSeq() {
205205
void Emitter::EmitEndSeq() {
206206
if (!good())
207207
return;
208+
FlowType::value originalType = m_pState->CurGroupFlowType();
208209

209210
if (m_pState->CurGroupChildCount() == 0)
210211
m_pState->ForceFlow();
@@ -213,8 +214,12 @@ void Emitter::EmitEndSeq() {
213214
if (m_stream.comment())
214215
m_stream << "\n";
215216
m_stream << IndentTo(m_pState->CurIndent());
216-
if (m_pState->CurGroupChildCount() == 0)
217+
if (originalType == FlowType::Block) {
217218
m_stream << "[";
219+
} else {
220+
if (m_pState->CurGroupChildCount() == 0 && !m_pState->HasBegunNode())
221+
m_stream << "[";
222+
}
218223
m_stream << "]";
219224
}
220225

@@ -235,6 +240,7 @@ void Emitter::EmitBeginMap() {
235240
void Emitter::EmitEndMap() {
236241
if (!good())
237242
return;
243+
FlowType::value originalType = m_pState->CurGroupFlowType();
238244

239245
if (m_pState->CurGroupChildCount() == 0)
240246
m_pState->ForceFlow();
@@ -243,8 +249,12 @@ void Emitter::EmitEndMap() {
243249
if (m_stream.comment())
244250
m_stream << "\n";
245251
m_stream << IndentTo(m_pState->CurIndent());
246-
if (m_pState->CurGroupChildCount() == 0)
252+
if (originalType == FlowType::Block) {
247253
m_stream << "{";
254+
} else {
255+
if (m_pState->CurGroupChildCount() == 0 && !m_pState->HasBegunNode())
256+
m_stream << "{";
257+
}
248258
m_stream << "}";
249259
}
250260

src/emitterstate.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ void EmitterState::EndedGroup(GroupType::value type) {
160160
return SetError(ErrorMsg::UNEXPECTED_END_MAP);
161161
}
162162

163+
if (m_hasTag) {
164+
SetError(ErrorMsg::INVALID_TAG);
165+
}
166+
if (m_hasAnchor) {
167+
SetError(ErrorMsg::INVALID_ANCHOR);
168+
}
169+
163170
// get rid of the current group
164171
{
165172
std::unique_ptr<Group> pFinishedGroup = std::move(m_groups.back());

test/integration/emitter_test.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,56 @@ TEST_F(EmitterTest, EmptyFlowSeq) {
138138
ExpectEmit("[]");
139139
}
140140

141+
TEST_F(EmitterTest, EmptyBlockSeqWithBegunContent) {
142+
out << BeginSeq;
143+
out << BeginSeq << Comment("comment") << EndSeq;
144+
out << BeginSeq << Newline << EndSeq;
145+
out << EndSeq;
146+
147+
ExpectEmit(R"(-
148+
# comment
149+
[]
150+
-
151+
152+
[])");
153+
}
154+
155+
TEST_F(EmitterTest, EmptyBlockMapWithBegunContent) {
156+
out << BeginSeq;
157+
out << BeginMap << Comment("comment") << EndMap;
158+
out << BeginMap << Newline << EndMap;
159+
out << EndSeq;
160+
161+
ExpectEmit(R"(- # comment
162+
{}
163+
-
164+
{})");
165+
}
166+
167+
TEST_F(EmitterTest, EmptyFlowSeqWithBegunContent) {
168+
out << Flow;
169+
out << BeginSeq;
170+
out << BeginSeq << Comment("comment") << EndSeq;
171+
out << BeginSeq << Newline << EndSeq;
172+
out << EndSeq;
173+
174+
ExpectEmit(R"([[ # comment
175+
], [
176+
]])");
177+
}
178+
179+
TEST_F(EmitterTest, EmptyFlowMapWithBegunContent) {
180+
out << Flow;
181+
out << BeginSeq;
182+
out << BeginMap << Comment("comment") << EndMap;
183+
out << BeginMap << Newline << EndMap;
184+
out << EndSeq;
185+
186+
ExpectEmit(R"([{ # comment
187+
}, {
188+
}])");
189+
}
190+
141191
TEST_F(EmitterTest, NestedBlockSeq) {
142192
out << BeginSeq;
143193
out << "item 1";
@@ -1553,6 +1603,26 @@ TEST_F(EmitterErrorTest, BadLocalTag) {
15531603
ExpectEmitError("invalid tag");
15541604
}
15551605

1606+
TEST_F(EmitterErrorTest, BadTagAndTag) {
1607+
out << VerbatimTag("!far") << VerbatimTag("!foo") << "bar";
1608+
ExpectEmitError(ErrorMsg::INVALID_TAG);
1609+
}
1610+
1611+
TEST_F(EmitterErrorTest, BadAnchorAndAnchor) {
1612+
out << Anchor("far") << Anchor("foo") << "bar";
1613+
ExpectEmitError(ErrorMsg::INVALID_ANCHOR);
1614+
}
1615+
1616+
TEST_F(EmitterErrorTest, BadEmptyAnchorOnGroup) {
1617+
out << BeginSeq << "bar" << Anchor("foo") << EndSeq;
1618+
ExpectEmitError(ErrorMsg::INVALID_ANCHOR);
1619+
}
1620+
1621+
TEST_F(EmitterErrorTest, BadEmptyTagOnGroup) {
1622+
out << BeginSeq << "bar" << VerbatimTag("!foo") << EndSeq;
1623+
ExpectEmitError(ErrorMsg::INVALID_TAG);
1624+
}
1625+
15561626
TEST_F(EmitterErrorTest, ExtraEndSeq) {
15571627
out << BeginSeq;
15581628
out << "Hello";

0 commit comments

Comments
 (0)