| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtTest module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <stdio.h> | 
|---|
| 43 | #include <string.h> | 
|---|
| 44 | #include <QtCore/qglobal.h> | 
|---|
| 45 |  | 
|---|
| 46 | #include "QtTest/private/qxmltestlogger_p.h" | 
|---|
| 47 | #include "QtTest/private/qtestresult_p.h" | 
|---|
| 48 | #include "QtTest/private/qbenchmark_p.h" | 
|---|
| 49 |  | 
|---|
| 50 | QT_BEGIN_NAMESPACE | 
|---|
| 51 |  | 
|---|
| 52 | namespace QTest { | 
|---|
| 53 |  | 
|---|
| 54 | static const char* xmlMessageType2String(QAbstractTestLogger::MessageTypes type) | 
|---|
| 55 | { | 
|---|
| 56 | switch (type) { | 
|---|
| 57 | case QAbstractTestLogger::Warn: | 
|---|
| 58 | return "warn"; | 
|---|
| 59 | case QAbstractTestLogger::QSystem: | 
|---|
| 60 | return "system"; | 
|---|
| 61 | case QAbstractTestLogger::QDebug: | 
|---|
| 62 | return "qdebug"; | 
|---|
| 63 | case QAbstractTestLogger::QWarning: | 
|---|
| 64 | return "qwarn"; | 
|---|
| 65 | case QAbstractTestLogger::QFatal: | 
|---|
| 66 | return "qfatal"; | 
|---|
| 67 | case QAbstractTestLogger::Skip: | 
|---|
| 68 | return "skip"; | 
|---|
| 69 | case QAbstractTestLogger::Info: | 
|---|
| 70 | return "info"; | 
|---|
| 71 | } | 
|---|
| 72 | return "??????"; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | static const char* xmlIncidentType2String(QAbstractTestLogger::IncidentTypes type) | 
|---|
| 76 | { | 
|---|
| 77 | switch (type) { | 
|---|
| 78 | case QAbstractTestLogger::Pass: | 
|---|
| 79 | return "pass"; | 
|---|
| 80 | case QAbstractTestLogger::XFail: | 
|---|
| 81 | return "xfail"; | 
|---|
| 82 | case QAbstractTestLogger::Fail: | 
|---|
| 83 | return "fail"; | 
|---|
| 84 | case QAbstractTestLogger::XPass: | 
|---|
| 85 | return "xpass"; | 
|---|
| 86 | } | 
|---|
| 87 | return "??????"; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 | QXmlTestLogger::QXmlTestLogger(XmlMode mode ): | 
|---|
| 94 | xmlmode(mode) | 
|---|
| 95 | { | 
|---|
| 96 |  | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | QXmlTestLogger::~QXmlTestLogger() | 
|---|
| 100 | { | 
|---|
| 101 |  | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | void QXmlTestLogger::startLogging() | 
|---|
| 106 | { | 
|---|
| 107 | QAbstractTestLogger::startLogging(); | 
|---|
| 108 | char buf[1024]; | 
|---|
| 109 |  | 
|---|
| 110 | if (xmlmode == QXmlTestLogger::Complete) { | 
|---|
| 111 | QTest::qt_snprintf(buf, sizeof(buf), | 
|---|
| 112 | "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" | 
|---|
| 113 | "<TestCase name=\"%s\">\n", QTestResult::currentTestObjectName()); | 
|---|
| 114 | outputString(buf); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | QTest::qt_snprintf(buf, sizeof(buf), | 
|---|
| 118 | "<Environment>\n" | 
|---|
| 119 | "    <QtVersion>%s</QtVersion>\n" | 
|---|
| 120 | "    <QTestVersion>"QTEST_VERSION_STR"</QTestVersion>\n" | 
|---|
| 121 | "</Environment>\n", qVersion()); | 
|---|
| 122 | outputString(buf); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | void QXmlTestLogger::stopLogging() | 
|---|
| 126 | { | 
|---|
| 127 | if (xmlmode == QXmlTestLogger::Complete) { | 
|---|
| 128 | outputString("</TestCase>\n"); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | QAbstractTestLogger::stopLogging(); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | void QXmlTestLogger::enterTestFunction(const char *function) | 
|---|
| 135 | { | 
|---|
| 136 | char buf[1024]; | 
|---|
| 137 | QTest::qt_snprintf(buf, sizeof(buf), "<TestFunction name=\"%s\">\n", function); | 
|---|
| 138 | outputString(buf); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void QXmlTestLogger::leaveTestFunction() | 
|---|
| 142 | { | 
|---|
| 143 | outputString("</TestFunction>\n"); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | namespace QTest | 
|---|
| 147 | { | 
|---|
| 148 |  | 
|---|
| 149 | inline static bool isEmpty(const char *str) | 
|---|
| 150 | { | 
|---|
| 151 | return !str || !str[0]; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | static const char *incidentFormatString(bool noDescription, bool noTag) | 
|---|
| 155 | { | 
|---|
| 156 | if (noDescription) { | 
|---|
| 157 | if (noTag) | 
|---|
| 158 | return "<Incident type=\"%s\" file=\"%s\" line=\"%d\" />\n"; | 
|---|
| 159 | else | 
|---|
| 160 | return "<Incident type=\"%s\" file=\"%s\" line=\"%d\">\n" | 
|---|
| 161 | "    <DataTag><![CDATA[%s%s%s%s]]></DataTag>\n" | 
|---|
| 162 | "</Incident>\n"; | 
|---|
| 163 | } else { | 
|---|
| 164 | if (noTag) | 
|---|
| 165 | return "<Incident type=\"%s\" file=\"%s\" line=\"%d\">\n" | 
|---|
| 166 | "    <Description><![CDATA[%s%s%s%s]]></Description>\n" | 
|---|
| 167 | "</Incident>\n"; | 
|---|
| 168 | else | 
|---|
| 169 | return "<Incident type=\"%s\" file=\"%s\" line=\"%d\">\n" | 
|---|
| 170 | "    <DataTag><![CDATA[%s%s%s]]></DataTag>\n" | 
|---|
| 171 | "    <Description><![CDATA[%s]]></Description>\n" | 
|---|
| 172 | "</Incident>\n"; | 
|---|
| 173 | } | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | static const char *benchmarkResultFormatString() | 
|---|
| 177 | { | 
|---|
| 178 | return "<BenchmarkResult metric=\"%s\" tag=\"%s\" value=\"%s\" iterations=\"%d\" />\n"; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | static const char *messageFormatString(bool noDescription, bool noTag) | 
|---|
| 182 | { | 
|---|
| 183 | if (noDescription) { | 
|---|
| 184 | if (noTag) | 
|---|
| 185 | return "<Message type=\"%s\" file=\"%s\" line=\"%d\" />\n"; | 
|---|
| 186 | else | 
|---|
| 187 | return "<Message type=\"%s\" file=\"%s\" line=\"%d\">\n" | 
|---|
| 188 | "    <DataTag><![CDATA[%s%s%s%s]]></DataTag>\n" | 
|---|
| 189 | "</Message>\n"; | 
|---|
| 190 | } else { | 
|---|
| 191 | if (noTag) | 
|---|
| 192 | return "<Message type=\"%s\" file=\"%s\" line=\"%d\">\n" | 
|---|
| 193 | "    <Description><![CDATA[%s%s%s%s]]></Description>\n" | 
|---|
| 194 | "</Message>\n"; | 
|---|
| 195 | else | 
|---|
| 196 | return "<Message type=\"%s\" file=\"%s\" line=\"%d\">\n" | 
|---|
| 197 | "    <DataTag><![CDATA[%s%s%s]]></DataTag>\n" | 
|---|
| 198 | "    <Description><![CDATA[%s]]></Description>\n" | 
|---|
| 199 | "</Message>\n"; | 
|---|
| 200 | } | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | } // namespace | 
|---|
| 204 |  | 
|---|
| 205 | void QXmlTestLogger::addIncident(IncidentTypes type, const char *description, | 
|---|
| 206 | const char *file, int line) | 
|---|
| 207 | { | 
|---|
| 208 | char buf[1536]; | 
|---|
| 209 | const char *tag = QTestResult::currentDataTag(); | 
|---|
| 210 | const char *gtag = QTestResult::currentGlobalDataTag(); | 
|---|
| 211 | const char *filler = (tag && gtag) ? ":" : ""; | 
|---|
| 212 | const bool notag = QTest::isEmpty(tag) && QTest::isEmpty(gtag); | 
|---|
| 213 |  | 
|---|
| 214 | QTest::qt_snprintf(buf, sizeof(buf), | 
|---|
| 215 | QTest::incidentFormatString(QTest::isEmpty(description), notag), | 
|---|
| 216 | QTest::xmlIncidentType2String(type), | 
|---|
| 217 | file ? file : "", line, | 
|---|
| 218 | gtag ? gtag : "", | 
|---|
| 219 | filler, | 
|---|
| 220 | tag ? tag : "", | 
|---|
| 221 | description ? description : ""); | 
|---|
| 222 |  | 
|---|
| 223 | outputString(buf); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | void QXmlTestLogger::addBenchmarkResult(const QBenchmarkResult &result) | 
|---|
| 227 | { | 
|---|
| 228 | char buf[1536]; | 
|---|
| 229 | QTest::qt_snprintf( | 
|---|
| 230 | buf, sizeof(buf), | 
|---|
| 231 | QTest::benchmarkResultFormatString(), | 
|---|
| 232 | QBenchmarkGlobalData::current->measurer->metricText().toAscii().data(), | 
|---|
| 233 | result.context.tag.toAscii().data(), | 
|---|
| 234 | QByteArray::number(result.value).constData(),  //no 64-bit qt_snprintf support | 
|---|
| 235 | result.iterations); | 
|---|
| 236 | outputString(buf); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | void QXmlTestLogger::addMessage(MessageTypes type, const char *message, | 
|---|
| 240 | const char *file, int line) | 
|---|
| 241 | { | 
|---|
| 242 | char buf[1536]; | 
|---|
| 243 | char msgbuf[1024]; | 
|---|
| 244 | const char *tag = QTestResult::currentDataTag(); | 
|---|
| 245 | const char *gtag = QTestResult::currentGlobalDataTag(); | 
|---|
| 246 | const char *filler = (tag && gtag) ? ":" : ""; | 
|---|
| 247 | const bool notag = QTest::isEmpty(tag) && QTest::isEmpty(gtag); | 
|---|
| 248 |  | 
|---|
| 249 | QTest::qt_snprintf(msgbuf, sizeof(msgbuf), "%s", | 
|---|
| 250 | message ? message : ""); | 
|---|
| 251 |  | 
|---|
| 252 | QTest::qt_snprintf(buf, sizeof(buf), | 
|---|
| 253 | QTest::messageFormatString(QTest::isEmpty(message), notag), | 
|---|
| 254 | QTest::xmlMessageType2String(type), | 
|---|
| 255 | file ? file : "", line, | 
|---|
| 256 | gtag ? gtag : "", | 
|---|
| 257 | filler, | 
|---|
| 258 | tag ? tag : "", | 
|---|
| 259 | msgbuf); | 
|---|
| 260 |  | 
|---|
| 261 | outputString(buf); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | QT_END_NAMESPACE | 
|---|