4#include <QtTest/private/qtestjunitstreamer_p.h>
5#include <QtTest/private/qjunittestlogger_p.h>
6#include <QtTest/private/qtestelement_p.h>
7#include <QtTest/private/qtestelementattribute_p.h>
8#include <QtTest/qtestassert.h>
9#include <QtTest/private/qtestlog_p.h>
10#include <QtTest/private/qtestresult_p.h>
11#include <QtTest/private/qxmltestlogger_p.h>
15QTestJUnitStreamer::QTestJUnitStreamer(QJUnitTestLogger *logger)
18 QTEST_ASSERT(testLogger);
21QTestJUnitStreamer::~QTestJUnitStreamer() =
default;
23void QTestJUnitStreamer::indentForElement(
const QTestElement* element,
char* buf,
int size)
25 if (size == 0)
return;
31 char* endbuf = buf + size;
32 element = element->parentElement();
33 while (element && buf+2 < endbuf) {
37 element = element->parentElement();
41void QTestJUnitStreamer::formatStart(
const QTestElement *element, QTestCharBuffer *formatted)
const
43 if (!element || !formatted )
47 indentForElement(element, indent,
sizeof(indent));
49 if (element->elementType() == QTest::LET_Text) {
50 QTest::qt_asprintf(formatted,
"%s<![CDATA[", indent);
54 QTest::qt_asprintf(formatted,
"%s<%s", indent, element->elementName());
57void QTestJUnitStreamer::formatEnd(
const QTestElement *element, QTestCharBuffer *formatted)
const
59 if (!element || !formatted )
62 if (element->childElements().empty()) {
63 formatted->data()[0] =
'\0';
68 indentForElement(element, indent,
sizeof(indent));
70 QTest::qt_asprintf(formatted,
"%s</%s>\n", indent, element->elementName());
73bool QTestJUnitStreamer::formatAttributes(
const QTestElement* element,
74 const QTestElementAttribute *attribute,
75 QTestCharBuffer *formatted)
const
77 if (!attribute || !formatted )
80 QTest::AttributeIndex attrindex = attribute->index();
82 if (element && element->elementType() == QTest::LET_Text) {
83 QTEST_ASSERT(attrindex == QTest::AI_Value);
84 return QXmlTestLogger::xmlCdata(formatted, attribute->value());
87 QTestCharBuffer quotedValue;
88 if (QXmlTestLogger::xmlQuote("edValue, attribute->value())) {
89 return QTest::qt_asprintf(formatted,
" %s=\"%s\"",
90 attribute->name(), quotedValue.constData()) != 0;
95void QTestJUnitStreamer::formatAfterAttributes(
const QTestElement *element, QTestCharBuffer *formatted)
const
97 if (!element || !formatted )
100 if (element->elementType() == QTest::LET_Text) {
101 QTest::qt_asprintf(formatted,
"]]>\n");
105 if (element->childElements().empty())
106 QTest::qt_asprintf(formatted,
"/>\n");
108 QTest::qt_asprintf(formatted,
">\n");
111void QTestJUnitStreamer::output(QTestElement *element)
const
113 QTEST_ASSERT(element);
115 if (!element->parentElement())
116 outputString(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
120 formatStart(element, &buf);
121 outputString(buf.data());
123 outputElementAttributes(element, element->attributes());
125 formatAfterAttributes(element, &buf);
126 outputString(buf.data());
128 if (!element->childElements().empty())
129 outputElements(element->childElements());
131 formatEnd(element, &buf);
132 outputString(buf.data());
135void QTestJUnitStreamer::outputElements(
const std::vector<QTestElement*> &elements)
const
137 for (
auto *element : elements)
141void QTestJUnitStreamer::outputElementAttributes(
const QTestElement* element,
const std::vector<QTestElementAttribute*> &attributes)
const
145 for (
auto *attribute : attributes) {
146 if (formatAttributes(element, attribute, &buf))
147 outputString(buf.data());
151void QTestJUnitStreamer::outputString(
const char *msg)
const
153 testLogger->outputString(msg);