1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #include "XMLWriter.h"
46 #include "XMLWriterTest.h"
47 #include "XMLWriterTest.moc"
49 using namespace QPatternistSDK;
51 QTEST_MAIN(XMLWriterTest)
53 void XMLWriterTest::serialize()
55 QFETCH(QString, input);
56 QFETCH(QString, expectedResult);
59 QBuffer returnBuffer(&result);
61 XMLWriter writer(&returnBuffer);
63 QXmlInputSource inputSource;
64 inputSource.setData(input);
66 QXmlSimpleReader reader;
67 reader.setContentHandler(&writer);
69 const bool parseSuccess = reader.parse(inputSource);
70 Q_ASSERT_X(parseSuccess, Q_FUNC_INFO,
71 "XMLWriter reported an error while serializing the input.");
73 QCOMPARE(QString::fromLatin1(result), expectedResult);
76 void XMLWriterTest::serialize_data()
78 QTest::addColumn<QString>("input");
79 QTest::addColumn<QString>("expectedResult");
81 /* ------------------- Elements ------------------- */
82 QTest::newRow("Only an document element")
84 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc/>";
86 QTest::newRow("Document element containing a short closed element")
88 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><f/></doc>";
89 QTest::newRow("Complex nested elements")
90 << "<doc><a/><b/><c><d/><e><f><x/></f></e></c></doc>"
91 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><a/><b/><c><d/><e><f><x/></f></e></c></doc>";
92 /* ------------------------------------------------- */
94 /* ---------------- Element Content ---------------- */
95 QTest::newRow("Element content with simple content")
96 << "<doc>content</doc>"
97 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc>content</doc>";
99 QTest::newRow("Element content with tricky to escape content")
100 << "<doc>>>&'\"''/></doc>"
101 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc>>>&'\"''/></doc>";
102 /* ------------------------------------------------- */
104 /* ----------- Processing Instructions ------------- */
105 QTest::newRow("Simple processing instruction.")
106 << "<doc><?php content?></doc>"
107 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><?php content?></doc>";
108 /* ------------------------------------------------- */
110 /* --------------- 'xml' attributes ---------------- */
111 QTest::newRow("Simple xml:space attribute.")
112 << "<doc xml:space='preserve'>content</doc>"
113 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc xml:space=\"preserve\">content</doc>";
115 QTest::newRow("Many 'xml' attributes.")
116 << "<doc xml:space='preserve' xml:foo='3' xml:s2='3'>content</doc>"
117 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
118 "<doc xml:space=\"preserve\" xml:foo=\"3\" xml:s2=\"3\">content</doc>";
119 /* ------------------------------------------------- */
121 /* ------------ namespace declarations ------------- */
122 QTest::newRow("One simple namespace declaration.")
123 << "<doc xmlns:f='example.org/'/>"
124 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
125 "<doc xmlns:f=\"example.org/\"/>";
127 QTest::newRow("Two simple namespace declarations.")
128 << "<doc xmlns:f='example.org/' xmlns:e='example.org/'/>"
129 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
130 "<doc xmlns:f=\"example.org/\" xmlns:e=\"example.org/\"/>";
132 QTest::newRow("A simple default namespace.")
133 << "<doc xmlns='example.org/'/>"
134 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
135 "<doc xmlns=\"example.org/\"/>";
136 /* ------------------------------------------------- */
138 /* -------- namespace declarations in use ---------- */
139 QTest::newRow("Simple use of a namespace declaration.")
140 << "<doc xmlns:f='example.org/' f:attr='chars'><n/><f:name f:attr='chars'/></doc>"
141 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
142 "<doc xmlns:f=\"example.org/\" f:attr=\"chars\"><n/><f:name f:attr=\"chars\"/></doc>";
143 /* ------------------------------------------------- */
146 void XMLWriterTest::cdata()
149 QTest::newRow("Simple CDATA")
150 << "<doc><![CDATA[content]]></doc>"
151 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><![CDATA[content]]></doc>";
153 QTest::newRow("Complex CDATA")
154 << "<doc><![CDATA[<<>>&'\";&987;]]></doc>"
155 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><![CDATA[<<>>&'\";&123;]]></doc>";
159 void XMLWriterTest::comments()
162 QTest::newRow("Simple comment")
163 << "<doc><!-- comment --></doc>"
164 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><!-- comment --></doc>";
165 QTest::newRow("Comment")
166 << "<doc><!--- comment --></doc>"
167 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><!--- comment --></doc>";
171 void XMLWriterTest::doxygenExample()
173 #include "../docs/XMLWriterExample.cpp"
175 /* When changing, remember to update the Doxygen in XMLWriter.h */
176 const QByteArray expectedResult(
177 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
178 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
179 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
180 "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><p>Hello World!</p></body></html>"
183 QCOMPARE(QString::fromLatin1(result), QString::fromLatin1(expectedResult));
186 // vim: et:ts=4:sw=4:sts=4