Remove Redundant Parsing of Links (#4507)
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
+84
-49
@@ -6,66 +6,96 @@
|
||||
|
||||
using namespace chatterino;
|
||||
|
||||
struct Case {
|
||||
QString protocol{};
|
||||
QString host{};
|
||||
QString rest{};
|
||||
|
||||
void check() const
|
||||
{
|
||||
auto input = this->protocol + this->host + this->rest;
|
||||
LinkParser p(input);
|
||||
ASSERT_TRUE(p.result().has_value()) << input.toStdString();
|
||||
|
||||
const auto &r = *p.result();
|
||||
ASSERT_EQ(r.source, input);
|
||||
ASSERT_EQ(r.protocol, this->protocol) << this->protocol.toStdString();
|
||||
ASSERT_EQ(r.host, this->host) << this->host.toStdString();
|
||||
ASSERT_EQ(r.rest, this->rest) << this->rest.toStdString();
|
||||
}
|
||||
};
|
||||
|
||||
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",
|
||||
const QList<Case> cases = {
|
||||
{"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"},
|
||||
{"", "wiki.chatterino.com", ":80/foo/bar"},
|
||||
{"", "wiki.chatterino.com", "/:80?foo/bar"},
|
||||
{"", "wiki.chatterino.com", "/127.0.0.1"},
|
||||
{"", "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"},
|
||||
// test case-insensitiveness
|
||||
{"HtTpS://", "127.0.0.1.CoM"},
|
||||
{"HTTP://", "XD.CHATTERINO.COM", "/#?FOO"},
|
||||
{"HTTPS://", "wikI.chatterino.com"},
|
||||
{"", "chatterino.Org", "#foo"},
|
||||
{"", "CHATTERINO.com", ""},
|
||||
};
|
||||
|
||||
for (const auto &input : inputs)
|
||||
for (const auto &c : cases)
|
||||
{
|
||||
LinkParser p(input);
|
||||
ASSERT_TRUE(p.hasMatch()) << input.toStdString();
|
||||
ASSERT_EQ(p.getCaptured(), input);
|
||||
c.check();
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
const QList<Case> cases = {
|
||||
{"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"},
|
||||
// test case-insensitiveness
|
||||
{"HTTP://", "196.168.4.0", "#Foo"},
|
||||
{"HTTPS://", "196.168.4.0", "#Foo"},
|
||||
{"htTp://", "127.0.0.1"},
|
||||
{"httpS://", "127.0.0.1"},
|
||||
|
||||
};
|
||||
|
||||
for (const auto &input : inputs)
|
||||
for (const auto &c : cases)
|
||||
{
|
||||
LinkParser p(input);
|
||||
ASSERT_TRUE(p.hasMatch()) << input.toStdString();
|
||||
ASSERT_EQ(p.getCaptured(), input);
|
||||
c.check();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,12 +110,14 @@ TEST(LinkParser, doesntParseInvalidIpv4Links)
|
||||
"1.2",
|
||||
"1",
|
||||
"1.2.3",
|
||||
"htt://256.255.255.255",
|
||||
"aliens://256.255.255.255",
|
||||
};
|
||||
|
||||
for (const auto &input : inputs)
|
||||
{
|
||||
LinkParser p(input);
|
||||
ASSERT_FALSE(p.hasMatch()) << input.toStdString();
|
||||
ASSERT_FALSE(p.result().has_value()) << input.toStdString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +131,9 @@ TEST(LinkParser, doesntParseInvalidLinks)
|
||||
"spotify://chatterino.com",
|
||||
"httpsx://chatterino.com",
|
||||
"https:chatterino.com",
|
||||
"https:/chatterino.com",
|
||||
"http:/chatterino.com",
|
||||
"htp://chatterino.com",
|
||||
"/chatterino.com",
|
||||
"word",
|
||||
".",
|
||||
@@ -114,6 +149,6 @@ TEST(LinkParser, doesntParseInvalidLinks)
|
||||
for (const auto &input : inputs)
|
||||
{
|
||||
LinkParser p(input);
|
||||
ASSERT_FALSE(p.hasMatch()) << input.toStdString();
|
||||
ASSERT_FALSE(p.result().has_value()) << input.toStdString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user