From cb8eee46f095b8cad7b737897cbff50eaaf5ea5c Mon Sep 17 00:00:00 2001 From: jbeder Date: Fri, 22 Oct 2010 03:53:33 +0000 Subject: [PATCH] Added more tests for the newline, and disallowed newlines after implicit block keys --- include/yaml-cpp/emitter.h | 2 ++ src/emitfromevents.cpp | 2 +- src/emitter.cpp | 15 +++++++++++++-- test/emittertests.cpp | 38 ++++++++++++++++++++++++++++++++++++++ util/parse.cpp | 9 ++++----- 5 files changed, 58 insertions(+), 8 deletions(-) diff --git a/include/yaml-cpp/emitter.h b/include/yaml-cpp/emitter.h index 9e01f76..69021e8 100644 --- a/include/yaml-cpp/emitter.h +++ b/include/yaml-cpp/emitter.h @@ -82,6 +82,8 @@ namespace YAML void EmitKindTag(); void EmitTag(bool verbatim, const _Tag& tag); + bool CanEmitNewline() const; + private: ostream m_stream; std::auto_ptr m_pState; diff --git a/src/emitfromevents.cpp b/src/emitfromevents.cpp index ce9c2ac..4105a18 100644 --- a/src/emitfromevents.cpp +++ b/src/emitfromevents.cpp @@ -98,7 +98,7 @@ namespace YAML void EmitFromEvents::EmitProps(const std::string& tag, anchor_t anchor) { - if(!tag.empty()) + if(!tag.empty() && tag != "?") m_emitter << VerbatimTag(tag); if(anchor) m_emitter << Anchor(ToString(anchor)); diff --git a/src/emitter.cpp b/src/emitter.cpp index 3d50d3f..a2ad2a8 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -513,8 +513,19 @@ namespace YAML { if(!good()) return; - - m_stream << '\n'; + + if(CanEmitNewline()) + m_stream << '\n'; + } + + bool Emitter::CanEmitNewline() const + { + FLOW_TYPE flowType = m_pState->GetCurGroupFlowType(); + if(flowType == FT_BLOCK && m_pState->CurrentlyInLongKey()) + return true; + + EMITTER_STATE curState = m_pState->GetCurState(); + return curState != ES_DONE_WITH_BLOCK_MAP_KEY && curState != ES_WAITING_FOR_BLOCK_MAP_VALUE && curState != ES_WRITING_BLOCK_MAP_VALUE; } // ******************************************************************************************* diff --git a/test/emittertests.cpp b/test/emittertests.cpp index 057300c..cce4e01 100644 --- a/test/emittertests.cpp +++ b/test/emittertests.cpp @@ -673,7 +673,42 @@ namespace Test out << YAML::EndSeq; desiredOutput = "--- [a\n, b, c\n, d]"; } + + void NewlineInBlockMap(YAML::Emitter& out, std::string& desiredOutput) + { + out << YAML::BeginMap; + out << YAML::Key << "a" << YAML::Value << "foo" << YAML::Newline; + out << YAML::Key << "b" << YAML::Newline << YAML::Value << "bar"; + out << YAML::LongKey << YAML::Key << "c" << YAML::Newline << YAML::Value << "car"; + out << YAML::EndMap; + desiredOutput = "---\na: foo\n\nb: bar\n? c\n\n: car"; + } + void NewlineInFlowMap(YAML::Emitter& out, std::string& desiredOutput) + { + out << YAML::Flow << YAML::BeginMap; + out << YAML::Key << "a" << YAML::Value << "foo" << YAML::Newline; + out << YAML::Key << "b" << YAML::Newline << YAML::Value << "bar"; + out << YAML::EndMap; + desiredOutput = "--- {a: foo\n, b\n: bar}"; + } + + void LotsOfNewlines(YAML::Emitter& out, std::string& desiredOutput) + { + out << YAML::BeginSeq; + out << "a" << YAML::Newline; + out << YAML::BeginSeq; + out << "b" << "c" << YAML::Newline; + out << YAML::EndSeq; + out << YAML::Newline; + out << YAML::BeginMap; + out << YAML::Newline << YAML::Key << "d" << YAML::Value << YAML::Newline << "e"; + out << YAML::LongKey << YAML::Key << "f" << YAML::Newline << YAML::Value << "foo"; + out << YAML::EndMap; + out << YAML::EndSeq; + desiredOutput = "---\n- a\n\n-\n - b\n - c\n\n\n-\n\n d: e\n ? f\n\n : foo"; + } + //////////////////////////////////////////////////////////////////////////////////////////////////////// // incorrect emitting @@ -857,6 +892,9 @@ namespace Test RunEmitterTest(&Emitter::NewlineAtEnd, "newline at end", passed, total); RunEmitterTest(&Emitter::NewlineInBlockSequence, "newline in block sequence", passed, total); RunEmitterTest(&Emitter::NewlineInFlowSequence, "newline in flow sequence", passed, total); + RunEmitterTest(&Emitter::NewlineInBlockMap, "newline in block map", passed, total); + RunEmitterTest(&Emitter::NewlineInFlowMap, "newline in flow map", passed, total); + RunEmitterTest(&Emitter::LotsOfNewlines, "lots of newlines", passed, total); RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed, total); RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total); diff --git a/util/parse.cpp b/util/parse.cpp index 35b9dd2..f8cdb52 100644 --- a/util/parse.cpp +++ b/util/parse.cpp @@ -47,11 +47,10 @@ int main(int argc, char **argv) YAML::Parser parser(input); YAML::Node doc; NullEventHandler handler; -// while(parser.GetNextDocument(doc)) { - while(parser.HandleNextDocument(handler)) { -// YAML::Emitter emitter; -// emitter << doc; -// std::cout << emitter.c_str() << "\n"; + while(parser.GetNextDocument(doc)) { + YAML::Emitter emitter; + emitter << doc; + std::cout << emitter.c_str() << "\n"; } } catch(const YAML::Exception& e) { std::cerr << e.what() << "\n";