From 72fe73a1043bef4a4f9e7032132f2aa50865d97e Mon Sep 17 00:00:00 2001 From: Chen Date: Wed, 17 Jun 2020 13:57:28 +0800 Subject: [PATCH] fix issue752: generate right long keys (#879) * fix issue752: generate right long keys * Improve the readability of test cases * update to raw string literal --- src/emitter.cpp | 6 ++++ test/integration/emitter_test.cpp | 46 +++++++++++++++++++++++++++++++ 2 files changed, 52 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 30607aa..8e1ee29 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -686,6 +686,52 @@ 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(R"(? &key + - a + - b +: &value + - c + - d +? *key +: *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(R"(? &key + ? a + : b +: &value + ? c + : d +? *key +: *value)"); +} + TEST_F(EmitterTest, ComplexGlobalSettings) { out << BeginSeq; out << Block;