Skip to content

Commit

Permalink
Test filters context map & message builder (#4886)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Oct 13, 2023
1 parent b85d666 commit 9f23c85
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ jobs:
docker pull ${{ env.TWITCH_PUBSUB_SERVER_IMAGE }}
docker run --network=host --detach ${{ env.TWITCH_PUBSUB_SERVER_IMAGE }}
docker run -p 9051:80 --detach kennethreitz/httpbin
./bin/chatterino-test || ./bin/chatterino-test || ./bin/chatterino-test
ctest --repeat until-pass:4
working-directory: build-test
shell: bash
16 changes: 16 additions & 0 deletions mocks/include/mocks/Channel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "common/Channel.hpp"

namespace chatterino::mock {

class MockChannel : public Channel
{
public:
MockChannel(const QString &name)
: Channel(name, Channel::Type::Twitch)
{
}
};

} // namespace chatterino::mock
6 changes: 6 additions & 0 deletions mocks/include/mocks/EmptyApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class EmptyApplication : public IApplication
return nullptr;
}

SeventvBadges *getSeventvBadges() override
{
assert(!"getSeventvBadges was called without being initialized");
return nullptr;
}

IUserDataController *getUserData() override
{
return nullptr;
Expand Down
46 changes: 46 additions & 0 deletions mocks/include/mocks/TwitchIrcServer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "mocks/Channel.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"

namespace chatterino::mock {

class MockTwitchIrcServer : public ITwitchIrcServer
{
public:
MockTwitchIrcServer()
: watchingChannelInner(
std::shared_ptr<Channel>(new MockChannel("testaccount_420")))
, watchingChannel(this->watchingChannelInner,
Channel::Type::TwitchWatching)
{
}

const BttvEmotes &getBttvEmotes() const override
{
return this->bttv;
}

const FfzEmotes &getFfzEmotes() const override
{
return this->ffz;
}

const SeventvEmotes &getSeventvEmotes() const override
{
return this->seventv;
}

const IndirectChannel &getWatchingChannel() const override
{
return this->watchingChannel;
}

BttvEmotes bttv;
FfzEmotes ffz;
SeventvEmotes seventv;
ChannelPtr watchingChannelInner;
IndirectChannel watchingChannel;
};

} // namespace chatterino::mock
5 changes: 5 additions & 0 deletions src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class IApplication
virtual ITwitchIrcServer *getTwitch() = 0;
virtual ChatterinoBadges *getChatterinoBadges() = 0;
virtual FfzBadges *getFfzBadges() = 0;
virtual SeventvBadges *getSeventvBadges() = 0;
virtual IUserDataController *getUserData() = 0;
virtual ITwitchLiveController *getTwitchLiveController() = 0;
};
Expand Down Expand Up @@ -160,6 +161,10 @@ class Application : public IApplication
{
return this->ffzBadges;
}
SeventvBadges *getSeventvBadges() override
{
return this->seventvBadges;
}
IUserDataController *getUserData() override;
ITwitchLiveController *getTwitchLiveController() override;

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/filters/lang/Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace chatterino::filters {

ContextMap buildContextMap(const MessagePtr &m, chatterino::Channel *channel)
{
auto watchingChannel = chatterino::getApp()->twitch->watchingChannel.get();
auto watchingChannel = getIApp()->getTwitch()->getWatchingChannel().get();

/*
* Looking to add a new identifier to filters? Here's what to do:
Expand Down
3 changes: 2 additions & 1 deletion src/messages/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ namespace detail {
else
{
this->durationOffset_ = std::min<int>(
int(getApp()->emotes->gifTimer.position() % totalLength),
int(getIApp()->getEmotes()->getGIFTimer().position() %
totalLength),
60000);
}
this->processOffset();
Expand Down
2 changes: 0 additions & 2 deletions src/messages/SharedMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,6 @@ void SharedMessageBuilder::triggerHighlights()
QString SharedMessageBuilder::stylizeUsername(const QString &username,
const Message &message)
{
auto app = getApp();

const QString &localizedName = message.localizedName;
bool hasLocalizedName = !localizedName.isEmpty();

Expand Down
6 changes: 6 additions & 0 deletions src/providers/twitch/TwitchIrcServer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "TwitchIrcServer.hpp"

#include "Application.hpp"
#include "common/Channel.hpp"
#include "common/Env.hpp"
#include "common/QLogging.hpp"
#include "controllers/accounts/AccountController.hpp"
Expand Down Expand Up @@ -520,6 +521,11 @@ const SeventvEmotes &TwitchIrcServer::getSeventvEmotes() const
return this->seventv_;
}

const IndirectChannel &TwitchIrcServer::getWatchingChannel() const
{
return this->watchingChannel;
}

void TwitchIrcServer::reloadBTTVGlobalEmotes()
{
this->bttv.loadEmotes();
Expand Down
2 changes: 2 additions & 0 deletions src/providers/twitch/TwitchIrcServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ITwitchIrcServer
virtual const BttvEmotes &getBttvEmotes() const = 0;
virtual const FfzEmotes &getFfzEmotes() const = 0;
virtual const SeventvEmotes &getSeventvEmotes() const = 0;
virtual const IndirectChannel &getWatchingChannel() const = 0;

// Update this interface with TwitchIrcServer methods as needed
};
Expand Down Expand Up @@ -85,6 +86,7 @@ class TwitchIrcServer final : public AbstractIrcServer,
const BttvEmotes &getBttvEmotes() const override;
const FfzEmotes &getFfzEmotes() const override;
const SeventvEmotes &getSeventvEmotes() const override;
const IndirectChannel &getWatchingChannel() const override;

protected:
virtual void initializeConnection(IrcConnection *connection,
Expand Down
29 changes: 17 additions & 12 deletions src/providers/twitch/TwitchMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ void TwitchMessageBuilder::triggerHighlights()

MessagePtr TwitchMessageBuilder::build()
{
assert(this->ircMessage != nullptr);
assert(this->channel != nullptr);

// PARSE
this->userId_ = this->ircMessage->tag("user-id").toString();

Expand Down Expand Up @@ -433,7 +436,8 @@ void TwitchMessageBuilder::addWords(

// 1. Add text before the emote
QString preText = word.left(currentTwitchEmote.start - cursor);
for (auto &variant : getApp()->emotes->emojis.parse(preText))
for (auto &variant :
getIApp()->getEmotes()->getEmojis()->parse(preText))
{
boost::apply_visitor(
[&](auto &&arg) {
Expand All @@ -453,7 +457,7 @@ void TwitchMessageBuilder::addWords(
}

// split words
for (auto &variant : getApp()->emotes->emojis.parse(word))
for (auto &variant : getIApp()->getEmotes()->getEmojis()->parse(word))
{
boost::apply_visitor(
[&](auto &&arg) {
Expand Down Expand Up @@ -748,7 +752,7 @@ void TwitchMessageBuilder::parseUsername()
}

// Update current user color if this is our message
auto currentUser = getApp()->accounts->twitch.getCurrent();
auto currentUser = getIApp()->getAccounts()->twitch.getCurrent();
if (this->ircMessage->nick() == currentUser->getUserName())
{
currentUser->setColor(this->usernameColor_);
Expand All @@ -757,7 +761,7 @@ void TwitchMessageBuilder::parseUsername()

void TwitchMessageBuilder::appendUsername()
{
auto app = getApp();
auto *app = getIApp();

QString username = this->userName;
this->message().loginName = username;
Expand Down Expand Up @@ -802,7 +806,7 @@ void TwitchMessageBuilder::appendUsername()
FontStyle::ChatMediumBold)
->setLink({Link::UserWhisper, this->message().displayName});

auto currentUser = app->accounts->twitch.getCurrent();
auto currentUser = app->getAccounts()->twitch.getCurrent();

// Separator
this->emplace<TextElement>("->", MessageElementFlag::Username,
Expand Down Expand Up @@ -1062,11 +1066,11 @@ void TwitchMessageBuilder::runIgnoreReplaces(

Outcome TwitchMessageBuilder::tryAppendEmote(const EmoteName &name)
{
auto *app = getApp();
auto *app = getIApp();

const auto &globalBttvEmotes = app->twitch->getBttvEmotes();
const auto &globalFfzEmotes = app->twitch->getFfzEmotes();
const auto &globalSeventvEmotes = app->twitch->getSeventvEmotes();
const auto &globalBttvEmotes = app->getTwitch()->getBttvEmotes();
const auto &globalFfzEmotes = app->getTwitch()->getFfzEmotes();
const auto &globalSeventvEmotes = app->getTwitch()->getSeventvEmotes();

auto flags = MessageElementFlags();
auto emote = std::optional<EmotePtr>{};
Expand Down Expand Up @@ -1312,7 +1316,8 @@ void TwitchMessageBuilder::appendTwitchBadges()

void TwitchMessageBuilder::appendChatterinoBadges()
{
if (auto badge = getApp()->chatterinoBadges->getBadge({this->userId_}))
if (auto badge =
getIApp()->getChatterinoBadges()->getBadge({this->userId_}))
{
this->emplace<BadgeElement>(*badge,
MessageElementFlag::BadgeChatterino);
Expand All @@ -1322,7 +1327,7 @@ void TwitchMessageBuilder::appendChatterinoBadges()
void TwitchMessageBuilder::appendFfzBadges()
{
for (const auto &badge :
getApp()->ffzBadges->getUserBadges({this->userId_}))
getIApp()->getFfzBadges()->getUserBadges({this->userId_}))
{
this->emplace<FfzBadgeElement>(
badge.emote, MessageElementFlag::BadgeFfz, badge.color);
Expand All @@ -1331,7 +1336,7 @@ void TwitchMessageBuilder::appendFfzBadges()

void TwitchMessageBuilder::appendSeventvBadges()
{
if (auto badge = getApp()->seventvBadges->getBadge({this->userId_}))
if (auto badge = getIApp()->getSeventvBadges()->getBadge({this->userId_}))
{
this->emplace<BadgeElement>(*badge, MessageElementFlag::BadgeSevenTV);
}
Expand Down
6 changes: 6 additions & 0 deletions src/singletons/Emotes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class IEmotes

virtual ITwitchEmotes *getTwitchEmotes() = 0;
virtual IEmojis *getEmojis() = 0;
virtual GIFTimer &getGIFTimer() = 0;
};

class Emotes final : public IEmotes, public Singleton
Expand All @@ -38,6 +39,11 @@ class Emotes final : public IEmotes, public Singleton
return &this->emojis;
}

GIFTimer &getGIFTimer() final
{
return this->gifTimer;
}

TwitchEmotes twitch;
Emojis emojis;

Expand Down
16 changes: 2 additions & 14 deletions tests/src/ChannelChatters.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
#include "common/ChannelChatters.hpp"

#include "common/Channel.hpp"
#include "mocks/Channel.hpp"

#include <gtest/gtest.h>
#include <QColor>
#include <QStringList>

namespace chatterino {

class MockChannel : public Channel
{
public:
MockChannel(const QString &name)
: Channel(name, Channel::Type::Twitch)
{
}
};

} // namespace chatterino

using namespace chatterino;
using chatterino::mock::MockChannel;

// Ensure inserting the same user does not increase the size of the stored colors
TEST(ChatterChatters, insertSameUser)
Expand Down
Loading

0 comments on commit 9f23c85

Please sign in to comment.