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 ****************************************************************************/
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