diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dcd34cde..7c65d6ff4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 1bc975fd0..b655b7f84 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -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! ) diff --git a/benchmarks/src/Highlights.cpp b/benchmarks/src/Highlights.cpp index e87a38bac..c35a0847f 100644 --- a/benchmarks/src/Highlights.cpp +++ b/benchmarks/src/Highlights.cpp @@ -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; diff --git a/benchmarks/src/LinkParser.cpp b/benchmarks/src/LinkParser.cpp new file mode 100644 index 000000000..9178e63ee --- /dev/null +++ b/benchmarks/src/LinkParser.cpp @@ -0,0 +1,43 @@ +#include "common/LinkParser.hpp" + +#include +#include +#include +#include + +#include + +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); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a5663126a..e57515d7b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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! ) diff --git a/tests/src/LinkParser.cpp b/tests/src/LinkParser.cpp new file mode 100644 index 000000000..33c3a7adc --- /dev/null +++ b/tests/src/LinkParser.cpp @@ -0,0 +1,119 @@ +#include "common/LinkParser.hpp" + +#include +#include +#include + +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(); + } +}