refactor: clean up toStdString usages in tests (#5346)

* tests: Add QString/QStringView << operators for std::ostream

This makes it easier to print QString/QStringView's in ASSERT_EQ outputs

* tests: clean up toStdString usages

* fix: use QByteArray.toStdString instead
This commit is contained in:
pajlada 2024-04-21 22:52:44 +02:00 committed by GitHub
parent dfa929e207
commit 4a1ce2a3b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 47 additions and 22 deletions

View file

@ -1,6 +1,7 @@
#include "providers/emoji/Emojis.hpp"
#include "common/Literals.hpp"
#include "TestHelpers.hpp"
#include <gtest/gtest.h>
#include <QDebug>
@ -53,7 +54,7 @@ TEST(Emojis, ShortcodeParsing)
}
EXPECT_EQ(output, test.expectedOutput)
<< "Input " << test.input.toStdString() << " failed";
<< "Input " << test.input << " failed";
}
}
@ -165,8 +166,7 @@ TEST(Emojis, Parse)
// can't use EXPECT_EQ because EmotePtr can't be printed
if (output != test.expectedOutput)
{
EXPECT_TRUE(false)
<< "Input " << test.input.toStdString() << " failed";
EXPECT_TRUE(false) << "Input " << test.input << " failed";
}
}
}

View file

@ -1,5 +1,7 @@
#include "common/LinkParser.hpp"
#include "TestHelpers.hpp"
#include <gtest/gtest.h>
#include <QString>
#include <QStringList>
@ -15,13 +17,13 @@ struct Case {
{
auto input = this->protocol + this->host + this->rest;
LinkParser p(input);
ASSERT_TRUE(p.result().has_value()) << input.toStdString();
ASSERT_TRUE(p.result().has_value()) << input;
const auto &r = *p.result();
ASSERT_EQ(r.source, input);
ASSERT_EQ(r.protocol, this->protocol) << this->protocol.toStdString();
ASSERT_EQ(r.host, this->host) << this->host.toStdString();
ASSERT_EQ(r.rest, this->rest) << this->rest.toStdString();
ASSERT_EQ(r.protocol, this->protocol) << this->protocol;
ASSERT_EQ(r.host, this->host) << this->host;
ASSERT_EQ(r.rest, this->rest) << this->rest;
}
};
@ -126,7 +128,7 @@ TEST(LinkParser, doesntParseInvalidIpv4Links)
for (const auto &input : inputs)
{
LinkParser p(input);
ASSERT_FALSE(p.result().has_value()) << input.toStdString();
ASSERT_FALSE(p.result().has_value()) << input;
}
}
@ -170,6 +172,6 @@ TEST(LinkParser, doesntParseInvalidLinks)
for (const auto &input : inputs)
{
LinkParser p(input);
ASSERT_FALSE(p.result().has_value()) << input.toStdString();
ASSERT_FALSE(p.result().has_value()) << input;
}
}

View file

@ -1,6 +1,10 @@
#pragma once
#include <QString>
#include <QStringView>
#include <mutex>
#include <ostream>
template <typename T>
class ReceivedMessage
@ -42,3 +46,21 @@ public:
return &this->t;
}
};
inline std::ostream &operator<<(std::ostream &os, const QStringView &str)
{
os << qUtf8Printable(str.toString());
return os;
}
inline std::ostream &operator<<(std::ostream &os, const QByteArray &bytes)
{
os << bytes.toStdString();
return os;
}
inline std::ostream &operator<<(std::ostream &os, const QString &str)
{
os << qUtf8Printable(str);
return os;
}

View file

@ -15,6 +15,7 @@
#include "providers/seventv/SeventvBadges.hpp"
#include "providers/twitch/TwitchBadge.hpp"
#include "singletons/Emotes.hpp"
#include "TestHelpers.hpp"
#include <gtest/gtest.h>
#include <IrcConnection>
@ -147,7 +148,7 @@ TEST(TwitchMessageBuilder, CommaSeparatedListTagParsing)
auto output = TwitchMessageBuilder::slashKeyValue(test.input);
EXPECT_EQ(output, test.expectedOutput)
<< "Input " << test.input.toStdString() << " failed";
<< "Input " << test.input << " failed";
}
}
@ -230,12 +231,12 @@ TEST(TwitchMessageBuilder, BadgeInfoParsing)
auto outputBadgeInfo =
TwitchMessageBuilder::parseBadgeInfoTag(privmsg->tags());
EXPECT_EQ(outputBadgeInfo, test.expectedBadgeInfo)
<< "Input for badgeInfo " << test.input.toStdString() << " failed";
<< "Input for badgeInfo " << test.input << " failed";
auto outputBadges =
SharedMessageBuilder::parseBadgeTag(privmsg->tags());
EXPECT_EQ(outputBadges, test.expectedBadges)
<< "Input for badges " << test.input.toStdString() << " failed";
<< "Input for badges " << test.input << " failed";
delete privmsg;
}
@ -413,8 +414,7 @@ TEST_F(TestTwitchMessageBuilder, ParseTwitchEmotes)
privmsg->tags(), originalMessage, 0);
EXPECT_EQ(actualTwitchEmotes, test.expectedTwitchEmotes)
<< "Input for twitch emotes " << test.input.toStdString()
<< " failed";
<< "Input for twitch emotes " << test.input << " failed";
delete privmsg;
}
@ -617,11 +617,11 @@ TEST_F(TestTwitchMessageBuilder, IgnoresReplace)
emotes);
EXPECT_EQ(message, test.expectedMessage)
<< "Message not equal for input '" << test.input.toStdString()
<< "' - expected: '" << test.expectedMessage.toStdString()
<< "' got: '" << message.toStdString() << "'";
<< "Message not equal for input '" << test.input
<< "' - expected: '" << test.expectedMessage << "' got: '"
<< message << "'";
EXPECT_EQ(emotes, test.expectedTwitchEmotes)
<< "Twitch emotes not equal for input '" << test.input.toStdString()
<< "' and output '" << message.toStdString() << "'";
<< "Twitch emotes not equal for input '" << test.input
<< "' and output '" << message << "'";
}
}

View file

@ -1,5 +1,7 @@
#include "util/XDGHelper.hpp"
#include "TestHelpers.hpp"
#include <gtest/gtest.h>
#include <QDebug>
@ -57,9 +59,8 @@ TEST(XDGHelper, ParseDesktopExecProgram)
auto output = parseDesktopExecProgram(test.input);
EXPECT_EQ(output, test.expectedOutput)
<< "Input '" << test.input.toStdString() << "' failed. Expected '"
<< test.expectedOutput.toStdString() << "' but got '"
<< output.toStdString() << "'";
<< "Input '" << test.input << "' failed. Expected '"
<< test.expectedOutput << "' but got '" << output << "'";
}
}