File: test-options-parse.cpp

package info (click to toggle)
osm2pgsql 2.1.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,652 kB
  • sloc: cpp: 59,934; python: 1,039; ansic: 763; sh: 25; makefile: 14
file content (213 lines) | stat: -rw-r--r-- 6,471 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This file is part of osm2pgsql (https://osm2pgsql.org/).
 *
 * Copyright (C) 2006-2025 by the osm2pgsql developer community.
 * For a full list of authors see the git log.
 */

#include <catch.hpp>

#include <vector>

#include "command-line-parser.hpp"
#include "taginfo-impl.hpp"
#include "tagtransform.hpp"

namespace {

char const *const TEST_PBF = "foo.pbf";

void bad_opt(std::vector<char const *> opts, char const *msg)
{
    opts.insert(opts.begin(), "osm2pgsql");
    opts.push_back(TEST_PBF);

    REQUIRE_THROWS_WITH(
        parse_command_line((int)opts.size(), (char **)opts.data()),
        Catch::Matchers::Contains(msg));
}

options_t opt(std::vector<char const *> opts)
{
    opts.insert(opts.begin(), "osm2pgsql");
    opts.push_back(TEST_PBF);

    return parse_command_line((int)opts.size(), (char **)opts.data());
}

} // anonymous namespace

TEST_CASE("Insufficient arguments", "[NoDB]")
{
    std::vector<char const *> opts = {"osm2pgsql", "-c", "--slim"};

    REQUIRE_THROWS_WITH(
        parse_command_line((int)opts.size(), (char **)opts.data()),
        Catch::Matchers::Contains("Missing input"));
}

TEST_CASE("Incompatible arguments", "[NoDB]")
{
    bad_opt({"-a", "-c", "--slim"}, "options can not be used at the same time");

    bad_opt({"-j", "-k"}, "--hstore excludes --hstore-all");

    bad_opt({"-a"}, "--append can only be used with slim mode");
}

TEST_CASE("Middle selection", "[NoDB]")
{
    auto options = opt({"--slim"});
    REQUIRE(options.slim);

    options = opt({});
    REQUIRE_FALSE(options.slim);
}

TEST_CASE("Lua styles", "[NoDB]")
{
    REQUIRE_THROWS_WITH(opt({"--tag-transform-script", "non_existing.lua"}),
                        Catch::Matchers::Contains("File does not exist"));
}

TEST_CASE("Parsing bbox", "[NoDB]")
{
    auto const opt1 = opt({"-b", "1.2,3.4,5.6,7.8"});
    CHECK(opt1.bbox == osmium::Box{1.2, 3.4, 5.6, 7.8});

    auto const opt2 = opt({"--bbox", "1.2,3.4,5.6,7.8"});
    CHECK(opt2.bbox == osmium::Box{1.2, 3.4, 5.6, 7.8});

    auto const opt3 = opt({"--bbox", "1.2, 3.4, 5.6, 7.8"});
    CHECK(opt3.bbox == osmium::Box{1.2, 3.4, 5.6, 7.8});
}

TEST_CASE("Parsing bbox fails if coordinates in wrong order", "[NoDB]")
{
    bad_opt({"--bbox", "1.0,2.0,0.0,0.0"}, "Bounding box failed due to");
}

TEST_CASE("Parsing bbox fails if wrong format", "[NoDB]")
{
    bad_opt({"-b", "123"}, "Bounding box must be specified like:"
                           " minlon,minlat,maxlon,maxlat.");

    bad_opt({"-b", "1,2,3,4x"}, "Bounding box must be specified like:"
                                " minlon,minlat,maxlon,maxlat.");

    bad_opt({"-b", "1,,3,4"}, "Bounding box must be specified like:"
                              " minlon,minlat,maxlon,maxlat.");

    bad_opt({"-b", "1,2,3"}, "Bounding box must be specified like:"
                             " minlon,minlat,maxlon,maxlat.");

    bad_opt({"-b", "1,2,3,4,5"}, "Bounding box must be specified like:"
                                 " minlon,minlat,maxlon,maxlat.");

    bad_opt({"-b", "1,2,INF,4"}, "Bounding box must be specified like:"
                                 " minlon,minlat,maxlon,maxlat.");

    bad_opt({"-b", "1,NAN,3,4"}, "Bounding box must be specified like:"
                                 " minlon,minlat,maxlon,maxlat.");
}

TEST_CASE("Parsing number-processes", "[NoDB]")
{
    auto const opt1 = opt({"--number-processes", "0"});
    CHECK(opt1.num_procs == 1);

    auto const opt2 = opt({"--number-processes", "1"});
    CHECK(opt2.num_procs == 1);

    auto const opt3 = opt({"--number-processes", "2"});
    CHECK(opt3.num_procs == 2);

    auto const opt4 = opt({"--number-processes", "32"});
    CHECK(opt4.num_procs == 32);

    auto const opt5 = opt({"--number-processes", "33"});
    CHECK(opt5.num_procs == 32);
}

TEST_CASE("Parsing tile expiry zoom levels", "[NoDB]")
{
    auto options = opt({"-e", "8-12"});
    CHECK(options.expire_tiles_zoom_min == 8);
    CHECK(options.expire_tiles_zoom == 12);

    options = opt({"-e", "12"});
    CHECK(options.expire_tiles_zoom_min == 12);
    CHECK(options.expire_tiles_zoom == 12);

    //  If very high zoom levels are set, back to still high but valid zoom levels.
    options = opt({"-e", "33-35"});
    CHECK(options.expire_tiles_zoom_min == 31);
    CHECK(options.expire_tiles_zoom == 31);
}

TEST_CASE("Parsing tile expiry zoom levels fails", "[NoDB]")
{
    bad_opt({"-e", "8--12"},
            "Invalid maximum zoom level given for tile expiry");

    bad_opt({"-e", "-8-12"}, "Missing argument for option --expire-tiles. Zoom "
                             "levels must be positive.");

    bad_opt({"-e", "--style", "default.style"},
            "Missing argument for option --expire-tiles. Zoom levels must be "
            "positive.");

    bad_opt({"-e", "a-8"}, "Bad argument for option --expire-tiles. Minimum "
                           "zoom level must be larger than 0.");

    bad_opt({"-e", "6:8"}, "Minimum and maximum zoom level for tile expiry "
                           "must be separated by '-'.");

    bad_opt({"-e", "6-0"}, "Invalid maximum zoom level given for tile expiry.");

    bad_opt({"-e", "6-9a"},
            "Invalid maximum zoom level given for tile expiry.");

    bad_opt({"-e", "0-8"},
            "Bad argument for option --expire-tiles. Minimum zoom level "
            "must be larger than 0.");

    bad_opt({"-e", "6-"}, "Invalid maximum zoom level given for tile expiry.");

    bad_opt({"-e", "-6"},
            "Missing argument for option --expire-tiles. Zoom levels "
            "must be positive.");

    bad_opt({"-e", "0"},
            "Bad argument for option --expire-tiles. Minimum zoom level "
            "must be larger than 0.");
}

TEST_CASE("Parsing log-level", "[NoDB]")
{
    opt({"--log-level", "debug"});
    opt({"--log-level", "info"});
    opt({"--log-level", "warn"});
    opt({"--log-level", "warning"});
    opt({"--log-level", "error"});
}

TEST_CASE("Parsing log-level fails for unknown level", "[NoDB]")
{
    bad_opt({"--log-level", "foo"}, "--log-level: foo not in");
}

TEST_CASE("Parsing log-progress", "[NoDB]")
{
    opt({"--log-progress", "true"});
    opt({"--log-progress", "false"});
    opt({"--log-progress", "auto"});
}

TEST_CASE("Parsing log-progress fails for unknown value", "[NoDB]")
{
    bad_opt({"--log-progress", "foo"},
            "Unknown value for --log-progress option: ");
}