From 683c4d851732299242a0ec5c25aee360024f44a1 Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 22 Jul 2020 17:31:04 +0800 Subject: [PATCH] handle the empty Tag/Anchor properly. --- src/emitterstate.cpp | 6 +++++ test/integration/emitter_test.cpp | 42 ++++++++++++++++++------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/emitterstate.cpp b/src/emitterstate.cpp index 40497f7..084f822 100644 --- a/src/emitterstate.cpp +++ b/src/emitterstate.cpp @@ -160,6 +160,12 @@ void EmitterState::EndedGroup(GroupType::value type) { return SetError(ErrorMsg::UNEXPECTED_END_MAP); } + // if have the unhandled Tag/Anchor, set the error message + if (m_hasTag) + SetError(ErrorMsg::INVALID_TAG); + if (m_hasAnchor) + SetError(ErrorMsg::INVALID_ANCHOR); + // get rid of the current group { std::unique_ptr pFinishedGroup = std::move(m_groups.back()); diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index 6ab3e8c..57cfb62 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -142,8 +142,6 @@ TEST_F(EmitterTest, EmptyBlockSeqWithBegunContent) { out << BeginSeq; out << BeginSeq << Comment("comment") << EndSeq; out << BeginSeq << Newline << EndSeq; - out << BeginSeq << Anchor("test") << EndSeq; - out << BeginSeq << VerbatimTag("foo") << EndSeq; out << EndSeq; ExpectEmit(R"(- @@ -151,27 +149,19 @@ TEST_F(EmitterTest, EmptyBlockSeqWithBegunContent) { [] - - [] -- - - &test[] -- - - ![])"); + [])"); } TEST_F(EmitterTest, EmptyBlockMapWithBegunContent) { out << BeginSeq; out << BeginMap << Comment("comment") << EndMap; out << BeginMap << Newline << EndMap; - out << BeginMap << Anchor("test") << EndMap; - out << BeginMap << VerbatimTag("foo") << EndMap; out << EndSeq; ExpectEmit(R"(- # comment {} - - {} -- &test{} -- !{})"); + {})"); } TEST_F(EmitterTest, EmptyFlowSeqWithBegunContent) { @@ -179,13 +169,11 @@ TEST_F(EmitterTest, EmptyFlowSeqWithBegunContent) { out << BeginSeq; out << BeginSeq << Comment("comment") << EndSeq; out << BeginSeq << Newline << EndSeq; - out << BeginSeq << Anchor("test") << EndSeq; - out << BeginSeq << VerbatimTag("foo") << EndSeq; out << EndSeq; ExpectEmit(R"([[ # comment ], [ - ], [&test], [!]])"); + ]])"); } TEST_F(EmitterTest, EmptyFlowMapWithBegunContent) { @@ -193,13 +181,11 @@ TEST_F(EmitterTest, EmptyFlowMapWithBegunContent) { out << BeginSeq; out << BeginMap << Comment("comment") << EndMap; out << BeginMap << Newline << EndMap; - out << BeginMap << Anchor("test") << EndMap; - out << BeginMap << VerbatimTag("foo") << EndMap; out << EndSeq; ExpectEmit(R"([{ # comment }, { - }, {&test}, {!}])"); + }])"); } TEST_F(EmitterTest, NestedBlockSeq) { @@ -1574,6 +1560,26 @@ TEST_F(EmitterErrorTest, BadLocalTag) { ExpectEmitError("invalid tag"); } +TEST_F(EmitterErrorTest, BadTagAndTag) { + out << VerbatimTag("!far") << VerbatimTag("!foo") << "bar"; + ExpectEmitError(ErrorMsg::INVALID_TAG); +} + +TEST_F(EmitterErrorTest, BadAnchorAndAnchor) { + out << Anchor("far") << Anchor("foo") << "bar"; + ExpectEmitError(ErrorMsg::INVALID_ANCHOR); +} + +TEST_F(EmitterErrorTest, BadEmptyAnchorOnGroup) { + out << BeginSeq << "bar" << Anchor("foo") << EndSeq; + ExpectEmitError(ErrorMsg::INVALID_ANCHOR); +} + +TEST_F(EmitterErrorTest, BadEmptyTagOnGroup) { + out << BeginSeq << "bar" << VerbatimTag("!foo") << EndSeq; + ExpectEmitError(ErrorMsg::INVALID_TAG); +} + TEST_F(EmitterErrorTest, ExtraEndSeq) { out << BeginSeq; out << "Hello";