KHtml

parser_tester.cpp
1 /*
2  * parser_tester.cc - Copyright 2005 Frerich Raabe <[email protected]>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "parsedstatement.h"
26 
27 #include <QDomDocument>
28 #include <QtDebug>
29 
30 QString indentedTree(const QString &markup)
31 {
32  QDomDocument doc;
33  doc.setContent(markup);
34  return doc.toString(2);
35 }
36 
37 void check(const QString &statement, const QString &expected)
38 {
39  QString result = ParsedStatement(statement).dump();
40  if (indentedTree(result) != indentedTree(expected)) {
41  qCDebug(KHTML_LOG) << "ERROR! Failed to parse '" << statement << "' as expected!";
42  qCDebug(KHTML_LOG) << "Expected:";
43  qCDebug(KHTML_LOG) << indentedTree(expected);
44  qCDebug(KHTML_LOG) << "Got:";
45  qCDebug(KHTML_LOG) << indentedTree(result);
46  exit(1);
47  }
48 }
49 
50 extern int xpathyydebug;
51 int main()
52 {
53 // xpathyydebug=1;
54  check("/book",
55  "<locationpath absolute=\"true\"><step axis=\"child\" nodetest=\"book\"></step></locationpath>");
56  check("book/self::chapter/@id",
57  "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"book\"></step><step axis=\"self\" nodetest=\"chapter\"></step><step axis=\"attribute\" nodetest=\"id\"></step></locationpath>");
58  check("child[\"foo\"=\"bar\" ]",
59  "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"child\"><predicate><relationEQ><operand><string>foo</string></operand><operand><string>bar</string></operand></relationEQ></predicate></step></locationpath>");
60  check("//parent[position() < last()-1]",
61  "<locationpath absolute=\"true\"><step axis=\"descendant-or-self\" nodetest=\"node()\"></step><step axis=\"child\" nodetest=\"parent\"><predicate><relationLT><operand><function name=\"position\"/></operand><operand><subtraction><operand><function name=\"last\"/></operand><operand><number>1</number></operand></subtraction></predicate></step></locationpath>");
62  check("/foo[true()][substring-after(\"First name=Joe\", \"=\") != \"Mary\"]",
63  "<locationpath absolute=\"true\"><step axis=\"child\" nodetest=\"foo\"><predicate><function name=\"true\"/></predicate><predicate><relationNE><operand><function name=\"substring-after\"><operand><string>First name=Joe</string></operand><operand><string>=</string></operand></function></operand><operand><string>Mary</string></operand></relationNE></predicate></step></locationpath>");
64  check("/foo[true()][substring-after(\"First name=Joe\", \"=\") != \"Mary\"]/child[false()][substring-before(\"First name=Joe\", '=') != 'Mary']",
65  "<locationpath absolute=\"true\"><step axis=\"child\" nodetest=\"foo\"><predicate><function name=\"true\"/></predicate><predicate><relationNE><operand><function name=\"substring-after\"><operand><string>First name=Joe</string></operand><operand><string>=</string></operand></function></operand><operand><string>Mary</string></operand></relationNE></predicate></step> <step axis=\"child\" nodetest=\"child\"><predicate><function name=\"false\"/></predicate><predicate><relationNE><operand><function name=\"substring-before\"><operand><string>First name=Joe</string></operand><operand><string>=</string></operand></function></operand><operand><string>Mary</string></operand></relationNE></predicate></step></locationpath>");
66  check("following::this[@that]",
67  "<locationpath absolute=\"false\"><step axis=\"following\" nodetest=\"this\"><predicate><locationpath absolute=\"false\"><step axis=\"attribute\" nodetest=\"that\"/></locationpath></predicate></step></locationpath>");
68  check("descendant::para[@type != $type]",
69  "<locationpath absolute=\"false\"><step axis=\"descendant\" nodetest=\"para\"><predicate><relationNE><operand><locationpath absolute=\"false\"><step axis=\"attribute\" nodetest=\"type\"></step></locationpath></operand><operand><variablereference name=\"type\"/></operand></relationNE></predicate></step></locationpath>");
70  check("child::processing-instruction()",
71  "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"processing-instruction\"/></locationpath>");
72  check("child::processing-instruction(\"someParameter\")",
73  "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"processing-instruction someParameter\"/></locationpath>");
74  check("*",
75  "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"*\"/></locationpath>");
76  check("comment()",
77  "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"comment()\"/></locationpath>");
78  check("comment() | text() | processing-instruction() | node()",
79  "<union><operand><union><operand><union><operand><locationpath absolute=\"false\"><step nodetest=\"comment()\" axis=\"child\"/></locationpath></operand><operand><locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"text()\"/></locationpath></operand></union></operand><operand><locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"processing-instruction\"/></locationpath></operand></union></operand><operand><locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"node()\"/></locationpath></operand></union>");
80  qDebug("All OK!");
81 }
82 
QString toString(int indent) const const
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:58 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.