1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
45 #ifdef QTEST_XMLPATTERNS
46 #include <QtXmlPatterns/QXmlFormatter>
47 #include <QtXmlPatterns/QXmlQuery>
50 \class tst_QXmlFormatter
53 \brief Tests class QXmlFormatter.
55 This test is not intended for testing the engine, but the functionality specific
56 to the QXmlFormatter class.
58 In other words, if you have an engine bug; don't add it here because it won't be
59 tested properly. Instead add it to the test suite.
62 class tst_QXmlFormatter : public QObject
70 void indentationDepth() const;
71 void setIndentationDepth() const;
72 void constCorrectness() const;
73 void objectSize() const;
75 void format_data() const;
76 void cleanupTestCase() const;
80 ExpectedTestCount = 19
83 int m_generatedBaselines;
86 tst_QXmlFormatter::tst_QXmlFormatter() : m_generatedBaselines(0)
90 void tst_QXmlFormatter::indentationDepth() const
92 /* Check default value. */
97 QVERIFY(buffer.open(QIODevice::WriteOnly));
99 QXmlFormatter formatter(query, &buffer);
100 QCOMPARE(formatter.indentationDepth(), 4);
104 void tst_QXmlFormatter::setIndentationDepth() const
108 QBuffer buffer(&out);
109 QVERIFY(buffer.open(QIODevice::WriteOnly));
111 QXmlFormatter formatter(query, &buffer);
113 formatter.setIndentationDepth(1);
114 QCOMPARE(formatter.indentationDepth(), 1);
116 formatter.setIndentationDepth(654987);
117 QCOMPARE(formatter.indentationDepth(), 654987);
120 void tst_QXmlFormatter::constCorrectness() const
124 QBuffer buffer(&out);
125 QVERIFY(buffer.open(QIODevice::WriteOnly));
127 const QXmlFormatter formatter(query, &buffer);
129 /* These functions should be const. */
130 formatter.indentationDepth();
133 void tst_QXmlFormatter::objectSize() const
135 /* We shouldn't add something. */
136 QCOMPARE(sizeof(QXmlFormatter), sizeof(QXmlSerializer));
139 void tst_QXmlFormatter::format()
141 QFETCH(QString, testName);
143 const QString location(QLatin1String("input/") + testName);
144 QFile queryFile(location);
145 QVERIFY(queryFile.open(QIODevice::ReadOnly));
148 query.setQuery(&queryFile, QUrl::fromLocalFile(location));
150 QByteArray formatted;
151 QBuffer bridge(&formatted);
152 QVERIFY(bridge.open(QIODevice::WriteOnly));
154 QXmlFormatter formatter(query, &bridge);
156 QVERIFY(query.evaluateTo(&formatter));
158 QFile expectedFile(QLatin1String("baselines/") + testName.left(testName.length() - 2) + QString::fromLatin1("xml"));
160 if(expectedFile.exists())
162 QVERIFY(expectedFile.open(QIODevice::ReadOnly));
163 const QByteArray expectedOutput(expectedFile.readAll());
164 QCOMPARE(formatted, expectedOutput);
168 ++m_generatedBaselines;
169 expectedFile.close();
170 QVERIFY(expectedFile.open(QIODevice::WriteOnly));
171 QCOMPARE(expectedFile.write(formatted), qint64(formatted.size()));
175 void tst_QXmlFormatter::format_data() const
177 // TODO test with pis and document nodes commencing indentaiton.
178 // TODO atomic values doesn't trigger characters, it seems.
179 QTest::addColumn<QString>("testName");
182 dir.cd(QLatin1String("input"));
184 const QStringList entries(dir.entryList(QStringList(QLatin1String("*.xq"))));
185 for(int i = 0; i < entries.count(); ++i)
187 const QString &at = entries.at(i);
188 QTest::newRow(at.toUtf8().constData()) << at;
191 QCOMPARE(int(ExpectedTestCount), entries.count());
194 void tst_QXmlFormatter::cleanupTestCase() const
196 QCOMPARE(m_generatedBaselines, 0);
199 QTEST_MAIN(tst_QXmlFormatter)
201 #include "tst_qxmlformatter.moc"
205 // vim: et:ts=4:sw=4:sts=4