Skip to content

Commit

Permalink
add some emoji tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Feb 11, 2021
1 parent 26bc06f commit f5509b0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 31 deletions.
17 changes: 14 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,25 @@ set(chatterino_SOURCES

src/common/UsernameSet.cpp
src/controllers/highlights/HighlightPhrase.cpp


src/providers/emoji/Emojis.cpp

src/messages/Emote.cpp
src/messages/Image.cpp
src/messages/ImageSet.cpp
src/util/RapidjsonHelpers.cpp

${CMAKE_CURRENT_LIST_DIR}/resources/resources.qrc
${CMAKE_CURRENT_LIST_DIR}/resources/resources_autogenerated.qrc
)

find_package(Qt5 5.9.0 REQUIRED COMPONENTS
Core Widgets Network Concurrent
)

set(CMAKE_AUTOMOC ON)

# set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

if (BUILD_TESTS)
set(BUILD_TESTS OFF)
Expand All @@ -41,12 +51,13 @@ if (BUILD_TESTS)
${chatterino_SOURCES}

tests/src/main.cpp
tests/src/Emojis.cpp
tests/src/NetworkRequest.cpp
tests/src/UsernameSet.cpp
tests/src/HighlightPhrase.cpp
)

target_compile_definitions(chatterino-test PRIVATE CHATTERINO_GIT_HASH="test" AB_CUSTOM_SETTINGS)
target_compile_definitions(chatterino-test PRIVATE CHATTERINO_GIT_HASH="test" AB_CUSTOM_SETTINGS CHATTERINO_TEST)

target_link_libraries(chatterino-test Qt5::Core Qt5::Widgets Qt5::Network Qt5::Concurrent)

Expand Down
13 changes: 12 additions & 1 deletion src/messages/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
#include "common/QLogging.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "debug/Benchmark.hpp"
#include "singletons/Emotes.hpp"
#ifndef CHATTERINO_TEST
# include "singletons/Emotes.hpp"
#endif
#include "singletons/WindowManager.hpp"
#include "singletons/helper/GifTimer.hpp"
#include "util/DebugCount.hpp"
#include "util/PostToThread.hpp"

#include <queue>

namespace chatterino {
namespace detail {
// Frames
Expand All @@ -38,10 +43,12 @@ namespace detail {
{
DebugCount::increase("animated images");

#ifndef CHATTERINO_TEST
this->gifTimerConnection_ =
getApp()->emotes->gifTimer.signal.connect([this] {
this->advance();
});
#endif
}

auto totalLength =
Expand All @@ -56,9 +63,11 @@ namespace detail {
}
else
{
#ifndef CHATTERINO_TEST
this->durationOffset_ = std::min<int>(
int(getApp()->emotes->gifTimer.position() % totalLength),
60000);
#endif
}
this->processOffset();
}
Expand Down Expand Up @@ -182,7 +191,9 @@ namespace detail {
}
}

#ifndef CHATTERINO_TEST
getApp()->windows->forceLayoutChannelViews();
#endif
loadedEventQueued = false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/messages/ImageSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ const ImagePtr &ImageSet::getImage3() const

const std::shared_ptr<Image> &getImagePriv(const ImageSet &set, float scale)
{
#ifndef CHATTERINO_TEST
scale *= getSettings()->emoteScale;
#endif

int quality = 1;

Expand Down
16 changes: 16 additions & 0 deletions src/providers/emoji/Emojis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ void Emojis::sortEmojis()

void Emojis::loadEmojiSet()
{
#ifndef CHATTERINO_TEST
getSettings()->emojiSet.connect([=](const auto &emojiSet) {
#else
const QString emojiSet = "twitter";
#endif
this->emojis.each([=](const auto &name,
std::shared_ptr<EmojiData> &emoji) {
QString emojiSetToUse = emojiSet;
Expand Down Expand Up @@ -318,7 +322,9 @@ void Emojis::loadEmojiSet()
EmoteName{emoji->value}, ImageSet{Image::fromUrl({url}, 0.35)},
Tooltip{":" + emoji->shortCodes[0] + ":<br/>Emoji"}, Url{}});
});
#ifndef CHATTERINO_TEST
});
#endif
}

std::vector<boost::variant<EmotePtr, QString>> Emojis::parse(
Expand All @@ -345,6 +351,12 @@ std::vector<boost::variant<EmotePtr, QString>> Emojis::parse(

const auto &possibleEmojis = it.value();

for (const auto &p : possibleEmojis)
{
printf("xd\n");
qDebug() << "possible emoji:" << p->value;
}

int remainingCharacters = text.length() - i - 1;

std::shared_ptr<EmojiData> matchedEmoji;
Expand Down Expand Up @@ -436,11 +448,15 @@ QString Emojis::replaceShortCodes(const QString &text)

if (emojiIt == this->emojiShortCodeToEmoji_.constEnd())
{
printf("No match for %s\n", matchString.toStdString().c_str());
continue;
}

auto emojiData = emojiIt.value();

printf("Found match for %s: '%s'\n", qUtf8Printable(matchString),
qUtf8Printable(emojiData->unifiedCode));

ret.replace(offset + match.capturedStart(), match.capturedLength(),
emojiData->value);

Expand Down
42 changes: 42 additions & 0 deletions tests/src/Emojis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "providers/emoji/Emojis.hpp"

#include <gtest/gtest.h>
#include <QDebug>
#include <QString>

using namespace chatterino;

TEST(Emojis, ShortcodeParsing)
{
Emojis emojis;

emojis.load();

struct TestCase {
QString input;
QString expectedOutput;
};

std::vector<TestCase> tests{
{
.input = "foo :penguin: bar",
.expectedOutput = "foo 🐧 bar",
},
{
.input = "foo :nonexistantcode: bar",
.expectedOutput = "foo :nonexistantcode: bar",
},
{
.input = ":male-doctor:",
.expectedOutput = "👨‍⚕️",
},
};

for (const auto &test : tests)
{
auto output = emojis.replaceShortCodes(test.input);

EXPECT_EQ(output, test.expectedOutput)
<< "Input " << test.input.toStdString() << " failed";
}
}
27 changes: 0 additions & 27 deletions tests/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,6 @@

using namespace chatterino;

void xd()
{
std::mutex mut;
bool requestDone = false;
std::condition_variable requestDoneCondition;

EXPECT_TRUE(NetworkManager::workerThread.isRunning());

using namespace std::chrono_literals;

auto url = "http://localhost:8000/status/200";
NetworkRequest(url)
.onSuccess([&](NetworkResult result) -> Outcome {
qDebug() << "ON SUCCESS";
qDebug() << url;
return Success;
})
.onError([&](NetworkResult result) {
qDebug() << "ON ERROR";
})
.execute();

EXPECT_TRUE(NetworkManager::workerThread.isRunning());

EXPECT_TRUE(true);
}

int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit f5509b0

Please sign in to comment.