28#include "XPathExceptionImpl.h"
30#include "DocumentImpl.h"
31#include "DOMStringImpl.h"
32#include "KDOMParser.h"
33#include "KDOMParserFactory.h"
34#include "NamedAttrMapImpl.h"
47void check( DocumentImpl *doc,
const QString &statement,
const QString &expected )
49 ParsedStatement s( statement );
50 Value result = s.evaluate( doc );
51 if ( !result.isString() ) {
52 qDebug(
"ERROR: Query '%s' did not return a string!", statement.latin1() );
56 QString
string = result.toString();
57 if (
string != expected ) {
58 qDebug(
"ERROR: Failed to interprete '%s' correctly!", statement.latin1() );
59 qDebug(
"Expected to get: %s", expected.latin1() );
60 qDebug(
"Got : %s",
string.latin1() );
66void check( DocumentImpl *doc,
const QString &statement,
double expected )
68 ParsedStatement s( statement );
69 Value result = s.evaluate( doc );
70 if ( !result.isNumber() ) {
71 qDebug(
"ERROR: Query '%s' did not return a number!", statement.latin1() );
75 double number = result.toNumber();
76 if (
number != expected ) {
77 qDebug(
"ERROR: Failed to interprete '%s' correctly!", statement.latin1() );
78 qDebug(
"Expected to get: %f", expected );
79 qDebug(
"Got : %f",
number );
84void check( DocumentImpl *doc,
const QString &statement,
85 const QStringList &idsOfExpectedMatches )
87 ParsedStatement s( statement );
88 Value result = s.evaluate( doc );
89 if ( !result.isNodeset() ) {
90 qDebug(
"ERROR: Query '%s' did not return a nodeset!", statement.latin1() );
94 QStringList idsOfResultMatches;
96 DomNodeList nodes = result.toNodeset();
97 foreach( NodeImpl *node, nodes ) {
98 if ( node->nodeType() != ELEMENT_NODE ) {
101 NodeImpl *idNode = 0;
102 NamedAttrMapImpl *attrs = node->attributes(
true );
103 for (
unsigned long i = 0; i < attrs->length(); ++i ) {
104 idNode = attrs->item( i );
105 if ( idNode->nodeName()->string() ==
"id" ) {
110 qDebug(
"ERROR: Found match without id attribute!" );
113 idsOfResultMatches.append( idNode->nodeValue()->string() );
116 bool failure =
false;
118 foreach( QString
id, idsOfExpectedMatches ) {
119 if ( !idsOfResultMatches.contains(
id ) ) {
126 foreach( QString
id, idsOfResultMatches ) {
127 if ( !idsOfExpectedMatches.contains(
id ) ) {
135 qDebug() <<
"ERROR: Failed to interprete '" << statement <<
"' correctly!";
136 qDebug() <<
"Expected to match: " << idsOfExpectedMatches.join(
"," );
137 qDebug() <<
"Got matches : " << idsOfResultMatches.join(
"," );
142int main(
int argc,
char **argv )
146 "<abstract id=\"2\">"
148 "<chapter id=\"3\" title=\"Introduction\">"
149 "<para id=\"4\">Blah blah blah</para>"
150 "<para id=\"5\">Foo bar yoyodyne</para>"
152 "<chapter id=\"6\" title=\"TEST\" type=\"x\">"
153 "<para id=\"7\">My sister got bitten by a yak.</para>"
155 "<chapter id=\"8\" title=\"note\" type=\"y\">"
156 "<para id=\"9\">141,000</para>"
158 "<chapter id=\"10\" title=\"note\">"
159 "<para id=\"11\">Hell yeah, yaks are nice.</para>"
163 KAboutData about(
"interpreter_tester",
"interpreter_tester",
"0.1",
"Tests KDOM's XPath 1.0 Interpreter" );
166 KApplication::disableAutoDcopRegistration();
171 QBuffer *buf =
new QBuffer;
172 buf->open( QBuffer::ReadWrite );
173 buf->write( bookMarkup.toUtf8() );
177 Parser *parser = ParserFactory::self()->request( KURL(), 0,
"qxml" );
178 DocumentImpl *doc = parser->syncParse( buf );
181 check( doc,
"/book", QStringList() <<
"1" );
182 check( doc,
"/book/chapter", QStringList() <<
"3" <<
"6" <<
"8" );
183 check( doc,
"/book/chapter[@title=\"TEST\"]", QStringList() <<
"6" );
184 check( doc,
"/book/chapter[last()-1]", QStringList() <<
"8" );
185 check( doc,
"/book/chapter[contains(string(@title), \"tro\")]", QStringList() <<
"3" );
186 check( doc,
"//para", QStringList() <<
"4" <<
"5" <<
"7" <<
"9" );
187 check( doc,
"/book/chapter[position()=2]/following::*", QStringList() <<
"8" <<
"9" );
188 check( doc,
"//chapter[@title and @type]", QStringList() <<
"6" <<
"8" );
189 check( doc,
"/book/chapter[attribute::title = \"note\"][position() = 2]", QStringList() <<
"10" );
190 check( doc,
"count(//para)", 5.0 );
191 check( doc,
"string(/book/chapter/para[text() = \"Foo bar yoyodyne\" ])", QLatin1String(
"Foo bar yoyodyne" ) );
192 check( doc,
"substring-before(/book/chapter/para[@id=\"9\" ], ',' )", QLatin1String(
"141" ) );
193 }
catch ( XPath::XPathExceptionImpl *e ) {
194 qDebug(
"Caught XPath exception '%s'.", e->codeAsString().latin1() );
static void init(const KAboutData *about)
void check(DocumentImpl *doc, const QString &statement, const QString &expected)
QString number(KIO::filesize_t size)