srctree

Robin Linden parent d0052ffb 1c08ceae
css: Implement to_string(MediaQuery)

inlinesplit
css/media_query.h added: 28, removed: 6, total 22
@@ -11,6 +11,7 @@
#include <charconv>
#include <limits>
#include <optional>
#include <string>
#include <string_view>
#include <variant>
 
@@ -104,6 +105,15 @@ public:
return std::visit([&](auto const &q) { return q.evaluate(ctx); }, query);
}
};
 
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 const &query) {
return std::visit([](auto const &q) { return to_string(q); }, query.query);
}
 
} // namespace css
 
#endif
 
css/media_query_test.cpp added: 28, removed: 6, total 22
@@ -45,6 +45,18 @@ void parser_tests() {
});
}
 
void to_string_tests() {
etest::test("to_string", [] {
expect_eq(css::to_string(css::MediaQuery::parse("(width: 300px)").value()), //
"300 <= width <= 300");
});
 
etest::test("to_string: width", [] {
expect_eq(css::to_string(css::MediaQuery::Width{.min = 299, .max = 301}), //
"299 <= width <= 301");
});
}
 
void width_tests() {
etest::test("width: width", [] {
expect_eq(css::MediaQuery::parse("(width: 300px)"),
@@ -78,6 +90,7 @@ void width_tests() {
 
int main() {
parser_tests();
to_string_tests();
width_tests();
return etest::run_all_tests();
}
 
css/rule.cpp added: 28, removed: 6, total 22
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2022 Mikael Larsson <c.mikael.larsson@gmail.com>
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -31,10 +32,8 @@ std::string to_string(Rule const &rule) {
ss << " " << to_string(property) << ": " << value << '\n';
}
if (rule.media_query.has_value()) {
// TODO(robinlinden): to_string for media queries.
auto query = std::get<MediaQuery::Width>(rule.media_query->query);
ss << "Media query:\n";
ss << " " << query.min << " <= width <= " << query.max << '\n';
ss << " " << to_string(*rule.media_query) << '\n';
}
return std::move(ss).str();
}