mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 04:41:16 +00:00
Added more tests for the newline, and disallowed newlines after implicit block keys
This commit is contained in:
@@ -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 <EmitterState> m_pState;
|
||||
|
@@ -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));
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
// *******************************************************************************************
|
||||
|
@@ -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);
|
||||
|
@@ -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";
|
||||
|
Reference in New Issue
Block a user