2020-06-17 02:04:36 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2020-06-17 02:04:36 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/OptionParser.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
|
|
|
TEST(OptionParser, run) {
|
|
|
|
td::OptionParser options;
|
|
|
|
options.set_description("test description");
|
|
|
|
|
2020-06-17 06:00:42 +02:00
|
|
|
td::string exename = "exename";
|
2020-06-17 02:04:36 +02:00
|
|
|
td::vector<td::string> args;
|
|
|
|
auto run_option_parser = [&](td::string command_line) {
|
2021-10-21 11:51:16 +02:00
|
|
|
args = td::full_split(std::move(command_line), ' ');
|
2020-06-17 02:04:36 +02:00
|
|
|
td::vector<char *> argv;
|
2020-06-17 06:00:42 +02:00
|
|
|
argv.push_back(&exename[0]);
|
2020-06-17 02:04:36 +02:00
|
|
|
for (auto &arg : args) {
|
|
|
|
argv.push_back(&arg[0]);
|
|
|
|
}
|
2020-11-20 01:33:09 +01:00
|
|
|
return options.run_impl(static_cast<int>(argv.size()), &argv[0], -1);
|
2020-06-17 02:04:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
td::uint64 chosen_options = 0;
|
|
|
|
td::vector<td::string> chosen_parameters;
|
|
|
|
auto test_success = [&](td::string command_line, td::uint64 expected_options,
|
2021-10-21 11:51:16 +02:00
|
|
|
const td::vector<td::string> &expected_parameters,
|
|
|
|
const td::vector<td::string> &expected_result) {
|
2020-06-17 02:04:36 +02:00
|
|
|
chosen_options = 0;
|
|
|
|
chosen_parameters.clear();
|
2021-10-21 11:51:16 +02:00
|
|
|
auto result = run_option_parser(std::move(command_line));
|
2020-06-17 02:04:36 +02:00
|
|
|
ASSERT_TRUE(result.is_ok());
|
|
|
|
ASSERT_EQ(expected_options, chosen_options);
|
|
|
|
ASSERT_EQ(expected_parameters, chosen_parameters);
|
|
|
|
ASSERT_EQ(expected_result.size(), result.ok().size());
|
|
|
|
for (size_t i = 0; i < expected_result.size(); i++) {
|
|
|
|
ASSERT_STREQ(expected_result[i], td::string(result.ok()[i]));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto test_fail = [&](td::string command_line) {
|
2021-10-21 11:51:16 +02:00
|
|
|
auto result = run_option_parser(std::move(command_line));
|
2020-06-17 02:04:36 +02:00
|
|
|
ASSERT_TRUE(result.is_error());
|
|
|
|
};
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
options.add_option('q', "", "", [&] { chosen_options += 1; });
|
|
|
|
options.add_option('\0', "http-port2", "", [&] { chosen_options += 10; });
|
2020-06-17 02:04:36 +02:00
|
|
|
options.add_option('p', "http-port", "", [&](td::Slice parameter) {
|
|
|
|
chosen_options += 100;
|
|
|
|
chosen_parameters.push_back(parameter.str());
|
|
|
|
});
|
2020-06-24 13:47:36 +02:00
|
|
|
options.add_option('v', "test", "", [&] { chosen_options += 1000; });
|
2020-06-17 02:04:36 +02:00
|
|
|
|
|
|
|
test_fail("-http-port2");
|
|
|
|
test_success("-", 0, {}, {"-"});
|
|
|
|
test_fail("--http-port");
|
|
|
|
test_fail("--http-port3");
|
|
|
|
test_fail("--http-por");
|
|
|
|
test_fail("--http-port2=1");
|
|
|
|
test_fail("--q");
|
|
|
|
test_fail("-qvp");
|
|
|
|
test_fail("-p");
|
|
|
|
test_fail("-u");
|
|
|
|
test_success("-q", 1, {}, {});
|
|
|
|
test_success("-vvvvvvvvvv", 10000, {}, {});
|
|
|
|
test_success("-qpv", 101, {"v"}, {});
|
|
|
|
test_success("-qp -v", 101, {"-v"}, {});
|
|
|
|
test_success("-qp --http-port2", 101, {"--http-port2"}, {});
|
|
|
|
test_success("-qp -- -v", 1101, {"--"}, {});
|
|
|
|
test_success("-qvqvpqv", 2102, {"qv"}, {});
|
|
|
|
test_success("aba --http-port2 caba --http-port2 dabacaba", 20, {}, {"aba", "caba", "dabacaba"});
|
|
|
|
test_success("das -pqwerty -- -v asd --http-port", 100, {"qwerty"}, {"das", "-v", "asd", "--http-port"});
|
|
|
|
test_success("-p option --http-port option2 --http-port=option3 --http-port=", 400,
|
|
|
|
{"option", "option2", "option3", ""}, {});
|
|
|
|
test_success("", 0, {}, {});
|
|
|
|
test_success("a", 0, {}, {"a"});
|
|
|
|
test_success("", 0, {}, {});
|
|
|
|
}
|