From e93a940e06dc80809567c4314864c834745c1982 Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Tue, 12 Mar 2019 22:39:36 +0200 Subject: [core] Add unit test for FormatSectionOverrides --- test/style/style_layer.test.cpp | 87 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/test/style/style_layer.test.cpp b/test/style/style_layer.test.cpp index 50aa643b50..e58a5fe5d0 100644 --- a/test/style/style_layer.test.cpp +++ b/test/style/style_layer.test.cpp @@ -1,6 +1,5 @@ -#include -#include -#include +#include +#include #include #include #include @@ -16,6 +15,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -25,6 +27,9 @@ using namespace mbgl; using namespace mbgl::style; +using namespace expression; +using namespace expression::dsl; +using namespace std::literals::string_literals; namespace { @@ -50,6 +55,10 @@ const auto saturation = 1.0f; const auto contrast = 1.0f; const auto duration = 1.0f; +class MockLayoutProperties : public Properties {}; +class MockPaintProperties : public Properties {}; +using MockOverrides = FormatSectionOverrides; + } // namespace TEST(Layer, BackgroundProperties) { @@ -291,3 +300,75 @@ TEST(Layer, DuplicateLayer) { } } +namespace { + +template class PropertyValueType, typename LayoutType> +void testHasOverrides(LayoutType& layout) { + // Undefined + layout.template get() = PropertyValueType(); + EXPECT_FALSE(MockOverrides::hasOverrides(layout.template get())); + + // Constant, no overrides. + layout.template get() = PropertyValueType(Formatted("")); + EXPECT_FALSE(MockOverrides::hasOverrides(layout.template get())); + + // Constant, overridden text-color. + auto formatted = Formatted(""); + formatted.sections.emplace_back("section text"s, nullopt, nullopt, Color::green()); + layout.template get() = PropertyValueType(std::move(formatted)); + EXPECT_TRUE(MockOverrides::hasOverrides(layout.template get())); + + // Expression, no overrides. + auto formatExpr = std::make_unique(std::vector{}); + PropertyExpression propExpr(std::move(formatExpr)); + layout.template get() = PropertyValueType(std::move(propExpr)); + EXPECT_FALSE(MockOverrides::hasOverrides(layout.template get())); + + // Expression, overridden text-color. + FormatExpressionSection section(literal(""), nullopt, nullopt, toColor(literal("red"))); + auto formatExprOverride = std::make_unique(std::vector{section}); + PropertyExpression propExprOverride(std::move(formatExprOverride)); + layout.template get() = PropertyValueType(std::move(propExprOverride)); + EXPECT_TRUE(MockOverrides::hasOverrides(layout.template get())); +} + +} // namespace + +TEST(Layer, SymbolLayerOverrides) { + + // Unevaluated / transitionable. + { + MockLayoutProperties::Unevaluated layout; + testHasOverrides(layout); + + MockPaintProperties::Transitionable current; + MockPaintProperties::Transitionable updated; + current.get() = Transitionable>{{Color::green()}, {}}; + updated.get() = Transitionable>{{Color::green()}, {}}; + EXPECT_FALSE(MockOverrides::hasPaintPropertyDifference(current, updated)); + + current.get() = Transitionable>{{Color::red()}, {}}; + EXPECT_TRUE(MockOverrides::hasPaintPropertyDifference(current, updated)); + } + + // Possibly evaluated. + { + MockLayoutProperties::PossiblyEvaluated layout; + MockPaintProperties::PossiblyEvaluated paint; + testHasOverrides(layout); + + // Constant, overridden text-color. + auto formatted = Formatted(""); + formatted.sections.emplace_back("section text"s, nullopt, nullopt, Color::green()); + layout.get() = PossiblyEvaluatedPropertyValue(std::move(formatted)); + paint.get() = PossiblyEvaluatedPropertyValue{Color::red()}; + EXPECT_TRUE(paint.get().isConstant()); + MockOverrides::setOverrides(layout, paint); + EXPECT_FALSE(paint.get().isConstant()); + + MockPaintProperties::PossiblyEvaluated updated; + updated.get() = PossiblyEvaluatedPropertyValue{Color::red()}; + MockOverrides::updateOverrides(paint, updated); + EXPECT_FALSE(updated.get().isConstant()); + } +} -- cgit v1.2.1