aboutsummaryrefslogtreecommitdiff
path: root/staging/adblock/test/rules.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-04-14 16:39:34 +0300
committerAqua-sama <aqua@iserlohn-fortress.net>2020-04-21 20:14:56 +0300
commit3e29d04bc564f89c94d1e3375de54870e03df7b1 (patch)
treea174d301eaa955ebebdddd7202cbb99039478a04 /staging/adblock/test/rules.cpp
parentMove smolbote headers to include/smolbote (diff)
downloadsmolbote-3e29d04bc564f89c94d1e3375de54870e03df7b1.tar.xz
Add tests for MatcherRule and RegexRule
Diffstat (limited to 'staging/adblock/test/rules.cpp')
-rw-r--r--staging/adblock/test/rules.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/staging/adblock/test/rules.cpp b/staging/adblock/test/rules.cpp
new file mode 100644
index 0000000..d192601
--- /dev/null
+++ b/staging/adblock/test/rules.cpp
@@ -0,0 +1,80 @@
+#define CATCH_CONFIG_MAIN
+#include "rule.h"
+#include <catch2/catch.hpp>
+
+SCENARIO("MatcherRule")
+{
+ GIVEN("options with case sensitive pattern")
+ {
+ const AdblockPlus::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")
+ {
+ AdblockPlus::MatcherRule rule("pattern", opt);
+ REQUIRE(rule.shouldBlock());
+
+ THEN("pattern is matched anywhere in the URL")
+ {
+ REQUIRE(rule.hasMatch(&patternContains));
+ REQUIRE(rule.hasMatch(&patternBegins));
+ REQUIRE(rule.hasMatch(&patternEnds));
+ REQUIRE(!rule.hasMatch(&patternMissing));
+ }
+ }
+
+ WHEN("startsWith")
+ {
+ AdblockPlus::MatcherRule rule("pattern", opt, AdblockPlus::MatcherRule::UrlStartsWith);
+ REQUIRE(rule.shouldBlock());
+
+ THEN("pattern is matched if at the start of the URL")
+ {
+ REQUIRE(!rule.hasMatch(&patternContains));
+ REQUIRE(rule.hasMatch(&patternBegins));
+ REQUIRE(!rule.hasMatch(&patternEnds));
+ REQUIRE(!rule.hasMatch(&patternMissing));
+ }
+ }
+
+ WHEN("endsWith")
+ {
+ AdblockPlus::MatcherRule rule("pattern", opt, AdblockPlus::MatcherRule::UrlEndsWith);
+ REQUIRE(rule.shouldBlock());
+
+ THEN("pattern is matched if at the end of the URL")
+ {
+ REQUIRE(!rule.hasMatch(&patternContains));
+ REQUIRE(!rule.hasMatch(&patternBegins));
+ REQUIRE(rule.hasMatch(&patternEnds));
+ REQUIRE(!rule.hasMatch(&patternMissing));
+ }
+ }
+ }
+}
+
+SCENARIO("RegexRule")
+{
+ GIVEN("options with case sensitive pattern")
+ {
+ const AdblockPlus::Options opt { .matchcase=true };
+ const QString patternContains("this string contains the pattern in it");
+ const QString patternMissing("and this one does not");
+
+ WHEN("contains")
+ {
+ AdblockPlus::RegexRule rule("pattern", opt);
+ REQUIRE(rule.shouldBlock());
+
+ THEN("pattern is matched anywhere in the URL")
+ {
+ REQUIRE(rule.hasMatch(&patternContains));
+ REQUIRE(!rule.hasMatch(&patternMissing));
+ }
+ }
+ }
+}
+