arabica/tests/Utils/test_qname.hpp

179 lines
6.1 KiB
C++
Raw Normal View History

#ifndef UTILS_QNAME_HPP
#define UTILS_QNAME_HPP
#include <XML/QName.hpp>
#include <iostream>
using namespace Arabica::XML;
2009-02-23 09:38:50 +01:00
template<class string_type, class string_adaptor>
class QualifiedNameTest : public TestCase
{
2009-02-23 09:38:50 +01:00
typedef string_adaptor SA;
typedef QualifiedName<string_type, string_adaptor> QN;
2009-02-23 10:14:35 +01:00
typedef QualifiedNameTest<string_type, string_adaptor> QNT;
public:
QualifiedNameTest(const std::string& name) :
TestCase(name)
{
} // QualifiedNameTest
void testNcNameEquality()
{
QN q1(SA::construct_from_utf8("foo"), SA::empty_string());
QN q2(SA::construct_from_utf8("foo"), SA::empty_string());
assertTrue(q1 == q2);
assertTrue(!(q1 != q2));
} // testNcNameEquality
void testNcNameCopy()
{
QN q1(SA::construct_from_utf8("foo"), SA::empty_string());
2009-02-23 09:38:50 +01:00
QN q2(q1);
assertTrue(q1 == q2);
assertTrue(!(q1 != q2));
} // testNcNameCopy
void testNcNameAssignment()
{
QN q1(SA::construct_from_utf8("foo"), SA::empty_string());
QN q2(SA::construct_from_utf8("bar"), SA::empty_string());
assertFalse(q1 == q2);
q2 = q1;
assertTrue(q1 == q2);
} // testNcNameAssignment
void testNcClarkName()
{
QN q(SA::construct_from_utf8("bar"), SA::empty_string());
2009-02-23 09:38:50 +01:00
assertTrue(SA::construct_from_utf8("bar") == q.clarkName());
} // testNcClarkName
void testEquality()
{
2009-02-23 09:38:50 +01:00
QN q1(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
QN q2(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
assertTrue(q1 == q2);
assertTrue(!(q1 != q2));
// prefix is not significant
2009-02-23 09:38:50 +01:00
QN q3(SA::construct_from_utf8("baz"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
assertTrue(q1 == q3);
assertTrue(!(q1 != q3));
} // testEquality
void testCopy()
{
2009-02-23 09:38:50 +01:00
QN q1(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
QN q2(q1);
assertTrue(q1 == q2);
} // testCopy
void testAssignment()
{
2009-02-23 09:38:50 +01:00
QN q1(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
QN q2(SA::construct_from_utf8("foo"), SA::construct_from_utf8("parp"), SA::construct_from_utf8("http://tst/"));
assertTrue(q1 != q2);
q2 = q1;
assertTrue(q1 == q2);
} // testAssignment
void testClarkName()
{
2009-02-23 09:38:50 +01:00
QN q(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
assertTrue(SA::construct_from_utf8("{http://test/}bar") == q.clarkName());
} // testClarkName
2009-02-23 08:57:24 +01:00
void testPrefix()
{
2009-02-23 09:38:50 +01:00
QN q(SA::construct_from_utf8(""), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
2009-02-23 08:57:24 +01:00
assertFalse(q.has_prefix());
2009-02-23 09:38:50 +01:00
assertTrue(SA::construct_from_utf8("bar") == q.localName());
assertTrue(SA::construct_from_utf8("http://test/") == q.namespaceUri());
2009-02-23 08:57:24 +01:00
2009-02-23 09:38:50 +01:00
q.set_prefix(SA::construct_from_utf8("t"));
2009-02-23 08:57:24 +01:00
assertTrue(q.has_prefix());
2009-02-23 09:38:50 +01:00
assertTrue(SA::construct_from_utf8("t") == q.prefix());
2009-02-23 08:57:24 +01:00
} // testPrefix
2009-02-23 10:14:35 +01:00
static string_type uri_mapper(const string_type& /* prefix */)
2009-02-23 10:14:35 +01:00
{
return SA::construct_from_utf8("http://test/");
} // uri_mapper
void testParseBadQName()
{
doTestParseBadQName("::::");
doTestParseBadQName("foo:");
doTestParseBadQName(":oo");
doTestParseBadQName("f:ooo:");
doTestParseBadQName("???");
} // testParseBadQName
void doTestParseBadQName(const char* q)
2009-02-23 10:14:35 +01:00
{
try {
QN::parseQName(SA::construct_from_utf8(q), false, QNT::uri_mapper);
2009-02-23 10:14:35 +01:00
assertFalse("oops - should have thrown here");
}
catch(std::runtime_error&) {
// yay
}
} // testParseBadQName
2009-02-23 19:42:52 +01:00
void testParseQName()
{
QN q = QN::parseQName(SA::construct_from_utf8("hello"), true, QNT::uri_mapper);
2009-02-23 19:42:52 +01:00
assertTrue(q.localName() == SA::construct_from_utf8("hello"));
assertFalse(q.has_prefix());
assertTrue(q.namespaceUri() == SA::empty_string());
assertTrue(q.rawName() == SA::construct_from_utf8("hello"));
2009-02-23 19:42:52 +01:00
QN q2 = QN::parseQName(SA::construct_from_utf8("h:hello"), true, QNT::uri_mapper);
2009-02-23 19:42:52 +01:00
assertTrue(q2.localName() == SA::construct_from_utf8("hello"));
assertTrue(q2.prefix() == SA::construct_from_utf8("h"));
assertTrue(q2.namespaceUri() == SA::construct_from_utf8("http://test/"));
assertTrue(q2.rawName() == SA::construct_from_utf8("h:hello"));
QN q3 = QN::parseQName(SA::construct_from_utf8("hello"), false, QNT::uri_mapper);
assertTrue(q3.localName() == SA::construct_from_utf8("hello"));
assertFalse(q3.has_prefix());
assertTrue(q3.namespaceUri() == SA::construct_from_utf8("http://test/"));
assertTrue(q3.rawName() == SA::construct_from_utf8("hello"));
2009-02-23 19:42:52 +01:00
} // testParseQName
}; // class QualifiedNameTest
2009-02-23 09:38:50 +01:00
template<class string_type, class string_adaptor>
TestSuite* QualifiedNameTest_suite()
{
2009-02-23 09:38:50 +01:00
typedef QualifiedNameTest<string_type, string_adaptor> QNT;
TestSuite* suiteOfTests = new TestSuite();
2009-02-23 09:38:50 +01:00
suiteOfTests->addTest(new TestCaller<QNT>("testNcNameEquality", &QNT::testNcNameEquality));
suiteOfTests->addTest(new TestCaller<QNT>("testNcNameCopy", &QNT::testNcNameCopy));
suiteOfTests->addTest(new TestCaller<QNT>("testNcNameAssignment", &QNT::testNcNameAssignment));
suiteOfTests->addTest(new TestCaller<QNT>("testNcClarkName", &QNT::testNcClarkName));
suiteOfTests->addTest(new TestCaller<QNT>("testEquality", &QNT::testEquality));
suiteOfTests->addTest(new TestCaller<QNT>("testCopy", &QNT::testCopy));
suiteOfTests->addTest(new TestCaller<QNT>("testAssignment", &QNT::testAssignment));
suiteOfTests->addTest(new TestCaller<QNT>("testClarkName", &QNT::testClarkName));
suiteOfTests->addTest(new TestCaller<QNT>("testPrefix", &QNT::testPrefix));
2009-02-23 10:14:35 +01:00
suiteOfTests->addTest(new TestCaller<QNT>("testParseBadQName", &QNT::testParseBadQName));
2009-02-23 19:42:52 +01:00
suiteOfTests->addTest(new TestCaller<QNT>("testParseQName", &QNT::testParseQName));
return suiteOfTests;
} // QualifiedNameTest_suite
#endif