From 1f271c99447048a67b8abce45ee1a8f848f60869 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Mon, 6 Feb 2017 16:00:05 +0000 Subject: [PATCH] XmlWriter reverts to XML 1.0. Character encodings that are not valid in XML 1.0 are instead written using C-style escapes --- include/internal/catch_xmlwriter.hpp | 52 +++++++++---------- .../Baselines/console.sw.approved.txt | 8 +-- .../SelfTest/Baselines/junit.sw.approved.txt | 2 +- .../SelfTest/Baselines/xml.sw.approved.txt | 10 ++-- projects/SelfTest/MiscTests.cpp | 4 +- 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/include/internal/catch_xmlwriter.hpp b/include/internal/catch_xmlwriter.hpp index ecbd7377..5ae7efd0 100644 --- a/include/internal/catch_xmlwriter.hpp +++ b/include/internal/catch_xmlwriter.hpp @@ -57,8 +57,11 @@ namespace Catch { default: // Escape control chars - based on contribution by @espenalb in PR #465 and // by @mrpi PR #588 - if ( ( c >= 0 && c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' ) - os << "&#x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << static_cast( c ) << ';'; + if ( ( c >= 0 && c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' ) { + // see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0 + os << "\\x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) + << static_cast( c ); + } else os << c; } @@ -112,20 +115,17 @@ namespace Catch { XmlWriter() : m_tagIsOpen( false ), m_needsNewline( false ), - m_os( &Catch::cout() ) + m_os( Catch::cout() ) { - // We encode control characters, which requires - // XML 1.1 - // see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0 - *m_os << "\n"; + writeDeclaration(); } XmlWriter( std::ostream& os ) : m_tagIsOpen( false ), m_needsNewline( false ), - m_os( &os ) + m_os( os ) { - *m_os << "\n"; + writeDeclaration(); } ~XmlWriter() { @@ -136,7 +136,7 @@ namespace Catch { XmlWriter& startElement( std::string const& name ) { ensureTagClosed(); newlineIfNecessary(); - stream() << m_indent << '<' << name; + m_os << m_indent << '<' << name; m_tags.push_back( name ); m_indent += " "; m_tagIsOpen = true; @@ -153,25 +153,25 @@ namespace Catch { newlineIfNecessary(); m_indent = m_indent.substr( 0, m_indent.size()-2 ); if( m_tagIsOpen ) { - stream() << "/>"; + m_os << "/>"; m_tagIsOpen = false; } else { - stream() << m_indent << ""; + m_os << m_indent << ""; } - stream() << std::endl; + m_os << std::endl; m_tags.pop_back(); return *this; } XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ) { if( !name.empty() && !attribute.empty() ) - stream() << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; + m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; return *this; } XmlWriter& writeAttribute( std::string const& name, bool attribute ) { - stream() << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; + m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; return *this; } @@ -187,8 +187,8 @@ namespace Catch { bool tagWasOpen = m_tagIsOpen; ensureTagClosed(); if( tagWasOpen && indent ) - stream() << m_indent; - stream() << XmlEncode( text ); + m_os << m_indent; + m_os << XmlEncode( text ); m_needsNewline = true; } return *this; @@ -196,39 +196,35 @@ namespace Catch { XmlWriter& writeComment( std::string const& text ) { ensureTagClosed(); - stream() << m_indent << ""; + m_os << m_indent << ""; m_needsNewline = true; return *this; } XmlWriter& writeBlankLine() { ensureTagClosed(); - stream() << '\n'; + m_os << '\n'; return *this; } - void setStream( std::ostream& os ) { - m_os = &os; - } - private: XmlWriter( XmlWriter const& ); void operator=( XmlWriter const& ); - std::ostream& stream() { - return *m_os; + void writeDeclaration() { + m_os << "\n"; } void ensureTagClosed() { if( m_tagIsOpen ) { - stream() << ">\n"; + m_os << ">\n"; m_tagIsOpen = false; } } void newlineIfNecessary() { if( m_needsNewline ) { - stream() << '\n'; + m_os << '\n'; m_needsNewline = false; } } @@ -237,7 +233,7 @@ namespace Catch { bool m_needsNewline; std::vector m_tags; std::string m_indent; - std::ostream* m_os; + std::ostream& m_os; }; } diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 8b172f52..f04d8566 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -8106,9 +8106,9 @@ MiscTests.cpp: MiscTests.cpp:: PASSED: - REQUIRE( encode( "[\x01]" ) == "[]" ) + REQUIRE( encode( "[\x01]" ) == "[\\x01]" ) with expansion: - "[]" == "[]" + "[\x01]" == "[\x01]" ------------------------------------------------------------------------------- XmlEncode @@ -8119,9 +8119,9 @@ MiscTests.cpp: MiscTests.cpp:: PASSED: - REQUIRE( encode( "[\x7F]" ) == "[]" ) + REQUIRE( encode( "[\x7F]" ) == "[\\x7F]" ) with expansion: - "[]" == "[]" + "[\x7F]" == "[\x7F]" ------------------------------------------------------------------------------- atomic if diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index c27d9419..4fe585b5 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,4 +1,4 @@ - + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index a9076d29..b7638216 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -1,4 +1,4 @@ - + @@ -8600,10 +8600,10 @@ there"
- encode( "[\x01]" ) == "[&#x01;]" + encode( "[\x01]" ) == "[\\x01]" - "[&#x01;]" == "[&#x01;]" + "[\x01]" == "[\x01]" @@ -8611,10 +8611,10 @@ there"
- encode( "[\x7F]" ) == "[&#x7F;]" + encode( "[\x7F]" ) == "[\\x7F]" - "[&#x7F;]" == "[&#x7F;]" + "[\x7F]" == "[\x7F]" diff --git a/projects/SelfTest/MiscTests.cpp b/projects/SelfTest/MiscTests.cpp index 5de2ca48..8eaf99bc 100644 --- a/projects/SelfTest/MiscTests.cpp +++ b/projects/SelfTest/MiscTests.cpp @@ -458,10 +458,10 @@ TEST_CASE( "XmlEncode" ) { REQUIRE( encode( stringWithQuotes, Catch::XmlEncode::ForAttributes ) == "don't "quote" me on that" ); } SECTION( "string with control char (1)" ) { - REQUIRE( encode( "[\x01]" ) == "[]" ); + REQUIRE( encode( "[\x01]" ) == "[\\x01]" ); } SECTION( "string with control char (x7F)" ) { - REQUIRE( encode( "[\x7F]" ) == "[]" ); + REQUIRE( encode( "[\x7F]" ) == "[\\x7F]" ); } }