1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include <QtXmlPatterns/QXmlFormatter>
33#include <QtXmlPatterns/QXmlQuery>
34
35/*!
36 \class tst_QXmlFormatter
37 \internal
38 \since 4.4
39 \brief Tests class QXmlFormatter.
40
41 This test is not intended for testing the engine, but the functionality specific
42 to the QXmlFormatter class.
43
44 In other words, if you have an engine bug; don't add it here because it won't be
45 tested properly. Instead add it to the test suite.
46
47 */
48class tst_QXmlFormatter : public QObject
49{
50 Q_OBJECT
51
52public:
53 tst_QXmlFormatter();
54
55private Q_SLOTS:
56 void indentationDepth() const;
57 void setIndentationDepth() const;
58 void constCorrectness() const;
59 void objectSize() const;
60 void format();
61 void format_data() const;
62 void cleanupTestCase() const;
63private:
64 enum
65 {
66 ExpectedTestCount = 19
67 };
68
69 int m_generatedBaselines;
70};
71
72tst_QXmlFormatter::tst_QXmlFormatter() : m_generatedBaselines(0)
73{
74}
75
76void tst_QXmlFormatter::indentationDepth() const
77{
78 /* Check default value. */
79 {
80 QXmlQuery query;
81 QByteArray out;
82 QBuffer buffer(&out);
83 QVERIFY(buffer.open(QIODevice::WriteOnly));
84
85 QXmlFormatter formatter(query, &buffer);
86 QCOMPARE(formatter.indentationDepth(), 4);
87 }
88}
89
90void tst_QXmlFormatter::setIndentationDepth() const
91{
92 QXmlQuery query;
93 QByteArray out;
94 QBuffer buffer(&out);
95 QVERIFY(buffer.open(QIODevice::WriteOnly));
96
97 QXmlFormatter formatter(query, &buffer);
98
99 formatter.setIndentationDepth(1);
100 QCOMPARE(formatter.indentationDepth(), 1);
101
102 formatter.setIndentationDepth(654987);
103 QCOMPARE(formatter.indentationDepth(), 654987);
104}
105
106void tst_QXmlFormatter::constCorrectness() const
107{
108 QXmlQuery query;
109 QByteArray out;
110 QBuffer buffer(&out);
111 QVERIFY(buffer.open(QIODevice::WriteOnly));
112
113 const QXmlFormatter formatter(query, &buffer);
114
115 /* These functions should be const. */
116 formatter.indentationDepth();
117}
118
119void tst_QXmlFormatter::objectSize() const
120{
121 /* We shouldn't add something. */
122 QCOMPARE(sizeof(QXmlFormatter), sizeof(QXmlSerializer));
123}
124
125void tst_QXmlFormatter::format()
126{
127 QFETCH(QString, testName);
128
129 const QString location(QFINDTESTDATA("input/") + testName);
130 QFile queryFile(location);
131 QVERIFY(queryFile.open(QIODevice::ReadOnly));
132
133 QXmlQuery query;
134 query.setQuery(sourceCode: &queryFile, documentURI: QUrl::fromLocalFile(localfile: location));
135
136 QByteArray formatted;
137 QBuffer bridge(&formatted);
138 QVERIFY(bridge.open(QIODevice::WriteOnly));
139
140 QXmlFormatter formatter(query, &bridge);
141
142 QVERIFY(query.evaluateTo(&formatter));
143
144 QFile expectedFile(QFINDTESTDATA("baselines/") + testName.left(n: testName.length() - 2) + QString::fromLatin1(str: "xml"));
145
146 if(expectedFile.exists())
147 {
148 QVERIFY(expectedFile.open(QIODevice::ReadOnly));
149 const QByteArray expectedOutput(expectedFile.readAll());
150 QCOMPARE(formatted, expectedOutput);
151 }
152 else
153 {
154 ++m_generatedBaselines;
155 expectedFile.close();
156 QVERIFY(expectedFile.open(QIODevice::WriteOnly));
157 QCOMPARE(expectedFile.write(formatted), qint64(formatted.size()));
158 }
159}
160
161void tst_QXmlFormatter::format_data() const
162{
163 // TODO test with pis and document nodes commencing indentaiton.
164 // TODO atomic values doesn't trigger characters, it seems.
165 QTest::addColumn<QString>(name: "testName");
166
167 QDir dir;
168 dir.cd(QFINDTESTDATA("input"));
169
170 const QStringList entries(dir.entryList(nameFilters: QStringList(QLatin1String("*.xq"))));
171 for(int i = 0; i < entries.count(); ++i)
172 {
173 const QString &at = entries.at(i);
174 QTest::newRow(dataTag: at.toUtf8().constData()) << at;
175 }
176
177 QCOMPARE(int(ExpectedTestCount), entries.count());
178}
179
180void tst_QXmlFormatter::cleanupTestCase() const
181{
182 QCOMPARE(m_generatedBaselines, 0);
183}
184
185QTEST_MAIN(tst_QXmlFormatter)
186
187#include "tst_qxmlformatter.moc"
188
189// vim: et:ts=4:sw=4:sts=4
190

source code of qtxmlpatterns/tests/auto/qxmlformatter/tst_qxmlformatter.cpp