Properly parameterise PYXWriter and SAX tests on string type

This commit is contained in:
Jez Higgins 2020-06-25 15:09:50 +01:00
parent cd99dba691
commit c37d974e47
2 changed files with 64 additions and 58 deletions

View file

@ -18,9 +18,8 @@ class PYXWriter : public XMLFilterImpl<string_type, string_adaptor>
typedef XMLReaderInterface<string_type, string_adaptor> XMLReaderT;
typedef XMLFilterImpl<string_type, string_adaptor> XMLFilterT;
typedef typename XMLFilterT::AttributesT AttributesT;
typedef typename string_type::value_type charT;
typedef typename string_type::traits_type traitsT;
typedef std::basic_ostream<charT, traitsT> ostreamT;
typedef typename string_adaptor::value_type charT;
typedef std::basic_ostream<charT> ostreamT;
typedef Arabica::text::Unicode<charT> UnicodeT;
private:
using XMLFilterT::getParent;
@ -103,7 +102,7 @@ void PYXWriter<string_type, string_adaptor>::processingInstruction(const string_
template<class string_type, class string_adaptor>
void PYXWriter<string_type, string_adaptor>::escape(const string_type& ch)
{
for(typename string_type::const_iterator s = ch.begin(), se = ch.end(); s != se; ++s)
for(typename string_adaptor::const_iterator s = string_adaptor::begin(ch), se = string_adaptor::end(ch); s != se; ++s)
if(*s == UnicodeT::LINE_FEED)
*stream_ << UnicodeT::BACK_SLASH << UnicodeT::LOWERCASE_N;
else

View file

@ -29,107 +29,114 @@ class WhitespaceStripperTest : public TestCase
{
} // setUp
void expect(char const* expected, std::basic_ostringstream<typename string_adaptor::value_type>& result) {
assertEquals(
expected,
string_adaptor::asStdString(string_adaptor::construct(result.str().c_str()))
);
}
void testNoStrip()
{
std::ostringstream o;
Arabica::SAX::XMLReader<std::string> parser;
Arabica::SAX::PYXWriter<std::string> writer(o, parser);
std::basic_ostringstream<typename string_adaptor::value_type> o;
Arabica::SAX::XMLReader<string_type, string_adaptor> parser;
Arabica::SAX::PYXWriter<string_type, string_adaptor> writer(o, parser);
writer.parse(*source("<test><p> Woo baby hooray </p></test>"));
assertEquals("(test\n(p\n- Woo baby hooray \n)p\n)test\n", o.str());
expect("(test\n(p\n- Woo baby hooray \n)p\n)test\n", o);
} // testNoStrip
void testStripLeading()
{
std::ostringstream o;
Arabica::SAX::XMLReader<std::string> parser;
Arabica::SAX::WhitespaceStripper<std::string> stripper(parser);
Arabica::SAX::PYXWriter<std::string> writer(o, stripper);
std::basic_ostringstream<typename string_adaptor::value_type> o;
Arabica::SAX::XMLReader<string_type, string_adaptor> parser;
Arabica::SAX::WhitespaceStripper<string_type, string_adaptor> stripper(parser);
Arabica::SAX::PYXWriter<string_type, string_adaptor> writer(o, stripper);
writer.parse(*source("<test><p>Woo</p></test>"));
assertEquals("(test\n(p\n-Woo\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p> Woo</p></test>"));
assertEquals("(test\n(p\n-Woo\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p> Woo</p></test>"));
assertEquals("(test\n(p\n-Woo\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
} // testStripLeading
void testStripTrailing()
{
std::ostringstream o;
Arabica::SAX::XMLReader<std::string> parser;
Arabica::SAX::WhitespaceStripper<std::string> stripper(parser);
Arabica::SAX::PYXWriter<std::string> writer(o, stripper);
std::basic_ostringstream<typename string_adaptor::value_type> o;
Arabica::SAX::XMLReader<string_type, string_adaptor> parser;
Arabica::SAX::WhitespaceStripper<string_type, string_adaptor> stripper(parser);
Arabica::SAX::PYXWriter<string_type, string_adaptor> writer(o, stripper);
writer.parse(*source("<test><p>Woo</p></test>"));
assertEquals("(test\n(p\n-Woo\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p>Woo </p></test>"));
assertEquals("(test\n(p\n-Woo\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p>Woo </p></test>"));
assertEquals("(test\n(p\n-Woo\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
} // testStripTrailing
void testStripMid()
{
std::ostringstream o;
Arabica::SAX::XMLReader<std::string> parser;
Arabica::SAX::WhitespaceStripper<std::string> stripper(parser);
Arabica::SAX::PYXWriter<std::string> writer(o, stripper);
std::basic_ostringstream<typename string_adaptor::value_type> o;
Arabica::SAX::XMLReader<string_type, string_adaptor> parser;
Arabica::SAX::WhitespaceStripper<string_type, string_adaptor> stripper(parser);
Arabica::SAX::PYXWriter<string_type, string_adaptor> writer(o, stripper);
writer.parse(*source("<test><p>Woo yea</p></test>"));
assertEquals("(test\n(p\n-Woo yea\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo yea\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p>Woo yea</p></test>"));
assertEquals("(test\n(p\n-Woo yea\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo yea\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p>Woo yea</p></test>"));
assertEquals("(test\n(p\n-Woo yea\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo yea\n)p\n)test\n", o);
} // testStripMid
void testStripMid2()
{
std::ostringstream o;
Arabica::SAX::XMLReader<std::string> parser;
Arabica::SAX::WhitespaceStripper<std::string> stripper(parser);
Arabica::SAX::PYXWriter<std::string> writer(o, stripper);
std::basic_ostringstream<typename string_adaptor::value_type> o;
Arabica::SAX::XMLReader<string_type, string_adaptor> parser;
Arabica::SAX::WhitespaceStripper<string_type, string_adaptor> stripper(parser);
Arabica::SAX::PYXWriter<string_type, string_adaptor> writer(o, stripper);
writer.parse(*source("<test><p>Woo yea man</p></test>"));
assertEquals("(test\n(p\n-Woo yea man\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p>Woo yea man</p></test>"));
assertEquals("(test\n(p\n-Woo yea man\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p>Woo yea man</p></test>"));
assertEquals("(test\n(p\n-Woo yea man\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o);
o.str("");
o.str(string_adaptor::empty_string());
writer.parse(*source("<test><p>Woo yea man</p></test>"));
assertEquals("(test\n(p\n-Woo yea man\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o);
} // testStripMid2
void testStrip()
{
std::ostringstream o;
Arabica::SAX::XMLReader<std::string> parser;
Arabica::SAX::WhitespaceStripper<std::string> stripper(parser);
Arabica::SAX::PYXWriter<std::string> writer(o, stripper);
std::basic_ostringstream<typename string_adaptor::value_type> o;
Arabica::SAX::XMLReader<string_type, string_adaptor> parser;
Arabica::SAX::WhitespaceStripper<string_type, string_adaptor> stripper(parser);
Arabica::SAX::PYXWriter<string_type, string_adaptor> writer(o, stripper);
writer.parse(*source("<test><p> Woo baby hooray </p><!-- yo mama --></test>"));
assertEquals("(test\n(p\n-Woo baby hooray\n)p\n)test\n", o.str());
expect("(test\n(p\n-Woo baby hooray\n)p\n)test\n", o);
} // testStrip
private:
std::unique_ptr<Arabica::SAX::InputSource<std::string> > source(const std::string& str)
std::unique_ptr<Arabica::SAX::InputSource<string_type, string_adaptor> > source(const std::string& str)
{
std::unique_ptr<std::iostream> ss(new std::stringstream());
(*ss) << str;
return std::make_unique<Arabica::SAX::InputSource<std::string>>(std::move(ss));
return std::make_unique<Arabica::SAX::InputSource<string_type, string_adaptor>>(std::move(ss));
} // source
}; // WhitespaceStripperTest