Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some more filter tests #4897

Merged
merged 3 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -679,15 +679,15 @@ endif()
if (CHATTERINO_GENERATE_COVERAGE)
include(CodeCoverage)
append_coverage_compiler_flags_to_target(${LIBRARY_PROJECT})
target_link_libraries(${LIBRARY_PROJECT} PUBLIC gcov)
message(STATUS "project source dir: ${PROJECT_SOURCE_DIR}/src")
setup_target_for_coverage_lcov(
setup_target_for_coverage_gcovr_html(
NAME coverage
EXECUTABLE ./bin/chatterino-test
BASE_DIRECTORY ${PROJECT_SOURCE_DIR}/src
EXECUTABLE ctest
EXCLUDE "/usr/include/*"
EXCLUDE "build-*/*"
EXCLUDE "lib/*"
EXCLUDE "*/ui_*.h"
EXCLUDE "*/moc_*.cpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also exclude */*.moc.

)
endif ()

Expand Down
20 changes: 0 additions & 20 deletions src/controllers/filters/lang/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,4 @@

namespace chatterino::filters {

QVariant Expression::execute(const ContextMap & /*context*/) const
{
return false;
}

PossibleType Expression::synthesizeType(const TypingContext & /*context*/) const
{
return IllTyped{this, "Not implemented"};
}

QString Expression::debug(const TypingContext & /*context*/) const
{
return "";
}

QString Expression::filterString() const
{
return "";
}

} // namespace chatterino::filters
8 changes: 4 additions & 4 deletions src/controllers/filters/lang/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class Expression
public:
virtual ~Expression() = default;

virtual QVariant execute(const ContextMap &context) const;
virtual PossibleType synthesizeType(const TypingContext &context) const;
virtual QString debug(const TypingContext &context) const;
virtual QString filterString() const;
virtual QVariant execute(const ContextMap &context) const = 0;
virtual PossibleType synthesizeType(const TypingContext &context) const = 0;
virtual QString debug(const TypingContext &context) const = 0;
virtual QString filterString() const = 0;
};

using ExpressionPtr = std::unique_ptr<Expression>;
Expand Down
86 changes: 86 additions & 0 deletions tests/src/Filters.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "controllers/accounts/AccountController.hpp"
#include "controllers/filters/lang/expressions/UnaryOperation.hpp"
#include "controllers/filters/lang/Filter.hpp"
#include "controllers/filters/lang/Types.hpp"
#include "controllers/highlights/HighlightController.hpp"
Expand Down Expand Up @@ -285,3 +286,88 @@ TEST_F(FiltersF, TypingContextChecks)

delete privmsg;
}

TEST_F(FiltersF, ExpressionDebug)
{
struct TestCase {
QString input;
QString debugString;
QString filterString;
};

// clang-format off
std::vector<TestCase> tests{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'tests' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<TestCase> tests{
std::vector<TestCase> const tests{

{
.input = R".(1 + 1).",
.debugString = "BinaryOp[Plus](Val(1) : Int, Val(1) : Int)",
.filterString = "(1 + 1)",
},
{
.input = R".(author.color == "#ff0000").",
.debugString = "BinaryOp[Eq](Val(author.color) : Color, Val(#ff0000) : String)",
.filterString = R".((author.color == "#ff0000")).",
},
{
.input = R".(1).",
.debugString = "Val(1)",
.filterString = R".(1).",
},
{
.input = R".("asd").",
.debugString = R".(Val(asd)).",
.filterString = R".("asd").",
},
{
.input = R".(("asd")).",
.debugString = R".(Val(asd)).",
.filterString = R".("asd").",
},
{
.input = R".(author.subbed).",
.debugString = R".(Val(author.subbed)).",
.filterString = R".(author.subbed).",
},
{
.input = R".(!author.subbed).",
.debugString = R".(UnaryOp[Not](Val(author.subbed) : Bool)).",
.filterString = R".((!author.subbed)).",
},
{
.input = R".({"foo", "bar"} contains "foo").",
.debugString = R".(BinaryOp[Contains](List(Val(foo) : String, Val(bar) : String) : StringList, Val(foo) : String)).",
.filterString = R".(({"foo", "bar"} contains "foo")).",
},
{
.input = R".(!({"foo", "bar"} contains "foo")).",
.debugString = R".(UnaryOp[Not](BinaryOp[Contains](List(Val(foo) : String, Val(bar) : String) : StringList, Val(foo) : String) : Bool)).",
.filterString = R".((!({"foo", "bar"} contains "foo"))).",
},
{
.input = R".(message.content match r"(\d\d)/(\d\d)/(\d\d\d\d)").",
.debugString = R".(BinaryOp[Match](Val(message.content) : String, RegEx((\d\d)/(\d\d)/(\d\d\d\d)) : RegularExpression)).",
.filterString = R".((message.content match r"(\d\d)/(\d\d)/(\d\d\d\d)")).",
},
};
// clang-format on

for (const auto &[input, debugString, filterString] : tests)
{
const auto filterResult = Filter::fromString(input);
const auto *filter = std::get_if<Filter>(&filterResult);
EXPECT_NE(filter, nullptr)
<< "Filter::fromString(" << qUtf8Printable(input)
<< ") did not build a proper filter";

const auto actualDebugString = filter->debugString(typingContext);
EXPECT_EQ(actualDebugString, debugString)
<< "filter->debugString() on '" << qUtf8Printable(input)
<< "' should be '" << qUtf8Printable(debugString) << "', but got '"
<< qUtf8Printable(actualDebugString) << "'";

const auto actualFilterString = filter->filterString();
EXPECT_EQ(actualFilterString, filterString)
<< "filter->filterString() on '" << qUtf8Printable(input)
<< "' should be '" << qUtf8Printable(filterString) << "', but got '"
<< qUtf8Printable(actualFilterString) << "'";
}
}