[core] #6071 - extract GeoJSONOptions conversion from GeoJSONSource conversion
* [core] geojson_options - retain original error message * [core] tests - initial style conversion stub methods * [core] geojsonoptions conversion - initial unit tests * [core] tests - fix forward reference issue * [core] geojsonoptions conversion - unit tests * [core] geojsonoptions conversion - renamed Holder to Value
This commit is contained in:
111
test/src/mbgl/test/conversion_stubs.hpp
Normal file
111
test/src/mbgl/test/conversion_stubs.hpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/util/feature.hpp>
|
||||
#include <mbgl/util/optional.hpp>
|
||||
#include <mbgl/util/variant.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
namespace conversion {
|
||||
|
||||
class Value;
|
||||
using ValueMap = std::map<std::string, Value>;
|
||||
using ValueVector = std::vector<Value>;
|
||||
class Value : public mbgl::variant<std::string, float, bool, ValueMap, ValueVector> {
|
||||
using variant::variant;
|
||||
};
|
||||
|
||||
inline bool isUndefined(const Value&) {
|
||||
//Variant is always intialized
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool isArray(const Value& value) {
|
||||
return value.is<ValueVector>();
|
||||
}
|
||||
|
||||
inline std::size_t arrayLength(const Value& value) {
|
||||
return value.get<ValueVector>().size();
|
||||
}
|
||||
|
||||
inline Value arrayMember(const Value& value, std::size_t i) {
|
||||
return value.get<ValueVector>()[i];
|
||||
}
|
||||
|
||||
inline bool isObject(const Value& value) {
|
||||
return value.is<ValueMap>();
|
||||
}
|
||||
|
||||
inline optional<Value> objectMember(const Value& value, const char* key) {
|
||||
auto map = value.get<ValueMap>();
|
||||
auto iter = map.find(key);
|
||||
|
||||
if (iter != map.end()) {
|
||||
return iter->second;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
using EachMemberFn = std::function<optional<Error>(const std::string&, const Value&)>;
|
||||
|
||||
optional<Error> eachMember(const Value& value, EachMemberFn&& fn) {
|
||||
auto map = value.get<ValueMap>();
|
||||
auto iter = map.begin();
|
||||
|
||||
while (iter != map.end()) {
|
||||
optional<Error> result = fn(iter->first, iter->second);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
++iter;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
inline optional<bool> toBool(const Value& value) {
|
||||
if (value.is<bool>()) {
|
||||
return value.get<bool>();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<float> toNumber(const Value& value) {
|
||||
if (value.is<float>()) {
|
||||
return value.get<float>();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
inline optional<std::string> toString(const Value& value) {
|
||||
if (value.is<std::string>()) {
|
||||
return value.get<std::string>();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<mbgl::Value> toValue(const Value& value) {
|
||||
if (value.is<bool>()) {
|
||||
return { value.get<bool>() };
|
||||
} else if (value.is<std::string>()) {
|
||||
return { value.get<std::string>() };
|
||||
} else if (value.is<float>()) {
|
||||
return { value.get<float>() };
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
68
test/style/conversion/geojson_options.cpp
Normal file
68
test/style/conversion/geojson_options.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/conversion/geojson_options.hpp>
|
||||
#include <mbgl/test/conversion_stubs.hpp>
|
||||
|
||||
#include <mbgl/platform/log.hpp>
|
||||
|
||||
using namespace mbgl::style;
|
||||
using namespace mbgl::style::conversion;
|
||||
|
||||
TEST(GeoJSONOptions, Basic) {
|
||||
ValueMap map;
|
||||
Value raw(map);
|
||||
Result<GeoJSONOptions> converted = convert<GeoJSONOptions>(raw);
|
||||
ASSERT_TRUE((bool) converted);
|
||||
}
|
||||
|
||||
TEST(GeoJSONOptions, ErrorHandling) {
|
||||
ValueMap map {{"maxzoom", "should not be a string"}};
|
||||
Value raw(map);
|
||||
Result<GeoJSONOptions> converted = convert<GeoJSONOptions>(raw);
|
||||
ASSERT_FALSE((bool) converted);
|
||||
}
|
||||
|
||||
TEST(GeoJSONOptions, RetainsDefaults) {
|
||||
ValueMap map;
|
||||
Value raw(map);
|
||||
GeoJSONOptions converted = *convert<GeoJSONOptions>(raw);
|
||||
GeoJSONOptions defaults;
|
||||
|
||||
//GeoJSON-VT
|
||||
ASSERT_EQ(converted.maxzoom, defaults.maxzoom);
|
||||
ASSERT_EQ(converted.buffer, defaults.buffer);
|
||||
ASSERT_EQ(converted.tolerance, defaults.tolerance);
|
||||
|
||||
//Supercluster
|
||||
ASSERT_EQ(converted.cluster, defaults.cluster);
|
||||
ASSERT_EQ(converted.clusterRadius, defaults.clusterRadius);
|
||||
ASSERT_EQ(converted.clusterMaxZoom, defaults.clusterMaxZoom);
|
||||
}
|
||||
|
||||
|
||||
TEST(GeoJSONOptions, FullConversion) {
|
||||
ValueMap map {
|
||||
//GeoJSON-VT
|
||||
{"maxzoom", 1},
|
||||
{"buffer", 2},
|
||||
{"tolerance", 3},
|
||||
|
||||
//Supercluster
|
||||
{"cluster", true},
|
||||
{"clusterRadius", 4},
|
||||
{"clusterMaxZoom", 5}
|
||||
};
|
||||
Value raw(map);
|
||||
GeoJSONOptions converted = *convert<GeoJSONOptions>(raw);
|
||||
|
||||
//GeoJSON-VT
|
||||
ASSERT_EQ(converted.maxzoom, 1);
|
||||
ASSERT_EQ(converted.buffer, 2);
|
||||
ASSERT_EQ(converted.tolerance, 3);
|
||||
|
||||
//Supercluster
|
||||
ASSERT_EQ(converted.cluster, true);
|
||||
ASSERT_EQ(converted.clusterRadius, 4);
|
||||
ASSERT_EQ(converted.clusterMaxZoom, 5);
|
||||
}
|
||||
Reference in New Issue
Block a user