aboutsummaryrefslogtreecommitdiff
path: root/staging/adblock/test/rule.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-02-10 20:58:39 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-05-30 11:01:35 +0300
commitd1287f43964633035938f4f4d4133bb6d9da7b3e (patch)
treed09efa4074815c20be9bd6348203fe4336dfe716 /staging/adblock/test/rule.cpp
parentFix segfault in release build (diff)
downloadsmolbote-d1287f43964633035938f4f4d4133bb6d9da7b3e.tar.xz
staging: smolblok
smolblok is a replacement for the current lib/urlfilter AdBlockPlus and hostlist format filter parser. It is a library that uses plugins to provide support for different filter formats. staging/adblock: AdBlockPlus parser plugin plugins/smolblok_hostlist: hostlist format parser plugin Headers will be installed to include/smolbote/ Remove lib/urlfilter
Diffstat (limited to 'staging/adblock/test/rule.cpp')
-rw-r--r--staging/adblock/test/rule.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/staging/adblock/test/rule.cpp b/staging/adblock/test/rule.cpp
new file mode 100644
index 0000000..07186b9
--- /dev/null
+++ b/staging/adblock/test/rule.cpp
@@ -0,0 +1,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));
+ }
+ }
+ }
+}
+