File indexing completed on 2024-05-05 16:11:46

0001 /*
0002  * parser_tester.cc - Copyright 2005 Frerich Raabe <raabe@kde.org>
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  *
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 #include "parsedstatement.h"
0026 
0027 #include <QDomDocument>
0028 #include <QtDebug>
0029 
0030 QString indentedTree(const QString &markup)
0031 {
0032     QDomDocument doc;
0033     doc.setContent(markup);
0034     return doc.toString(2);
0035 }
0036 
0037 void check(const QString &statement, const QString &expected)
0038 {
0039     QString result = ParsedStatement(statement).dump();
0040     if (indentedTree(result) != indentedTree(expected)) {
0041         qCDebug(KHTML_LOG) << "ERROR! Failed to parse '" << statement << "' as expected!";
0042         qCDebug(KHTML_LOG) << "Expected:";
0043         qCDebug(KHTML_LOG) << indentedTree(expected);
0044         qCDebug(KHTML_LOG) << "Got:";
0045         qCDebug(KHTML_LOG) << indentedTree(result);
0046         exit(1);
0047     }
0048 }
0049 
0050 extern int xpathyydebug;
0051 int main()
0052 {
0053 //  xpathyydebug=1;
0054     check("/book",
0055           "<locationpath absolute=\"true\"><step axis=\"child\" nodetest=\"book\"></step></locationpath>");
0056     check("book/self::chapter/@id",
0057           "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"book\"></step><step axis=\"self\" nodetest=\"chapter\"></step><step axis=\"attribute\" nodetest=\"id\"></step></locationpath>");
0058     check("child[\"foo\"=\"bar\" ]",
0059           "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"child\"><predicate><relationEQ><operand><string>foo</string></operand><operand><string>bar</string></operand></relationEQ></predicate></step></locationpath>");
0060     check("//parent[position() < last()-1]",
0061           "<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>");
0062     check("/foo[true()][substring-after(\"First name=Joe\", \"=\") != \"Mary\"]",
0063           "<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>");
0064     check("/foo[true()][substring-after(\"First name=Joe\", \"=\") != \"Mary\"]/child[false()][substring-before(\"First name=Joe\", '=') != 'Mary']",
0065           "<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>");
0066     check("following::this[@that]",
0067           "<locationpath absolute=\"false\"><step axis=\"following\" nodetest=\"this\"><predicate><locationpath absolute=\"false\"><step axis=\"attribute\" nodetest=\"that\"/></locationpath></predicate></step></locationpath>");
0068     check("descendant::para[@type != $type]",
0069           "<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>");
0070     check("child::processing-instruction()",
0071           "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"processing-instruction\"/></locationpath>");
0072     check("child::processing-instruction(\"someParameter\")",
0073           "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"processing-instruction someParameter\"/></locationpath>");
0074     check("*",
0075           "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"*\"/></locationpath>");
0076     check("comment()",
0077           "<locationpath absolute=\"false\"><step axis=\"child\" nodetest=\"comment()\"/></locationpath>");
0078     check("comment() | text() | processing-instruction() | node()",
0079           "<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>");
0080     qDebug("All OK!");
0081 }
0082