Skip to content

Commit

Permalink
Move mocks to their own interface (#4645)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored May 20, 2023
1 parent 5d0bdc1 commit e1a6c24
Show file tree
Hide file tree
Showing 12 changed files with 564 additions and 501 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ include(cmake/resources/generate_resources.cmake)

add_subdirectory(src)

if (BUILD_TESTS OR BUILD_BENCHMARKS)
add_subdirectory(mocks)
endif ()

if (BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
Expand Down
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_executable(${PROJECT_NAME} ${benchmark_SOURCES})
add_sanitizers(${PROJECT_NAME})

target_link_libraries(${PROJECT_NAME} PRIVATE chatterino-lib)
target_link_libraries(${PROJECT_NAME} PRIVATE chatterino-mocks)

target_link_libraries(${PROJECT_NAME} PRIVATE benchmark::benchmark)

Expand Down
53 changes: 3 additions & 50 deletions benchmarks/src/Highlights.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "Application.hpp"
#include "singletons/Settings.hpp"
#include "common/Channel.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/highlights/HighlightController.hpp"
#include "controllers/highlights/HighlightPhrase.hpp"
#include "messages/Message.hpp"
#include "messages/SharedMessageBuilder.hpp"
#include "mocks/EmptyApplication.hpp"
#include "singletons/Settings.hpp"
#include "util/Helpers.hpp"

#include <benchmark/benchmark.h>
Expand Down Expand Up @@ -45,65 +46,17 @@ class BenchmarkMessageBuilder : public SharedMessageBuilder
}
};

class MockApplication : IApplication
class MockApplication : mock::EmptyApplication
{
public:
Theme *getThemes() override
{
return nullptr;
}
Fonts *getFonts() override
{
return nullptr;
}
IEmotes *getEmotes() override
{
return nullptr;
}
AccountController *getAccounts() override
{
return &this->accounts;
}
HotkeyController *getHotkeys() override
{
return nullptr;
}
WindowManager *getWindows() override
{
return nullptr;
}
Toasts *getToasts() override
{
return nullptr;
}
CommandController *getCommands() override
{
return nullptr;
}
NotificationController *getNotifications() override
{
return nullptr;
}
HighlightController *getHighlights() override
{
return &this->highlights;
}
TwitchIrcServer *getTwitch() override
{
return nullptr;
}
ChatterinoBadges *getChatterinoBadges() override
{
return nullptr;
}
FfzBadges *getFfzBadges() override
{
return nullptr;
}
IUserDataController *getUserData() override
{
return nullptr;
}

AccountController accounts;
HighlightController highlights;
Expand Down
7 changes: 7 additions & 0 deletions benchmarks/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#include "singletons/Settings.hpp"

#include <benchmark/benchmark.h>
#include <QApplication>
#include <QtConcurrent>

using namespace chatterino;

int main(int argc, char **argv)
{
QApplication app(argc, argv);

::benchmark::Initialize(&argc, argv);

// Ensure settings are initialized before any tests are run
chatterino::Settings settings("/tmp/c2-empty-test");

QtConcurrent::run([&app] {
::benchmark::RunSpecifiedBenchmarks();

Expand Down
55 changes: 55 additions & 0 deletions mocks/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Language: Cpp

AccessModifierOffset: -4
AlignEscapedNewlinesLeft: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakBeforeMultilineStrings: false
BasedOnStyle: Google
BraceWrapping:
AfterClass: "true"
AfterControlStatement: "true"
AfterFunction: "true"
AfterNamespace: "false"
BeforeCatch: "true"
BeforeElse: "true"
BreakBeforeBraces: Custom
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: false
DerivePointerBinding: false
FixNamespaceComments: true
IndentCaseLabels: true
IndentWidth: 4
IndentWrappedFunctionNames: true
IndentPPDirectives: AfterHash
SortIncludes: CaseInsensitive
IncludeBlocks: Regroup
IncludeCategories:
# Project includes
- Regex: '^"[a-zA-Z\._-]+(/[a-zA-Z0-9\._-]+)*"$'
Priority: 1
# Third party library includes
- Regex: '<[[:alnum:].]+/[a-zA-Z0-9\._\/-]+>'
Priority: 3
# Qt includes
- Regex: '^<Q[a-zA-Z0-9\._\/-]+>$'
Priority: 3
CaseSensitive: true
# LibCommuni includes
- Regex: "^<Irc[a-zA-Z]+>$"
Priority: 3
# Misc libraries
- Regex: '^<[a-zA-Z_0-9]+\.h(pp)?>$'
Priority: 3
# Standard library includes
- Regex: "^<[a-zA-Z_]+>$"
Priority: 4
NamespaceIndentation: Inner
PointerBindsToType: false
SpacesBeforeTrailingComments: 2
Standard: Auto
ReflowComments: false
7 changes: 7 additions & 0 deletions mocks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
project(chatterino-mocks)

add_library(chatterino-mocks INTERFACE)

target_include_directories(chatterino-mocks INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)

target_link_libraries(${PROJECT_NAME} INTERFACE gmock)
81 changes: 81 additions & 0 deletions mocks/include/mocks/EmptyApplication.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include "Application.hpp"

namespace chatterino::mock {

class EmptyApplication : public IApplication
{
public:
Theme *getThemes() override
{
return nullptr;
}

Fonts *getFonts() override
{
return nullptr;
}

IEmotes *getEmotes() override
{
return nullptr;
}

AccountController *getAccounts() override
{
return nullptr;
}

HotkeyController *getHotkeys() override
{
return nullptr;
}

WindowManager *getWindows() override
{
return nullptr;
}

Toasts *getToasts() override
{
return nullptr;
}

CommandController *getCommands() override
{
return nullptr;
}

NotificationController *getNotifications() override
{
return nullptr;
}

HighlightController *getHighlights() override
{
return nullptr;
}

TwitchIrcServer *getTwitch() override
{
return nullptr;
}

ChatterinoBadges *getChatterinoBadges() override
{
return nullptr;
}

FfzBadges *getFfzBadges() override
{
return nullptr;
}

IUserDataController *getUserData() override
{
return nullptr;
}
};

} // namespace chatterino::mock
Loading

0 comments on commit e1a6c24

Please sign in to comment.