chore: remove Singleton & replace getIApp with getApp (#5514)

This commit is contained in:
pajlada
2024-07-21 15:09:59 +02:00
committed by GitHub
parent 21186df058
commit 5deec1f02f
145 changed files with 802 additions and 856 deletions
+11 -12
View File
@@ -27,6 +27,7 @@ class MockApplication : mock::EmptyApplication
public:
MockApplication()
: settings(this->settingsDir.filePath("settings.json"))
, commands(this->paths_)
{
}
@@ -833,8 +834,6 @@ TEST(Commands, E2E)
::testing::InSequence seq;
MockApplication app;
app.commands.initialize(*getSettings(), getIApp()->getPaths());
QJsonObject pajlada;
pajlada["id"] = "11148817";
pajlada["login"] = "pajlada";
@@ -879,10 +878,10 @@ TEST(Commands, E2E)
auto account = std::make_shared<TwitchAccount>(
testaccount420["login"].toString(), "token", "oauthclient",
testaccount420["id"].toString());
getIApp()->getAccounts()->twitch.accounts.append(account);
getIApp()->getAccounts()->twitch.currentUsername =
getApp()->getAccounts()->twitch.accounts.append(account);
getApp()->getAccounts()->twitch.currentUsername =
testaccount420["login"].toString();
getIApp()->getAccounts()->twitch.load();
getApp()->getAccounts()->twitch.load();
// Simple single-channel ban
EXPECT_CALL(mockHelix, fetchUsers(QStringList{"11148817"},
@@ -901,7 +900,7 @@ TEST(Commands, E2E)
QString(""), _, _))
.Times(1);
getIApp()->getCommands()->execCommand("/ban forsen", channel, false);
getApp()->getCommands()->execCommand("/ban forsen", channel, false);
// Multi-channel ban
EXPECT_CALL(mockHelix, fetchUsers(QStringList{"11148817"},
@@ -937,7 +936,7 @@ TEST(Commands, E2E)
std::optional<int>{}, QString(""), _, _))
.Times(1);
getIApp()->getCommands()->execCommand(
getApp()->getCommands()->execCommand(
"/ban --channel id:11148817 --channel testaccount_420 forsen", channel,
false);
@@ -948,7 +947,7 @@ TEST(Commands, E2E)
QString(""), _, _))
.Times(1);
getIApp()->getCommands()->execCommand("/ban id:22484632", channel, false);
getApp()->getCommands()->execCommand("/ban id:22484632", channel, false);
// ID-based redirected ban
EXPECT_CALL(mockHelix,
@@ -957,7 +956,7 @@ TEST(Commands, E2E)
QString(""), _, _))
.Times(1);
getIApp()->getCommands()->execCommand(
getApp()->getCommands()->execCommand(
"/ban --channel id:117166826 id:22484632", channel, false);
// name-based redirected ban
@@ -976,7 +975,7 @@ TEST(Commands, E2E)
QString(""), _, _))
.Times(1);
getIApp()->getCommands()->execCommand(
getApp()->getCommands()->execCommand(
"/ban --channel testaccount_420 id:22484632", channel, false);
// Multi-channel timeout
@@ -1013,7 +1012,7 @@ TEST(Commands, E2E)
std::optional<int>{600}, QString(""), _, _))
.Times(1);
getIApp()->getCommands()->execCommand(
getApp()->getCommands()->execCommand(
"/timeout --channel id:11148817 --channel testaccount_420 forsen",
channel, false);
@@ -1049,7 +1048,7 @@ TEST(Commands, E2E)
forsen["id"].toString(), _, _))
.Times(1);
getIApp()->getCommands()->execCommand(
getApp()->getCommands()->execCommand(
"/unban --channel id:11148817 --channel testaccount_420 forsen",
channel, false);
}
+7
View File
@@ -29,6 +29,12 @@ namespace {
class MockApplication : mock::EmptyApplication
{
public:
MockApplication()
: settings(this->settingsDir.filePath("settings.json"))
, highlights(this->settings, &this->accounts)
{
}
IEmotes *getEmotes() override
{
return &this->emotes;
@@ -69,6 +75,7 @@ public:
return &this->highlights;
}
Settings settings;
AccountController accounts;
Emotes emotes;
mock::UserDataController userData;
+15 -34
View File
@@ -8,7 +8,6 @@
#include "mocks/UserData.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/TwitchBadge.hpp" // for Badge
#include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include "Test.hpp"
@@ -23,9 +22,16 @@ using ::testing::Exactly;
namespace {
class MockApplication : mock::EmptyApplication
class MockApplication : public mock::EmptyApplication
{
public:
MockApplication(const QString &settingsBody)
: mock::EmptyApplication(settingsBody)
, settings(this->settingsDir.path())
, highlights(this->settings, &this->accounts)
{
}
AccountController *getAccounts() override
{
return &this->accounts;
@@ -41,10 +47,10 @@ public:
return &this->userData;
}
Settings settings;
AccountController accounts;
HighlightController highlights;
mock::UserDataController userData;
// TODO: Figure this out
};
} // namespace
@@ -180,13 +186,7 @@ protected:
void configure(const QString &settings, bool isAnon)
{
// Write default settings to the mock settings json file
this->settingsDir_ = std::make_unique<QTemporaryDir>();
QFile settingsFile(this->settingsDir_->filePath("settings.json"));
ASSERT_TRUE(settingsFile.open(QIODevice::WriteOnly | QIODevice::Text));
ASSERT_GT(settingsFile.write(settings.toUtf8()), 0);
ASSERT_TRUE(settingsFile.flush());
settingsFile.close();
this->mockApplication = std::make_unique<MockApplication>(settings);
this->mockHelix = new mock::Helix;
@@ -195,24 +195,17 @@ protected:
EXPECT_CALL(*this->mockHelix, loadBlocks).Times(Exactly(1));
EXPECT_CALL(*this->mockHelix, update).Times(Exactly(isAnon ? 0 : 1));
this->mockApplication = std::make_unique<MockApplication>();
this->settings = std::make_unique<Settings>(this->settingsDir_->path());
this->paths = std::make_unique<Paths>();
this->controller = std::make_unique<HighlightController>();
this->mockApplication->accounts.initialize(*this->settings,
*this->paths);
this->controller->initialize(*this->settings, *this->paths);
this->mockApplication->accounts.load();
}
void runTests(const std::vector<TestCase> &tests)
{
for (const auto &[input, expected] : tests)
{
auto [isMatch, matchResult] = this->controller->check(
input.args, input.badges, input.senderName,
input.originalMessage, input.flags);
auto [isMatch, matchResult] =
this->mockApplication->getHighlights()->check(
input.args, input.badges, input.senderName,
input.originalMessage, input.flags);
EXPECT_EQ(isMatch, expected.state)
<< input.senderName << ": " << input.originalMessage;
@@ -224,23 +217,11 @@ protected:
void TearDown() override
{
this->mockApplication.reset();
this->settings.reset();
this->paths.reset();
this->controller.reset();
this->settingsDir_.reset();
delete this->mockHelix;
}
std::unique_ptr<QTemporaryDir> settingsDir_;
std::unique_ptr<MockApplication> mockApplication;
std::unique_ptr<Settings> settings;
std::unique_ptr<Paths> paths;
std::unique_ptr<HighlightController> controller;
mock::Helix *mockHelix;
};
+1 -3
View File
@@ -129,9 +129,7 @@ protected:
this->settings = std::make_unique<Settings>(this->settingsDir_->path());
this->paths = std::make_unique<Paths>();
this->mockApplication->accounts.initialize(*this->settings,
*this->paths);
this->mockApplication->emotes.initialize(*this->settings, *this->paths);
this->mockApplication->accounts.load();
this->channelPtr = std::make_shared<MockChannel>("icelys");
+10 -4
View File
@@ -4,7 +4,7 @@
#include "controllers/accounts/AccountController.hpp"
#include "messages/MessageBuilder.hpp"
#include "messages/MessageElement.hpp"
#include "mocks/EmptyApplication.hpp"
#include "mocks/BaseApplication.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Fonts.hpp"
#include "singletons/Settings.hpp"
@@ -21,15 +21,16 @@ using namespace chatterino;
namespace {
class MockApplication : mock::EmptyApplication
class MockApplication : mock::BaseApplication
{
public:
MockApplication()
: settings(this->settingsDir.filePath("settings.json"))
: theme(this->paths_)
, fonts(this->settings)
, windowManager(this->paths_)
{
}
Theme *getThemes() override
{
return &this->theme;
@@ -45,7 +46,12 @@ public:
return &this->windowManager;
}
Settings settings;
AccountController *getAccounts() override
{
return &this->accounts;
}
AccountController accounts;
Theme theme;
Fonts fonts;
WindowManager windowManager;
+1
View File
@@ -23,6 +23,7 @@ class MockApplication : mock::EmptyApplication
public:
MockApplication()
: settings(this->settingsDir.filePath("settings.json"))
, theme(this->paths_)
, fonts(this->settings)
{
}
+3 -4
View File
@@ -1,7 +1,7 @@
#include "widgets/Scrollbar.hpp"
#include "Application.hpp"
#include "mocks/EmptyApplication.hpp"
#include "mocks/BaseApplication.hpp"
#include "singletons/Fonts.hpp"
#include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
@@ -17,11 +17,11 @@ using namespace chatterino;
namespace {
class MockApplication : mock::EmptyApplication
class MockApplication : mock::BaseApplication
{
public:
MockApplication()
: settings(this->settingsDir.filePath("settings.json"))
: theme(this->paths_)
, fonts(this->settings)
, windowManager(this->paths_)
{
@@ -41,7 +41,6 @@ public:
return &this->windowManager;
}
Settings settings;
Theme theme;
Fonts fonts;
WindowManager windowManager;
+5 -6
View File
@@ -5,7 +5,7 @@
#include "controllers/commands/Command.hpp"
#include "controllers/commands/CommandController.hpp"
#include "controllers/hotkeys/HotkeyController.hpp"
#include "mocks/EmptyApplication.hpp"
#include "mocks/BaseApplication.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Fonts.hpp"
#include "singletons/Paths.hpp"
@@ -24,13 +24,14 @@ using ::testing::Exactly;
namespace {
class MockApplication : mock::EmptyApplication
class MockApplication : public mock::BaseApplication
{
public:
MockApplication()
: settings(this->settingsDir.filePath("settings.json"))
: theme(this->paths_)
, fonts(this->settings)
, windowManager(this->paths)
, windowManager(this->paths_)
, commands(this->paths_)
{
}
Theme *getThemes() override
@@ -68,11 +69,9 @@ public:
return &this->emotes;
}
Settings settings;
Theme theme;
HotkeyController hotkeys;
Fonts fonts;
Paths paths;
WindowManager windowManager;
AccountController accounts;
CommandController commands;
+7
View File
@@ -32,6 +32,12 @@ namespace {
class MockApplication : mock::EmptyApplication
{
public:
MockApplication()
: settings(this->settingsDir.filePath("settings.json"))
, highlights(this->settings, &this->accounts)
{
}
IEmotes *getEmotes() override
{
return &this->emotes;
@@ -92,6 +98,7 @@ public:
return &this->streamerMode;
}
Settings settings;
AccountController accounts;
Emotes emotes;
mock::UserDataController userData;