#ifndef UTILS_QNAME_HPP #define UTILS_QNAME_HPP #include #include using namespace Arabica::XML; template class QualifiedNameTest : public TestCase { typedef string_adaptor SA; typedef QualifiedName QN; typedef QualifiedNameTest 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()); 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()); assertTrue(SA::construct_from_utf8("bar") == q.clarkName()); } // testNcClarkName void testEquality() { 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 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() { 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() { 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() { 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 void testPrefix() { QN q(SA::construct_from_utf8(""), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/")); assertFalse(q.has_prefix()); assertTrue(SA::construct_from_utf8("bar") == q.localName()); assertTrue(SA::construct_from_utf8("http://test/") == q.namespaceUri()); q.set_prefix(SA::construct_from_utf8("t")); assertTrue(q.has_prefix()); assertTrue(SA::construct_from_utf8("t") == q.prefix()); } // testPrefix static string_type uri_mapper(const string_type& /* prefix */) { 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) { try { QN::parseQName(SA::construct_from_utf8(q), false, QNT::uri_mapper); assertFalse("oops - should have thrown here"); } catch(std::runtime_error&) { // yay } } // testParseBadQName void testParseQName() { QN q = QN::parseQName(SA::construct_from_utf8("hello"), true, QNT::uri_mapper); 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")); QN q2 = QN::parseQName(SA::construct_from_utf8("h:hello"), true, QNT::uri_mapper); 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")); } // testParseQName void testParseQName2() { std::map namespaces; namespaces[SA::empty_string()] = SA::construct_from_utf8("http://test/"); namespaces[SA::construct_from_utf8("h")] = SA::construct_from_utf8("http://test/"); QN q = QN::parseQName(SA::construct_from_utf8("hello"), true, namespaces); 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")); QN q2 = QN::parseQName(SA::construct_from_utf8("h:hello"), true, namespaces); 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, namespaces); 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")); } // testParseQName2 }; // class QualifiedNameTest template TestSuite* QualifiedNameTest_suite() { typedef QualifiedNameTest QNT; TestSuite* suiteOfTests = new TestSuite(); suiteOfTests->addTest(new TestCaller("testNcNameEquality", &QNT::testNcNameEquality)); suiteOfTests->addTest(new TestCaller("testNcNameCopy", &QNT::testNcNameCopy)); suiteOfTests->addTest(new TestCaller("testNcNameAssignment", &QNT::testNcNameAssignment)); suiteOfTests->addTest(new TestCaller("testNcClarkName", &QNT::testNcClarkName)); suiteOfTests->addTest(new TestCaller("testEquality", &QNT::testEquality)); suiteOfTests->addTest(new TestCaller("testCopy", &QNT::testCopy)); suiteOfTests->addTest(new TestCaller("testAssignment", &QNT::testAssignment)); suiteOfTests->addTest(new TestCaller("testClarkName", &QNT::testClarkName)); suiteOfTests->addTest(new TestCaller("testPrefix", &QNT::testPrefix)); suiteOfTests->addTest(new TestCaller("testParseBadQName", &QNT::testParseBadQName)); suiteOfTests->addTest(new TestCaller("testParseQName", &QNT::testParseQName)); suiteOfTests->addTest(new TestCaller("testParseQName", &QNT::testParseQName2)); return suiteOfTests; } // QualifiedNameTest_suite #endif