2007-08-07 23:31:18 +02:00
|
|
|
#ifndef ARABICA_TEST_WHITESPACE_STRIPPER_HPP
|
|
|
|
#define ARABICA_TEST_WHITESPACE_STRIPPER_HPP
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
2007-09-05 00:55:47 +02:00
|
|
|
#include <SAX/XMLReader.hpp>
|
|
|
|
#include <SAX/InputSource.hpp>
|
2007-08-07 23:31:18 +02:00
|
|
|
#include <SAX/filter/WhitespaceStripperFilter.hpp>
|
|
|
|
#include <SAX/filter/PYXWriter.hpp>
|
|
|
|
|
|
|
|
#include "../CppUnit/framework/TestCase.h"
|
|
|
|
#include "../CppUnit/framework/TestSuite.h"
|
|
|
|
#include "../CppUnit/framework/TestCaller.h"
|
|
|
|
|
|
|
|
template<class string_type, class string_adaptor>
|
|
|
|
class WhitespaceStripperTest : public TestCase
|
|
|
|
{
|
|
|
|
typedef string_adaptor SA;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WhitespaceStripperTest(std::string name) :
|
|
|
|
TestCase(name)
|
|
|
|
{
|
|
|
|
} // WhitespaceStripperTest
|
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
|
|
|
} // setUp
|
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
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()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-08-07 23:31:18 +02:00
|
|
|
void testNoStrip()
|
|
|
|
{
|
2020-06-25 16:09:50 +02:00
|
|
|
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);
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p> Woo baby hooray </p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n- Woo baby hooray \n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
} // testNoStrip
|
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
void testStripLeading()
|
2007-08-07 23:31:18 +02:00
|
|
|
{
|
2020-06-25 16:09:50 +02:00
|
|
|
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);
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p> Woo</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p> Woo</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
} // testStripLeading
|
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
void testStripTrailing()
|
2007-08-07 23:31:18 +02:00
|
|
|
{
|
2020-06-25 16:09:50 +02:00
|
|
|
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);
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo </p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo </p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
} // testStripTrailing
|
|
|
|
|
|
|
|
void testStripMid()
|
|
|
|
{
|
2020-06-25 16:09:50 +02:00
|
|
|
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);
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo yea</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo yea\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo yea</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo yea\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
|
2020-06-25 16:09:50 +02:00
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo yea</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo yea\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
} // testStripMid
|
|
|
|
|
|
|
|
void testStripMid2()
|
|
|
|
{
|
2020-06-25 16:09:50 +02:00
|
|
|
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);
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo yea man</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o);
|
|
|
|
|
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo yea man</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o);
|
|
|
|
|
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo yea man</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o);
|
|
|
|
|
|
|
|
o.str(string_adaptor::empty_string());
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p>Woo yea man</p></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo yea man\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
} // testStripMid2
|
|
|
|
|
|
|
|
void testStrip()
|
|
|
|
{
|
2020-06-25 16:09:50 +02:00
|
|
|
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);
|
2007-08-07 23:31:18 +02:00
|
|
|
writer.parse(*source("<test><p> Woo baby hooray </p><!-- yo mama --></test>"));
|
2020-06-25 16:09:50 +02:00
|
|
|
expect("(test\n(p\n-Woo baby hooray\n)p\n)test\n", o);
|
2007-08-07 23:31:18 +02:00
|
|
|
} // testStrip
|
|
|
|
|
|
|
|
private:
|
2020-06-25 16:09:50 +02:00
|
|
|
std::unique_ptr<Arabica::SAX::InputSource<string_type, string_adaptor> > source(const std::string& str)
|
2007-08-07 23:31:18 +02:00
|
|
|
{
|
2020-04-16 01:06:30 +02:00
|
|
|
std::unique_ptr<std::iostream> ss(new std::stringstream());
|
2007-08-07 23:31:18 +02:00
|
|
|
(*ss) << str;
|
2020-06-25 16:09:50 +02:00
|
|
|
return std::make_unique<Arabica::SAX::InputSource<string_type, string_adaptor>>(std::move(ss));
|
2007-08-07 23:31:18 +02:00
|
|
|
} // source
|
|
|
|
}; // WhitespaceStripperTest
|
|
|
|
|
|
|
|
template<class string_type, class string_adaptor>
|
|
|
|
TestSuite* WhitespaceStripper_test_suite()
|
|
|
|
{
|
|
|
|
TestSuite *suiteOfTests = new TestSuite;
|
|
|
|
|
|
|
|
suiteOfTests->addTest(new TestCaller<WhitespaceStripperTest<string_type, string_adaptor> >("testNoStrip", &WhitespaceStripperTest<string_type, string_adaptor>::testNoStrip));
|
|
|
|
suiteOfTests->addTest(new TestCaller<WhitespaceStripperTest<string_type, string_adaptor> >("testStripLeading", &WhitespaceStripperTest<string_type, string_adaptor>::testStripLeading));
|
|
|
|
suiteOfTests->addTest(new TestCaller<WhitespaceStripperTest<string_type, string_adaptor> >("testStripTrailing", &WhitespaceStripperTest<string_type, string_adaptor>::testStripTrailing));
|
|
|
|
suiteOfTests->addTest(new TestCaller<WhitespaceStripperTest<string_type, string_adaptor> >("testStripMid", &WhitespaceStripperTest<string_type, string_adaptor>::testStripMid));
|
|
|
|
suiteOfTests->addTest(new TestCaller<WhitespaceStripperTest<string_type, string_adaptor> >("testStripMid2", &WhitespaceStripperTest<string_type, string_adaptor>::testStripMid2));
|
|
|
|
suiteOfTests->addTest(new TestCaller<WhitespaceStripperTest<string_type, string_adaptor> >("testStrip", &WhitespaceStripperTest<string_type, string_adaptor>::testStrip));
|
|
|
|
|
|
|
|
return suiteOfTests;
|
|
|
|
} // WhitespaceStripperTest_suite
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|