aboutsummaryrefslogtreecommitdiff
path: root/staging/adblock/test/rule.cpp
blob: 07186b9159a739c4f70d06210cc0b5d067be034f (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#define CATCH_CONFIG_MAIN
#include "rule.h"
#include <catch2/catch.hpp>

using namespace AdblockPlus;

SCENARIO("MatcherRule")
{
    GIVEN("options with case sensitive pattern")
    {
        const QString defaultUrl = "";

        const Options opt { .matchcase=true };
        const QString patternContains("this string contains the pattern in it");
        const QString patternBegins("pattern starts this string");
        const QString patternEnds("this string ends with pattern");
        const QString patternMissing("and this one does not");

        WHEN("contains")
        {
            MatcherRule rule("pattern", opt);
            REQUIRE(rule.shouldBlock());

            THEN("pattern is matched anywhere in the URL")
            {
                REQUIRE(rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl));
                REQUIRE(rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl));
                REQUIRE(rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl));
                REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl));
            }
        }

        WHEN("startsWith")
        {
            MatcherRule rule("pattern", opt, MatcherRule::UrlStartsWith);
            REQUIRE(rule.shouldBlock());

            THEN("pattern is matched if at the start of the URL")
            {
                REQUIRE(!rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl));
                REQUIRE(rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl));
                REQUIRE(!rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl));
                REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl));
            }
        }

        WHEN("endsWith")
        {
            MatcherRule rule("pattern", opt, MatcherRule::UrlEndsWith);
            REQUIRE(rule.shouldBlock());

            THEN("pattern is matched if at the end of the URL")
            {
                REQUIRE(!rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl));
                REQUIRE(!rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl));
                REQUIRE(rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl));
                REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl));
            }
        }
    }
}

SCENARIO("RegexRule")
{
    GIVEN("options with case sensitive pattern")
    {
        const QString defaultUrl;

        const Options opt { .matchcase=true };
        const QString patternContains("this string contains the pattern in it");
        const QString patternMissing("and this one does not");

        WHEN("contains")
        {
            RegexRule rule("pattern", opt);
            REQUIRE(rule.shouldBlock());

            THEN("pattern is matched anywhere in the URL")
            {
                REQUIRE(rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl));
                REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl));
            }
        }
    }
}