From f69ca7987c45ff4411a14cc1edbc79801e6bf28e Mon Sep 17 00:00:00 2001 From: dota17 Date: Tue, 9 Jun 2020 16:40:37 +0800 Subject: [PATCH] fix issue752: generate right long keys --- src/emitter.cpp | 6 ++++++ test/integration/emitter_test.cpp | 32 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/emitter.cpp b/src/emitter.cpp index 06f633c..56ef403 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -556,6 +556,8 @@ void Emitter::BlockMapPrepareLongKey(EmitterNodeType::value child) { break; case EmitterNodeType::BlockSeq: case EmitterNodeType::BlockMap: + if (m_pState->HasBegunContent()) + m_stream << "\n"; break; } } @@ -579,8 +581,12 @@ void Emitter::BlockMapPrepareLongKeyValue(EmitterNodeType::value child) { case EmitterNodeType::Scalar: case EmitterNodeType::FlowSeq: case EmitterNodeType::FlowMap: + SpaceOrIndentTo(true, curIndent + 1); + break; case EmitterNodeType::BlockSeq: case EmitterNodeType::BlockMap: + if (m_pState->HasBegunContent()) + m_stream << "\n"; SpaceOrIndentTo(true, curIndent + 1); break; } diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index 3a5783b..0d2a1ab 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -612,6 +612,38 @@ TEST_F(EmitterTest, SimpleGlobalSettings) { ExpectEmit("- ? key 1\n : value 1\n ? key 2\n : [a, b, c]"); } +TEST_F(EmitterTest, GlobalLongKeyOnSeq) { + out.SetMapFormat(LongKey); + + out << BeginMap; + out << Key << Anchor("key"); + out << BeginSeq << "a" + << "b" << EndSeq; + out << Value << Anchor("value"); + out << BeginSeq << "c" + << "d" << EndSeq; + out << Key << Alias("key") << Value << Alias("value"); + out << EndMap; + + ExpectEmit("? &key\n - a\n - b\n: &value\n - c\n - d\n? *key\n: *value"); +} + +TEST_F(EmitterTest, GlobalLongKeyOnMap) { + out.SetMapFormat(LongKey); + + out << BeginMap; + out << Key << Anchor("key"); + out << BeginMap << "a" + << "b" << EndMap; + out << Value << Anchor("value"); + out << BeginMap << "c" + << "d" << EndMap; + out << Key << Alias("key") << Value << Alias("value"); + out << EndMap; + + ExpectEmit("? &key\n ? a\n : b\n: &value\n ? c\n : d\n? *key\n: *value"); +} + TEST_F(EmitterTest, ComplexGlobalSettings) { out << BeginSeq; out << Block;