mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-18 11:36:09 +01:00
27fa7218ca
According to UTF-8 encoding rules, there are no valid one byte long codepoints which start with a bigh bit set (i.e. 0x80 or higher value). Hence such XML output needs to be escaped, same as a control character.
46 lines
1.6 KiB
C++
46 lines
1.6 KiB
C++
#include "catch.hpp"
|
|
|
|
#include "internal/catch_xmlwriter.h"
|
|
|
|
#include <sstream>
|
|
|
|
inline std::string encode( std::string const& str, Catch::XmlEncode::ForWhat forWhat = Catch::XmlEncode::ForTextNodes ) {
|
|
std::ostringstream oss;
|
|
oss << Catch::XmlEncode( str, forWhat );
|
|
return oss.str();
|
|
}
|
|
|
|
TEST_CASE( "XmlEncode" ) {
|
|
SECTION( "normal string" ) {
|
|
REQUIRE( encode( "normal string" ) == "normal string" );
|
|
}
|
|
SECTION( "empty string" ) {
|
|
REQUIRE( encode( "" ) == "" );
|
|
}
|
|
SECTION( "string with ampersand" ) {
|
|
REQUIRE( encode( "smith & jones" ) == "smith & jones" );
|
|
}
|
|
SECTION( "string with less-than" ) {
|
|
REQUIRE( encode( "smith < jones" ) == "smith < jones" );
|
|
}
|
|
SECTION( "string with greater-than" ) {
|
|
REQUIRE( encode( "smith > jones" ) == "smith > jones" );
|
|
REQUIRE( encode( "smith ]]> jones" ) == "smith ]]> jones" );
|
|
}
|
|
SECTION( "string with quotes" ) {
|
|
std::string stringWithQuotes = "don't \"quote\" me on that";
|
|
REQUIRE( encode( stringWithQuotes ) == stringWithQuotes );
|
|
REQUIRE( encode( stringWithQuotes, Catch::XmlEncode::ForAttributes ) == "don't "quote" me on that" );
|
|
}
|
|
SECTION( "string with control char (1)" ) {
|
|
REQUIRE( encode( "[\x01]" ) == "[\\x01]" );
|
|
}
|
|
SECTION( "string with control char (x7F)" ) {
|
|
REQUIRE( encode( "[\x7F]" ) == "[\\x7F]" );
|
|
}
|
|
SECTION( "string with high bit set (x80 and xFF)" ) {
|
|
REQUIRE( encode( "[\x80]" ) == "[\\x80]" );
|
|
REQUIRE( encode( "[\xFF]" ) == "[\\xFF]" );
|
|
}
|
|
}
|