Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow + Newline + EmptyMap bug #743

Closed
MidoriYakumo opened this issue Sep 24, 2019 · 2 comments
Closed

Flow + Newline + EmptyMap bug #743

MidoriYakumo opened this issue Sep 24, 2019 · 2 comments

Comments

@MidoriYakumo
Copy link

Add following test case:

TEST_F(EmitterTest, FlowNewlineEmpty)
{
        out << YAML::BeginMap;
        out << YAML::Key << "foo";
        out << YAML::Value << YAML::Flow << YAML::BeginMap << YAML::Newline << YAML::EndMap;
        out << YAML::EndMap;

        ExpectEmit("foo: {\n  }\n");
}

but result is "foo: {\n {}\n"

@MidoriYakumo
Copy link
Author

MidoriYakumo commented Sep 24, 2019

Can be fixed with diff:

diff --git a/src/emitter.cpp b/src/emitter.cpp
index 016beb1..334d0d9 100644
--- a/src/emitter.cpp
+++ b/src/emitter.cpp
@@ -198,6 +198,7 @@ void Emitter::EmitEndSeq() {
   if (!good())
     return;
 
+  bool need_left_brace = !m_pState->HasBegunNode() || m_stream.comment();
   if (m_pState->CurGroupChildCount() == 0)
     m_pState->ForceFlow();
 
@@ -205,7 +206,7 @@ void Emitter::EmitEndSeq() {
     if (m_stream.comment())
       m_stream << "\n";
     m_stream << IndentTo(m_pState->CurIndent());
-    if (m_pState->CurGroupChildCount() == 0)
+    if (m_pState->CurGroupChildCount() == 0 && need_left_brace)
       m_stream << "[";
     m_stream << "]";
   }
@@ -228,6 +229,7 @@ void Emitter::EmitEndMap() {
   if (!good())
     return;
 
+  bool need_left_brace = !m_pState->HasBegunNode() || m_stream.comment();
   if (m_pState->CurGroupChildCount() == 0)
     m_pState->ForceFlow();
 
@@ -235,7 +237,7 @@ void Emitter::EmitEndMap() {
     if (m_stream.comment())
       m_stream << "\n";
     m_stream << IndentTo(m_pState->CurIndent());
-    if (m_pState->CurGroupChildCount() == 0)
+    if (m_pState->CurGroupChildCount() == 0 && need_left_brace)
       m_stream << "{";
     m_stream << "}";
   }
diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp
index 3a5783b..8ae2452 100644
--- a/test/integration/emitter_test.cpp
+++ b/test/integration/emitter_test.cpp
@@ -1075,5 +1075,15 @@ TEST_F(EmitterErrorTest, InvalidAlias) {
 
   ExpectEmitError(ErrorMsg::INVALID_ALIAS);
 }
+
+TEST_F(EmitterTest, FlowEmptyNewlineMap)
+{
+  out << YAML::BeginMap;
+  out << YAML::Key << "foo";
+  out << YAML::Value << YAML::Flow << YAML::BeginMap << YAML::Newline << YAML::EndMap;
+  out << YAML::EndMap;
+
+  ExpectEmit("foo: {\n  }");
+}
 }  // namespace
 }  // namespace YAML

@dota17
Copy link
Collaborator

dota17 commented Jul 30, 2020

Fixed by #921.

@dota17 dota17 closed this as completed Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants