Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: util: Re-write XML for comparison instead of str replacement #663

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tests/TestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#ifndef CLIENTTESTING_H
#define CLIENTTESTING_H

#include "QXmppClient.h"

Check failure on line 8 in tests/TestClient.h

View workflow job for this annotation

GitHub Actions / cpp-linter

tests/TestClient.h:8:10 [clang-diagnostic-error]

'QXmppClient.h' file not found
#include "QXmppClientExtension.h" // needed for qDeleteAll(d->extensions)
#include "QXmppClient_p.h"
#include "QXmppOutgoingClient.h"

#include "util.h"

class TestClient : public QXmppClient

Check warning on line 15 in tests/TestClient.h

View workflow job for this annotation

GitHub Actions / cpp-linter

tests/TestClient.h:15:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'TestClient' is non-const and globally accessible, consider making it const

Check warning on line 15 in tests/TestClient.h

View workflow job for this annotation

GitHub Actions / cpp-linter

tests/TestClient.h:15:27 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'QXmppClient' is non-const and globally accessible, consider making it const
{
Q_OBJECT
public:
Expand Down Expand Up @@ -43,11 +43,14 @@
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 @@ -7,7 +7,7 @@
#ifndef TESTS_UTIL_H
#define TESTS_UTIL_H

#include "QXmppError.h"

Check failure on line 10 in tests/util.h

View workflow job for this annotation

GitHub Actions / cpp-linter

tests/util.h:10:10 [clang-diagnostic-error]

'QXmppError.h' file not found
#include "QXmppPasswordChecker.h"
#include "QXmppTask.h"

Expand Down Expand Up @@ -51,6 +51,29 @@
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 Expand Up @@ -190,7 +213,7 @@
}
}

class TestPasswordChecker : public QXmppPasswordChecker

Check warning on line 216 in tests/util.h

View workflow job for this annotation

GitHub Actions / cpp-linter

tests/util.h:216:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'TestPasswordChecker' is non-const and globally accessible, consider making it const

Check warning on line 216 in tests/util.h

View workflow job for this annotation

GitHub Actions / cpp-linter

tests/util.h:216:36 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'QXmppPasswordChecker' is non-const and globally accessible, consider making it const
{
public:
void addCredentials(const QString &user, const QString &password)
Expand Down
Loading