Added more tests for the newline, and disallowed newlines after implicit block keys

This commit is contained in:
Jesse Beder
2010-10-22 03:53:33 +00:00
parent 1e4210401f
commit 6b7cb45ac8
5 changed files with 58 additions and 8 deletions

View File

@@ -82,6 +82,8 @@ namespace YAML
void EmitKindTag(); void EmitKindTag();
void EmitTag(bool verbatim, const _Tag& tag); void EmitTag(bool verbatim, const _Tag& tag);
bool CanEmitNewline() const;
private: private:
ostream m_stream; ostream m_stream;
std::auto_ptr <EmitterState> m_pState; std::auto_ptr <EmitterState> m_pState;

View File

@@ -98,7 +98,7 @@ namespace YAML
void EmitFromEvents::EmitProps(const std::string& tag, anchor_t anchor) void EmitFromEvents::EmitProps(const std::string& tag, anchor_t anchor)
{ {
if(!tag.empty()) if(!tag.empty() && tag != "?")
m_emitter << VerbatimTag(tag); m_emitter << VerbatimTag(tag);
if(anchor) if(anchor)
m_emitter << Anchor(ToString(anchor)); m_emitter << Anchor(ToString(anchor));

View File

@@ -514,9 +514,20 @@ namespace YAML
if(!good()) if(!good())
return; return;
if(CanEmitNewline())
m_stream << '\n'; 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;
}
// ******************************************************************************************* // *******************************************************************************************
// overloads of Write // overloads of Write

View File

@@ -674,6 +674,41 @@ namespace Test
desiredOutput = "--- [a\n, b, c\n, d]"; 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 // incorrect emitting
@@ -857,6 +892,9 @@ namespace Test
RunEmitterTest(&Emitter::NewlineAtEnd, "newline at end", passed, total); RunEmitterTest(&Emitter::NewlineAtEnd, "newline at end", passed, total);
RunEmitterTest(&Emitter::NewlineInBlockSequence, "newline in block sequence", passed, total); RunEmitterTest(&Emitter::NewlineInBlockSequence, "newline in block sequence", passed, total);
RunEmitterTest(&Emitter::NewlineInFlowSequence, "newline in flow 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::ExtraEndSeq, "extra EndSeq", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total); RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total);

View File

@@ -47,11 +47,10 @@ int main(int argc, char **argv)
YAML::Parser parser(input); YAML::Parser parser(input);
YAML::Node doc; YAML::Node doc;
NullEventHandler handler; NullEventHandler handler;
// while(parser.GetNextDocument(doc)) { while(parser.GetNextDocument(doc)) {
while(parser.HandleNextDocument(handler)) { YAML::Emitter emitter;
// YAML::Emitter emitter; emitter << doc;
// emitter << doc; std::cout << emitter.c_str() << "\n";
// std::cout << emitter.c_str() << "\n";
} }
} catch(const YAML::Exception& e) { } catch(const YAML::Exception& e) {
std::cerr << e.what() << "\n"; std::cerr << e.what() << "\n";