fix: support captures in ignores (#5126)

This commit is contained in:
nerix
2024-01-27 15:46:11 +01:00
committed by GitHub
parent c32ee8e5b5
commit 36ef8fb99d
4 changed files with 295 additions and 22 deletions
+140
View File
@@ -3,6 +3,7 @@
#include "common/Channel.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/highlights/HighlightController.hpp"
#include "controllers/ignores/IgnorePhrase.hpp"
#include "messages/MessageBuilder.hpp"
#include "mocks/Channel.hpp"
#include "mocks/ChatterinoBadges.hpp"
@@ -478,3 +479,142 @@ TEST_F(TestTwitchMessageBuilder, ParseMessage)
delete privmsg;
}
}
TEST_F(TestTwitchMessageBuilder, IgnoresReplace)
{
struct TestCase {
std::vector<IgnorePhrase> phrases;
QString input;
std::vector<TwitchEmoteOccurrence> twitchEmotes;
QString expectedMessage;
std::vector<TwitchEmoteOccurrence> expectedTwitchEmotes;
};
auto *twitchEmotes = this->mockApplication->getEmotes()->getTwitchEmotes();
auto emoteAt = [&](int at, const QString &name) {
return TwitchEmoteOccurrence{
.start = at,
.end = static_cast<int>(at + name.size() - 1),
.ptr =
twitchEmotes->getOrCreateEmote(EmoteId{name}, EmoteName{name}),
.name = EmoteName{name},
};
};
auto regularReplace = [](auto pattern, auto replace,
bool caseSensitive = true) {
return IgnorePhrase(pattern, false, false, replace, caseSensitive);
};
auto regexReplace = [](auto pattern, auto regex,
bool caseSensitive = true) {
return IgnorePhrase(pattern, true, false, regex, caseSensitive);
};
std::vector<TestCase> testCases{
{
{regularReplace("foo1", "baz1")},
"foo1 Kappa",
{emoteAt(4, "Kappa")},
"baz1 Kappa",
{emoteAt(4, "Kappa")},
},
{
{regularReplace("foo1", "baz1", false)},
"FoO1 Kappa",
{emoteAt(4, "Kappa")},
"baz1 Kappa",
{emoteAt(4, "Kappa")},
},
{
{regexReplace("f(o+)1", "baz1[\\1]")},
"foo1 Kappa",
{emoteAt(4, "Kappa")},
"baz1[oo] Kappa",
{emoteAt(8, "Kappa")},
},
{
{regexReplace("f(o+)1", R"(baz1[\0][\1][\2])")},
"foo1 Kappa",
{emoteAt(4, "Kappa")},
"baz1[\\0][oo][\\2] Kappa",
{emoteAt(16, "Kappa")},
},
{
{regexReplace("f(o+)(\\d+)", "baz1[\\1+\\2]")},
"foo123 Kappa",
{emoteAt(6, "Kappa")},
"baz1[oo+123] Kappa",
{emoteAt(12, "Kappa")},
},
{
{regexReplace("(?<=foo)(\\d+)", "[\\1]")},
"foo123 Kappa",
{emoteAt(6, "Kappa")},
"foo[123] Kappa",
{emoteAt(8, "Kappa")},
},
{
{regexReplace("a(?=a| )", "b")},
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
"Kappa",
{emoteAt(127, "Kappa")},
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"bbbb"
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb "
"Kappa",
{emoteAt(127, "Kappa")},
},
{
{regexReplace("abc", "def", false)},
"AbC Kappa",
{emoteAt(3, "Kappa")},
"def Kappa",
{emoteAt(3, "Kappa")},
},
{
{
regexReplace("abc", "def", false),
regularReplace("def", "ghi"),
},
"AbC Kappa",
{emoteAt(3, "Kappa")},
"ghi Kappa",
{emoteAt(3, "Kappa")},
},
{
{
regexReplace("a(?=a| )", "b"),
regexReplace("b(?=b| )", "c"),
},
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
"Kappa",
{emoteAt(127, "Kappa")},
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc "
"Kappa",
{emoteAt(127, "Kappa")},
},
};
for (const auto &test : testCases)
{
auto message = test.input;
auto emotes = test.twitchEmotes;
TwitchMessageBuilder::processIgnorePhrases(test.phrases, message,
emotes);
EXPECT_EQ(message, test.expectedMessage)
<< "Message not equal for input '" << test.input.toStdString()
<< "' - expected: '" << test.expectedMessage.toStdString()
<< "' got: '" << message.toStdString() << "'";
EXPECT_EQ(emotes, test.expectedTwitchEmotes)
<< "Twitch emotes not equal for input '" << test.input.toStdString()
<< "' and output '" << message.toStdString() << "'";
}
}