author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 <QDirIterator> |
|
44 |
#include <QEventLoop> |
|
45 |
#include <QNetworkAccessManager> |
|
46 |
#include <QNetworkReply> |
|
47 |
#include <QNetworkRequest> |
|
48 |
#include <QtTest/QtTest> |
|
49 |
#include <QUrl> |
|
50 |
#include <QXmlDefaultHandler> |
|
51 |
#include <QXmlStreamReader> |
|
52 |
||
53 |
#include "qc14n.h" |
|
54 |
||
55 |
//TESTED_CLASS=QXmlStreamReader QXmlStreamWriter |
|
56 |
//TESTED_FILES=corelib/xml/stream/qxmlutils.cpp corelib/xml/stream/qxmlstream.cpp corelib/xml/stream/qxmlstream_p.h |
|
57 |
||
58 |
Q_DECLARE_METATYPE(QXmlStreamReader::ReadElementTextBehaviour) |
|
59 |
||
60 |
static const char *const catalogFile = "XML-Test-Suite/xmlconf/finalCatalog.xml"; |
|
61 |
static const int expectedRunCount = 1646; |
|
62 |
static const int expectedSkipCount = 532; |
|
63 |
||
64 |
static inline int best(int a, int b) |
|
65 |
{ |
|
66 |
if (a < 0) |
|
67 |
return b; |
|
68 |
if (b < 0) |
|
69 |
return a; |
|
70 |
return qMin(a, b); |
|
71 |
} |
|
72 |
||
73 |
static inline int best(int a, int b, int c) |
|
74 |
{ |
|
75 |
if (a < 0) |
|
76 |
return best(b, c); |
|
77 |
if (b < 0) |
|
78 |
return best(a, c); |
|
79 |
if (c < 0) |
|
80 |
return best(a, b); |
|
81 |
return qMin(qMin(a, b), c); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Opens @p filename and returns content produced as per |
|
86 |
* xmlconf/xmltest/canonxml.html. |
|
87 |
* |
|
88 |
* @p docType is the DOCTYPE name that the returned output should |
|
89 |
* have, if it doesn't already have one. |
|
90 |
*/ |
|
91 |
static QByteArray makeCanonical(const QString &filename, |
|
92 |
const QString &docType, |
|
93 |
bool &hasError, |
|
94 |
bool testIncremental = false) |
|
95 |
{ |
|
96 |
QFile file(filename); |
|
97 |
file.open(QIODevice::ReadOnly); |
|
98 |
||
99 |
QXmlStreamReader reader; |
|
100 |
||
101 |
QByteArray buffer; |
|
102 |
int bufferPos = 0; |
|
103 |
||
104 |
if (testIncremental) |
|
105 |
buffer = file.readAll(); |
|
106 |
else |
|
107 |
reader.setDevice(&file); |
|
108 |
||
109 |
QByteArray outarray; |
|
110 |
QXmlStreamWriter writer(&outarray); |
|
111 |
||
112 |
forever { |
|
113 |
while (!reader.atEnd()) { |
|
114 |
reader.readNext(); |
|
115 |
if (reader.isDTD()) { |
|
116 |
if (!reader.notationDeclarations().isEmpty()) { |
|
117 |
QString dtd; |
|
118 |
QTextStream writeDtd(&dtd); |
|
119 |
||
120 |
writeDtd << "<!DOCTYPE "; |
|
121 |
writeDtd << docType; |
|
122 |
writeDtd << " ["; |
|
123 |
writeDtd << endl; |
|
124 |
QMap<QString, QXmlStreamNotationDeclaration> sortedNotationDeclarations; |
|
125 |
foreach (QXmlStreamNotationDeclaration notation, reader.notationDeclarations()) |
|
126 |
sortedNotationDeclarations.insert(notation.name().toString(), notation); |
|
127 |
foreach (QXmlStreamNotationDeclaration notation, sortedNotationDeclarations.values()) { |
|
128 |
writeDtd << "<!NOTATION "; |
|
129 |
writeDtd << notation.name().toString(); |
|
130 |
if (notation.publicId().isEmpty()) { |
|
131 |
writeDtd << " SYSTEM \'"; |
|
132 |
writeDtd << notation.systemId().toString(); |
|
133 |
writeDtd << "\'"; |
|
134 |
} else { |
|
135 |
writeDtd << " PUBLIC \'"; |
|
136 |
writeDtd << notation.publicId().toString(); |
|
137 |
writeDtd << "\'"; |
|
138 |
if (!notation.systemId().isEmpty() ) { |
|
139 |
writeDtd << " \'"; |
|
140 |
writeDtd << notation.systemId().toString(); |
|
141 |
writeDtd << "\'"; |
|
142 |
} |
|
143 |
} |
|
144 |
writeDtd << ">"; |
|
145 |
writeDtd << endl; |
|
146 |
} |
|
147 |
||
148 |
writeDtd << "]>"; |
|
149 |
writeDtd << endl; |
|
150 |
writer.writeDTD(dtd); |
|
151 |
} |
|
152 |
} else if (reader.isStartElement()) { |
|
153 |
writer.writeStartElement(reader.namespaceUri().toString(), reader.name().toString()); |
|
154 |
||
155 |
QMap<QString, QXmlStreamAttribute> sortedAttributes; |
|
156 |
foreach(QXmlStreamAttribute attribute, reader.attributes()) |
|
157 |
sortedAttributes.insert(attribute.name().toString(), attribute); |
|
158 |
foreach(QXmlStreamAttribute attribute, sortedAttributes.values()) |
|
159 |
writer.writeAttribute(attribute); |
|
160 |
writer.writeCharacters(QString()); // write empty string to avoid having empty xml tags |
|
161 |
} else if (reader.isCharacters()) { |
|
162 |
// make canonical |
|
163 |
||
164 |
QString text = reader.text().toString(); |
|
165 |
int i = 0; |
|
166 |
int p = 0; |
|
167 |
while ((i = best(text.indexOf(QLatin1Char(10), p), |
|
168 |
text.indexOf(QLatin1Char(13), p), |
|
169 |
text.indexOf(QLatin1Char(9), p))) >= 0) { |
|
170 |
writer.writeCharacters(text.mid(p, i - p)); |
|
171 |
writer.writeEntityReference(QString("#%1").arg(text.at(i).unicode())); |
|
172 |
p = i + 1; |
|
173 |
} |
|
174 |
writer.writeCharacters(text.mid(p)); |
|
175 |
} else if (reader.isStartDocument() || reader.isEndDocument() || reader.isComment()){ |
|
176 |
// canonical does not want any of those |
|
177 |
} else if (reader.isProcessingInstruction() && reader.processingInstructionData().isEmpty()) { |
|
178 |
// for some reason canonical wants a space |
|
179 |
writer.writeProcessingInstruction(reader.processingInstructionTarget().toString(), QLatin1String("")); |
|
180 |
} else if (!reader.hasError()){ |
|
181 |
writer.writeCurrentToken(reader); |
|
182 |
} |
|
183 |
} |
|
184 |
if (testIncremental && bufferPos < buffer.size()) { |
|
185 |
reader.addData(QByteArray(buffer.data() + (bufferPos++), 1)); |
|
186 |
} else { |
|
187 |
break; |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
if (reader.hasError()) { |
|
192 |
hasError = true; |
|
193 |
outarray += "ERROR:"; |
|
194 |
outarray += reader.errorString().toLatin1(); |
|
195 |
} |
|
196 |
else |
|
197 |
hasError = false; |
|
198 |
||
199 |
return outarray; |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* @short Returns the lexical QName of the document element in |
|
204 |
* @p document. |
|
205 |
* |
|
206 |
* It is assumed that @p document is a well-formed XML document. |
|
207 |
*/ |
|
208 |
static QString documentElement(const QByteArray &document) |
|
209 |
{ |
|
210 |
QXmlStreamReader reader(document); |
|
211 |
||
212 |
while(!reader.atEnd()) |
|
213 |
{ |
|
214 |
if(reader.isStartElement()) |
|
215 |
return reader.qualifiedName().toString(); |
|
216 |
||
217 |
reader.readNext(); |
|
218 |
} |
|
219 |
||
220 |
Q_ASSERT_X(false, Q_FUNC_INFO, |
|
221 |
qPrintable(QString::fromLatin1("The input %1 didn't contain an element.").arg(QString::fromUtf8(document.constData())))); |
|
222 |
return QString(); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* @short Loads W3C's XML conformance test suite and runs it on QXmlStreamReader. |
|
227 |
* |
|
228 |
* Since this suite is fairly large, it runs the tests sequentially in order to not |
|
229 |
* have them all loaded into memory at once. In this way, the maximum memory usage stays |
|
230 |
* low, which means one can run valgrind on this test. However, the drawback is that |
|
231 |
* QTestLib's usual error reporting and testing mechanisms are slightly bypassed. |
|
232 |
* |
|
233 |
* Part of this code is a manual, ad-hoc implementation of xml:base. |
|
234 |
* |
|
235 |
* @see <a href="http://www.w3.org/XML/Test/">Extensible |
|
236 |
* Markup Language (XML) Conformance Test Suites</a> |
|
237 |
*/ |
|
238 |
class TestSuiteHandler : public QXmlDefaultHandler |
|
239 |
{ |
|
240 |
public: |
|
241 |
/** |
|
242 |
* The first string is the test ID, the second is |
|
243 |
* a description of what went wrong. |
|
244 |
*/ |
|
245 |
typedef QPair<QString, QString> GeneralFailure; |
|
246 |
||
247 |
/** |
|
248 |
* The string is the test ID. |
|
249 |
*/ |
|
250 |
QStringList successes; |
|
251 |
||
252 |
/** |
|
253 |
* The first value is the baseline, while the se |
|
254 |
*/ |
|
255 |
class MissedBaseline |
|
256 |
{ |
|
257 |
public: |
|
258 |
MissedBaseline(const QString &aId, |
|
259 |
const QByteArray &aExpected, |
|
260 |
const QByteArray &aOutput) : id(aId), |
|
261 |
expected(aExpected), |
|
262 |
output(aOutput) |
|
263 |
{ |
|
264 |
Q_ASSERT(!aId.isEmpty()); |
|
265 |
} |
|
266 |
||
267 |
QString id; |
|
268 |
QByteArray expected; |
|
269 |
QByteArray output; |
|
270 |
}; |
|
271 |
||
272 |
QList<GeneralFailure> failures; |
|
273 |
QList<MissedBaseline> missedBaselines; |
|
274 |
||
275 |
/** |
|
276 |
* The count of how many tests that were run. |
|
277 |
*/ |
|
278 |
int runCount; |
|
279 |
||
280 |
int skipCount; |
|
281 |
||
282 |
/** |
|
283 |
* @p baseURI is the the URI of where the catalog file resides. |
|
284 |
*/ |
|
285 |
TestSuiteHandler(const QUrl &baseURI) : runCount(0), |
|
286 |
skipCount(0) |
|
287 |
{ |
|
288 |
Q_ASSERT(baseURI.isValid()); |
|
289 |
m_baseURI.push(baseURI); |
|
290 |
} |
|
291 |
||
292 |
virtual bool characters(const QString &chars) |
|
293 |
{ |
|
294 |
m_ch = chars; |
|
295 |
return true; |
|
296 |
} |
|
297 |
||
298 |
virtual bool startElement(const QString &, |
|
299 |
const QString &, |
|
300 |
const QString &, |
|
301 |
const QXmlAttributes &atts) |
|
302 |
{ |
|
303 |
m_atts.push(atts); |
|
304 |
const int i = atts.index(QLatin1String("xml:base")); |
|
305 |
||
306 |
if(i != -1) |
|
307 |
m_baseURI.push(m_baseURI.top().resolved(atts.value(i))); |
|
308 |
||
309 |
return true; |
|
310 |
} |
|
311 |
||
312 |
virtual bool endElement(const QString &, |
|
313 |
const QString &localName, |
|
314 |
const QString &) |
|
315 |
{ |
|
316 |
if(localName == QLatin1String("TEST")) |
|
317 |
{ |
|
318 |
/* We don't want tests for XML 1.1.0, in fact). */ |
|
319 |
if(m_atts.top().value(QString(), QLatin1String("VERSION")) == QLatin1String("1.1")) |
|
320 |
{ |
|
321 |
++skipCount; |
|
322 |
m_atts.pop(); |
|
323 |
return true; |
|
324 |
} |
|
325 |
||
326 |
/* We don't want tests that conflict with the namespaces spec. Our parser is a |
|
327 |
* namespace-aware parser. */ |
|
328 |
else if(m_atts.top().value(QString(), QLatin1String("NAMESPACE")) == QLatin1String("no")) |
|
329 |
{ |
|
330 |
++skipCount; |
|
331 |
m_atts.pop(); |
|
332 |
return true; |
|
333 |
} |
|
334 |
||
335 |
const QString inputFilePath(m_baseURI.top().resolved(m_atts.top().value(QString(), QLatin1String("URI"))) |
|
336 |
.toLocalFile()); |
|
337 |
const QString id(m_atts.top().value(QString(), QLatin1String("ID"))); |
|
338 |
const QString type(m_atts.top().value(QString(), QLatin1String("TYPE"))); |
|
339 |
||
340 |
QString expectedFilePath; |
|
341 |
const int index = m_atts.top().index(QString(), QLatin1String("OUTPUT")); |
|
342 |
||
343 |
//qDebug() << "Running test case:" << id; |
|
344 |
||
345 |
if(index != -1) |
|
346 |
{ |
|
347 |
expectedFilePath = m_baseURI.top().resolved(m_atts.top().value(QString(), |
|
348 |
QLatin1String("OUTPUT"))).toLocalFile(); |
|
349 |
} |
|
350 |
||
351 |
/* testcases.dtd: 'No parser should accept a "not-wf" testcase |
|
352 |
* unless it's a nonvalidating parser and the test contains |
|
353 |
* external entities that the parser doesn't read.' |
|
354 |
* |
|
355 |
* We also let this apply to "valid", "invalid" and "error" tests, although |
|
356 |
* I'm not fully sure this is correct. */ |
|
357 |
const QString ents(m_atts.top().value(QString(), QLatin1String("ENTITIES"))); |
|
358 |
m_atts.pop(); |
|
359 |
||
360 |
if(ents == QLatin1String("both") || |
|
361 |
ents == QLatin1String("general") || |
|
362 |
ents == QLatin1String("parameter")) |
|
363 |
{ |
|
364 |
++skipCount; |
|
365 |
return true; |
|
366 |
} |
|
367 |
||
368 |
++runCount; |
|
369 |
||
370 |
QFile inputFile(inputFilePath); |
|
371 |
if(!inputFile.open(QIODevice::ReadOnly)) |
|
372 |
{ |
|
373 |
failures.append(qMakePair(id, QString::fromLatin1("Failed to open input file %1").arg(inputFilePath))); |
|
374 |
return true; |
|
375 |
} |
|
376 |
||
377 |
if(type == QLatin1String("not-wf")) |
|
378 |
{ |
|
379 |
if(isWellformed(&inputFile, ParseSinglePass)) |
|
380 |
{ |
|
381 |
failures.append(qMakePair(id, QString::fromLatin1("Failed to flag %1 as not well-formed.") |
|
382 |
.arg(inputFilePath))); |
|
383 |
||
384 |
/* Exit, the incremental test will fail as well, no need to flood the output. */ |
|
385 |
return true; |
|
386 |
} |
|
387 |
else |
|
388 |
successes.append(id); |
|
389 |
||
390 |
if(isWellformed(&inputFile, ParseIncrementally)) |
|
391 |
{ |
|
392 |
failures.append(qMakePair(id, QString::fromLatin1("Failed to flag %1 as not well-formed with incremental parsing.") |
|
393 |
.arg(inputFilePath))); |
|
394 |
} |
|
395 |
else |
|
396 |
successes.append(id); |
|
397 |
||
398 |
return true; |
|
399 |
} |
|
400 |
||
401 |
QXmlStreamReader reader(&inputFile); |
|
402 |
||
403 |
/* See testcases.dtd which reads: 'Nonvalidating parsers |
|
404 |
* must also accept "invalid" testcases, but validating ones must reject them.' */ |
|
405 |
if(type == QLatin1String("invalid") || type == QLatin1String("valid")) |
|
406 |
{ |
|
407 |
QByteArray expected; |
|
408 |
QString docType; |
|
409 |
||
410 |
/* We only want to compare against a baseline when we have |
|
411 |
* one. Some "invalid"-tests, for instance, doesn't have baselines. */ |
|
412 |
if(!expectedFilePath.isEmpty()) |
|
413 |
{ |
|
414 |
QFile expectedFile(expectedFilePath); |
|
415 |
||
416 |
if(!expectedFile.open(QIODevice::ReadOnly)) |
|
417 |
{ |
|
418 |
failures.append(qMakePair(id, QString::fromLatin1("Failed to open baseline %1").arg(expectedFilePath))); |
|
419 |
return true; |
|
420 |
} |
|
421 |
||
422 |
expected = expectedFile.readAll(); |
|
423 |
docType = documentElement(expected); |
|
424 |
} |
|
425 |
else |
|
426 |
docType = QLatin1String("dummy"); |
|
427 |
||
428 |
bool hasError = true; |
|
429 |
bool incremental = false; |
|
430 |
||
431 |
QByteArray input(makeCanonical(inputFilePath, docType, hasError, incremental)); |
|
432 |
||
433 |
if (!hasError && !expectedFilePath.isEmpty() && input == expected) |
|
434 |
input = makeCanonical(inputFilePath, docType, hasError, (incremental = true)); |
|
435 |
||
436 |
if(hasError) |
|
437 |
failures.append(qMakePair(id, QString::fromLatin1("Failed to parse %1%2") |
|
438 |
.arg(incremental?"(incremental run only) ":"") |
|
439 |
.arg(inputFilePath))); |
|
440 |
||
441 |
if(!expectedFilePath.isEmpty() && input != expected) |
|
442 |
{ |
|
443 |
missedBaselines.append(MissedBaseline(id, expected, input)); |
|
444 |
return true; |
|
445 |
} |
|
446 |
else |
|
447 |
{ |
|
448 |
successes.append(id); |
|
449 |
return true; |
|
450 |
} |
|
451 |
} |
|
452 |
else if(type == QLatin1String("error")) |
|
453 |
{ |
|
454 |
/* Not yet sure about this one. */ |
|
455 |
// TODO |
|
456 |
return true; |
|
457 |
} |
|
458 |
else |
|
459 |
{ |
|
460 |
Q_ASSERT_X(false, Q_FUNC_INFO, "The input catalog is invalid."); |
|
461 |
return false; |
|
462 |
} |
|
463 |
} |
|
464 |
else if(localName == QLatin1String("TESTCASES") && m_atts.top().index(QLatin1String("xml:base")) != -1) |
|
465 |
m_baseURI.pop(); |
|
466 |
||
467 |
m_atts.pop(); |
|
468 |
||
469 |
return true; |
|
470 |
} |
|
471 |
||
472 |
enum ParseMode |
|
473 |
{ |
|
474 |
ParseIncrementally, |
|
475 |
ParseSinglePass |
|
476 |
}; |
|
477 |
||
478 |
static bool isWellformed(QIODevice *const inputFile, const ParseMode mode) |
|
479 |
{ |
|
480 |
Q_ASSERT(inputFile); |
|
481 |
Q_ASSERT_X(inputFile->isOpen(), Q_FUNC_INFO, "The caller is responsible for opening the device."); |
|
482 |
Q_ASSERT(mode == ParseIncrementally || mode == ParseSinglePass); |
|
483 |
||
484 |
if(mode == ParseIncrementally) |
|
485 |
{ |
|
486 |
QXmlStreamReader reader; |
|
487 |
QByteArray buffer; |
|
488 |
int bufferPos = 0; |
|
489 |
||
490 |
buffer = inputFile->readAll(); |
|
491 |
||
492 |
while(true) |
|
493 |
{ |
|
494 |
while(!reader.atEnd()) |
|
495 |
reader.readNext(); |
|
496 |
||
497 |
if(bufferPos < buffer.size()) |
|
498 |
{ |
|
499 |
++bufferPos; |
|
500 |
reader.addData(QByteArray(buffer.data() + bufferPos, 1)); |
|
501 |
} |
|
502 |
else |
|
503 |
break; |
|
504 |
} |
|
505 |
||
506 |
return !reader.hasError(); |
|
507 |
} |
|
508 |
else |
|
509 |
{ |
|
510 |
QXmlStreamReader reader; |
|
511 |
reader.setDevice(inputFile); |
|
512 |
||
513 |
while(!reader.atEnd()) |
|
514 |
reader.readNext(); |
|
515 |
||
516 |
return !reader.hasError(); |
|
517 |
} |
|
518 |
} |
|
519 |
||
520 |
private: |
|
521 |
QStack<QXmlAttributes> m_atts; |
|
522 |
QString m_ch; |
|
523 |
QStack<QUrl> m_baseURI; |
|
524 |
}; |
|
525 |
||
526 |
class tst_QXmlStream: public QObject |
|
527 |
{ |
|
528 |
Q_OBJECT |
|
529 |
public: |
|
530 |
tst_QXmlStream() : m_handler(QUrl::fromLocalFile(QDir::currentPath() + QLatin1Char('/')) |
|
531 |
.resolved(QUrl(QLatin1String(catalogFile)))) |
|
532 |
{ |
|
533 |
} |
|
534 |
||
535 |
private slots: |
|
536 |
void initTestCase(); |
|
537 |
void reportFailures() const; |
|
538 |
void reportFailures_data(); |
|
539 |
void checkBaseline() const; |
|
540 |
void checkBaseline_data() const; |
|
541 |
void testReader() const; |
|
542 |
void testReader_data() const; |
|
543 |
void reportSuccess() const; |
|
544 |
void reportSuccess_data() const; |
|
545 |
void parseXSLTTestSuite() const; |
|
546 |
void writerHangs() const; |
|
547 |
void writerAutoFormattingWithComments() const; |
|
548 |
void writerAutoFormattingWithTabs() const; |
|
549 |
void writerAutoEmptyTags() const; |
|
550 |
void writeAttributesWithSpace() const; |
|
551 |
void addExtraNamespaceDeclarations(); |
|
552 |
void setEntityResolver(); |
|
553 |
void readFromQBuffer() const; |
|
554 |
void readFromQBufferInvalid() const; |
|
555 |
void readNextStartElement() const; |
|
556 |
void readElementText() const; |
|
557 |
void readElementText_data() const; |
|
558 |
void crashInUTF16Codec() const; |
|
559 |
void hasAttributeSignature() const; |
|
560 |
void hasAttribute() const; |
|
561 |
void writeWithCodec() const; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
562 |
void writeWithUtf8Codec() const; |
0 | 563 |
void writeWithStandalone() const; |
564 |
void entitiesAndWhitespace_1() const; |
|
565 |
void entitiesAndWhitespace_2() const; |
|
566 |
void testFalsePrematureError() const; |
|
567 |
void garbageInXMLPrologDefaultCodec() const; |
|
568 |
void garbageInXMLPrologUTF8Explicitly() const; |
|
569 |
void clear() const; |
|
570 |
void checkCommentIndentation() const; |
|
571 |
void checkCommentIndentation_data() const; |
|
572 |
||
573 |
private: |
|
574 |
static QByteArray readFile(const QString &filename); |
|
575 |
||
576 |
TestSuiteHandler m_handler; |
|
577 |
}; |
|
578 |
||
579 |
void tst_QXmlStream::initTestCase() |
|
580 |
{ |
|
581 |
QFile file(QString::fromLatin1(catalogFile)); |
|
582 |
QVERIFY2(file.open(QIODevice::ReadOnly), |
|
583 |
qPrintable(QString::fromLatin1("Failed to open the test suite catalog; %1").arg(file.fileName()))); |
|
584 |
||
585 |
QXmlInputSource source(&file); |
|
586 |
QXmlSimpleReader reader; |
|
587 |
reader.setContentHandler(&m_handler); |
|
588 |
||
589 |
QVERIFY(reader.parse(&source, false)); |
|
590 |
} |
|
591 |
||
592 |
void tst_QXmlStream::reportFailures() const |
|
593 |
{ |
|
594 |
QFETCH(bool, isError); |
|
595 |
QFETCH(QString, description); |
|
596 |
||
597 |
QVERIFY2(!isError, qPrintable(description)); |
|
598 |
} |
|
599 |
||
600 |
void tst_QXmlStream::reportFailures_data() |
|
601 |
{ |
|
602 |
const int len = m_handler.failures.count(); |
|
603 |
||
604 |
QTest::addColumn<bool>("isError"); |
|
605 |
QTest::addColumn<QString>("description"); |
|
606 |
||
607 |
/* We loop over all our failures(if any!), and output them such |
|
608 |
* that they appear in the QTestLib log. */ |
|
609 |
for(int i = 0; i < len; ++i) |
|
610 |
QTest::newRow(m_handler.failures.at(i).first.toLatin1().constData()) << true << m_handler.failures.at(i).second; |
|
611 |
||
612 |
/* We need to add at least one column of test data, otherwise QTestLib complains. */ |
|
613 |
if(len == 0) |
|
614 |
QTest::newRow("Whole test suite passed") << false << QString(); |
|
615 |
||
616 |
/* We compare the test case counts to ensure that we've actually run test cases, that |
|
617 |
* the driver hasn't been broken or changed without updating the expected count, and |
|
618 |
* similar reasons. */ |
|
619 |
QCOMPARE(m_handler.runCount, expectedRunCount); |
|
620 |
QCOMPARE(m_handler.skipCount, expectedSkipCount); |
|
621 |
} |
|
622 |
||
623 |
void tst_QXmlStream::checkBaseline() const |
|
624 |
{ |
|
625 |
QFETCH(bool, isError); |
|
626 |
QFETCH(QString, expected); |
|
627 |
QFETCH(QString, output); |
|
628 |
||
629 |
if(isError) |
|
630 |
QCOMPARE(output, expected); |
|
631 |
} |
|
632 |
||
633 |
void tst_QXmlStream::checkBaseline_data() const |
|
634 |
{ |
|
635 |
QTest::addColumn<bool>("isError"); |
|
636 |
QTest::addColumn<QString>("expected"); |
|
637 |
QTest::addColumn<QString>("output"); |
|
638 |
||
639 |
const int len = m_handler.missedBaselines.count(); |
|
640 |
||
641 |
for(int i = 0; i < len; ++i) |
|
642 |
{ |
|
643 |
const TestSuiteHandler::MissedBaseline &b = m_handler.missedBaselines.at(i); |
|
644 |
||
645 |
/* We indeed don't know what encoding the content is in so in some cases fromUtf8 |
|
646 |
* is all wrong, but it's an acceptable guess for error reporting. */ |
|
647 |
QTest::newRow(b.id.toLatin1().constData()) |
|
648 |
<< true |
|
649 |
<< QString::fromUtf8(b.expected.constData()) |
|
650 |
<< QString::fromUtf8(b.output.constData()); |
|
651 |
} |
|
652 |
||
653 |
if(len == 0) |
|
654 |
QTest::newRow("dummy") << false << QString() << QString(); |
|
655 |
} |
|
656 |
||
657 |
void tst_QXmlStream::reportSuccess() const |
|
658 |
{ |
|
659 |
QFETCH(bool, isError); |
|
660 |
||
661 |
QVERIFY(!isError); |
|
662 |
} |
|
663 |
||
664 |
void tst_QXmlStream::reportSuccess_data() const |
|
665 |
{ |
|
666 |
QTest::addColumn<bool>("isError"); |
|
667 |
||
668 |
const int len = m_handler.successes.count(); |
|
669 |
||
670 |
for(int i = 0; i < len; ++i) |
|
671 |
QTest::newRow(m_handler.successes.at(i).toLatin1().constData()) << false; |
|
672 |
||
673 |
if(len == 0) |
|
674 |
QTest::newRow("No test cases succeeded.") << true; |
|
675 |
} |
|
676 |
||
677 |
QByteArray tst_QXmlStream::readFile(const QString &filename) |
|
678 |
{ |
|
679 |
QFile file(filename); |
|
680 |
file.open(QIODevice::ReadOnly); |
|
681 |
||
682 |
QXmlStreamReader reader; |
|
683 |
||
684 |
reader.setDevice(&file); |
|
685 |
QByteArray outarray; |
|
686 |
QTextStream writer(&outarray); |
|
687 |
// We always want UTF-8, and not what the system picks up. |
|
688 |
writer.setCodec("UTF-8"); |
|
689 |
||
690 |
while (!reader.atEnd()) { |
|
691 |
reader.readNext(); |
|
692 |
writer << reader.tokenString() << "("; |
|
693 |
if (reader.isWhitespace()) |
|
694 |
writer << " whitespace"; |
|
695 |
if (reader.isCDATA()) |
|
696 |
writer << " CDATA"; |
|
697 |
if (reader.isStartDocument() && reader.isStandaloneDocument()) |
|
698 |
writer << " standalone"; |
|
699 |
if (!reader.text().isEmpty()) |
|
700 |
writer << " text=\"" << reader.text().toString() << "\""; |
|
701 |
if (!reader.processingInstructionTarget().isEmpty()) |
|
702 |
writer << " processingInstructionTarget=\"" << reader.processingInstructionTarget().toString() << "\""; |
|
703 |
if (!reader.processingInstructionData().isEmpty()) |
|
704 |
writer << " processingInstructionData=\"" << reader.processingInstructionData().toString() << "\""; |
|
705 |
if (!reader.dtdName().isEmpty()) |
|
706 |
writer << " dtdName=\"" << reader.dtdName().toString() << "\""; |
|
707 |
if (!reader.dtdPublicId().isEmpty()) |
|
708 |
writer << " dtdPublicId=\"" << reader.dtdPublicId().toString() << "\""; |
|
709 |
if (!reader.dtdSystemId().isEmpty()) |
|
710 |
writer << " dtdSystemId=\"" << reader.dtdSystemId().toString() << "\""; |
|
711 |
if (!reader.documentVersion().isEmpty()) |
|
712 |
writer << " documentVersion=\"" << reader.documentVersion().toString() << "\""; |
|
713 |
if (!reader.documentEncoding().isEmpty()) |
|
714 |
writer << " documentEncoding=\"" << reader.documentEncoding().toString() << "\""; |
|
715 |
if (!reader.name().isEmpty()) |
|
716 |
writer << " name=\"" << reader.name().toString() << "\""; |
|
717 |
if (!reader.namespaceUri().isEmpty()) |
|
718 |
writer << " namespaceUri=\"" << reader.namespaceUri().toString() << "\""; |
|
719 |
if (!reader.qualifiedName().isEmpty()) |
|
720 |
writer << " qualifiedName=\"" << reader.qualifiedName().toString() << "\""; |
|
721 |
if (!reader.prefix().isEmpty()) |
|
722 |
writer << " prefix=\"" << reader.prefix().toString() << "\""; |
|
723 |
if (reader.attributes().size()) { |
|
724 |
foreach(QXmlStreamAttribute attribute, reader.attributes()) { |
|
725 |
writer << endl << " Attribute("; |
|
726 |
if (!attribute.name().isEmpty()) |
|
727 |
writer << " name=\"" << attribute.name().toString() << "\""; |
|
728 |
if (!attribute.namespaceUri().isEmpty()) |
|
729 |
writer << " namespaceUri=\"" << attribute.namespaceUri().toString() << "\""; |
|
730 |
if (!attribute.qualifiedName().isEmpty()) |
|
731 |
writer << " qualifiedName=\"" << attribute.qualifiedName().toString() << "\""; |
|
732 |
if (!attribute.prefix().isEmpty()) |
|
733 |
writer << " prefix=\"" << attribute.prefix().toString() << "\""; |
|
734 |
if (!attribute.value().isEmpty()) |
|
735 |
writer << " value=\"" << attribute.value().toString() << "\""; |
|
736 |
writer << " )" << endl; |
|
737 |
} |
|
738 |
} |
|
739 |
if (reader.namespaceDeclarations().size()) { |
|
740 |
foreach(QXmlStreamNamespaceDeclaration namespaceDeclaration, reader.namespaceDeclarations()) { |
|
741 |
writer << endl << " NamespaceDeclaration("; |
|
742 |
if (!namespaceDeclaration.prefix().isEmpty()) |
|
743 |
writer << " prefix=\"" << namespaceDeclaration.prefix().toString() << "\""; |
|
744 |
if (!namespaceDeclaration.namespaceUri().isEmpty()) |
|
745 |
writer << " namespaceUri=\"" << namespaceDeclaration.namespaceUri().toString() << "\""; |
|
746 |
writer << " )" << endl; |
|
747 |
} |
|
748 |
} |
|
749 |
if (reader.notationDeclarations().size()) { |
|
750 |
foreach(QXmlStreamNotationDeclaration notationDeclaration, reader.notationDeclarations()) { |
|
751 |
writer << endl << " NotationDeclaration("; |
|
752 |
if (!notationDeclaration.name().isEmpty()) |
|
753 |
writer << " name=\"" << notationDeclaration.name().toString() << "\""; |
|
754 |
if (!notationDeclaration.systemId().isEmpty()) |
|
755 |
writer << " systemId=\"" << notationDeclaration.systemId().toString() << "\""; |
|
756 |
if (!notationDeclaration.publicId().isEmpty()) |
|
757 |
writer << " publicId=\"" << notationDeclaration.publicId().toString() << "\""; |
|
758 |
writer << " )" << endl; |
|
759 |
} |
|
760 |
} |
|
761 |
if (reader.entityDeclarations().size()) { |
|
762 |
foreach(QXmlStreamEntityDeclaration entityDeclaration, reader.entityDeclarations()) { |
|
763 |
writer << endl << " EntityDeclaration("; |
|
764 |
if (!entityDeclaration.name().isEmpty()) |
|
765 |
writer << " name=\"" << entityDeclaration.name().toString() << "\""; |
|
766 |
if (!entityDeclaration.notationName().isEmpty()) |
|
767 |
writer << " notationName=\"" << entityDeclaration.notationName().toString() << "\""; |
|
768 |
if (!entityDeclaration.systemId().isEmpty()) |
|
769 |
writer << " systemId=\"" << entityDeclaration.systemId().toString() << "\""; |
|
770 |
if (!entityDeclaration.publicId().isEmpty()) |
|
771 |
writer << " publicId=\"" << entityDeclaration.publicId().toString() << "\""; |
|
772 |
if (!entityDeclaration.value().isEmpty()) |
|
773 |
writer << " value=\"" << entityDeclaration.value().toString() << "\""; |
|
774 |
writer << " )" << endl; |
|
775 |
} |
|
776 |
} |
|
777 |
writer << " )" << endl; |
|
778 |
} |
|
779 |
if (reader.hasError()) |
|
780 |
writer << "ERROR: " << reader.errorString() << endl; |
|
781 |
return outarray; |
|
782 |
} |
|
783 |
||
784 |
void tst_QXmlStream::testReader() const |
|
785 |
{ |
|
786 |
QFETCH(QString, xml); |
|
787 |
QFETCH(QString, ref); |
|
788 |
QFile file(ref); |
|
789 |
if (!file.exists()) { |
|
790 |
QByteArray reference = readFile(xml); |
|
791 |
QVERIFY(file.open(QIODevice::WriteOnly)); |
|
792 |
file.write(reference); |
|
793 |
file.close(); |
|
794 |
} else { |
|
795 |
QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
796 |
QString reference = QString::fromUtf8(file.readAll()); |
|
797 |
QString qxmlstream = QString::fromUtf8(readFile(xml)); |
|
798 |
QCOMPARE(qxmlstream, reference); |
|
799 |
} |
|
800 |
} |
|
801 |
||
802 |
void tst_QXmlStream::testReader_data() const |
|
803 |
{ |
|
804 |
QTest::addColumn<QString>("xml"); |
|
805 |
QTest::addColumn<QString>("ref"); |
|
806 |
QDir dir; |
|
807 |
dir.cd("data/"); |
|
808 |
foreach(QString filename , dir.entryList(QStringList() << "*.xml")) { |
|
809 |
QString reference = QFileInfo(filename).baseName() + ".ref"; |
|
810 |
QTest::newRow(dir.filePath(filename).toLatin1().data()) << dir.filePath(filename) << dir.filePath(reference); |
|
811 |
} |
|
812 |
} |
|
813 |
||
814 |
void tst_QXmlStream::parseXSLTTestSuite() const |
|
815 |
{ |
|
816 |
/* We disable this test for now, so it doesn't show up as an XFAIL. */ |
|
817 |
#if 0 |
|
818 |
QEXPECT_FAIL("", "Two problems needs to be solved in order to enable this test: \n" |
|
819 |
"* The XSLT suite is 69 MB large, which is quite a lot compared to the existing XML suite on 2 mb.\n" |
|
820 |
"* We need a c14n-like implementation in order to compare the outputs.", Abort); |
|
821 |
QVERIFY(false); |
|
822 |
||
823 |
/* We don't yet know this. TODO */ |
|
824 |
int xsltExpectedRunCount = -1; |
|
825 |
||
826 |
QStringList nameFilters; |
|
827 |
nameFilters.append("*.xsl"); |
|
828 |
nameFilters.append("*.xml"); |
|
829 |
||
830 |
QDirIterator dirIterator("XSLT-Test-Suite/", nameFilters, |
|
831 |
QDir::AllEntries, QDirIterator::Subdirectories); |
|
832 |
||
833 |
int filesParsed = 0; |
|
834 |
||
835 |
while(dirIterator.hasNext()) |
|
836 |
{ |
|
837 |
dirIterator.next(); |
|
838 |
||
839 |
const QString fp(dirIterator.filePath()); |
|
840 |
qDebug() << "Found" << fp; |
|
841 |
||
842 |
QFile inputFile(fp); |
|
843 |
QVERIFY(inputFile.open(QIODevice::ReadOnly)); |
|
844 |
||
845 |
/* Read in and write out to the QByteArray. */ |
|
846 |
QByteArray outputArray; |
|
847 |
{ |
|
848 |
QXmlStreamReader reader(&inputFile); |
|
849 |
||
850 |
QXmlStreamWriter writer(&outputArray); |
|
851 |
||
852 |
while(!reader.atEnd()) |
|
853 |
{ |
|
854 |
writer.writeCurrentToken(reader); |
|
855 |
reader.readNext(); |
|
856 |
||
857 |
QVERIFY2(!reader.hasError(), qPrintable(reader.errorString())); |
|
858 |
} |
|
859 |
/* Might be we got an error here, but we don't care. */ |
|
860 |
} |
|
861 |
||
862 |
/* Read in the two files, and compare them. */ |
|
863 |
{ |
|
864 |
QBuffer outputBuffer(&outputArray); |
|
865 |
outputBuffer.open(QIODevice::ReadOnly); |
|
866 |
inputFile.close(); |
|
867 |
inputFile.open(QIODevice::ReadOnly); |
|
868 |
||
869 |
QString message; |
|
870 |
const bool isEqual = QC14N::isEqual(&inputFile, &outputBuffer, &message); |
|
871 |
||
872 |
QVERIFY2(isEqual, message.toLatin1().constData()); |
|
873 |
||
874 |
++filesParsed; |
|
875 |
} |
|
876 |
} |
|
877 |
||
878 |
QCOMPARE(xsltExpectedRunCount, filesParsed); |
|
879 |
#endif |
|
880 |
} |
|
881 |
||
882 |
void tst_QXmlStream::addExtraNamespaceDeclarations() |
|
883 |
{ |
|
884 |
const char *data = "<bla><undeclared:foo/><undeclared_too:foo/></bla>"; |
|
885 |
{ |
|
886 |
QXmlStreamReader xml(data); |
|
887 |
while (!xml.atEnd()) { |
|
888 |
xml.readNext(); |
|
889 |
} |
|
890 |
QVERIFY2(xml.hasError(), "namespaces undeclared"); |
|
891 |
} |
|
892 |
{ |
|
893 |
QXmlStreamReader xml(data); |
|
894 |
xml.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("undeclared", "blabla")); |
|
895 |
xml.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("undeclared_too", "foofoo")); |
|
896 |
while (!xml.atEnd()) { |
|
897 |
xml.readNext(); |
|
898 |
} |
|
899 |
QVERIFY2(!xml.hasError(), xml.errorString().toLatin1().constData()); |
|
900 |
} |
|
901 |
} |
|
902 |
||
903 |
||
904 |
class EntityResolver : public QXmlStreamEntityResolver { |
|
905 |
public: |
|
906 |
QString resolveUndeclaredEntity(const QString &name) { |
|
907 |
static int count = 0; |
|
908 |
return name.toUpper() + QString::number(++count); |
|
909 |
} |
|
910 |
}; |
|
911 |
void tst_QXmlStream::setEntityResolver() |
|
912 |
{ |
|
913 |
const char *data = "<bla foo=\"&undeclared;\">&undeclared_too;</bla>"; |
|
914 |
{ |
|
915 |
QXmlStreamReader xml(data); |
|
916 |
while (!xml.atEnd()) { |
|
917 |
xml.readNext(); |
|
918 |
} |
|
919 |
QVERIFY2(xml.hasError(), "undeclared entities"); |
|
920 |
} |
|
921 |
{ |
|
922 |
QString foo; |
|
923 |
QString bla_text; |
|
924 |
QXmlStreamReader xml(data); |
|
925 |
EntityResolver resolver; |
|
926 |
xml.setEntityResolver(&resolver); |
|
927 |
while (!xml.atEnd()) { |
|
928 |
xml.readNext(); |
|
929 |
if (xml.isStartElement()) |
|
930 |
foo = xml.attributes().value("foo").toString(); |
|
931 |
if (xml.isCharacters()) |
|
932 |
bla_text += xml.text().toString(); |
|
933 |
} |
|
934 |
QVERIFY2(!xml.hasError(), xml.errorString().toLatin1().constData()); |
|
935 |
QCOMPARE(foo, QLatin1String("UNDECLARED1")); |
|
936 |
QCOMPARE(bla_text, QLatin1String("UNDECLARED_TOO2")); |
|
937 |
} |
|
938 |
} |
|
939 |
||
940 |
void tst_QXmlStream::testFalsePrematureError() const // task 179320 |
|
941 |
{ |
|
942 |
const char *illegal_start = "illegal<sta"; |
|
943 |
const char *legal_start = "<sta"; |
|
944 |
const char* end = "rt/>"; |
|
945 |
{ |
|
946 |
QXmlStreamReader xml(""); |
|
947 |
while (!xml.atEnd()) { |
|
948 |
xml.readNext(); |
|
949 |
} |
|
950 |
QVERIFY(xml.error() == QXmlStreamReader::PrematureEndOfDocumentError); |
|
951 |
QCOMPARE(xml.errorString(), QLatin1String("Premature end of document.")); |
|
952 |
xml.addData(legal_start); |
|
953 |
while (!xml.atEnd()) { |
|
954 |
xml.readNext(); |
|
955 |
} |
|
956 |
QVERIFY(xml.error() == QXmlStreamReader::PrematureEndOfDocumentError); |
|
957 |
QCOMPARE(xml.errorString(), QLatin1String("Premature end of document.")); |
|
958 |
xml.addData(end); |
|
959 |
while (!xml.atEnd()) { |
|
960 |
xml.readNext(); |
|
961 |
} |
|
962 |
QVERIFY(!xml.hasError()); |
|
963 |
} |
|
964 |
{ |
|
965 |
QXmlStreamReader xml(illegal_start); |
|
966 |
while (!xml.atEnd()) { |
|
967 |
xml.readNext(); |
|
968 |
} |
|
969 |
QVERIFY(xml.hasError()); |
|
970 |
QCOMPARE(xml.errorString(), QLatin1String("Start tag expected.")); |
|
971 |
QVERIFY(xml.error() == QXmlStreamReader::NotWellFormedError); |
|
972 |
} |
|
973 |
} |
|
974 |
||
975 |
/*! |
|
976 |
See task 188737. Crash due to using empty QStack. |
|
977 |
*/ |
|
978 |
void tst_QXmlStream::writerHangs() const |
|
979 |
{ |
|
980 |
QFile file("test.xml"); |
|
981 |
||
982 |
QVERIFY(file.open(QIODevice::WriteOnly)); |
|
983 |
||
984 |
QXmlStreamWriter writer(&file); |
|
985 |
double radius = 4.0; |
|
986 |
writer.setAutoFormatting(true); |
|
987 |
writer.writeStartDocument(); |
|
988 |
writer.writeEmptyElement("circle"); |
|
989 |
writer.writeAttribute("radius", QString::number(radius)); |
|
990 |
writer.writeEndElement(); |
|
991 |
writer.writeEndDocument(); |
|
992 |
} |
|
993 |
/*! |
|
994 |
Task 189611 |
|
995 |
*/ |
|
996 |
void tst_QXmlStream::writerAutoFormattingWithComments() const |
|
997 |
{ |
|
998 |
QBuffer buffer; |
|
999 |
buffer.open(QIODevice::WriteOnly); |
|
1000 |
||
1001 |
QXmlStreamWriter writer(&buffer); |
|
1002 |
writer.setAutoFormatting(true); |
|
1003 |
writer.writeStartDocument(); |
|
1004 |
writer.writeComment("This is a comment"); |
|
1005 |
writer.writeEndDocument(); |
|
1006 |
const char *str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--This is a comment-->\n"; |
|
1007 |
QCOMPARE(buffer.buffer().data(), str); |
|
1008 |
} |
|
1009 |
||
1010 |
||
1011 |
/*! |
|
1012 |
Task 206782 |
|
1013 |
*/ |
|
1014 |
void tst_QXmlStream::writerAutoFormattingWithTabs() const |
|
1015 |
{ |
|
1016 |
QBuffer buffer; |
|
1017 |
buffer.open(QIODevice::WriteOnly); |
|
1018 |
||
1019 |
||
1020 |
QXmlStreamWriter writer(&buffer); |
|
1021 |
writer.setAutoFormatting(true); |
|
1022 |
writer.setAutoFormattingIndent(-1); |
|
1023 |
QCOMPARE(writer.autoFormattingIndent(), -1); |
|
1024 |
writer.writeStartDocument(); |
|
1025 |
writer.writeStartElement("A"); |
|
1026 |
writer.writeStartElement("B"); |
|
1027 |
writer.writeEndDocument(); |
|
1028 |
const char *str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<A>\n\t<B/>\n</A>\n"; |
|
1029 |
QCOMPARE(buffer.buffer().data(), str); |
|
1030 |
} |
|
1031 |
||
1032 |
/*! |
|
1033 |
Task 204822 |
|
1034 |
*/ |
|
1035 |
void tst_QXmlStream::writeAttributesWithSpace() const |
|
1036 |
{ |
|
1037 |
QBuffer buffer; |
|
1038 |
buffer.open(QIODevice::WriteOnly); |
|
1039 |
||
1040 |
||
1041 |
QXmlStreamWriter writer(&buffer); |
|
1042 |
writer.writeStartDocument(); |
|
1043 |
writer.writeEmptyElement("A"); |
|
1044 |
writer.writeAttribute("attribute", QString("value")+QChar::Nbsp); |
|
1045 |
writer.writeEndDocument(); |
|
1046 |
QString s = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><A attribute=\"value%1\"/>\n").arg(QChar(QChar::Nbsp)); |
|
1047 |
QCOMPARE(buffer.buffer().data(), s.toUtf8().data()); |
|
1048 |
} |
|
1049 |
||
1050 |
/*! |
|
1051 |
Task 209340 |
|
1052 |
*/ |
|
1053 |
void tst_QXmlStream::writerAutoEmptyTags() const |
|
1054 |
{ |
|
1055 |
QBuffer buffer; |
|
1056 |
buffer.open(QIODevice::WriteOnly); |
|
1057 |
||
1058 |
||
1059 |
QXmlStreamWriter writer(&buffer); |
|
1060 |
||
1061 |
writer.writeStartDocument(); |
|
1062 |
||
1063 |
writer.writeStartElement("Hans"); |
|
1064 |
writer.writeAttribute("key", "value"); |
|
1065 |
writer.writeEndElement(); |
|
1066 |
||
1067 |
writer.writeStartElement("Hans"); |
|
1068 |
writer.writeAttribute("key", "value"); |
|
1069 |
writer.writeEmptyElement("Leer"); |
|
1070 |
writer.writeAttribute("key", "value"); |
|
1071 |
writer.writeEndElement(); |
|
1072 |
||
1073 |
writer.writeStartElement("Hans"); |
|
1074 |
writer.writeAttribute("key", "value"); |
|
1075 |
writer.writeCharacters("stuff"); |
|
1076 |
writer.writeEndElement(); |
|
1077 |
||
1078 |
writer.writeEndDocument(); |
|
1079 |
||
1080 |
QString s = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Hans key=\"value\"/><Hans key=\"value\"><Leer key=\"value\"/></Hans><Hans key=\"value\">stuff</Hans>\n"); |
|
1081 |
QCOMPARE(buffer.buffer().data(), s.toUtf8().data()); |
|
1082 |
} |
|
1083 |
||
1084 |
void tst_QXmlStream::readFromQBuffer() const |
|
1085 |
{ |
|
1086 |
QByteArray in("<e/>"); |
|
1087 |
QBuffer buffer(&in); |
|
1088 |
QVERIFY(buffer.open(QIODevice::ReadOnly)); |
|
1089 |
||
1090 |
QXmlStreamReader reader(&buffer); |
|
1091 |
||
1092 |
while(!reader.atEnd()) |
|
1093 |
{ |
|
1094 |
reader.readNext(); |
|
1095 |
} |
|
1096 |
||
1097 |
QVERIFY(!reader.hasError()); |
|
1098 |
} |
|
1099 |
||
1100 |
void tst_QXmlStream::readFromQBufferInvalid() const |
|
1101 |
{ |
|
1102 |
QByteArray in("<e/><e/>"); |
|
1103 |
QBuffer buffer(&in); |
|
1104 |
QVERIFY(buffer.open(QIODevice::ReadOnly)); |
|
1105 |
||
1106 |
QXmlStreamReader reader(&buffer); |
|
1107 |
||
1108 |
while(!reader.atEnd()) |
|
1109 |
{ |
|
1110 |
reader.readNext(); |
|
1111 |
} |
|
1112 |
||
1113 |
QVERIFY(reader.hasError()); |
|
1114 |
} |
|
1115 |
||
1116 |
void tst_QXmlStream::readNextStartElement() const |
|
1117 |
{ |
|
1118 |
QLatin1String in("<?xml version=\"1.0\"?><A><!-- blah --><B><C/></B><B attr=\"value\"/>text</A>"); |
|
1119 |
QXmlStreamReader reader(in); |
|
1120 |
||
1121 |
QVERIFY(reader.readNextStartElement()); |
|
1122 |
QVERIFY(reader.isStartElement() && reader.name() == "A"); |
|
1123 |
||
1124 |
int amountOfB = 0; |
|
1125 |
while (reader.readNextStartElement()) { |
|
1126 |
QVERIFY(reader.isStartElement() && reader.name() == "B"); |
|
1127 |
++amountOfB; |
|
1128 |
reader.skipCurrentElement(); |
|
1129 |
} |
|
1130 |
||
1131 |
QCOMPARE(amountOfB, 2); |
|
1132 |
} |
|
1133 |
||
1134 |
void tst_QXmlStream::readElementText() const |
|
1135 |
{ |
|
1136 |
QFETCH(QXmlStreamReader::ReadElementTextBehaviour, behaviour); |
|
1137 |
QFETCH(QString, input); |
|
1138 |
QFETCH(QString, expected); |
|
1139 |
||
1140 |
QXmlStreamReader reader(input); |
|
1141 |
||
1142 |
QVERIFY(reader.readNextStartElement()); |
|
1143 |
QCOMPARE(reader.readElementText(behaviour), expected); |
|
1144 |
} |
|
1145 |
||
1146 |
void tst_QXmlStream::readElementText_data() const |
|
1147 |
{ |
|
1148 |
QTest::addColumn<QXmlStreamReader::ReadElementTextBehaviour>("behaviour"); |
|
1149 |
QTest::addColumn<QString>("input"); |
|
1150 |
QTest::addColumn<QString>("expected"); |
|
1151 |
||
1152 |
QString validInput("<p>He was <em>never</em> going to admit<!-- TODO: rephrase --> his mistake.</p>"); |
|
1153 |
QString invalidInput("<p>invalid...<p>"); |
|
1154 |
QString invalidOutput("invalid..."); |
|
1155 |
||
1156 |
QTest::newRow("ErrorOnUnexpectedElement") |
|
1157 |
<< QXmlStreamReader::ErrorOnUnexpectedElement |
|
1158 |
<< validInput << QString("He was "); |
|
1159 |
||
1160 |
QTest::newRow("IncludeChildElements") |
|
1161 |
<< QXmlStreamReader::IncludeChildElements |
|
1162 |
<< validInput << QString("He was never going to admit his mistake."); |
|
1163 |
||
1164 |
QTest::newRow("SkipChildElements") |
|
1165 |
<< QXmlStreamReader::SkipChildElements |
|
1166 |
<< validInput << QString("He was going to admit his mistake."); |
|
1167 |
||
1168 |
QTest::newRow("ErrorOnUnexpectedElement Invalid") |
|
1169 |
<< QXmlStreamReader::ErrorOnUnexpectedElement |
|
1170 |
<< invalidInput << invalidOutput; |
|
1171 |
||
1172 |
QTest::newRow("IncludeChildElements Invalid") |
|
1173 |
<< QXmlStreamReader::IncludeChildElements |
|
1174 |
<< invalidInput << invalidOutput; |
|
1175 |
||
1176 |
QTest::newRow("SkipChildElements Invalid") |
|
1177 |
<< QXmlStreamReader::SkipChildElements |
|
1178 |
<< invalidInput << invalidOutput; |
|
1179 |
} |
|
1180 |
||
1181 |
void tst_QXmlStream::crashInUTF16Codec() const |
|
1182 |
{ |
|
1183 |
QEventLoop eventLoop; |
|
1184 |
||
1185 |
QNetworkAccessManager networkManager; |
|
1186 |
QNetworkRequest request(QUrl::fromLocalFile(QLatin1String("data/051reduced.xml"))); |
|
1187 |
QNetworkReply *const reply = networkManager.get(request); |
|
1188 |
eventLoop.connect(reply, SIGNAL(finished()), SLOT(quit())); |
|
1189 |
||
1190 |
QCOMPARE(eventLoop.exec(), 0); |
|
1191 |
||
1192 |
QXmlStreamReader reader(reply); |
|
1193 |
while(!reader.atEnd()) |
|
1194 |
{ |
|
1195 |
reader.readNext(); |
|
1196 |
continue; |
|
1197 |
} |
|
1198 |
||
1199 |
QVERIFY(!reader.hasError()); |
|
1200 |
} |
|
1201 |
||
1202 |
/* |
|
1203 |
In addition to QTestLib's flags, one can specify "-c <filename>" and have that file output in its canonical form. |
|
1204 |
*/ |
|
1205 |
int main(int argc, char *argv[]) |
|
1206 |
{ |
|
1207 |
QCoreApplication app(argc, argv); |
|
1208 |
||
1209 |
if (argc == 3 && QByteArray(argv[1]).startsWith("-c")) { |
|
1210 |
// output canonical only |
|
1211 |
bool error = false; |
|
1212 |
QByteArray canonical = makeCanonical(argv[2], "doc", error); |
|
1213 |
QTextStream myStdOut(stdout); |
|
1214 |
myStdOut << canonical << endl; |
|
1215 |
exit(0); |
|
1216 |
} |
|
1217 |
||
1218 |
tst_QXmlStream tc; |
|
1219 |
return QTest::qExec(&tc, argc, argv); |
|
1220 |
} |
|
1221 |
||
1222 |
void tst_QXmlStream::hasAttributeSignature() const |
|
1223 |
{ |
|
1224 |
/* These functions should be const so invoke all |
|
1225 |
* of them on a const object. */ |
|
1226 |
const QXmlStreamAttributes atts; |
|
1227 |
atts.hasAttribute(QLatin1String("localName")); |
|
1228 |
atts.hasAttribute(QString::fromLatin1("localName")); |
|
1229 |
atts.hasAttribute(QString::fromLatin1("http://example.com/"), QLatin1String("localName")); |
|
1230 |
||
1231 |
/* The input arguments should be const references, not mutable references |
|
1232 |
* so pass const references. */ |
|
1233 |
const QLatin1String latin1StringLocalName(QLatin1String("localName")); |
|
1234 |
const QString qStringLocalname(QLatin1String("localName")); |
|
1235 |
const QString namespaceURI(QLatin1String("http://example.com/")); |
|
1236 |
||
1237 |
/* QLatin1String overload. */ |
|
1238 |
atts.hasAttribute(latin1StringLocalName); |
|
1239 |
||
1240 |
/* QString overload. */ |
|
1241 |
atts.hasAttribute(latin1StringLocalName); |
|
1242 |
||
1243 |
/* namespace/local name overload. */ |
|
1244 |
atts.hasAttribute(namespaceURI, qStringLocalname); |
|
1245 |
} |
|
1246 |
||
1247 |
void tst_QXmlStream::hasAttribute() const |
|
1248 |
{ |
|
1249 |
QXmlStreamReader reader(QLatin1String("<e xmlns:p='http://example.com/2' xmlns='http://example.com/' " |
|
1250 |
"attr1='value' attr2='value2' p:attr3='value3' emptyAttr=''><noAttributes/></e>")); |
|
1251 |
||
1252 |
QCOMPARE(reader.readNext(), QXmlStreamReader::StartDocument); |
|
1253 |
QCOMPARE(reader.readNext(), QXmlStreamReader::StartElement); |
|
1254 |
const QXmlStreamAttributes &atts = reader.attributes(); |
|
1255 |
||
1256 |
/* QLatin1String overload. */ |
|
1257 |
QVERIFY(atts.hasAttribute(QLatin1String("attr1"))); |
|
1258 |
QVERIFY(atts.hasAttribute(QLatin1String("attr2"))); |
|
1259 |
QVERIFY(atts.hasAttribute(QLatin1String("p:attr3"))); |
|
1260 |
QVERIFY(atts.hasAttribute(QLatin1String("emptyAttr"))); |
|
1261 |
QVERIFY(!atts.hasAttribute(QLatin1String("DOESNOTEXIST"))); |
|
1262 |
||
1263 |
/* Test with an empty & null namespaces. */ |
|
1264 |
QVERIFY(atts.hasAttribute(QString(), QLatin1String("attr2"))); /* A null string. */ |
|
1265 |
QVERIFY(atts.hasAttribute(QLatin1String(""), QLatin1String("attr2"))); /* An empty string. */ |
|
1266 |
||
1267 |
/* QString overload. */ |
|
1268 |
QVERIFY(atts.hasAttribute(QString::fromLatin1("attr1"))); |
|
1269 |
QVERIFY(atts.hasAttribute(QString::fromLatin1("attr2"))); |
|
1270 |
QVERIFY(atts.hasAttribute(QString::fromLatin1("p:attr3"))); |
|
1271 |
QVERIFY(atts.hasAttribute(QString::fromLatin1("emptyAttr"))); |
|
1272 |
QVERIFY(!atts.hasAttribute(QString::fromLatin1("DOESNOTEXIST"))); |
|
1273 |
||
1274 |
/* namespace/local name overload. */ |
|
1275 |
QVERIFY(atts.hasAttribute(QString(), QString::fromLatin1("attr1"))); |
|
1276 |
/* Attributes do not pick up the default namespace. */ |
|
1277 |
QVERIFY(!atts.hasAttribute(QLatin1String("http://example.com/"), QString::fromLatin1("attr1"))); |
|
1278 |
QVERIFY(atts.hasAttribute(QLatin1String("http://example.com/2"), QString::fromLatin1("attr3"))); |
|
1279 |
QVERIFY(atts.hasAttribute(QString(), QString::fromLatin1("emptyAttr"))); |
|
1280 |
QVERIFY(!atts.hasAttribute(QLatin1String("http://example.com/2"), QString::fromLatin1("DOESNOTEXIST"))); |
|
1281 |
QVERIFY(!atts.hasAttribute(QLatin1String("WRONG_NAMESPACE"), QString::fromLatin1("attr3"))); |
|
1282 |
||
1283 |
/* Invoke on an QXmlStreamAttributes that has no attributes at all. */ |
|
1284 |
QCOMPARE(reader.readNext(), QXmlStreamReader::StartElement); |
|
1285 |
||
1286 |
const QXmlStreamAttributes &atts2 = reader.attributes(); |
|
1287 |
QVERIFY(atts2.isEmpty()); |
|
1288 |
||
1289 |
/* QLatin1String overload. */ |
|
1290 |
QVERIFY(!atts.hasAttribute(QLatin1String("arbitraryName"))); |
|
1291 |
||
1292 |
/* QString overload. */ |
|
1293 |
QVERIFY(!atts.hasAttribute(QString::fromLatin1("arbitraryName"))); |
|
1294 |
||
1295 |
/* namespace/local name overload. */ |
|
1296 |
QVERIFY(!atts.hasAttribute(QLatin1String("http://example.com/"), QString::fromLatin1("arbitraryName"))); |
|
1297 |
||
1298 |
while(!reader.atEnd()) |
|
1299 |
reader.readNext(); |
|
1300 |
||
1301 |
QVERIFY(!reader.hasError()); |
|
1302 |
} |
|
1303 |
||
1304 |
||
1305 |
void tst_QXmlStream::writeWithCodec() const |
|
1306 |
{ |
|
1307 |
QByteArray outarray; |
|
1308 |
QXmlStreamWriter writer(&outarray); |
|
1309 |
writer.setAutoFormatting(true); |
|
1310 |
||
1311 |
QTextCodec *codec = QTextCodec::codecForName("ISO 8859-15"); |
|
1312 |
QVERIFY(codec); |
|
1313 |
writer.setCodec(codec); |
|
1314 |
||
1315 |
const char *latin2 = "hé hé"; |
|
1316 |
const QString string = codec->toUnicode(latin2); |
|
1317 |
||
1318 |
||
1319 |
writer.writeStartDocument("1.0"); |
|
1320 |
||
1321 |
writer.writeTextElement("foo", string); |
|
1322 |
writer.writeEndElement(); |
|
1323 |
writer.writeEndDocument(); |
|
1324 |
||
1325 |
QVERIFY(outarray.contains(latin2)); |
|
1326 |
QVERIFY(outarray.contains(codec->name())); |
|
1327 |
} |
|
1328 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1329 |
void tst_QXmlStream::writeWithUtf8Codec() const |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1330 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1331 |
QByteArray outarray; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1332 |
QXmlStreamWriter writer(&outarray); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1333 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1334 |
QTextCodec *codec = QTextCodec::codecForMib(106); // utf-8 |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1335 |
QVERIFY(codec); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1336 |
writer.setCodec(codec); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1337 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1338 |
writer.writeStartDocument("1.0"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1339 |
static const char begin[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1340 |
QVERIFY(outarray.startsWith(begin)); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1341 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1342 |
|
0 | 1343 |
void tst_QXmlStream::writeWithStandalone() const |
1344 |
{ |
|
1345 |
{ |
|
1346 |
QByteArray outarray; |
|
1347 |
QXmlStreamWriter writer(&outarray); |
|
1348 |
writer.setAutoFormatting(true); |
|
1349 |
writer.writeStartDocument("1.0", true); |
|
1350 |
writer.writeEndDocument(); |
|
1351 |
const char *ref = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
|
1352 |
QCOMPARE(outarray.constData(), ref); |
|
1353 |
} |
|
1354 |
{ |
|
1355 |
QByteArray outarray; |
|
1356 |
QXmlStreamWriter writer(&outarray); |
|
1357 |
writer.setAutoFormatting(true); |
|
1358 |
writer.writeStartDocument("1.0", false); |
|
1359 |
writer.writeEndDocument(); |
|
1360 |
const char *ref = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"; |
|
1361 |
QCOMPARE(outarray.constData(), ref); |
|
1362 |
} |
|
1363 |
} |
|
1364 |
||
1365 |
void tst_QXmlStream::entitiesAndWhitespace_1() const |
|
1366 |
{ |
|
1367 |
QXmlStreamReader reader(QLatin1String("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"><test>&extEnt;</test>")); |
|
1368 |
||
1369 |
int entityCount = 0; |
|
1370 |
int characterCount = 0; |
|
1371 |
while(!reader.atEnd()) |
|
1372 |
{ |
|
1373 |
QXmlStreamReader::TokenType token = reader.readNext(); |
|
1374 |
switch(token) |
|
1375 |
{ |
|
1376 |
case QXmlStreamReader::Characters: |
|
1377 |
characterCount++; |
|
1378 |
break; |
|
1379 |
case QXmlStreamReader::EntityReference: |
|
1380 |
entityCount++; |
|
1381 |
break; |
|
1382 |
default: |
|
1383 |
; |
|
1384 |
} |
|
1385 |
} |
|
1386 |
||
1387 |
QCOMPARE(entityCount, 1); |
|
1388 |
QCOMPARE(characterCount, 0); |
|
1389 |
QVERIFY(!reader.hasError()); |
|
1390 |
} |
|
1391 |
||
1392 |
void tst_QXmlStream::entitiesAndWhitespace_2() const |
|
1393 |
{ |
|
1394 |
QXmlStreamReader reader(QLatin1String("<test>&extEnt;</test>")); |
|
1395 |
||
1396 |
int entityCount = 0; |
|
1397 |
int characterCount = 0; |
|
1398 |
while(!reader.atEnd()) |
|
1399 |
{ |
|
1400 |
QXmlStreamReader::TokenType token = reader.readNext(); |
|
1401 |
switch(token) |
|
1402 |
{ |
|
1403 |
case QXmlStreamReader::Characters: |
|
1404 |
characterCount++; |
|
1405 |
break; |
|
1406 |
case QXmlStreamReader::EntityReference: |
|
1407 |
entityCount++; |
|
1408 |
break; |
|
1409 |
default: |
|
1410 |
; |
|
1411 |
} |
|
1412 |
} |
|
1413 |
||
1414 |
QCOMPARE(entityCount, 0); |
|
1415 |
QCOMPARE(characterCount, 0); |
|
1416 |
QVERIFY(reader.hasError()); |
|
1417 |
} |
|
1418 |
||
1419 |
void tst_QXmlStream::garbageInXMLPrologDefaultCodec() const |
|
1420 |
{ |
|
1421 |
QBuffer out; |
|
1422 |
QVERIFY(out.open(QIODevice::ReadWrite)); |
|
1423 |
||
1424 |
QXmlStreamWriter writer (&out); |
|
1425 |
writer.writeStartDocument(); |
|
1426 |
writer.writeEmptyElement("Foo"); |
|
1427 |
writer.writeEndDocument(); |
|
1428 |
||
1429 |
QCOMPARE(out.data(), QByteArray("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Foo/>\n")); |
|
1430 |
} |
|
1431 |
||
1432 |
void tst_QXmlStream::garbageInXMLPrologUTF8Explicitly() const |
|
1433 |
{ |
|
1434 |
QBuffer out; |
|
1435 |
QVERIFY(out.open(QIODevice::ReadWrite)); |
|
1436 |
||
1437 |
QXmlStreamWriter writer (&out); |
|
1438 |
writer.setCodec("UTF-8"); |
|
1439 |
writer.writeStartDocument(); |
|
1440 |
writer.writeEmptyElement("Foo"); |
|
1441 |
writer.writeEndDocument(); |
|
1442 |
||
1443 |
QCOMPARE(out.data(), QByteArray("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Foo/>\n")); |
|
1444 |
} |
|
1445 |
||
1446 |
void tst_QXmlStream::clear() const // task 228768 |
|
1447 |
{ |
|
1448 |
QString xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><body></body>"; |
|
1449 |
QXmlStreamReader reader; |
|
1450 |
||
1451 |
reader.addData(xml); |
|
1452 |
while (!reader.atEnd()) { |
|
1453 |
reader.readNext(); |
|
1454 |
} |
|
1455 |
QCOMPARE(reader.tokenType(), QXmlStreamReader::EndDocument); |
|
1456 |
||
1457 |
reader.clear(); |
|
1458 |
reader.addData(xml); |
|
1459 |
while (!reader.atEnd()) { |
|
1460 |
reader.readNext(); |
|
1461 |
} |
|
1462 |
QCOMPARE(reader.tokenType(), QXmlStreamReader::EndDocument); |
|
1463 |
||
1464 |
||
1465 |
// now we stop in the middle to check whether clear really works |
|
1466 |
reader.clear(); |
|
1467 |
reader.addData(xml); |
|
1468 |
reader.readNext(); |
|
1469 |
reader.readNext(); |
|
1470 |
QCOMPARE(reader.tokenType(), QXmlStreamReader::StartElement); |
|
1471 |
||
1472 |
// and here the final read |
|
1473 |
reader.clear(); |
|
1474 |
reader.addData(xml); |
|
1475 |
while (!reader.atEnd()) { |
|
1476 |
reader.readNext(); |
|
1477 |
} |
|
1478 |
QCOMPARE(reader.tokenType(), QXmlStreamReader::EndDocument); |
|
1479 |
} |
|
1480 |
||
1481 |
void tst_QXmlStream::checkCommentIndentation_data() const |
|
1482 |
{ |
|
1483 |
||
1484 |
QTest::addColumn<QString>("input"); |
|
1485 |
QTest::addColumn<QString>("expectedOutput"); |
|
1486 |
||
1487 |
QString simpleInput = "<a><!-- bla --></a>"; |
|
1488 |
QString simpleOutput = "<?xml version=\"1.0\"?>\n" |
|
1489 |
"<a>\n" |
|
1490 |
" <!-- bla -->\n" |
|
1491 |
"</a>\n"; |
|
1492 |
QTest::newRow("simple-comment") << simpleInput << simpleOutput; |
|
1493 |
||
1494 |
QString advancedInput = "<a><!-- bla --><!-- bla --><b><!-- bla --><c><!-- bla --></c><!-- bla --></b></a>"; |
|
1495 |
QString advancedOutput = "<?xml version=\"1.0\"?>\n" |
|
1496 |
"<a>\n" |
|
1497 |
" <!-- bla -->\n" |
|
1498 |
" <!-- bla -->\n" |
|
1499 |
" <b>\n" |
|
1500 |
" <!-- bla -->\n" |
|
1501 |
" <c>\n" |
|
1502 |
" <!-- bla -->\n" |
|
1503 |
" </c>\n" |
|
1504 |
" <!-- bla -->\n" |
|
1505 |
" </b>\n" |
|
1506 |
"</a>\n"; |
|
1507 |
QTest::newRow("advanced-comment") << advancedInput << advancedOutput; |
|
1508 |
} |
|
1509 |
||
1510 |
void tst_QXmlStream::checkCommentIndentation() const // task 256468 |
|
1511 |
{ |
|
1512 |
QFETCH(QString, input); |
|
1513 |
QFETCH(QString, expectedOutput); |
|
1514 |
QString output; |
|
1515 |
QXmlStreamReader reader(input); |
|
1516 |
QXmlStreamWriter writer(&output); |
|
1517 |
writer.setAutoFormatting(true); |
|
1518 |
writer.setAutoFormattingIndent(3); |
|
1519 |
||
1520 |
while (!reader.atEnd()) { |
|
1521 |
reader.readNext(); |
|
1522 |
if (reader.error()) { |
|
1523 |
QFAIL("error reading XML input"); |
|
1524 |
} else { |
|
1525 |
writer.writeCurrentToken(reader); |
|
1526 |
} |
|
1527 |
} |
|
1528 |
QCOMPARE(output, expectedOutput); |
|
1529 |
} |
|
1530 |
||
1531 |
#include "tst_qxmlstream.moc" |
|
1532 |
// vim: et:ts=4:sw=4:sts=4 |