summaryrefslogtreecommitdiff
path: root/test/test_geo_json.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_geo_json.cc')
-rw-r--r--test/test_geo_json.cc123
1 files changed, 123 insertions, 0 deletions
diff --git a/test/test_geo_json.cc b/test/test_geo_json.cc
new file mode 100644
index 0000000..e466e75
--- /dev/null
+++ b/test/test_geo_json.cc
@@ -0,0 +1,123 @@
+#include "common.hh"
+
+#include "file_test.hh"
+#include "geo_json.hh"
+#include "logger.hh"
+
+#include <gtest/gtest.h>
+
+namespace {
+
+class GeoJsonTest : public FileTest {
+public:
+ std::unique_ptr<GeoJson> load(std::string_view data) {
+ write(data);
+ close();
+ return GeoJson::create(logger_, path());
+ }
+
+private:
+ std::shared_ptr<Logger> logger_{Logger::create_null()};
+};
+
+} // namespace
+
+TEST_F(GeoJsonTest, empty) {
+ auto geo_json = load("");
+
+ auto opt = geo_json->get_data(0.0, 0.0, "prop0");
+ EXPECT_FALSE(opt.has_value());
+}
+
+TEST_F(GeoJsonTest, sanity) {
+ auto geo_json = load(
+ "{"
+ " \"type\": \"FeatureCollection\","
+ " \"features\": ["
+ " {"
+ " \"type\": \"Feature\","
+ " \"geometry\": {"
+ " \"type\": \"Polygon\","
+ " \"coordinates\": ["
+ " ["
+ " [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],"
+ " [100.0, 1.0], [100.0, 0.0]"
+ " ]"
+ " ]"
+ " },"
+ " \"properties\": {"
+ " \"prop0\": \"value0\","
+ " \"prop1\": { \"this\": \"that\" }"
+ " }"
+ " }"
+ " ]"
+ "}");
+
+ auto opt = geo_json->get_data(0.0, 0.0, "prop0");
+ EXPECT_FALSE(opt.has_value());
+
+ opt = geo_json->get_data(0.5, 100.5, "prop0");
+ EXPECT_TRUE(opt.has_value());
+ if (opt.has_value()) {
+ EXPECT_EQ("value0", opt.value());
+ }
+
+ opt = geo_json->get_data(0.0, 100.0, "prop0");
+ EXPECT_TRUE(opt.has_value());
+ if (opt.has_value()) {
+ EXPECT_EQ("value0", opt.value());
+ }
+
+ opt = geo_json->get_data(0.5, 100.5, "prop1");
+ EXPECT_FALSE(opt.has_value());
+}
+
+TEST_F(GeoJsonTest, hole) {
+ auto geo_json = load(
+ "{"
+ " \"type\": \"FeatureCollection\","
+ " \"features\": ["
+ " {"
+ " \"type\": \"Feature\","
+ " \"geometry\": {"
+ " \"type\": \"Polygon\","
+ " \"coordinates\": ["
+ " ["
+ " [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],"
+ " [100.0, 1.0], [100.0, 0.0]"
+ " ],"
+ " ["
+ " [100.25, 0.25], [100.75, 0.25], [100.75, 0.75],"
+ " [100.25, 0.75], [100.25, 0.25]"
+ " ]"
+ " ]"
+ " },"
+ " \"properties\": {"
+ " \"prop0\": \"value0\""
+ " }"
+ " }"
+ " ]"
+ "}");
+
+ auto opt = geo_json->get_data(0.5, 100.5, "prop0");
+ EXPECT_FALSE(opt.has_value());
+
+ opt = geo_json->get_data(0.0, 100.0, "prop0");
+ EXPECT_TRUE(opt.has_value());
+ if (opt.has_value()) {
+ EXPECT_EQ("value0", opt.value());
+ }
+
+ opt = geo_json->get_data(0.1, 100.20, "prop0");
+ EXPECT_TRUE(opt.has_value());
+ if (opt.has_value()) {
+ EXPECT_EQ("value0", opt.value());
+ }
+}
+
+TEST_F(GeoJsonTest, bad) {
+ auto geo_json = load(std::string(1000, '{'));
+
+ auto opt = geo_json->get_data(0.0, 0.0, "prop0");
+ EXPECT_FALSE(opt.has_value());
+}