Initial import from the monolithic Qt.
[profile/ivi/qtxmlpatterns.git] / tests / auto / qxmlformatter / tst_qxmlformatter.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44
45 #ifdef QTEST_XMLPATTERNS
46 #include <QtXmlPatterns/QXmlFormatter>
47 #include <QtXmlPatterns/QXmlQuery>
48
49 /*!
50  \class tst_QXmlFormatter
51  \internal
52  \since 4.4
53  \brief Tests class QXmlFormatter.
54
55  This test is not intended for testing the engine, but the functionality specific
56  to the QXmlFormatter class.
57
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.
60
61  */
62 class tst_QXmlFormatter : public QObject
63 {
64     Q_OBJECT
65
66 public:
67     tst_QXmlFormatter();
68
69 private Q_SLOTS:
70     void indentationDepth() const;
71     void setIndentationDepth() const;
72     void constCorrectness() const;
73     void objectSize() const;
74     void format();
75     void format_data() const;
76     void cleanupTestCase() const;
77 private:
78     enum
79     {
80         ExpectedTestCount = 19
81     };
82
83     int m_generatedBaselines;
84 };
85
86 tst_QXmlFormatter::tst_QXmlFormatter() : m_generatedBaselines(0)
87 {
88 }
89
90 void tst_QXmlFormatter::indentationDepth() const
91 {
92     /* Check default value. */
93     {
94         QXmlQuery query;
95         QByteArray out;
96         QBuffer buffer(&out);
97         QVERIFY(buffer.open(QIODevice::WriteOnly));
98
99         QXmlFormatter formatter(query, &buffer);
100         QCOMPARE(formatter.indentationDepth(), 4);
101     }
102 }
103
104 void tst_QXmlFormatter::setIndentationDepth() const
105 {
106     QXmlQuery query;
107     QByteArray out;
108     QBuffer buffer(&out);
109     QVERIFY(buffer.open(QIODevice::WriteOnly));
110
111     QXmlFormatter formatter(query, &buffer);
112
113     formatter.setIndentationDepth(1);
114     QCOMPARE(formatter.indentationDepth(), 1);
115
116     formatter.setIndentationDepth(654987);
117     QCOMPARE(formatter.indentationDepth(), 654987);
118 }
119
120 void tst_QXmlFormatter::constCorrectness() const
121 {
122     QXmlQuery query;
123     QByteArray out;
124     QBuffer buffer(&out);
125     QVERIFY(buffer.open(QIODevice::WriteOnly));
126
127     const QXmlFormatter formatter(query, &buffer);
128
129     /* These functions should be const. */
130     formatter.indentationDepth();
131 }
132
133 void tst_QXmlFormatter::objectSize() const
134 {
135     /* We shouldn't add something. */
136     QCOMPARE(sizeof(QXmlFormatter), sizeof(QXmlSerializer));
137 }
138
139 void tst_QXmlFormatter::format()
140 {
141     QFETCH(QString, testName);
142
143     const QString location(QLatin1String("input/") + testName);
144     QFile queryFile(location);
145     QVERIFY(queryFile.open(QIODevice::ReadOnly));
146
147     QXmlQuery query;
148     query.setQuery(&queryFile, QUrl::fromLocalFile(location));
149
150     QByteArray formatted;
151     QBuffer bridge(&formatted);
152     QVERIFY(bridge.open(QIODevice::WriteOnly));
153
154     QXmlFormatter formatter(query, &bridge);
155
156     QVERIFY(query.evaluateTo(&formatter));
157
158     QFile expectedFile(QLatin1String("baselines/") + testName.left(testName.length() - 2) + QString::fromLatin1("xml"));
159
160     if(expectedFile.exists())
161     {
162         QVERIFY(expectedFile.open(QIODevice::ReadOnly));
163         const QByteArray expectedOutput(expectedFile.readAll());
164         QCOMPARE(formatted, expectedOutput);
165     }
166     else
167     {
168         ++m_generatedBaselines;
169         expectedFile.close();
170         QVERIFY(expectedFile.open(QIODevice::WriteOnly));
171         QCOMPARE(expectedFile.write(formatted), qint64(formatted.size()));
172     }
173 }
174
175 void tst_QXmlFormatter::format_data() const
176 {
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");
180
181     QDir dir;
182     dir.cd(QLatin1String("input"));
183
184     const QStringList entries(dir.entryList(QStringList(QLatin1String("*.xq"))));
185     for(int i = 0; i < entries.count(); ++i)
186     {
187         const QString &at = entries.at(i);
188         QTest::newRow(at.toUtf8().constData()) << at;
189     }
190
191     QCOMPARE(int(ExpectedTestCount), entries.count());
192 }
193
194 void tst_QXmlFormatter::cleanupTestCase() const
195 {
196     QCOMPARE(m_generatedBaselines, 0);
197 }
198
199 QTEST_MAIN(tst_QXmlFormatter)
200
201 #include "tst_qxmlformatter.moc"
202 #else
203 QTEST_NOOP_MAIN
204 #endif
205 // vim: et:ts=4:sw=4:sts=4