940c7879144d03289b493617816566cd9739f7be
[profile/ivi/qtxmlpatterns.git] / tests / auto / xmlpatternssdk / tests / XMLWriterTest.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #include "XMLWriter.h"
45
46 #include "XMLWriterTest.h"
47 #include "XMLWriterTest.moc"
48
49 using namespace QPatternistSDK;
50
51 QTEST_MAIN(XMLWriterTest)
52
53 void XMLWriterTest::serialize()
54 {
55     QFETCH(QString, input);
56     QFETCH(QString, expectedResult);
57
58     QByteArray result;
59     QBuffer returnBuffer(&result);
60
61     XMLWriter writer(&returnBuffer);
62
63     QXmlInputSource inputSource;
64     inputSource.setData(input);
65
66     QXmlSimpleReader reader;
67     reader.setContentHandler(&writer);
68
69     const bool parseSuccess = reader.parse(inputSource);
70     Q_ASSERT_X(parseSuccess, Q_FUNC_INFO,
71                "XMLWriter reported an error while serializing the input.");
72
73     QCOMPARE(QString::fromLatin1(result), expectedResult);
74 }
75
76 void XMLWriterTest::serialize_data()
77 {
78     QTest::addColumn<QString>("input");
79     QTest::addColumn<QString>("expectedResult");
80
81     /* ------------------- Elements ------------------- */
82     QTest::newRow("Only an document element")
83         << "<doc></doc>"
84         << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc/>";
85
86     QTest::newRow("Document element containing a short closed element")
87         << "<doc><f/></doc>"
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     /* ------------------------------------------------- */
93
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>";
98
99     QTest::newRow("Element content with tricky to escape content")
100         << "<doc>>>&amp;'\"''/></doc>"
101         << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc>>>&amp;'\"''/></doc>";
102     /* ------------------------------------------------- */
103
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     /* ------------------------------------------------- */
109
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>";
114
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     /* ------------------------------------------------- */
120
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/\"/>";
126
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/\"/>";
131
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     /* ------------------------------------------------- */
137
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     /* ------------------------------------------------- */
144 }
145
146 void XMLWriterTest::cdata()
147 {
148     /*
149     QTest::newRow("Simple CDATA")
150         << "<doc><![CDATA[content]]></doc>"
151         << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><![CDATA[content]]></doc>";
152
153     QTest::newRow("Complex CDATA")
154         << "<doc><![CDATA[<<>>&'\";&987;]]></doc>"
155         << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc><![CDATA[<<>>&'\";&123;]]></doc>";
156         */
157 }
158
159 void XMLWriterTest::comments()
160 {
161     /*
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>";
168         */
169 }
170
171 void XMLWriterTest::doxygenExample()
172 {
173 #include "../docs/XMLWriterExample.cpp"
174
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>"
181         );
182
183     QCOMPARE(QString::fromLatin1(result), QString::fromLatin1(expectedResult));
184 }
185
186 // vim: et:ts=4:sw=4:sts=4