Пример #1
0
    osmium::memory::Buffer read() {
        osmium::memory::Buffer buffer = m_reader.read();

        if (buffer) {
            if (m_with_areas) {
                std::vector<osmium::memory::Buffer> area_buffers;
                osmium::apply(buffer, m_location_handler, m_collector.handler([&area_buffers](osmium::memory::Buffer&& area_buffer) {
                    area_buffers.push_back(std::move(area_buffer));
                }));
                for (const osmium::memory::Buffer& b : area_buffers) {
                    buffer.add_buffer(b);
                    buffer.commit();
                }
            } else if (m_entities & (osmium::osm_entity_bits::node | osmium::osm_entity_bits::way)) {
                osmium::apply(buffer, m_location_handler);
            }
        }

        return buffer;
    }
TEST_CASE("Test Reader using MockParser") {

    std::string fail_in;

    osmium::io::detail::ParserFactory::instance().register_parser(
        osmium::io::file_format::xml,
        [&](osmium::io::detail::parser_arguments& args) {
        return std::unique_ptr<osmium::io::detail::Parser>(new MockParser(args, fail_in));
    });

    SECTION("no failure") {
        fail_in = "";
        osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
        auto header = reader.header();
        REQUIRE(reader.read());
        REQUIRE_FALSE(reader.read());
        REQUIRE(reader.eof());
        reader.close();
    }

    SECTION("throw in header") {
        fail_in = "header";
        try {
            osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
            reader.header();
        } catch (const std::runtime_error& e) {
            REQUIRE(std::string{e.what()} == "error in header");
        }
    }
    std::string fail_in;

    osmium::io::detail::ParserFactory::instance().register_parser(
        osmium::io::file_format::xml,
        [&](osmium::io::detail::future_string_queue_type& input_queue,
            osmium::io::detail::future_buffer_queue_type& output_queue,
            std::promise<osmium::io::Header>& header_promise,
            osmium::osm_entity_bits::type read_which_entities) {
        return std::unique_ptr<osmium::io::detail::Parser>(new MockParser(input_queue, output_queue, header_promise, read_which_entities, fail_in));
    });

    SECTION("no failure") {
        fail_in = "";
        osmium::io::Reader reader(with_data_dir("t/io/data.osm"));
        auto header = reader.header();
        REQUIRE(reader.read());
        REQUIRE(!reader.read());
        REQUIRE(reader.eof());
        reader.close();
    }

    SECTION("throw in header") {
        fail_in = "header";
        try {
            osmium::io::Reader reader(with_data_dir("t/io/data.osm"));
            reader.header();
        } catch (std::runtime_error& e) {
            REQUIRE(std::string{e.what()} == "error in header");
        }
    }
TEST_CASE("Write with mock compressor") {

    std::string fail_in;

    osmium::io::CompressionFactory::instance().register_compression(osmium::io::file_compression::gzip,
        [&](int, osmium::io::fsync) { return new MockCompressor(fail_in); },
        [](int) { return nullptr; },
        [](const char*, size_t) { return nullptr; }
    );

    osmium::io::Header header;
    header.set("generator", "test_writer_with_mock_compression.cpp");

    osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
    osmium::memory::Buffer buffer = reader.read();
    REQUIRE(buffer);
    REQUIRE(buffer.committed() > 0);
    REQUIRE(buffer.select<osmium::OSMObject>().size() > 0);

    SECTION("fail on construction") {

        fail_in = "constructor";

        REQUIRE_THROWS_AS([&](){
            osmium::io::Writer writer("test-writer-mock-fail-on-construction.osm.gz", header, osmium::io::overwrite::allow);
            writer(std::move(buffer));
            writer.close();
        }(), const std::logic_error&);

    }