mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 12:17:11 +01:00 
			
		
		
		
	Add more tests for XmlWrite::write* members
This commit is contained in:
		| @@ -295,13 +295,14 @@ namespace { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     XmlWriter& XmlWriter::writeText( StringRef text, XmlFormatting fmt ) { |     XmlWriter& XmlWriter::writeText( StringRef text, XmlFormatting fmt ) { | ||||||
|  |         CATCH_ENFORCE(!m_tags.empty(), "Cannot write text as top level element"); | ||||||
|         if( !text.empty() ){ |         if( !text.empty() ){ | ||||||
|             bool tagWasOpen = m_tagIsOpen; |             bool tagWasOpen = m_tagIsOpen; | ||||||
|             ensureTagClosed(); |             ensureTagClosed(); | ||||||
|             if (tagWasOpen && shouldIndent(fmt)) { |             if (tagWasOpen && shouldIndent(fmt)) { | ||||||
|                 m_os << m_indent; |                 m_os << m_indent; | ||||||
|             } |             } | ||||||
|             m_os << XmlEncode( text ); |             m_os << XmlEncode( text, XmlEncode::ForTextNodes ); | ||||||
|             applyFormatting(fmt); |             applyFormatting(fmt); | ||||||
|         } |         } | ||||||
|         return *this; |         return *this; | ||||||
| @@ -312,7 +313,7 @@ namespace { | |||||||
|         if (shouldIndent(fmt)) { |         if (shouldIndent(fmt)) { | ||||||
|             m_os << m_indent; |             m_os << m_indent; | ||||||
|         } |         } | ||||||
|         m_os << "<!--" << text << "-->"; |         m_os << "<!-- " << text << " -->"; | ||||||
|         applyFormatting(fmt); |         applyFormatting(fmt); | ||||||
|         return *this; |         return *this; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -117,10 +117,12 @@ namespace Catch { | |||||||
|             return writeAttribute( name, rss.str() ); |             return writeAttribute( name, rss.str() ); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         //! Writes escaped `text` in a element | ||||||
|         XmlWriter& writeText( StringRef text, |         XmlWriter& writeText( StringRef text, | ||||||
|                               XmlFormatting fmt = XmlFormatting::Newline | |                               XmlFormatting fmt = XmlFormatting::Newline | | ||||||
|                                                   XmlFormatting::Indent ); |                                                   XmlFormatting::Indent ); | ||||||
|  |  | ||||||
|  |         //! Writes XML comment as "<!-- text -->" | ||||||
|         XmlWriter& writeComment( StringRef text, |         XmlWriter& writeComment( StringRef text, | ||||||
|                                  XmlFormatting fmt = XmlFormatting::Newline | |                                  XmlFormatting fmt = XmlFormatting::Newline | | ||||||
|                                                      XmlFormatting::Indent ); |                                                      XmlFormatting::Indent ); | ||||||
|   | |||||||
| @@ -131,3 +131,47 @@ TEST_CASE("XmlWriter writes boolean attributes as true/false", "[XML][XmlWriter] | |||||||
|                   ContainsSubstring(R"(attr1="true")") && |                   ContainsSubstring(R"(attr1="true")") && | ||||||
|                   ContainsSubstring(R"(attr2="false")") ); |                   ContainsSubstring(R"(attr2="false")") ); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | TEST_CASE("XmlWriter does not escape comments", "[XML][XmlWriter][approvals]") { | ||||||
|  |     using Catch::Matchers::ContainsSubstring; | ||||||
|  |     std::stringstream stream; | ||||||
|  |     { | ||||||
|  |         Catch::XmlWriter xml(stream); | ||||||
|  |  | ||||||
|  |         xml.writeComment(R"(unescaped special chars: < > ' " &)"); | ||||||
|  |     } | ||||||
|  |     REQUIRE_THAT( stream.str(), | ||||||
|  |                   ContainsSubstring(R"(<!-- unescaped special chars: < > ' " & -->)")); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_CASE("XmlWriter errors out when writing text without enclosing element", "[XmlWriter][approvals]") { | ||||||
|  |     std::stringstream stream; | ||||||
|  |     Catch::XmlWriter xml(stream); | ||||||
|  |     REQUIRE_THROWS(xml.writeText("some text")); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_CASE("XmlWriter escapes text properly", "[XML][XmlWriter][approvals]") { | ||||||
|  |     using Catch::Matchers::ContainsSubstring; | ||||||
|  |     std::stringstream stream; | ||||||
|  |     { | ||||||
|  |         Catch::XmlWriter xml(stream); | ||||||
|  |         xml.scopedElement("root") | ||||||
|  |            .writeText(R"(Special chars need escaping: < > ' " &)"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     REQUIRE_THAT( stream.str(), | ||||||
|  |                   ContainsSubstring(R"(Special chars need escaping: < > ' " &)")); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_CASE("XmlWriter escapes attributes properly", "[XML][XmlWriter][approvals]") { | ||||||
|  |     using Catch::Matchers::ContainsSubstring; | ||||||
|  |     std::stringstream stream; | ||||||
|  |     { | ||||||
|  |         Catch::XmlWriter xml(stream); | ||||||
|  |         xml.scopedElement("root") | ||||||
|  |            .writeAttribute("some-attribute", R"(Special chars need escaping: < > ' " &)"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     REQUIRE_THAT(stream.str(), | ||||||
|  |                  ContainsSubstring(R"(some-attribute="Special chars need escaping: < > ' " &")")); | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Martin Hořeňovský
					Martin Hořeňovský