diff options
| author | Ioannis Tzavaras <tzavaras.jannis@gmail.com> | 2026-03-31 18:38:18 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-31 16:38:18 +0100 |
| commit | 179e1de50715bbb99bac15a5d38b34275106ea67 (patch) | |
| tree | 0734bd8f882858347a2baf0a4a0385bd203b5d75 | |
| parent | fdd2607b9b3e976f6dca1e04a8f567a7df23f1a6 (diff) | |
tests: add unit tests for match engine types (#13903)
* tests: add unit tests for match engine types
* clang-format: fix formatting in TagMatchEngine test
| -rw-r--r-- | tests/desktop/rule/matchEngine/BoolMatchEngine.cpp | 70 | ||||
| -rw-r--r-- | tests/desktop/rule/matchEngine/IntMatchEngine.cpp | 52 | ||||
| -rw-r--r-- | tests/desktop/rule/matchEngine/RegexMatchEngine.cpp | 69 | ||||
| -rw-r--r-- | tests/desktop/rule/matchEngine/TagMatchEngine.cpp | 68 |
4 files changed, 259 insertions, 0 deletions
diff --git a/tests/desktop/rule/matchEngine/BoolMatchEngine.cpp b/tests/desktop/rule/matchEngine/BoolMatchEngine.cpp new file mode 100644 index 00000000..99f6d99c --- /dev/null +++ b/tests/desktop/rule/matchEngine/BoolMatchEngine.cpp @@ -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)); +} diff --git a/tests/desktop/rule/matchEngine/IntMatchEngine.cpp b/tests/desktop/rule/matchEngine/IntMatchEngine.cpp new file mode 100644 index 00000000..a5fb13ea --- /dev/null +++ b/tests/desktop/rule/matchEngine/IntMatchEngine.cpp @@ -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)); +} diff --git a/tests/desktop/rule/matchEngine/RegexMatchEngine.cpp b/tests/desktop/rule/matchEngine/RegexMatchEngine.cpp new file mode 100644 index 00000000..daaed29b --- /dev/null +++ b/tests/desktop/rule/matchEngine/RegexMatchEngine.cpp @@ -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")); +} diff --git a/tests/desktop/rule/matchEngine/TagMatchEngine.cpp b/tests/desktop/rule/matchEngine/TagMatchEngine.cpp new file mode 100644 index 00000000..c6fe90ef --- /dev/null +++ b/tests/desktop/rule/matchEngine/TagMatchEngine.cpp @@ -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)); +} |
