Mercurial > code
diff modules/options/test/main.cpp @ 618:1ae8106369e5
Options: initial reimport, closes #705
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 26 Sep 2017 09:50:02 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/options/test/main.cpp Tue Sep 26 09:50:02 2017 +0200 @@ -0,0 +1,233 @@ +/* + * main.cpp -- main test file for OptionParser + * + * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#define BOOST_TEST_MODULE "Options" +#include <boost/test/unit_test.hpp> + +#include <options.hpp> + +/* + * Short options. + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(short_options) + +BOOST_AUTO_TEST_CASE(simple) +{ + std::vector<std::string> args{"-a", "-b"}; + + auto pack = option::read(args, { + { "-a", false }, + { "-b", false } + }); + + BOOST_TEST(pack.size() == 2U); + BOOST_TEST(args.size() == 0U); + BOOST_TEST(pack.count("-a") != 0); + BOOST_TEST(pack.count("-b") != 0); +} + +BOOST_AUTO_TEST_CASE(simple_arg) +{ + std::vector<std::string> args{"-v", "-cfoo.conf"}; + + auto pack = option::read(args, { + { "-v", false }, + { "-c", true } + }); + + BOOST_TEST(pack.size() == 2U); + BOOST_TEST(args.size() == 0U); + BOOST_TEST(pack.count("-v") != 0); + BOOST_TEST(pack.find("-c")->second == "foo.conf"); +} + +BOOST_AUTO_TEST_CASE(spaced_arg) +{ + std::vector<std::string> args{"-v", "-c", "foo.conf"}; + + auto pack = option::read(args, { + { "-v", false }, + { "-c", true } + }); + + BOOST_TEST(pack.size() == 2U); + BOOST_TEST(args.size() == 0U); + BOOST_TEST(pack.count("-v") != 0); + BOOST_TEST(pack.find("-c")->second == "foo.conf"); +} + +BOOST_AUTO_TEST_CASE(compacted) +{ + std::vector<std::string> args{"-abc"}; + + auto pack = option::read(args, { + { "-a", false }, + { "-b", false }, + { "-c", false }, + }); + + BOOST_TEST(pack.size() == 3U); + BOOST_TEST(args.size() == 0U); + BOOST_TEST(pack.count("-a") != 0); + BOOST_TEST(pack.count("-b") != 0); + BOOST_TEST(pack.count("-c") != 0); +} + +BOOST_AUTO_TEST_CASE(compacted_arg) +{ + std::vector<std::string> args{"-vdcfoo.conf"}; + + auto pack = option::read(args, { + { "-v", false }, + { "-d", false }, + { "-c", true }, + }); + + BOOST_TEST(pack.size() == 3U); + BOOST_TEST(args.size() == 0U); + BOOST_TEST(pack.count("-v") != 0); + BOOST_TEST(pack.count("-d") != 0); + BOOST_TEST(pack.find("-c")->second == "foo.conf"); +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * Long options. + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(long_options) + +BOOST_AUTO_TEST_CASE(simple) +{ + std::vector<std::string> args{"--fullscreen"}; + + auto pack = option::read(args, { + { "--verbose", false }, + { "--fullscreen", false } + }); + + BOOST_TEST(pack.size() == 1U); + BOOST_TEST(args.size() == 0U); + BOOST_TEST(pack.count("--fullscreen") != 0); +} + +BOOST_AUTO_TEST_CASE(simple_arg) +{ + std::vector<std::string> args{"--config", "config.conf", "--level", "2"}; + + auto pack = option::read(args, { + { "--config", true }, + { "--level", true } + }); + + BOOST_TEST(pack.size() == 2U); + BOOST_TEST(args.size() == 0U); + BOOST_TEST(pack.find("--config")->second == "config.conf"); + BOOST_TEST(pack.find("--level")->second == "2"); +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * Errors. + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(errors) + +BOOST_AUTO_TEST_CASE(stop) +{ + std::vector<std::string> args{"-v", "install", "-y", "irccd"}; + std::vector<std::string> expected{"install", "-y", "irccd"}; + + auto pack = option::read(args, {{ "-v", false }}); + + BOOST_TEST(pack.size() == 1U); + BOOST_TEST(args.size() == 3U); + BOOST_TEST(pack.count("-v") != 0); + BOOST_TEST(expected == args); +} + +BOOST_AUTO_TEST_CASE(missing_short_arg) +{ + std::vector<std::string> args{"-c"}; + + BOOST_REQUIRE_THROW(option::read(args, {{ "-c", true }}), option::missing_value); +} + +BOOST_AUTO_TEST_CASE(missing_short_arg2) +{ + const option::options options{ + { "-v", false }, + { "-c", true } + }; + + std::vector<std::string> args{"-vc"}; + + BOOST_REQUIRE_THROW(option::read(args, options), option::missing_value); +} + +BOOST_AUTO_TEST_CASE(missing_long_arg) +{ + const option::options options{ + { "--config", true } + }; + + std::vector<std::string> args{"--config"}; + + BOOST_REQUIRE_THROW(option::read(args, options), option::missing_value); +} + +BOOST_AUTO_TEST_CASE(invalid_option) +{ + const option::options options{ + { "-v", true } + }; + + std::vector<std::string> args{"-x"}; + + BOOST_REQUIRE_THROW(option::read(args, options), option::invalid_option); +} + +BOOST_AUTO_TEST_CASE(invalid_option2) +{ + const option::options options{ + { "--verbose", true } + }; + + std::vector<std::string> args{"--destroy"}; + + BOOST_REQUIRE_THROW(option::read(args, options), option::invalid_option); +} + +BOOST_AUTO_TEST_CASE(invalid_option3) +{ + const option::options options{ + { "-x", true } + }; + + std::vector<std::string> args{"-vx"}; + + BOOST_REQUIRE_THROW(option::read(args, options), option::invalid_option); +} + +BOOST_AUTO_TEST_SUITE_END()