Benchmark and Test LinkParser (#4436)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix 2023-04-02 16:59:26 +02:00 committed by GitHub
parent b54fcd2869
commit bdab5e021c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 171 additions and 2 deletions

View file

@ -24,6 +24,7 @@
- Dev: Ensure tests have default-initialized settings. (#4498)
- Dev: Add scripting capabilities with Lua (#4341)
- Dev: Conan 2.0 is now used instead of Conan 1.0. (#4417)
- Dev: Added tests and benchmarks for `LinkParser`. (#4436)
## 2.4.2

View file

@ -7,6 +7,7 @@ set(benchmark_SOURCES
${CMAKE_CURRENT_LIST_DIR}/src/FormatTime.cpp
${CMAKE_CURRENT_LIST_DIR}/src/Helpers.cpp
${CMAKE_CURRENT_LIST_DIR}/src/LimitedQueue.cpp
${CMAKE_CURRENT_LIST_DIR}/src/LinkParser.cpp
# Add your new file above this line!
)

View file

@ -1,5 +1,5 @@
#include "Application.hpp"
#include "BaseSettings.hpp"
#include "singletons/Settings.hpp"
#include "common/Channel.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/highlights/HighlightController.hpp"
@ -56,7 +56,7 @@ public:
{
return nullptr;
}
Emotes *getEmotes() override
IEmotes *getEmotes() override
{
return nullptr;
}
@ -100,6 +100,10 @@ public:
{
return nullptr;
}
IUserDataController *getUserData() override
{
return nullptr;
}
AccountController accounts;
HighlightController highlights;

View file

@ -0,0 +1,43 @@
#include "common/LinkParser.hpp"
#include <benchmark/benchmark.h>
#include <QDebug>
#include <QString>
#include <QStringList>
#include <optional>
using namespace chatterino;
const QString INPUT = QStringLiteral(
"If your Chatterino isn't loading FFZ emotes, update to the latest nightly "
"(or 2.4.2 if its out) "
"https://github.com/Chatterino/chatterino2/releases/tag/nightly-build "
"AlienPls https://www.youtube.com/watch?v=ELBBiBDcWc0 "
"127.0.3 aaaa xd 256.256.256.256 AsdQwe xd 127.0.0.1 https://. https://.be "
"https://a http://a.b https://a.be ftp://xdd.com "
"this is a text lol . ://foo.com //aa.de :/foo.de xd.XDDDDDD ");
static void BM_LinkParsing(benchmark::State &state)
{
QStringList words = INPUT.split(' ');
// Make sure the TLDs are loaded
{
benchmark::DoNotOptimize(LinkParser("xd.com").getCaptured());
}
for (auto _ : state)
{
for (auto word : words)
{
LinkParser parser(word);
if (parser.hasMatch())
{
benchmark::DoNotOptimize(parser.getCaptured());
}
}
}
}
BENCHMARK(BM_LinkParsing);

View file

@ -24,6 +24,7 @@ set(test_SOURCES
${CMAKE_CURRENT_LIST_DIR}/src/SeventvEventAPI.cpp
${CMAKE_CURRENT_LIST_DIR}/src/BttvLiveUpdates.cpp
${CMAKE_CURRENT_LIST_DIR}/src/Updates.cpp
${CMAKE_CURRENT_LIST_DIR}/src/LinkParser.cpp
# Add your new file above this line!
)

119
tests/src/LinkParser.cpp Normal file
View file

@ -0,0 +1,119 @@
#include "common/LinkParser.hpp"
#include <gtest/gtest.h>
#include <QString>
#include <QStringList>
using namespace chatterino;
TEST(LinkParser, parseDomainLinks)
{
const QStringList inputs = {
"https://chatterino.com",
"http://chatterino.com",
"chatterino.com",
"wiki.chatterino.com",
"https://wiki.chatterino.com",
"http://chatterino.co.uk",
"http://a.io",
"chatterino.com:80",
"wiki.chatterino.com:80",
"a.b.c.chatterino.com",
"https://a.b.c.chatterino.com/foo",
"http://chatterino.com?foo",
"http://xd.chatterino.com/#?foo",
"chatterino.com#foo",
"1.com",
"127.0.0.1.com",
"https://127.0.0.1.com",
};
for (const auto &input : inputs)
{
LinkParser p(input);
ASSERT_TRUE(p.hasMatch()) << input.toStdString();
ASSERT_EQ(p.getCaptured(), input);
}
}
TEST(LinkParser, parseIpv4Links)
{
const QStringList inputs = {
"https://127.0.0.1",
"http://127.0.0.1",
"127.0.0.1",
"127.0.0.1:8080",
"255.255.255.255",
"0.0.0.0",
"1.1.1.1",
"001.001.01.1",
"123.246.87.0",
"196.168.0.1:",
"196.168.4.2/foo",
"196.168.4.2?foo",
"http://196.168.4.0#foo",
"196.168.4.0/?#foo",
"196.168.4.0#?/foo",
"256.255.255.255",
"http://256.255.255.255",
"255.256.255.255",
"255.255.256.255",
"255.255.255.256",
};
for (const auto &input : inputs)
{
LinkParser p(input);
ASSERT_TRUE(p.hasMatch()) << input.toStdString();
ASSERT_EQ(p.getCaptured(), input);
}
}
TEST(LinkParser, doesntParseInvalidIpv4Links)
{
const QStringList inputs = {
"https://127.0.0.",
"http://127.0.01",
"127.0.0000.1",
"1.",
".127.0.0.1",
"1.2",
"1",
"1.2.3",
};
for (const auto &input : inputs)
{
LinkParser p(input);
ASSERT_FALSE(p.hasMatch()) << input.toStdString();
}
}
TEST(LinkParser, doesntParseInvalidLinks)
{
const QStringList inputs = {
"h://foo.com",
"spotify:1234",
"ftp://chatterino.com",
"ftps://chatterino.com",
"spotify://chatterino.com",
"httpsx://chatterino.com",
"https:chatterino.com",
"/chatterino.com",
"word",
".",
"/",
"#",
":",
"?",
"a",
"://chatterino.com",
"//chatterino.com",
};
for (const auto &input : inputs)
{
LinkParser p(input);
ASSERT_FALSE(p.hasMatch()) << input.toStdString();
}
}