#include "common.hh" #include "file_test.hh" #include "geo_json.hh" #include "logger.hh" #include namespace { class GeoJsonTest : public FileTest { public: std::unique_ptr load(std::string_view data) { write(data); close(); return GeoJson::create(logger_, path()); } private: std::shared_ptr 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()); }