arabica/test/SAX2DOM/test_SAX.h

178 lines
7.5 KiB
C
Raw Normal View History

2002-09-03 15:26:08 +02:00
#ifndef test_SAX_H
#define test_SAX_H
2005-10-26 22:52:48 +02:00
#include "../CppUnit/framework/TestCase.h"
#include "../CppUnit/framework/TestSuite.h"
#include "../CppUnit/framework/TestCaller.h"
#include <sstream>
#include <DOM/SAX2DOM/SAX2DOM.h>
template<class string_type, class string_adaptor>
class SAXTest : public TestCase
{
typedef string_adaptor SA;
2005-10-26 22:52:48 +02:00
public:
SAXTest(std::string name) :
TestCase(name)
{
} // SAXTest
void setUp()
{
} // setUp
DOM::Document<string_type> parse(string_type str)
{
std::stringstream ss;
ss << SA::asStdString(str);
2005-10-26 22:52:48 +02:00
SAX::basic_InputSource<string_type> is(ss);
SAX2DOM::Parser<string_type, string_adaptor> parser;
2005-10-26 22:52:48 +02:00
parser.parse(is);
return parser.getDocument();
} // parse
void test1()
{
DOM::Element<string_type> d;
DOM::Node<string_type> n;
assert(d == 0);
assert(n == 0);
assert(n == d);
} // test1
void test2()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root/>"));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> elem = d.getDocumentElement();
assert(elem.getParentNode() == d);
assert(elem.getOwnerDocument() == d);
} // test2
void test3()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root attr=\"poop\"/>"));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> elem = d.getDocumentElement();
assert(elem.hasAttributes() == true);
assert(elem.getAttribute(SA::construct_from_utf8("attr")) == SA::construct_from_utf8("poop"));
2005-10-26 22:52:48 +02:00
} // test3
void test4()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root><child attr=\"poop\"/></root>"));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> elem = DOM::Element<string_type>(d.getDocumentElement().getFirstChild());
assert(SA::construct_from_utf8("child") == elem.getNodeName());
2005-10-26 22:52:48 +02:00
assertEquals(true, elem.hasAttributes());
assert(SA::construct_from_utf8("poop") == elem.getAttribute(SA::construct_from_utf8("attr")));
2005-10-26 22:52:48 +02:00
} // test4
void test5()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<stuff:elem attr='something' xmlns:stuff='http://example.com/stuff'/>"));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> elem = d.getDocumentElement();
assertEquals(true, elem.hasNamespaceURI());
assert(SA::construct_from_utf8("http://example.com/stuff") == elem.getNamespaceURI());
2005-10-26 22:52:48 +02:00
DOM::Attr<string_type> attr = elem.getAttributeNode(SA::construct_from_utf8("attr"));
2005-10-26 22:52:48 +02:00
assertEquals(false, attr.hasNamespaceURI());
} // test5
void test6()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<stuff:elem stuff:attr='something' xmlns:stuff='http://example.com/stuff'/>"));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> elem = d.getDocumentElement();
assertEquals(true, elem.hasNamespaceURI());
assert(SA::construct_from_utf8("http://example.com/stuff") == elem.getNamespaceURI());
2005-10-26 22:52:48 +02:00
DOM::Attr<string_type> attr = elem.getAttributeNodeNS(SA::construct_from_utf8("http://example.com/stuff"), SA::construct_from_utf8("attr"));
2005-10-26 22:52:48 +02:00
assertEquals(true, attr.hasNamespaceURI());
assert(SA::construct_from_utf8("http://example.com/stuff") == attr.getNamespaceURI());
2005-10-26 22:52:48 +02:00
} // test6
void test7()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<elem stuff:attr='something' xmlns:stuff='http://example.com/stuff'/>"));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> elem = d.getDocumentElement();
assertEquals(false, elem.hasNamespaceURI());
DOM::Attr<string_type> attr = elem.getAttributeNodeNS(SA::construct_from_utf8("http://example.com/stuff"), SA::construct_from_utf8("attr"));
2005-10-26 22:52:48 +02:00
assertEquals(true, attr.hasNamespaceURI());
assert(SA::construct_from_utf8("http://example.com/stuff") == attr.getNamespaceURI());
2005-10-26 22:52:48 +02:00
} // test7
void test8()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root attr=\"poop\"><child/></root>"));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> elem = d.getDocumentElement();
DOM::Element<string_type> e2 = DOM::Element<string_type>(elem.cloneNode(true));
assert(e2.getOwnerDocument() == d);
assert(e2.getParentNode() == 0);
assert(e2.hasAttributes() == true);
assert(e2.getAttribute(SA::construct_from_utf8("attr")) == SA::construct_from_utf8("poop"));
assert(e2.getFirstChild().getNodeName() == SA::construct_from_utf8("child"));
2005-10-26 22:52:48 +02:00
} // test8
void test9()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<elem attr='something' xmlns:stuff='http://example.com/stuff'/>"));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> elem = d.getDocumentElement();
assertEquals(false, elem.hasNamespaceURI());
DOM::Attr<string_type> attr = elem.getAttributeNode(SA::construct_from_utf8("attr"));
2005-10-26 22:52:48 +02:00
assertEquals(false, attr.hasNamespaceURI());
} // test9
void test10()
{
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<elem stuff:attr='something' poop:attr='fail' xmlns:stuff='http://example.com/stuff'/>"));
2005-10-26 22:52:48 +02:00
assert(d == 0);
} // test10
void test11()
{
SAX2DOM::Parser<string_type, string_adaptor> parser;
assert(parser.getFeature(SA::construct_from_utf8("http://xml.org/sax/features/validation")) == true);
parser.setFeature(SA::construct_from_utf8("http://xml.org/sax/features/validation"), false);
assert(parser.getFeature(SA::construct_from_utf8("http://xml.org/sax/features/validation")) == false);
2005-10-26 22:52:48 +02:00
parser.parse(SA::construct_from_utf8("<root attr=\"poop\"><child/></root>"));
2005-10-26 22:52:48 +02:00
} // test11
void test12()
{
SAX2DOM::Parser<string_type, string_adaptor> parser;
2005-10-26 22:52:48 +02:00
try
{
parser.getFeature(SA::construct_from_utf8("made up name"));
2005-10-26 22:52:48 +02:00
assert(false);
}
catch(const SAX::SAXNotRecognizedException&)
{
}
} // test12
};
template<class string_type, class string_adaptor>
TestSuite* SAXTest_suite()
{
TestSuite *suiteOfTests = new TestSuite;
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test1", &SAXTest<string_type, string_adaptor>::test1));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test2", &SAXTest<string_type, string_adaptor>::test2));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test3", &SAXTest<string_type, string_adaptor>::test3));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test4", &SAXTest<string_type, string_adaptor>::test4));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test5", &SAXTest<string_type, string_adaptor>::test5));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test6", &SAXTest<string_type, string_adaptor>::test6));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test7", &SAXTest<string_type, string_adaptor>::test7));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test8", &SAXTest<string_type, string_adaptor>::test8));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test9", &SAXTest<string_type, string_adaptor>::test9));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test10", &SAXTest<string_type, string_adaptor>::test10));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test11", &SAXTest<string_type, string_adaptor>::test11));
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test12", &SAXTest<string_type, string_adaptor>::test12));
return suiteOfTests;
} // SAXTest_suite
2002-09-03 15:26:08 +02:00
#endif