source: trunk/src/testlib/qtestxmlstreamer.cpp@ 1168

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtTest module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qtestxmlstreamer.h"
43#include "qtestelement.h"
44#include "qtestelementattribute.h"
45
46#include "QtTest/private/qtestlog_p.h"
47#include "QtTest/private/qtestresult_p.h"
48#include "QtTest/private/qxmltestlogger_p.h"
49
50#include <string.h>
51#include <stdio.h>
52
53QT_BEGIN_NAMESPACE
54
55QTestXmlStreamer::QTestXmlStreamer()
56 :QTestBasicStreamer()
57{
58}
59
60QTestXmlStreamer::~QTestXmlStreamer()
61{}
62
63void QTestXmlStreamer::formatStart(const QTestElement *element, QTestCharBuffer *formatted) const
64{
65 if(!element || !formatted)
66 return;
67
68 switch(element->elementType()){
69 case QTest::LET_TestCase: {
70 QTestCharBuffer quotedTf;
71 QXmlTestLogger::xmlQuote(&quotedTf, element->attributeValue(QTest::AI_Name));
72
73 QTest::qt_asprintf(formatted, "<TestFunction name=\"%s\">\n", quotedTf.constData());
74 break;
75 }
76 case QTest::LET_Failure: {
77 QTestCharBuffer cdataDesc;
78 QXmlTestLogger::xmlCdata(&cdataDesc, element->attributeValue(QTest::AI_Description));
79
80 QTestCharBuffer location;
81 QTestCharBuffer quotedFile;
82 QXmlTestLogger::xmlQuote(&quotedFile, element->attributeValue(QTest::AI_File));
83
84 QTest::qt_asprintf(&location, "%s=\"%s\" %s=\"%s\"",
85 element->attributeName(QTest::AI_File),
86 quotedFile.constData(),
87 element->attributeName(QTest::AI_Line),
88 element->attributeValue(QTest::AI_Line));
89
90 if (element->attribute(QTest::AI_Tag)) {
91 QTestCharBuffer cdataTag;
92 QXmlTestLogger::xmlCdata(&cdataTag, element->attributeValue(QTest::AI_Tag));
93 QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n"
94 " <DataTag><![CDATA[%s]]></DataTag>\n"
95 " <Description><![CDATA[%s]]></Description>\n"
96 "</Incident>\n", element->attributeValue(QTest::AI_Result),
97 location.constData(), cdataTag.constData(), cdataDesc.constData());
98 }
99 else {
100 QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n"
101 " <Description><![CDATA[%s]]></Description>\n"
102 "</Incident>\n", element->attributeValue(QTest::AI_Result),
103 location.constData(), cdataDesc.constData());
104 }
105 break;
106 }
107 case QTest::LET_Error: {
108 // assuming type and attribute names don't need quoting
109 QTestCharBuffer quotedFile;
110 QTestCharBuffer cdataDesc;
111 QXmlTestLogger::xmlQuote(&quotedFile, element->attributeValue(QTest::AI_File));
112 QXmlTestLogger::xmlCdata(&cdataDesc, element->attributeValue(QTest::AI_Description));
113
114 QTestCharBuffer tagbuf;
115 if (element->attribute(QTest::AI_Tag)) {
116 QTestCharBuffer cdataTag;
117 QXmlTestLogger::xmlCdata(&cdataTag, element->attributeValue(QTest::AI_Tag));
118 QTest::qt_asprintf(&tagbuf, " <DataTag><![CDATA[%s]]></DataTag>\n", cdataTag.constData());
119 }
120
121 QTest::qt_asprintf(formatted, "<Message type=\"%s\" %s=\"%s\" %s=\"%s\">\n%s <Description><![CDATA[%s]]></Description>\n</Message>\n",
122 element->attributeValue(QTest::AI_Type),
123 element->attributeName(QTest::AI_File),
124 quotedFile.constData(),
125 element->attributeName(QTest::AI_Line),
126 element->attributeValue(QTest::AI_Line),
127 tagbuf.constData(),
128 cdataDesc.constData());
129 break;
130 }
131 case QTest::LET_Benchmark: {
132 // assuming value and iterations don't need quoting
133 QTestCharBuffer quotedMetric;
134 QTestCharBuffer quotedTag;
135 QXmlTestLogger::xmlQuote(&quotedMetric, element->attributeValue(QTest::AI_Metric));
136 QXmlTestLogger::xmlQuote(&quotedTag, element->attributeValue(QTest::AI_Tag));
137
138 QTest::qt_asprintf(formatted, "<BenchmarkResult %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\" />\n",
139 element->attributeName(QTest::AI_Metric),
140 quotedMetric.constData(),
141 element->attributeName(QTest::AI_Tag),
142 quotedTag.constData(),
143 element->attributeName(QTest::AI_Value),
144 element->attributeValue(QTest::AI_Value),
145 element->attributeName(QTest::AI_Iterations),
146 element->attributeValue(QTest::AI_Iterations) );
147 break;
148 }
149 default:
150 formatted->data()[0] = '\0';
151 }
152}
153
154void QTestXmlStreamer::formatEnd(const QTestElement *element, QTestCharBuffer *formatted) const
155{
156 if(!element || !formatted)
157 return;
158
159 if (element->elementType() == QTest::LET_TestCase) {
160 bool failed = false;
161 for (QTestElement* child = element->childElements(); child; child = child->nextElement()) {
162 if ( child->elementType() == QTest::LET_Failure
163 && child->attribute(QTest::AI_Result)
164 && ( !strcmp(child->attributeValue(QTest::AI_Result), "fail")
165 || !strcmp(child->attributeValue(QTest::AI_Result), "xpass"))
166 )
167 {
168 failed = true;
169 break;
170 }
171 }
172
173 // For passing functions, no Incident has been output yet.
174 // For failing functions, we already output one.
175 // Please note: we are outputting "pass" even if there was an xfail etc.
176 // This is by design (arguably bad design, but dangerous to change now!)
177 if (element->attribute(QTest::AI_Result) && !failed) {
178 QTest::qt_asprintf(formatted, "<Incident type=\"pass\" file=\"\" line=\"0\" />\n</TestFunction>\n");
179 }
180 else {
181 QTest::qt_asprintf(formatted, "</TestFunction>\n");
182 }
183 } else {
184 formatted->data()[0] = '\0';
185 }
186}
187
188void QTestXmlStreamer::formatBeforeAttributes(const QTestElement *element, QTestCharBuffer *formatted) const
189{
190 Q_UNUSED(element);
191 if (!formatted)
192 return;
193
194 formatted->data()[0] = '\0';
195}
196
197void QTestXmlStreamer::output(QTestElement *element) const
198{
199 QTestCharBuffer buf;
200 QTestCharBuffer quotedTc;
201 QXmlTestLogger::xmlQuote(&quotedTc, QTestResult::currentTestObjectName());
202
203 QTest::qt_asprintf(&buf, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<TestCase name=\"%s\">\n",
204 quotedTc.constData());
205 outputString(buf.constData());
206
207 QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n",
208 qVersion(), QTEST_VERSION_STR );
209 outputString(buf.constData());
210
211 QTest::qt_asprintf(&buf, "</Environment>\n");
212 outputString(buf.constData());
213
214 QTestBasicStreamer::output(element);
215
216 QTest::qt_asprintf(&buf, "</TestCase>\n");
217 outputString(buf.constData());
218}
219
220QT_END_NAMESPACE
221
Note: See TracBrowser for help on using the repository browser.