File: test_reader_with_mock_parser.cpp

package info (click to toggle)
libosmium 2.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,564 kB
  • sloc: cpp: 53,570; sh: 148; makefile: 19
file content (110 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "catch.hpp"

#include "utils.hpp"

#include <osmium/builder/attr.hpp>
#include <osmium/builder/osm_object_builder.hpp>
#include <osmium/io/compression.hpp>
#include <osmium/io/detail/input_format.hpp>
#include <osmium/io/file_format.hpp>
#include <osmium/io/header.hpp>
#include <osmium/io/reader.hpp>
#include <osmium/thread/queue.hpp>
#include <osmium/thread/util.hpp>

#include <memory>
#include <stdexcept>
#include <string>
#include <utility>

class MockParser : public osmium::io::detail::Parser {

    std::string m_fail_in;

public:

    MockParser(osmium::io::detail::parser_arguments& args,
               std::string fail_in) :
        Parser(args),
        m_fail_in(std::move(fail_in)) {
    }

    void run() final {
        osmium::thread::set_thread_name("_osmium_mock_in");

        if (m_fail_in == "header") {
            throw std::runtime_error{"error in header"};
        }

        set_header_value(osmium::io::Header{});

        osmium::memory::Buffer buffer(1000);
        osmium::builder::add_node(buffer, osmium::builder::attr::_user("foo"));
        send_to_output_queue(std::move(buffer));

        if (m_fail_in == "read") {
            throw std::runtime_error{"error in read"};
        }
    }

}; // class MockParser

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");
        }
    }

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

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

}