/src/testlib/qtestxmlstreamer.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 227 lines · 153 code · 28 blank · 46 comment · 23 complexity · 4977933fbf1bc9a620d34ab8791d0800 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtTest module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qtestxmlstreamer.h"
  42. #include "qtestelement.h"
  43. #include "qtestelementattribute.h"
  44. #include "qtestlogger_p.h"
  45. #include "QtTest/private/qtestlog_p.h"
  46. #include "QtTest/private/qtestresult_p.h"
  47. #include "QtTest/private/qxmltestlogger_p.h"
  48. #include <string.h>
  49. #include <stdio.h>
  50. QT_BEGIN_NAMESPACE
  51. QTestXmlStreamer::QTestXmlStreamer()
  52. :QTestBasicStreamer()
  53. {
  54. }
  55. QTestXmlStreamer::~QTestXmlStreamer()
  56. {}
  57. void QTestXmlStreamer::formatStart(const QTestElement *element, QTestCharBuffer *formatted) const
  58. {
  59. if(!element || !formatted)
  60. return;
  61. switch(element->elementType()){
  62. case QTest::LET_TestCase: {
  63. QTestCharBuffer quotedTf;
  64. QXmlTestLogger::xmlQuote(&quotedTf, element->attributeValue(QTest::AI_Name));
  65. QTest::qt_asprintf(formatted, "<TestFunction name=\"%s\">\n", quotedTf.constData());
  66. break;
  67. }
  68. case QTest::LET_Failure: {
  69. QTestCharBuffer cdataDesc;
  70. QXmlTestLogger::xmlCdata(&cdataDesc, element->attributeValue(QTest::AI_Description));
  71. QTestCharBuffer location;
  72. QTestCharBuffer quotedFile;
  73. QXmlTestLogger::xmlQuote(&quotedFile, element->attributeValue(QTest::AI_File));
  74. QTest::qt_asprintf(&location, "%s=\"%s\" %s=\"%s\"",
  75. element->attributeName(QTest::AI_File),
  76. quotedFile.constData(),
  77. element->attributeName(QTest::AI_Line),
  78. element->attributeValue(QTest::AI_Line));
  79. if (element->attribute(QTest::AI_Tag)) {
  80. QTestCharBuffer cdataTag;
  81. QXmlTestLogger::xmlCdata(&cdataTag, element->attributeValue(QTest::AI_Tag));
  82. QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n"
  83. " <DataTag><![CDATA[%s]]></DataTag>\n"
  84. " <Description><![CDATA[%s]]></Description>\n"
  85. "</Incident>\n", element->attributeValue(QTest::AI_Result),
  86. location.constData(), cdataTag.constData(), cdataDesc.constData());
  87. }
  88. else {
  89. QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n"
  90. " <Description><![CDATA[%s]]></Description>\n"
  91. "</Incident>\n", element->attributeValue(QTest::AI_Result),
  92. location.constData(), cdataDesc.constData());
  93. }
  94. break;
  95. }
  96. case QTest::LET_Error: {
  97. // assuming type and attribute names don't need quoting
  98. QTestCharBuffer quotedFile;
  99. QTestCharBuffer cdataDesc;
  100. QXmlTestLogger::xmlQuote(&quotedFile, element->attributeValue(QTest::AI_File));
  101. QXmlTestLogger::xmlCdata(&cdataDesc, element->attributeValue(QTest::AI_Description));
  102. QTestCharBuffer tagbuf;
  103. if (element->attribute(QTest::AI_Tag)) {
  104. QTestCharBuffer cdataTag;
  105. QXmlTestLogger::xmlCdata(&cdataTag, element->attributeValue(QTest::AI_Tag));
  106. QTest::qt_asprintf(&tagbuf, " <DataTag><![CDATA[%s]]></DataTag>\n", cdataTag.constData());
  107. }
  108. QTest::qt_asprintf(formatted, "<Message type=\"%s\" %s=\"%s\" %s=\"%s\">\n%s <Description><![CDATA[%s]]></Description>\n</Message>\n",
  109. element->attributeValue(QTest::AI_Type),
  110. element->attributeName(QTest::AI_File),
  111. quotedFile.constData(),
  112. element->attributeName(QTest::AI_Line),
  113. element->attributeValue(QTest::AI_Line),
  114. tagbuf.constData(),
  115. cdataDesc.constData());
  116. break;
  117. }
  118. case QTest::LET_Benchmark: {
  119. // assuming value and iterations don't need quoting
  120. QTestCharBuffer quotedMetric;
  121. QTestCharBuffer quotedTag;
  122. QXmlTestLogger::xmlQuote(&quotedMetric, element->attributeValue(QTest::AI_Metric));
  123. QXmlTestLogger::xmlQuote(&quotedTag, element->attributeValue(QTest::AI_Tag));
  124. QTest::qt_asprintf(formatted, "<BenchmarkResult %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\" />\n",
  125. element->attributeName(QTest::AI_Metric),
  126. quotedMetric.constData(),
  127. element->attributeName(QTest::AI_Tag),
  128. quotedTag.constData(),
  129. element->attributeName(QTest::AI_Value),
  130. element->attributeValue(QTest::AI_Value),
  131. element->attributeName(QTest::AI_Iterations),
  132. element->attributeValue(QTest::AI_Iterations) );
  133. break;
  134. }
  135. default:
  136. formatted->data()[0] = '\0';
  137. }
  138. }
  139. void QTestXmlStreamer::formatEnd(const QTestElement *element, QTestCharBuffer *formatted) const
  140. {
  141. if(!element || !formatted)
  142. return;
  143. if (element->elementType() == QTest::LET_TestCase) {
  144. bool failed = false;
  145. for (QTestElement* child = element->childElements(); child; child = child->nextElement()) {
  146. if ( child->elementType() == QTest::LET_Failure
  147. && child->attribute(QTest::AI_Result)
  148. && ( !strcmp(child->attributeValue(QTest::AI_Result), "fail")
  149. || !strcmp(child->attributeValue(QTest::AI_Result), "xpass"))
  150. )
  151. {
  152. failed = true;
  153. break;
  154. }
  155. }
  156. // For passing functions, no Incident has been output yet.
  157. // For failing functions, we already output one.
  158. // Please note: we are outputting "pass" even if there was an xfail etc.
  159. // This is by design (arguably bad design, but dangerous to change now!)
  160. if (element->attribute(QTest::AI_Result) && !failed) {
  161. QTest::qt_asprintf(formatted, "<Incident type=\"pass\" file=\"\" line=\"0\" />\n</TestFunction>\n");
  162. }
  163. else {
  164. QTest::qt_asprintf(formatted, "</TestFunction>\n");
  165. }
  166. } else {
  167. formatted->data()[0] = '\0';
  168. }
  169. }
  170. void QTestXmlStreamer::formatBeforeAttributes(const QTestElement *element, QTestCharBuffer *formatted) const
  171. {
  172. Q_UNUSED(element);
  173. if (!formatted)
  174. return;
  175. formatted->data()[0] = '\0';
  176. }
  177. void QTestXmlStreamer::output(QTestElement *element) const
  178. {
  179. QTestCharBuffer buf;
  180. QTestCharBuffer quotedTc;
  181. QXmlTestLogger::xmlQuote(&quotedTc, QTestResult::currentTestObjectName());
  182. QTest::qt_asprintf(&buf, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<TestCase name=\"%s\">\n",
  183. quotedTc.constData());
  184. outputString(buf.constData());
  185. if (logger()->hasRandomSeed()) {
  186. QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n <RandomSeed>%d</RandomSeed>\n",
  187. qVersion(), QTEST_VERSION_STR, logger()->randomSeed() );
  188. } else {
  189. QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n",
  190. qVersion(), QTEST_VERSION_STR );
  191. }
  192. outputString(buf.constData());
  193. QTest::qt_asprintf(&buf, "</Environment>\n");
  194. outputString(buf.constData());
  195. QTestBasicStreamer::output(element);
  196. QTest::qt_asprintf(&buf, "</TestCase>\n");
  197. outputString(buf.constData());
  198. }
  199. QT_END_NAMESPACE