parameterised XML string tests on string_adaptor

hooked into qname parsing
This commit is contained in:
jez 2009-02-23 19:23:12 +00:00
parent 1620541fb8
commit a958b03544
6 changed files with 104 additions and 74 deletions

View file

@ -1,8 +1,5 @@
#ifndef ARABICA_UTILS_STRING_ADAPTOR_H
#define ARABICA_UTILS_STRING_ADAPTOR_H
/*
* $Id$
*/
#include <SAX/ArabicaConfig.hpp>
#include <string>

View file

@ -3,6 +3,7 @@
#include <SAX/ArabicaConfig.hpp>
#include <Arabica/StringAdaptor.hpp>
#include <XML/strings.hpp>
#include <stdexcept>
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
@ -21,7 +22,7 @@ public:
*
* <p>Note that attribute names are processed differently than
* element names: an unprefixed element name will received the
* default Namespace (if any), while an unprefixed element name
* default Namespace (if any), while an unprefixed attribute name
* will not.</p>
*/
template<typename UriMapper>
@ -29,6 +30,9 @@ public:
bool is_attribute,
const UriMapper& mapper)
{
if(!Arabica::XML::is_qname<string_adaptor>(rawname))
throw std::runtime_error("Bad qname : " + SA::asStdString(rawname));
static string_type COLON = SA::construct_from_utf8(":");
typename string_adaptor::size_type index = string_adaptor::find(rawname, COLON);
@ -41,12 +45,6 @@ public:
// prefix
string_type prefix = string_adaptor::substr(rawname, 0, index);
string_type localName = string_adaptor::substr(rawname, index + 1);
if((string_adaptor::length(prefix) == 0) ||
(string_adaptor::length(localName) == 0) ||
(string_adaptor::find(localName, COLON) != string_adaptor::npos()))
throw std::runtime_error("Bad qname : " + SA::asStdString(rawname));
string_type uri = mapper(prefix);
return QualifiedName(prefix, localName, uri);

View file

@ -20,48 +20,54 @@ namespace Arabica
{
namespace XML
{
bool is_ncname(const std::string::const_iterator& b,
const std::string::const_iterator& e);
inline bool is_ncname(const std::string& str)
{
return is_ncname(str.begin(), str.end());
} // is_ncname
inline bool is_ncname(const std::string::const_iterator& b,
const std::string::const_iterator& e)
template<typename string_adaptor>
inline bool is_ncname(const typename string_adaptor::const_iterator& b,
const typename string_adaptor::const_iterator& e)
{
using namespace Arabica::text;
typedef typename string_adaptor::const_iterator const_iterator;
typedef typename string_adaptor::value_type value_type;
if(b == e)
return false; // zero length
std::string::const_iterator s = b;
if(!(is_letter(*s) || (*s == Unicode<char>::LOW_LINE)))
const_iterator s = b;
if(!(is_letter(*s) || (*s == Unicode<value_type>::LOW_LINE)))
return false;
++s;
for( ; s != e; ++s)
{
wchar_t c = static_cast<wchar_t>(*s);
value_type c = *s;
if(!is_ncname_char(c))
return false;
}
return true;
} // is_ncname
inline bool is_qname(const std::string& str)
template<typename string_adaptor>
inline bool is_ncname(const typename string_adaptor::string_type& str)
{
return is_ncname<string_adaptor>(string_adaptor::begin(str),
string_adaptor::end(str));
} // is_ncname
template<typename string_adaptor>
inline bool is_qname(const typename string_adaptor::string_type& str)
{
using namespace Arabica::text;
typedef typename string_adaptor::const_iterator const_iterator;
typedef typename string_adaptor::value_type value_type;
size_t colon_index = str.find(Unicode<char>::COLON);
size_t colon_index = string_adaptor::find(str, Unicode<value_type>::COLON);
if(colon_index == std::string::npos)
return is_ncname(str);
if(colon_index == string_adaptor::npos())
return is_ncname<string_adaptor>(str);
std::string::const_iterator b = str.begin();
return is_ncname(b, b+colon_index) &&
is_ncname(b+(colon_index+1), str.end());
const_iterator b = string_adaptor::begin(str);
const_iterator e = string_adaptor::end(str);
return is_ncname<string_adaptor>(b, b+colon_index) &&
is_ncname<string_adaptor>(b+(colon_index+1), e);
} // is_qname
} // namespace XML

View file

@ -2,7 +2,7 @@
#define UTILS_QNAME_HPP
#include <XML/QName.hpp>
#include <iostream>
using namespace Arabica::XML;
template<class string_type, class string_adaptor>
@ -112,9 +112,18 @@ public:
} // 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("::::"), false, QNT::uri_mapper);
QN::parseQName(SA::construct_from_utf8(q), false, QNT::uri_mapper);
assertFalse("oops - should have thrown here");
}
catch(std::runtime_error&) {
@ -124,13 +133,20 @@ public:
void testParseQName()
{
QN q = QN::parseQName(SA::construct_from_utf8("hello"), false, QNT::uri_mapper);
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());
QN q2 = QN::parseQName(SA::construct_from_utf8("h:hello"), false, QNT::uri_mapper);
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/"));
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/"));
} // testParseQName
}; // class QualifiedNameTest

View file

@ -5,8 +5,11 @@
using namespace Arabica::XML;
template<class string_type, class string_adaptor>
class NCNameTest : public TestCase
{
typedef string_adaptor SA;
public:
NCNameTest(std::string name) :
TestCase(name)
@ -15,51 +18,57 @@ public:
void test1()
{
assertTrue(is_ncname("woo"));
assertTrue(is_ncname<SA>(SA::construct_from_utf8("woo")));
} // test1
void test2()
{
assertTrue(is_ncname("WOO"));
assertTrue(is_ncname<SA>(SA::construct_from_utf8("WOO")));
} // test2
void test3()
{
assertFalse(is_ncname("???"));
assertFalse(is_ncname<SA>(SA::construct_from_utf8("???")));
} // test3
void test4()
{
assertTrue(is_ncname("a_b"));
assertTrue(is_ncname<SA>(SA::construct_from_utf8("a_b")));
} // test4
void test5()
{
assertFalse(is_ncname("a:b"));
assertFalse(is_ncname<SA>(SA::construct_from_utf8("a:b")));
} // test5
void test6()
{
assertFalse(is_ncname("xsl:foo"));
assertFalse(is_ncname<SA>(SA::construct_from_utf8("xsl:foo")));
} // test6
}; // class NCNameTest
template<class string_type, class string_adaptor>
TestSuite* NCNameTest_suite()
{
typedef NCNameTest<string_type, string_adaptor> NCNT;
TestSuite* suiteOfTests = new TestSuite();
suiteOfTests->addTest(new TestCaller<NCNameTest>("test1", &NCNameTest::test1));
suiteOfTests->addTest(new TestCaller<NCNameTest>("test2", &NCNameTest::test2));
suiteOfTests->addTest(new TestCaller<NCNameTest>("test3", &NCNameTest::test3));
suiteOfTests->addTest(new TestCaller<NCNameTest>("test4", &NCNameTest::test4));
suiteOfTests->addTest(new TestCaller<NCNameTest>("test5", &NCNameTest::test5));
suiteOfTests->addTest(new TestCaller<NCNameTest>("test6", &NCNameTest::test6));
suiteOfTests->addTest(new TestCaller<NCNT>("test1", &NCNT::test1));
suiteOfTests->addTest(new TestCaller<NCNT>("test2", &NCNT::test2));
suiteOfTests->addTest(new TestCaller<NCNT>("test3", &NCNT::test3));
suiteOfTests->addTest(new TestCaller<NCNT>("test4", &NCNT::test4));
suiteOfTests->addTest(new TestCaller<NCNT>("test5", &NCNT::test5));
suiteOfTests->addTest(new TestCaller<NCNT>("test6", &NCNT::test6));
return suiteOfTests;
} // NCNameTest_suite
} // NCNT_suite
template<typename string_type, typename string_adaptor>
class QNameTest : public TestCase
{
typedef string_adaptor SA;
public:
QNameTest(std::string name) :
TestCase(name)
@ -68,91 +77,95 @@ public:
void test1()
{
assertTrue(is_qname("woo"));
assertTrue(is_qname<SA>(SA::construct_from_utf8("woo")));
} // test1
void test2()
{
assertTrue(is_qname("WOO"));
assertTrue(is_qname<SA>(SA::construct_from_utf8("WOO")));
} // test2
void test3()
{
assertFalse(is_qname("???"));
assertFalse(is_qname<SA>(SA::construct_from_utf8("???")));
} // test3
void test4()
{
assertTrue(is_qname("a_b"));
assertTrue(is_qname<SA>(SA::construct_from_utf8("a_b")));
} // test4
void test5()
{
assertTrue(is_qname("a:b"));
assertTrue(is_qname<SA>(SA::construct_from_utf8("a:b")));
} // test5
void test6()
{
assertTrue(is_qname("xsl:foo"));
assertTrue(is_qname<SA>(SA::construct_from_utf8("xsl:foo")));
} // test6
void test7()
{
assertFalse(is_qname(":foo"));
assertFalse(is_qname<SA>(SA::construct_from_utf8(":foo")));
} // test7
void test8()
{
assertFalse(is_qname("foo:"));
assertFalse(is_qname<SA>(SA::construct_from_utf8("foo:")));
} // test7
void test9()
{
assertFalse(is_qname("xsl::foo"));
assertFalse(is_qname<SA>(SA::construct_from_utf8("xsl::foo")));
} // test7
void test10()
{
assertFalse(is_qname("xsl:foo:"));
assertFalse(is_qname<SA>(SA::construct_from_utf8("xsl:foo:")));
} // test7
void test11()
{
assertFalse(is_qname("$rr"));
assertFalse(is_qname<SA>(SA::construct_from_utf8("$rr")));
} // test11
void test12()
{
assertFalse(is_qname("root/child"));
assertFalse(is_qname<SA>(SA::construct_from_utf8("root/child")));
} // test12
}; // class QNameTest
template<typename string_type, typename string_adaptor>
TestSuite* QNameTest_suite()
{
typedef QNameTest<string_type, string_adaptor> QNT;
TestSuite* suiteOfTests = new TestSuite();
suiteOfTests->addTest(new TestCaller<QNameTest>("test1", &QNameTest::test1));
suiteOfTests->addTest(new TestCaller<QNameTest>("test2", &QNameTest::test2));
suiteOfTests->addTest(new TestCaller<QNameTest>("test3", &QNameTest::test3));
suiteOfTests->addTest(new TestCaller<QNameTest>("test4", &QNameTest::test4));
suiteOfTests->addTest(new TestCaller<QNameTest>("test5", &QNameTest::test5));
suiteOfTests->addTest(new TestCaller<QNameTest>("test6", &QNameTest::test6));
suiteOfTests->addTest(new TestCaller<QNameTest>("test7", &QNameTest::test7));
suiteOfTests->addTest(new TestCaller<QNameTest>("test8", &QNameTest::test8));
suiteOfTests->addTest(new TestCaller<QNameTest>("test9", &QNameTest::test9));
suiteOfTests->addTest(new TestCaller<QNameTest>("test10", &QNameTest::test10));
suiteOfTests->addTest(new TestCaller<QNameTest>("test11", &QNameTest::test11));
suiteOfTests->addTest(new TestCaller<QNameTest>("test12", &QNameTest::test12));
suiteOfTests->addTest(new TestCaller<QNT>("test1", &QNT::test1));
suiteOfTests->addTest(new TestCaller<QNT>("test2", &QNT::test2));
suiteOfTests->addTest(new TestCaller<QNT>("test3", &QNT::test3));
suiteOfTests->addTest(new TestCaller<QNT>("test4", &QNT::test4));
suiteOfTests->addTest(new TestCaller<QNT>("test5", &QNT::test5));
suiteOfTests->addTest(new TestCaller<QNT>("test6", &QNT::test6));
suiteOfTests->addTest(new TestCaller<QNT>("test7", &QNT::test7));
suiteOfTests->addTest(new TestCaller<QNT>("test8", &QNT::test8));
suiteOfTests->addTest(new TestCaller<QNT>("test9", &QNT::test9));
suiteOfTests->addTest(new TestCaller<QNT>("test10", &QNT::test10));
suiteOfTests->addTest(new TestCaller<QNT>("test11", &QNT::test11));
suiteOfTests->addTest(new TestCaller<QNT>("test12", &QNT::test12));
return suiteOfTests;
} // QNameTest_suite
template<typename string_type, typename string_adaptor>
TestSuite* XMLStringTest_suite()
{
TestSuite* suiteOfTests = new TestSuite();
suiteOfTests->addTest(NCNameTest_suite());
suiteOfTests->addTest(QNameTest_suite());
suiteOfTests->addTest(NCNameTest_suite<string_type, string_adaptor>());
suiteOfTests->addTest(QNameTest_suite<string_type, string_adaptor>());
return suiteOfTests;
} // XMLStringTest_suite

View file

@ -19,7 +19,7 @@ bool Util_test_suite(int argc, const char** argv)
runner.addTest("NormalizeWhitespaceTest", NormalizeWhitespaceTest_suite<string_type, string_adaptor >());
runner.addTest("Base64Test", Base64Test_suite());
runner.addTest("URITest", URITest_suite());
runner.addTest("XMLString", XMLStringTest_suite());
runner.addTest("XMLString", XMLStringTest_suite<string_type, string_adaptor>());
runner.addTest("QualifiedName", QualifiedNameTest_suite<string_type, string_adaptor>());
return runner.run(argc, argv);