File: options_parser.test.cpp

package info (click to toggle)
tilemaker 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,488 kB
  • sloc: cpp: 29,461; ansic: 12,510; makefile: 229; ruby: 77; sh: 43
file content (123 lines) | stat: -rw-r--r-- 3,994 bytes parent folder | download
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
#include <iostream>
#include "external/minunit.h"
#include "options_parser.h"

const char* PROGRAM_NAME = "./tilemaker";
using namespace OptionsParser;

Options parse(std::vector<std::string>& args) {
	const char* argv[100];

	argv[0] = PROGRAM_NAME;
	for(int i = 0; i < args.size(); i++)
		argv[1 + i] = args[i].data();

	return parse(1 + args.size(), argv);
}

#define ASSERT_THROWS(MESSAGE, ...) \
{ \
	std::vector<std::string> args = { __VA_ARGS__ }; \
	bool threw = false; \
	try { \
		auto opts = parse(args); \
	} catch(OptionsParser::OptionException& e) { \
		threw = std::string(e.what()).find(MESSAGE) != std::string::npos; \
	} \
	if (!threw) mu_check((std::string("expected exception with ") + MESSAGE).empty()); \
}

MU_TEST(test_options_parser) {
	// No args is invalid.
	ASSERT_THROWS("You must specify an output file");

	// Output without input is valid
	{
		std::vector<std::string> args = {"--output", "foo.mbtiles"};
		auto opts = parse(args);
		mu_check(opts.inputFiles.size() == 0);
	}

	// You can ask for --help.
	{
		std::vector<std::string> args = {"--help"};
		auto opts = parse(args);
		mu_check(opts.showHelp);
	}

	// Common happy path is output and input
	{
		std::vector<std::string> args = {"--output", "foo.mbtiles", "--input", "ontario.pbf"};
		auto opts = parse(args);
		mu_check(opts.inputFiles.size() == 1);
		mu_check(opts.inputFiles[0] == "ontario.pbf");
		mu_check(opts.outputFile == "foo.mbtiles");
		mu_check(opts.outputMode == OutputMode::MBTiles);
		mu_check(!opts.osm.materializeGeometries);
		mu_check(!opts.osm.shardStores);
	}

	// --fast without store should have materialized geometries
	{
		std::vector<std::string> args = {"--output", "foo.mbtiles", "--input", "ontario.pbf", "--fast"};
		auto opts = parse(args);
		mu_check(opts.inputFiles.size() == 1);
		mu_check(opts.inputFiles[0] == "ontario.pbf");
		mu_check(opts.outputFile == "foo.mbtiles");
		mu_check(opts.outputMode == OutputMode::MBTiles);
		mu_check(opts.osm.materializeGeometries);
		mu_check(!opts.osm.shardStores);
	}

	// --store should optimize for reduced memory
	{
		std::vector<std::string> args = {"--output", "foo.mbtiles", "--input", "ontario.pbf", "--store", "/tmp/store"};
		auto opts = parse(args);
		mu_check(opts.inputFiles.size() == 1);
		mu_check(opts.inputFiles[0] == "ontario.pbf");
		mu_check(opts.outputFile == "foo.mbtiles");
		mu_check(opts.outputMode == OutputMode::MBTiles);
		mu_check(opts.osm.storeFile == "/tmp/store");
		mu_check(!opts.osm.materializeGeometries);
		mu_check(opts.osm.shardStores);
	}

	// --store --fast should optimize for speed
	{
		std::vector<std::string> args = {"--output", "foo.pmtiles", "--input", "ontario.pbf", "--store", "/tmp/store", "--fast"};
		auto opts = parse(args);
		mu_check(opts.inputFiles.size() == 1);
		mu_check(opts.inputFiles[0] == "ontario.pbf");
		mu_check(opts.outputFile == "foo.pmtiles");
		mu_check(opts.outputMode == OutputMode::PMTiles);
		mu_check(opts.osm.storeFile == "/tmp/store");
		mu_check(!opts.osm.materializeGeometries);
		mu_check(!opts.osm.shardStores);
	}

	// Two input files implies --materialize
	{
		std::vector<std::string> args = {"--output", "foo.mbtiles", "--input", "ontario.pbf", "--input", "alberta.pbf"};
		auto opts = parse(args);
		mu_check(opts.inputFiles.size() == 2);
		mu_check(opts.inputFiles[0] == "ontario.pbf");
		mu_check(opts.inputFiles[1] == "alberta.pbf");
		mu_check(opts.outputFile == "foo.mbtiles");
		mu_check(opts.outputMode == OutputMode::MBTiles);
		mu_check(opts.osm.materializeGeometries);
		mu_check(!opts.osm.shardStores);
	}

	ASSERT_THROWS("Couldn't open .json config", "--input", "foo", "--output", "bar", "--config", "nonexistent-config.json");
	ASSERT_THROWS("Couldn't open .lua script", "--input", "foo", "--output", "bar", "--process", "nonexistent-script.lua");
}

MU_TEST_SUITE(test_suite_options_parser) {
	MU_RUN_TEST(test_options_parser);
}

int main() {
	MU_RUN_SUITE(test_suite_options_parser);
	MU_REPORT();
	return MU_EXIT_CODE;
}