diff options
author | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2016-08-23 14:57:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-23 14:57:55 +0200 |
commit | df3b44531e1c2a95edd2a035d3744f34ebb8d0e9 (patch) | |
tree | 2eb0ae3e4a3b793767643831cd8063aeab56075d /test/src | |
parent | 50da6e5e715f0356f430fba176dea13d15fe9d52 (diff) | |
download | qtlocation-mapboxgl-df3b44531e1c2a95edd2a035d3744f34ebb8d0e9.tar.gz |
[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
Diffstat (limited to 'test/src')
-rw-r--r-- | test/src/mbgl/test/conversion_stubs.hpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/test/src/mbgl/test/conversion_stubs.hpp b/test/src/mbgl/test/conversion_stubs.hpp new file mode 100644 index 0000000000..60f110ea0d --- /dev/null +++ b/test/src/mbgl/test/conversion_stubs.hpp @@ -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 |