mirror-chatterino2/tests/src/HighlightPhrase.cpp
pajlada 0cbddf7e9b
Fix/be respectful of special characters like exclamation marks in highlight phrases since they are also word boundaries (#1890)
* Add missing includes

We would normally have these included in another file already, or even
the precompiled headers, but having the files included here too makes
testing single parts easier.

* Modify the regex building of highlight phrases for non-regex phrases

For phrases like !test, the word boundary checking we did before was not
enough, so we now check for either a word boundary, a whitespace
character, or the line start/end.

* Add tests for ensuring I haven't fully broken the highlight system

* Add changelog entry
2020-08-22 15:37:03 +02:00

154 lines
4.8 KiB
C++

#include "controllers/highlights/HighlightPhrase.hpp"
#include <gtest/gtest.h>
using namespace chatterino;
namespace {
HighlightPhrase buildHighlightPhrase(const QString &phrase, bool isRegex,
bool isCaseSensitive)
{
return HighlightPhrase(phrase, false, false, isRegex, isCaseSensitive, "",
QColor());
}
} // namespace
TEST(HighlightPhrase, Normal)
{
constexpr bool isCaseSensitive = false;
constexpr bool isRegex = false;
auto p = buildHighlightPhrase("test", isRegex, isCaseSensitive);
EXPECT_TRUE(p.isMatch("test"));
EXPECT_TRUE(p.isMatch("TEst"));
EXPECT_TRUE(p.isMatch("foo tEst"));
EXPECT_TRUE(p.isMatch("foo teSt bar"));
EXPECT_TRUE(p.isMatch("test bar"));
EXPECT_TRUE(p.isMatch("!teSt"));
EXPECT_TRUE(p.isMatch("test!"));
EXPECT_FALSE(p.isMatch("testbar"));
EXPECT_FALSE(p.isMatch("footest"));
EXPECT_FALSE(p.isMatch("footestbar"));
p = buildHighlightPhrase("!test", isRegex, isCaseSensitive);
EXPECT_TRUE(p.isMatch("!test"));
EXPECT_TRUE(p.isMatch("foo !test"));
EXPECT_TRUE(p.isMatch("foo !test bar"));
EXPECT_TRUE(p.isMatch("!test bar"));
EXPECT_TRUE(p.isMatch("!test"));
EXPECT_FALSE(p.isMatch("test!"));
EXPECT_FALSE(p.isMatch("!testbar"));
EXPECT_TRUE(p.isMatch(
"foo!test")); // Consequence of matching on ! is that before the ! there will be a word boundary, assuming text is smashed right before it EXPECT_FALSE(p.isMatch("foo!testbar"));
EXPECT_FALSE(p.isMatch("footest!bar"));
p = buildHighlightPhrase("test!", isRegex, isCaseSensitive);
EXPECT_TRUE(p.isMatch("test!"));
EXPECT_TRUE(p.isMatch("foo test!"));
EXPECT_TRUE(p.isMatch("foo test! bar"));
EXPECT_TRUE(p.isMatch("test! bar"));
EXPECT_TRUE(p.isMatch("test!"));
EXPECT_FALSE(p.isMatch("test"));
EXPECT_TRUE(p.isMatch(
"test!bar")); // Consequence of matching on ! is that before the ! there will be a word boundary, assuming text is smashed right before it
EXPECT_FALSE(p.isMatch("footest!"));
EXPECT_FALSE(p.isMatch("footest!bar"));
}
TEST(HighlightPhrase, CaseSensitive)
{
constexpr bool isCaseSensitive = true;
constexpr bool isRegex = false;
using namespace chatterino;
auto p = buildHighlightPhrase("test", isRegex, isCaseSensitive);
EXPECT_TRUE(p.isMatch("test"));
EXPECT_TRUE(p.isMatch("test"));
EXPECT_TRUE(p.isMatch("foo test"));
EXPECT_TRUE(p.isMatch("foo test bar"));
EXPECT_FALSE(p.isMatch("TEst"));
EXPECT_FALSE(p.isMatch("foo tEst"));
EXPECT_FALSE(p.isMatch("foo teSt bar"));
EXPECT_TRUE(p.isMatch("test bar"));
EXPECT_TRUE(p.isMatch("!test"));
EXPECT_FALSE(p.isMatch("!teSt"));
EXPECT_TRUE(p.isMatch("test!"));
EXPECT_FALSE(p.isMatch("testbar"));
EXPECT_FALSE(p.isMatch("footest"));
EXPECT_FALSE(p.isMatch("footestbar"));
p = buildHighlightPhrase("!test", isRegex, isCaseSensitive);
EXPECT_FALSE(p.isMatch("!teSt"));
EXPECT_TRUE(p.isMatch("!test"));
EXPECT_TRUE(p.isMatch("foo !test"));
EXPECT_TRUE(p.isMatch("foo !test bar"));
EXPECT_TRUE(p.isMatch("!test bar"));
EXPECT_TRUE(p.isMatch("!test"));
EXPECT_FALSE(p.isMatch("test!"));
EXPECT_FALSE(p.isMatch("!testbar"));
EXPECT_TRUE(p.isMatch(
"foo!test")); // Consequence of matching on ! is that before the ! there will be a word boundary, assuming text is smashed right before it EXPECT_FALSE(p.isMatch("foo!testbar"));
EXPECT_FALSE(p.isMatch("footest!bar"));
p = buildHighlightPhrase("test!", isRegex, isCaseSensitive);
EXPECT_TRUE(p.isMatch("test!"));
EXPECT_FALSE(p.isMatch("teSt!"));
EXPECT_TRUE(p.isMatch("foo test!"));
EXPECT_TRUE(p.isMatch("foo test! bar"));
EXPECT_TRUE(p.isMatch("test! bar"));
EXPECT_TRUE(p.isMatch("test!"));
EXPECT_FALSE(p.isMatch("test"));
EXPECT_TRUE(p.isMatch(
"test!bar")); // Consequence of matching on ! is that before the ! there will be a word boundary, assuming text is smashed right before it
EXPECT_FALSE(p.isMatch("footest!"));
EXPECT_FALSE(p.isMatch("footest!bar"));
}
TEST(HighlightPhrase, Regex)
{
constexpr bool isCaseSensitive = false;
constexpr bool isRegex = true;
using namespace chatterino;
auto p = buildHighlightPhrase("[a-z]+", isRegex, isCaseSensitive);
EXPECT_TRUE(p.isMatch("foo"));
EXPECT_TRUE(p.isMatch("foo bar"));
EXPECT_FALSE(p.isMatch("!"));
p = buildHighlightPhrase("^[a-z]+$", isRegex, isCaseSensitive);
EXPECT_TRUE(p.isMatch("foo"));
EXPECT_FALSE(p.isMatch("foo bar"));
EXPECT_FALSE(p.isMatch("!"));
p = buildHighlightPhrase("^[a-z]+ [a-z]+", isRegex, isCaseSensitive);
EXPECT_FALSE(p.isMatch("foo"));
EXPECT_TRUE(p.isMatch("foo bar"));
EXPECT_TRUE(p.isMatch("foo bar baz"));
EXPECT_FALSE(p.isMatch("!foo bar"));
EXPECT_FALSE(p.isMatch("!"));
}