Skip to content

Commit

Permalink
css: Add an always-false media query type
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Nov 9, 2023
1 parent 500f366 commit 4045095
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion css/media_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ struct Context {
int window_width{};
};

struct False {
[[nodiscard]] bool operator==(False const &) const = default;
constexpr bool evaluate(Context const &) const { return false; }
};

struct Width {
int min{};
int max{std::numeric_limits<int>::max()};
Expand All @@ -39,9 +44,10 @@ struct Width {
class MediaQuery {
public:
using Context = detail::Context;
using False = detail::False;
using Width = detail::Width;

using Query = std::variant<Width>;
using Query = std::variant<False, Width>;
Query query{};
[[nodiscard]] bool operator==(MediaQuery const &) const = default;

Expand Down Expand Up @@ -110,6 +116,10 @@ inline std::string to_string(MediaQuery::Width const &width) {
return std::to_string(width.min) + " <= width <= " + std::to_string(width.max);
}

constexpr std::string to_string(MediaQuery::False const &) {
return "false";
}

constexpr std::string to_string(MediaQuery const &query) {
return std::visit([](auto const &q) { return to_string(q); }, query.query);
}
Expand Down
11 changes: 11 additions & 0 deletions css/media_query_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ void to_string_tests() {
expect_eq(css::to_string(css::MediaQuery::Width{.min = 299, .max = 301}), //
"299 <= width <= 301");
});

etest::test("to_string: false", [] {
expect_eq(css::to_string(css::MediaQuery::False{}), "false"); //
});
}

void false_tests() {
etest::test("false", [] {
expect(!css::MediaQuery::False{}.evaluate({.window_width = 299})); //
});
}

void width_tests() {
Expand Down Expand Up @@ -91,6 +101,7 @@ void width_tests() {
int main() {
parser_tests();
to_string_tests();
false_tests();
width_tests();
return etest::run_all_tests();
}

0 comments on commit 4045095

Please sign in to comment.