tests: add unit tests for match engine types (#13903)

* tests: add unit tests for match engine types

* clang-format: fix formatting in TagMatchEngine test
This commit is contained in:
Ioannis Tzavaras
2026-03-31 16:38:18 +01:00
committed by GitHub
parent fdd2607b9b
commit 179e1de507
4 changed files with 259 additions and 0 deletions
@@ -0,0 +1,70 @@
#include <desktop/rule/matchEngine/BoolMatchEngine.hpp>
#include <gtest/gtest.h>
using namespace Desktop::Rule;
TEST(BoolMatchEngine, truthyStringOne) {
CBoolMatchEngine engine("1");
EXPECT_TRUE(engine.match(true));
EXPECT_FALSE(engine.match(false));
}
TEST(BoolMatchEngine, truthyStringTrue) {
CBoolMatchEngine engine("true");
EXPECT_TRUE(engine.match(true));
EXPECT_FALSE(engine.match(false));
}
TEST(BoolMatchEngine, truthyStringYes) {
CBoolMatchEngine engine("yes");
EXPECT_TRUE(engine.match(true));
EXPECT_FALSE(engine.match(false));
}
TEST(BoolMatchEngine, truthyStringOn) {
CBoolMatchEngine engine("on");
EXPECT_TRUE(engine.match(true));
EXPECT_FALSE(engine.match(false));
}
TEST(BoolMatchEngine, truthyCaseInsensitive) {
CBoolMatchEngine upper("TRUE");
EXPECT_TRUE(upper.match(true));
CBoolMatchEngine mixed("Yes");
EXPECT_TRUE(mixed.match(true));
CBoolMatchEngine onUpper("ON");
EXPECT_TRUE(onUpper.match(true));
}
TEST(BoolMatchEngine, falsyStringZero) {
CBoolMatchEngine engine("0");
EXPECT_TRUE(engine.match(false));
EXPECT_FALSE(engine.match(true));
}
TEST(BoolMatchEngine, falsyStringFalse) {
CBoolMatchEngine engine("false");
EXPECT_TRUE(engine.match(false));
EXPECT_FALSE(engine.match(true));
}
TEST(BoolMatchEngine, falsyStringNo) {
CBoolMatchEngine engine("no");
EXPECT_TRUE(engine.match(false));
EXPECT_FALSE(engine.match(true));
}
TEST(BoolMatchEngine, falsyStringEmpty) {
CBoolMatchEngine engine("");
EXPECT_TRUE(engine.match(false));
EXPECT_FALSE(engine.match(true));
}
TEST(BoolMatchEngine, falsyStringArbitrary) {
CBoolMatchEngine engine("banana");
EXPECT_TRUE(engine.match(false));
EXPECT_FALSE(engine.match(true));
}
@@ -0,0 +1,52 @@
#include <desktop/rule/matchEngine/IntMatchEngine.hpp>
#include <gtest/gtest.h>
using namespace Desktop::Rule;
TEST(IntMatchEngine, positiveInteger) {
CIntMatchEngine engine("42");
EXPECT_TRUE(engine.match(42));
EXPECT_FALSE(engine.match(41));
EXPECT_FALSE(engine.match(0));
}
TEST(IntMatchEngine, zero) {
CIntMatchEngine engine("0");
EXPECT_TRUE(engine.match(0));
EXPECT_FALSE(engine.match(1));
}
TEST(IntMatchEngine, negativeInteger) {
CIntMatchEngine engine("-5");
EXPECT_TRUE(engine.match(-5));
EXPECT_FALSE(engine.match(5));
}
TEST(IntMatchEngine, invalidStringDefaultsToZero) {
CIntMatchEngine engine("abc");
EXPECT_TRUE(engine.match(0));
EXPECT_FALSE(engine.match(1));
}
TEST(IntMatchEngine, emptyStringDefaultsToZero) {
CIntMatchEngine engine("");
EXPECT_TRUE(engine.match(0));
EXPECT_FALSE(engine.match(1));
}
TEST(IntMatchEngine, leadingWhitespace) {
CIntMatchEngine engine(" 123");
EXPECT_TRUE(engine.match(123));
}
TEST(IntMatchEngine, trailingNonDigits) {
CIntMatchEngine engine("123abc");
EXPECT_TRUE(engine.match(123));
}
TEST(IntMatchEngine, largeValue) {
CIntMatchEngine engine("999999");
EXPECT_TRUE(engine.match(999999));
EXPECT_FALSE(engine.match(0));
}
@@ -0,0 +1,69 @@
#include <desktop/rule/matchEngine/RegexMatchEngine.hpp>
#include <gtest/gtest.h>
using namespace Desktop::Rule;
TEST(RegexMatchEngine, exactMatch) {
CRegexMatchEngine engine("firefox");
EXPECT_TRUE(engine.match("firefox"));
EXPECT_FALSE(engine.match("Firefox"));
EXPECT_FALSE(engine.match("firefox2"));
}
TEST(RegexMatchEngine, wildcardPattern) {
CRegexMatchEngine engine("fire.*");
EXPECT_TRUE(engine.match("firefox"));
EXPECT_TRUE(engine.match("firewall"));
EXPECT_FALSE(engine.match("ice"));
}
TEST(RegexMatchEngine, fullMatchRequired) {
CRegexMatchEngine engine("fire");
EXPECT_TRUE(engine.match("fire"));
EXPECT_FALSE(engine.match("firefox"));
EXPECT_FALSE(engine.match("campfire"));
}
TEST(RegexMatchEngine, characterClass) {
CRegexMatchEngine engine("kitty_[ABC]");
EXPECT_TRUE(engine.match("kitty_A"));
EXPECT_TRUE(engine.match("kitty_B"));
EXPECT_TRUE(engine.match("kitty_C"));
EXPECT_FALSE(engine.match("kitty_D"));
}
TEST(RegexMatchEngine, negativePrefix) {
CRegexMatchEngine engine("negative:firefox");
EXPECT_FALSE(engine.match("firefox"));
EXPECT_TRUE(engine.match("chromium"));
EXPECT_TRUE(engine.match("anything"));
}
TEST(RegexMatchEngine, negativeWithWildcard) {
CRegexMatchEngine engine("negative:.*\\.tmp");
EXPECT_FALSE(engine.match("file.tmp"));
EXPECT_TRUE(engine.match("file.txt"));
EXPECT_TRUE(engine.match("file.cpp"));
}
TEST(RegexMatchEngine, emptyPattern) {
CRegexMatchEngine engine("");
EXPECT_TRUE(engine.match(""));
EXPECT_FALSE(engine.match("anything"));
}
TEST(RegexMatchEngine, dotMatchesSingleChar) {
CRegexMatchEngine engine("a.c");
EXPECT_TRUE(engine.match("abc"));
EXPECT_TRUE(engine.match("axc"));
EXPECT_FALSE(engine.match("ac"));
EXPECT_FALSE(engine.match("abbc"));
}
TEST(RegexMatchEngine, alternation) {
CRegexMatchEngine engine("cat|dog");
EXPECT_TRUE(engine.match("cat"));
EXPECT_TRUE(engine.match("dog"));
EXPECT_FALSE(engine.match("bird"));
}
@@ -0,0 +1,68 @@
#include <desktop/rule/matchEngine/TagMatchEngine.hpp>
#include <helpers/TagKeeper.hpp>
#include <gtest/gtest.h>
using namespace Desktop::Rule;
TEST(TagMatchEngine, matchesExistingTag) {
CTagKeeper keeper;
keeper.applyTag("myTag");
CTagMatchEngine engine("myTag");
EXPECT_TRUE(engine.match(keeper));
}
TEST(TagMatchEngine, doesNotMatchMissingTag) {
CTagKeeper keeper;
keeper.applyTag("otherTag");
CTagMatchEngine engine("myTag");
EXPECT_FALSE(engine.match(keeper));
}
TEST(TagMatchEngine, emptyKeeper) {
CTagKeeper keeper;
CTagMatchEngine engine("myTag");
EXPECT_FALSE(engine.match(keeper));
}
TEST(TagMatchEngine, negativeTagMatching) {
CTagKeeper keeper;
keeper.applyTag("myTag");
CTagMatchEngine negative("negative:myTag");
EXPECT_FALSE(negative.match(keeper));
CTagMatchEngine negativeOther("negative:otherTag");
EXPECT_TRUE(negativeOther.match(keeper));
}
TEST(TagMatchEngine, dynamicTagMatchesWithoutStrict) {
CTagKeeper keeper;
keeper.applyTag("myTag", true); // dynamic adds "*" suffix
CTagMatchEngine engine("myTag");
EXPECT_TRUE(engine.match(keeper));
}
TEST(TagMatchEngine, caseSensitive) {
CTagKeeper keeper;
keeper.applyTag("MyTag");
CTagMatchEngine lower("mytag");
EXPECT_FALSE(lower.match(keeper));
CTagMatchEngine exact("MyTag");
EXPECT_TRUE(exact.match(keeper));
}
TEST(TagMatchEngine, tagRemoval) {
CTagKeeper keeper;
keeper.applyTag("myTag");
keeper.applyTag("-myTag");
CTagMatchEngine engine("myTag");
EXPECT_FALSE(engine.match(keeper));
}