arabica/tests/DOM/test_Element.hpp

165 lines
7.1 KiB
C++
Raw Normal View History

2002-06-21 13:16:28 +02:00
#ifndef test_Element_H
#define test_Element_H
2005-10-26 22:52:48 +02:00
#include "../CppUnit/framework/TestCase.h"
#include "../CppUnit/framework/TestSuite.h"
#include "../CppUnit/framework/TestCaller.h"
2007-09-05 00:55:47 +02:00
#include <DOM/Simple/DOMImplementation.hpp>
2005-10-26 22:52:48 +02:00
template<class string_type, class string_adaptor>
class ElementTest : public TestCase
{
DOM::DOMImplementation<string_type> factory;
2005-10-31 13:07:30 +01:00
typedef string_adaptor SA;
2005-10-26 22:52:48 +02:00
public:
ElementTest(std::string name) :
TestCase(name)
{
} // ElementTest
void setUp()
{
2005-10-31 13:07:30 +01:00
factory = SimpleDOM::DOMImplementation<string_type, string_adaptor>::getDOMImplementation();
2005-10-26 22:52:48 +02:00
} // setUp
void test1()
{
DOM::Element<string_type> d;
DOM::Node<string_type> n;
assert(d == 0);
assert(n == 0);
assert(n == d);
} // test1
void test2()
{
2005-10-31 13:07:30 +01:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
DOM::Element<string_type> elem = d.createElement(SA::construct_from_utf8("root"));
2005-10-26 22:52:48 +02:00
assert(elem.getParentNode() == 0);
assert(elem.getOwnerDocument() == d);
d.appendChild(elem);
2005-10-31 13:07:30 +01:00
DOM::Element<string_type> child = d.createElement(SA::construct_from_utf8("child"));
2005-10-26 22:52:48 +02:00
elem.appendChild(child);
} // test2
void test3()
{
2005-10-31 13:07:30 +01:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
DOM::Element<string_type> elem = d.createElement(SA::construct_from_utf8("root"));
2005-10-26 22:52:48 +02:00
assert(elem.getParentNode() == 0);
assert(elem.getOwnerDocument() == d);
d.appendChild(elem);
2005-10-31 13:07:30 +01:00
DOM::Attr<string_type> attr = d.createAttribute(SA::construct_from_utf8("attr"));
attr.setNodeValue(SA::construct_from_utf8("trousers"));
2005-10-26 22:52:48 +02:00
try
{
elem.appendChild(attr);
}
catch(const DOM::DOMException&) { }
assert(elem.getFirstChild() == 0);
} // test3
void test4()
{
2005-10-31 13:07:30 +01:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
DOM::Element<string_type> elem = d.createElement(SA::construct_from_utf8("root"));
2005-10-26 22:52:48 +02:00
assert(elem.getParentNode() == 0);
assert(elem.getOwnerDocument() == d);
d.appendChild(elem);
2005-10-31 13:07:30 +01:00
DOM::Attr<string_type> attr = d.createAttribute(SA::construct_from_utf8("attr"));
2005-10-26 22:52:48 +02:00
elem.setAttributeNode(attr);
2005-10-31 13:07:30 +01:00
assert(elem.getAttributeNode(SA::construct_from_utf8("attr")) == attr);
2005-10-26 22:52:48 +02:00
assert(elem.removeAttributeNode(attr) == attr);
} // test4
void test5()
{
2005-10-31 13:07:30 +01:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
DOM::Element<string_type> elem = d.createElement(SA::construct_from_utf8("root"));
2005-10-26 22:52:48 +02:00
assert(elem.getParentNode() == 0);
assert(elem.getOwnerDocument() == d);
d.appendChild(elem);
assert(elem.hasAttributes() == false);
2005-10-31 13:07:30 +01:00
assert(elem.hasAttribute(SA::construct_from_utf8("attr")) == false);
elem.setAttribute(SA::construct_from_utf8("attr"), SA::construct_from_utf8("poop"));
2005-10-26 22:52:48 +02:00
assert(elem.hasAttributes() == true);
2005-10-31 13:07:30 +01:00
assert(elem.hasAttribute(SA::construct_from_utf8("attr")) == true);
2005-10-26 22:52:48 +02:00
2005-10-31 13:07:30 +01:00
DOM::Node<string_type> n = elem.getAttributeNode(SA::construct_from_utf8("attr"));
assert(n.getNodeValue() == SA::construct_from_utf8("poop"));
assert(elem.setAttributeNode(d.createAttribute(SA::construct_from_utf8("attr"))) == n);
2005-10-26 22:52:48 +02:00
} // test5
void test6()
{
2005-10-31 13:07:30 +01:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
DOM::Element<string_type> elem = d.createElement(SA::construct_from_utf8("root"));
2005-10-26 22:52:48 +02:00
assert(elem.getParentNode() == 0);
assert(elem.getOwnerDocument() == d);
d.appendChild(elem);
assert(elem.hasAttributes() == false);
2005-10-31 13:07:30 +01:00
assert(elem.hasAttribute(SA::construct_from_utf8("attr")) == false);
elem.setAttribute(SA::construct_from_utf8("attr"), SA::construct_from_utf8("poop"));
2005-10-26 22:52:48 +02:00
assert(elem.hasAttributes() == true);
2005-10-31 13:07:30 +01:00
assert(elem.hasAttribute(SA::construct_from_utf8("attr")) == true);
elem.removeAttribute(SA::construct_from_utf8("attr"));
2005-10-26 22:52:48 +02:00
assert(elem.hasAttributes() == false);
2005-10-31 13:07:30 +01:00
assert(elem.hasAttribute(SA::construct_from_utf8("attr")) == false);
2005-10-26 22:52:48 +02:00
} // test6
void test7()
{
2005-10-31 13:07:30 +01:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
DOM::Element<string_type> elem = d.createElement(SA::construct_from_utf8("root"));
elem.setAttribute(SA::construct_from_utf8("attr"), SA::construct_from_utf8("poop"));
elem.appendChild(d.createElement(SA::construct_from_utf8("child")));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> e2 = DOM::Element<string_type>(elem.cloneNode(false));
assert(e2.getOwnerDocument() == d);
assert(e2.getParentNode() == 0);
assert(e2.hasAttributes() == true);
2005-10-31 13:07:30 +01:00
assert(e2.getAttribute(SA::construct_from_utf8("attr")) == SA::construct_from_utf8("poop"));
2005-10-26 22:52:48 +02:00
assert(e2.getFirstChild() == 0);
} // test7
void test8()
{
2005-10-31 13:07:30 +01:00
DOM::Document<string_type> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
DOM::Element<string_type> elem = d.createElement(SA::construct_from_utf8("root"));
elem.setAttribute(SA::construct_from_utf8("attr"), SA::construct_from_utf8("poop"));
elem.appendChild(d.createElement(SA::construct_from_utf8("child")));
2005-10-26 22:52:48 +02:00
DOM::Element<string_type> e2 = DOM::Element<string_type>(elem.cloneNode(true));
assert(e2.getOwnerDocument() == d);
assert(e2.getParentNode() == 0);
assert(e2.hasAttributes() == true);
2005-10-31 13:07:30 +01:00
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
} // test
};
template<class string_type, class string_adaptor>
TestSuite* ElementTest_suite()
{
TestSuite *suiteOfTests = new TestSuite;
suiteOfTests->addTest(new TestCaller<ElementTest<string_type, string_adaptor> >("test1", &ElementTest<string_type, string_adaptor>::test1));
suiteOfTests->addTest(new TestCaller<ElementTest<string_type, string_adaptor> >("test2", &ElementTest<string_type, string_adaptor>::test2));
suiteOfTests->addTest(new TestCaller<ElementTest<string_type, string_adaptor> >("test3", &ElementTest<string_type, string_adaptor>::test3));
suiteOfTests->addTest(new TestCaller<ElementTest<string_type, string_adaptor> >("test4", &ElementTest<string_type, string_adaptor>::test4));
suiteOfTests->addTest(new TestCaller<ElementTest<string_type, string_adaptor> >("test5", &ElementTest<string_type, string_adaptor>::test5));
suiteOfTests->addTest(new TestCaller<ElementTest<string_type, string_adaptor> >("test6", &ElementTest<string_type, string_adaptor>::test6));
suiteOfTests->addTest(new TestCaller<ElementTest<string_type, string_adaptor> >("test7", &ElementTest<string_type, string_adaptor>::test7));
suiteOfTests->addTest(new TestCaller<ElementTest<string_type, string_adaptor> >("test8", &ElementTest<string_type, string_adaptor>::test8));
return suiteOfTests;
} // ElementTest_suite
2002-06-21 13:16:28 +02:00
#endif