From 1da813f56aaed91b0bb1f38d770ab4d9cc1776fb Mon Sep 17 00:00:00 2001 From: IMaloney Date: Sat, 12 Oct 2024 16:03:37 -0400 Subject: [PATCH] ensuring local tags are retained after loading a node + test --- src/emitfromevents.cpp | 9 +++++++-- test/integration/emitter_test.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/emitfromevents.cpp b/src/emitfromevents.cpp index 2e97187b9..350bde648 100644 --- a/src/emitfromevents.cpp +++ b/src/emitfromevents.cpp @@ -116,8 +116,13 @@ void EmitFromEvents::BeginNode() { } void EmitFromEvents::EmitProps(const std::string& tag, anchor_t anchor) { - if (!tag.empty() && tag != "?" && tag != "!") - m_emitter << VerbatimTag(tag); + if (!tag.empty() && tag != "?" && tag != "!"){ + if (tag[0] == '!') { + m_emitter << LocalTag(std::string(tag.begin()+1, tag.end())); + } else { + m_emitter << VerbatimTag(tag); + } + } if (anchor) m_emitter << Anchor(ToString(anchor)); } diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index 1a4c61773..95225591f 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -629,6 +629,17 @@ TEST_F(EmitterTest, LocalTagWithScalar) { ExpectEmit("!foo bar"); } +TEST_F(EmitterTest, LocalTagRetainedAfterLoadingNode) { + Node n = Node("hello"); + out << LocalTag("foo") << n; + std::string expected = "!foo hello"; + ExpectEmit(expected); + Node yamlNode = Load(out.c_str()); + Emitter emitter; + emitter << yamlNode; + EXPECT_EQ(expected, emitter.c_str()); +} + TEST_F(EmitterTest, ComplexDoc) { out << BeginMap; out << Key << "receipt";