arabica/test/DOM/test_CDATA.hpp

154 lines
5 KiB
C++
Raw Normal View History

2005-08-30 13:35:57 +02:00
#ifndef ARABICA_TEST_SAX2DOM_CDATA_HPP
#define ARABICA_TEST_SAX2DOM_CDATA_HPP
2005-10-24 23:59:44 +02:00
#include "../CppUnit/framework/TestCase.h"
#include "../CppUnit/framework/TestSuite.h"
#include "../CppUnit/framework/TestCaller.h"
#include <DOM/Simple/DOMImplementation.h>
template<class string_type, class string_adaptor>
class CDATATest : public TestCase
{
DOM::DOMImplementation<string_type> factory;
2005-10-29 01:00:54 +02:00
typedef string_adaptor SA;
2005-10-24 23:59:44 +02:00
public:
CDATATest(std::string name) :
TestCase(name)
{
} // CDATATest
void setUp()
{
2005-10-31 13:07:30 +01:00
factory = SimpleDOM::DOMImplementation<string_type, string_adaptor>::getDOMImplementation();
2005-10-24 23:59:44 +02:00
} // setUp
void testNull()
{
DOM::CDATASection<string_type> d;
DOM::Node<string_type> n;
assert(d == 0);
assert(n == 0);
assert(n == d);
} // testNull
void testCreate()
{
2005-10-29 01:00:54 +02:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""),SA::construct_from_utf8(""), 0);
DOM::CDATASection<string_type> t = d.createCDATASection(SA::construct_from_utf8("some data"));
2005-10-24 23:59:44 +02:00
2005-10-29 01:00:54 +02:00
assert(t.getData() == SA::construct_from_utf8("some data"));
assert(t.getNodeName() == SA::construct_from_utf8("#cdata-section"));
2005-10-24 23:59:44 +02:00
assert(t.getNodeValue() == t.getData());
2005-10-29 01:00:54 +02:00
t.setData(SA::construct_from_utf8("newData"));
assert(t.getData() == SA::construct_from_utf8("newData"));
2005-10-24 23:59:44 +02:00
} // testCreate
void testConversion()
{
2005-10-29 01:00:54 +02:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""),SA::construct_from_utf8(""), 0);
DOM::CDATASection<string_type> t = d.createCDATASection(SA::construct_from_utf8("some data"));
2005-10-24 23:59:44 +02:00
DOM::Node<string_type> n;
assert(n != t);
n = t;
assert(n == t);
DOM::CDATASection<string_type> t2;
assert(n != t2);
t2 = DOM::CDATASection<string_type>(n);
assert(t == t2);
assert(n == t2);
DOM::CDATASection<string_type> t3;
t3 = t2;
assert(t3 == t2);
assert(t3 == t);
2005-10-29 01:00:54 +02:00
DOM::Comment<string_type> c = d.createComment(SA::construct_from_utf8("woo"));
2005-10-24 23:59:44 +02:00
try {
t = DOM::CDATASection<string_type>(c);
assertImplementation(false, "converted comment to text");
}
catch(const std::bad_cast& ex) {
assertEquals(ex.what(), "Cannot convert Node to CDATA section");
}
} // testConverstion
void testConversionToText()
{
2005-10-29 01:00:54 +02:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""),SA::construct_from_utf8(""), 0);
DOM::CDATASection<string_type> cd = d.createCDATASection(SA::construct_from_utf8("some data"));
2005-10-24 23:59:44 +02:00
DOM::Text<string_type> t;
assert(t == 0);
assert(cd != t);
t = cd;
assert(cd == t);
DOM::CDATASection<string_type> cd2;
assert(cd2 == 0);
cd2 = DOM::CDATASection<string_type>(t);
assert(cd2 == t);
assert(t == cd2);
assert(cd == cd2);
assert(cd2 == cd);
2005-10-29 01:00:54 +02:00
t = d.createTextNode(SA::construct_from_utf8("boom"));
2005-10-24 23:59:44 +02:00
try {
cd = DOM::CDATASection<string_type>(t);
assertImplementation(false, "converted text to CDATA");
}
catch(const std::bad_cast& ex) {
assertEquals(ex.what(), "Cannot convert Node to CDATA section");
}
} // testConversionToTest
void testEverythingElse()
{
2005-10-29 01:00:54 +02:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""),SA::construct_from_utf8(""), 0);
DOM::CDATASection<string_type> t = d.createCDATASection(SA::construct_from_utf8("some data"));
2005-10-24 23:59:44 +02:00
assert(t.getNodeType() == DOM::Node<string_type>::CDATA_SECTION_NODE);
assert(t.hasAttributes() == false);
assert(t.getAttributes() == 0);
assert(t.getChildNodes() == 0);
assert(t.getFirstChild() == 0);
try
{
t.appendChild(DOM::Node<string_type>());
}
catch(const DOM::DOMException&)
{
}
assert(t.getFirstChild() == 0);
assert(t.getOwnerDocument() == d);
assert(t.getParentNode() == 0);
} //
};
template<class string_type, class string_adaptor>
TestSuite* CDATATest_suite()
{
TestSuite *suiteOfTests = new TestSuite;
suiteOfTests->addTest(new TestCaller<CDATATest<string_type, string_adaptor> >("testNull", &CDATATest<string_type, string_adaptor> ::testNull));
suiteOfTests->addTest(new TestCaller<CDATATest<string_type, string_adaptor> >("testCreate", &CDATATest<string_type, string_adaptor> ::testCreate));
suiteOfTests->addTest(new TestCaller<CDATATest<string_type, string_adaptor> >("testConversion", &CDATATest<string_type, string_adaptor> ::testConversion));
suiteOfTests->addTest(new TestCaller<CDATATest<string_type, string_adaptor> >("testConversionToText", &CDATATest<string_type, string_adaptor> ::testConversionToText));
suiteOfTests->addTest(new TestCaller<CDATATest<string_type, string_adaptor> >("testEverythingElse", &CDATATest<string_type, string_adaptor> ::testEverythingElse));
return suiteOfTests;
} // CDATATest_suite
2005-08-30 13:35:57 +02:00
2005-10-31 13:07:30 +01:00
#endif