aboutsummaryrefslogtreecommitdiff
path: root/lib/urlfilter/test/matcher.cpp
blob: 1c1efbfd981d0905d8e94e0da0f9cd318e826ebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "urlfilter.h"
#include "matcher.h"
#include <gtest/gtest.h>

TEST(Matcher, StringContains) {
    ContentsMatcher<QStringMatcher> matcher("spam-pattern", UrlFilter::StringContains);
    EXPECT_TRUE(matcher.hasMatch("this string contains a spam-pattern"));
    EXPECT_FALSE(matcher.hasMatch("this string does not contain the pattern"));
}

TEST(Matcher, StringStartsWith) {
    ContentsMatcher<QStringMatcher> matcher("beginning", UrlFilter::StringStartsWith);
    EXPECT_TRUE(matcher.hasMatch("beginning this string is the pattern"));
    EXPECT_FALSE(matcher.hasMatch("ending this string is the pattern, the word beginning"));
    EXPECT_FALSE(matcher.hasMatch("this would be a string where the pattern cannot be found"));
}

TEST(Matcher, StringEndsWith) {
    ContentsMatcher<QStringMatcher> matcher("ending", UrlFilter::StringEndsWith);
    EXPECT_TRUE(matcher.hasMatch("this string has the proper ending"));
    EXPECT_FALSE(matcher.hasMatch("and this string doesn't"));
}

TEST(Matcher, StringEquals) {
    ContentsMatcher<QStringMatcher> matcher("string-to-match", UrlFilter::StringEquals);
    EXPECT_TRUE(matcher.hasMatch("string-to-match"));
    EXPECT_FALSE(matcher.hasMatch("same-len-string"));
    EXPECT_FALSE(matcher.hasMatch("not the string-to-match"));
}

TEST(Matcher, RegularExpression) {
    ContentsMatcher<QRegularExpression> matcher("banner\\d+", UrlFilter::RegularExpressionMatch);
    EXPECT_TRUE(matcher.hasMatch("http://another.com/banner123"));
    EXPECT_TRUE(matcher.hasMatch("http://another.com/banner321"));
    EXPECT_FALSE(matcher.hasMatch("http://another.com/banners"));

}

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}