Skip to content

Commit

Permalink
tests: util: Re-write XML for comparison instead of str replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX committed Nov 8, 2024
1 parent 03df550 commit d782d6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tests/TestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ class TestClient : public QXmppClient
QCoreApplication::processEvents();
resetIdCount();
}

void expect(QString &&packet)
{
QVERIFY2(!m_sentPackets.empty(), "No packet was sent!");
QCOMPARE(m_sentPackets.takeFirst().replace(u'\'', u'"'), packet.replace(u'\'', u'"'));

auto expectedXml = rewriteXml(packet);
auto actualXml = rewriteXml(m_sentPackets.takeFirst());
QCOMPARE(actualXml, expectedXml);

resetIdCount();
}
QString takePacket()
Expand Down
23 changes: 23 additions & 0 deletions tests/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ inline QDomElement xmlToDom(const String &xml)
return doc.documentElement();
}

template<typename String>
QString rewriteXml(const String &inputXml)
{
QString outputXml;
QXmlStreamReader reader(inputXml);
QXmlStreamWriter writer(&outputXml);
while (reader.readNext() != QXmlStreamReader::EndDocument) {
if (reader.hasError()) {
qDebug() << "Parsing error:";
qDebug().noquote() << inputXml;
qDebug().noquote() << reader.error() << reader.errorString();
throw std::exception();
}

// do not generate '<?xml version="1.0"?>'
if (reader.tokenType() == QXmlStreamReader::StartDocument) {
continue;
}
writer.writeCurrentToken(reader);
}
return outputXml;
}

template<typename T>
static QByteArray packetToXml(const T &packet)
{
Expand Down

0 comments on commit d782d6f

Please sign in to comment.