From c37d974e476dd25e0229cfdfefabe1ec130877a4 Mon Sep 17 00:00:00 2001 From: Jez Higgins Date: Thu, 25 Jun 2020 15:09:50 +0100 Subject: [PATCH] Properly parameterise PYXWriter and SAX tests on string type --- include/SAX/filter/PYXWriter.hpp | 7 +- tests/SAX/test_WhitespaceStripper.hpp | 115 ++++++++++++++------------ 2 files changed, 64 insertions(+), 58 deletions(-) diff --git a/include/SAX/filter/PYXWriter.hpp b/include/SAX/filter/PYXWriter.hpp index ab7d1f05..a978f32e 100644 --- a/include/SAX/filter/PYXWriter.hpp +++ b/include/SAX/filter/PYXWriter.hpp @@ -18,9 +18,8 @@ class PYXWriter : public XMLFilterImpl typedef XMLReaderInterface XMLReaderT; typedef XMLFilterImpl XMLFilterT; typedef typename XMLFilterT::AttributesT AttributesT; - typedef typename string_type::value_type charT; - typedef typename string_type::traits_type traitsT; - typedef std::basic_ostream ostreamT; + typedef typename string_adaptor::value_type charT; + typedef std::basic_ostream ostreamT; typedef Arabica::text::Unicode UnicodeT; private: using XMLFilterT::getParent; @@ -103,7 +102,7 @@ void PYXWriter::processingInstruction(const string_ template void PYXWriter::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 diff --git a/tests/SAX/test_WhitespaceStripper.hpp b/tests/SAX/test_WhitespaceStripper.hpp index ab6b8e2e..499da27e 100644 --- a/tests/SAX/test_WhitespaceStripper.hpp +++ b/tests/SAX/test_WhitespaceStripper.hpp @@ -29,107 +29,114 @@ class WhitespaceStripperTest : public TestCase { } // setUp + void expect(char const* expected, std::basic_ostringstream& result) { + assertEquals( + expected, + string_adaptor::asStdString(string_adaptor::construct(result.str().c_str())) + ); + } + void testNoStrip() { - std::ostringstream o; - Arabica::SAX::XMLReader parser; - Arabica::SAX::PYXWriter writer(o, parser); + std::basic_ostringstream o; + Arabica::SAX::XMLReader parser; + Arabica::SAX::PYXWriter writer(o, parser); writer.parse(*source("

Woo baby hooray

")); - 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() + void testStripLeading() { - std::ostringstream o; - Arabica::SAX::XMLReader parser; - Arabica::SAX::WhitespaceStripper stripper(parser); - Arabica::SAX::PYXWriter writer(o, stripper); + std::basic_ostringstream o; + Arabica::SAX::XMLReader parser; + Arabica::SAX::WhitespaceStripper stripper(parser); + Arabica::SAX::PYXWriter writer(o, stripper); writer.parse(*source("

Woo

")); - 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("

Woo

")); - 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("

Woo

")); - 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() + void testStripTrailing() { - std::ostringstream o; - Arabica::SAX::XMLReader parser; - Arabica::SAX::WhitespaceStripper stripper(parser); - Arabica::SAX::PYXWriter writer(o, stripper); + std::basic_ostringstream o; + Arabica::SAX::XMLReader parser; + Arabica::SAX::WhitespaceStripper stripper(parser); + Arabica::SAX::PYXWriter writer(o, stripper); writer.parse(*source("

Woo

")); - 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("

Woo

")); - 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("

Woo

")); - 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 parser; - Arabica::SAX::WhitespaceStripper stripper(parser); - Arabica::SAX::PYXWriter writer(o, stripper); + std::basic_ostringstream o; + Arabica::SAX::XMLReader parser; + Arabica::SAX::WhitespaceStripper stripper(parser); + Arabica::SAX::PYXWriter writer(o, stripper); writer.parse(*source("

Woo yea

")); - 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("

Woo yea

")); - 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("

Woo yea

")); - 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 parser; - Arabica::SAX::WhitespaceStripper stripper(parser); - Arabica::SAX::PYXWriter writer(o, stripper); + std::basic_ostringstream o; + Arabica::SAX::XMLReader parser; + Arabica::SAX::WhitespaceStripper stripper(parser); + Arabica::SAX::PYXWriter writer(o, stripper); writer.parse(*source("

Woo yea man

")); - assertEquals("(test\n(p\n-Woo yea man\n)p\n)test\n", o.str()); - - o.str(""); + expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o); + + o.str(string_adaptor::empty_string()); writer.parse(*source("

Woo yea man

")); - assertEquals("(test\n(p\n-Woo yea man\n)p\n)test\n", o.str()); - - o.str(""); + expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o); + + o.str(string_adaptor::empty_string()); writer.parse(*source("

Woo yea man

")); - assertEquals("(test\n(p\n-Woo yea man\n)p\n)test\n", o.str()); - - o.str(""); + expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o); + + o.str(string_adaptor::empty_string()); writer.parse(*source("

Woo yea man

")); - 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 parser; - Arabica::SAX::WhitespaceStripper stripper(parser); - Arabica::SAX::PYXWriter writer(o, stripper); + std::basic_ostringstream o; + Arabica::SAX::XMLReader parser; + Arabica::SAX::WhitespaceStripper stripper(parser); + Arabica::SAX::PYXWriter writer(o, stripper); writer.parse(*source("

Woo baby hooray

")); - 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 > source(const std::string& str) + std::unique_ptr > source(const std::string& str) { std::unique_ptr ss(new std::stringstream()); (*ss) << str; - return std::make_unique>(std::move(ss)); + return std::make_unique>(std::move(ss)); } // source }; // WhitespaceStripperTest