view modules/options/test/main.cpp @ 574:d1afc6cc5974

Net: allow ownership and non-ownership in TlsSocket
author David Demelier <markand@malikania.fr>
date Tue, 19 Jul 2016 15:05:42 +0200
parents 2d1a04e8240a
children
line wrap: on
line source

/*
 * 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.
 */

#include <gtest/gtest.h>

#include <options.hpp>

/*
 * Short options.
 * ------------------------------------------------------------------
 */

TEST(Short, simpleNoArg)
{
    std::vector<std::string> args{"-a", "-b"};

    option::Options options{
        { "-a", false },
        { "-b", false }
    };

    option::Result pack = option::read(args, options);

    ASSERT_EQ(2U, pack.size());
    ASSERT_EQ(0U, args.size());

    ASSERT_TRUE(pack.count("-a") != 0);
    ASSERT_TRUE(pack.count("-b") != 0);
}

TEST(Short, simpleArg)
{
    std::vector<std::string> args{"-v", "-cfoo.conf"};

    option::Options options{
        { "-v", false },
        { "-c", true  }
    };

    option::Result pack = option::read(args, options);

    ASSERT_EQ(2U, pack.size());
    ASSERT_EQ(0U, args.size());

    ASSERT_TRUE(pack.count("-v") != 0);
    ASSERT_EQ("foo.conf", pack.find("-c")->second);
}

TEST(Short, spacedArg)
{
    std::vector<std::string> args{"-v", "-c", "foo.conf"};

    option::Options options{
        { "-v", false },
        { "-c", true  }
    };

    option::Result pack = option::read(args, options);

    ASSERT_EQ(2U, pack.size());
    ASSERT_EQ(0U, args.size());

    ASSERT_TRUE(pack.count("-v") != 0);
    ASSERT_EQ("foo.conf", pack.find("-c")->second);
}

TEST(Short, compacted)
{
    std::vector<std::string> args{"-abc"};

    option::Options options{
        { "-a", false },
        { "-b", false },
        { "-c", false },
    };

    option::Result pack = option::read(args, options);

    ASSERT_EQ(3U, pack.size());
    ASSERT_EQ(0U, args.size());

    ASSERT_TRUE(pack.count("-a") != 0);
    ASSERT_TRUE(pack.count("-b") != 0);
    ASSERT_TRUE(pack.count("-c") != 0);
}

TEST(Short, compactedArg)
{
    std::vector<std::string> args{"-vdcfoo.conf"};

    option::Options options{
        { "-v", false },
        { "-d", false },
        { "-c", true },
    };

    option::Result pack = option::read(args, options);

    ASSERT_EQ(3U, pack.size());
    ASSERT_EQ(0U, args.size());

    ASSERT_TRUE(pack.count("-v") != 0);
    ASSERT_TRUE(pack.count("-d") != 0);
    ASSERT_EQ("foo.conf", pack.find("-c")->second);
}

/*
 * Long options.
 * ------------------------------------------------------------------
 */

TEST(Long, simple)
{
    std::vector<std::string> args{"--fullscreen"};

    option::Options options{
        { "--verbose",      false },
        { "--fullscreen",   false }
    };

    option::Result pack = option::read(args, options);

    ASSERT_EQ(1U, pack.size());
    ASSERT_EQ(0U, args.size());

    ASSERT_TRUE(pack.count("--fullscreen") != 0);
}

TEST(Long, simpleArg)
{
    std::vector<std::string> args{"--config", "config.conf", "--level", "2"};

    option::Options options{
        { "--config",   true },
        { "--level",    true }
    };

    option::Result pack = option::read(args, options);

    ASSERT_EQ(2U, pack.size());
    ASSERT_EQ(0U, args.size());

    ASSERT_EQ("config.conf", pack.find("--config")->second);
    ASSERT_EQ("2", pack.find("--level")->second);
}

/*
 * Errors.
 * ------------------------------------------------------------------
 */

TEST(Errors, stop)
{
    std::vector<std::string> args{"-v", "install", "-y", "irccd"};
    std::vector<std::string> expected{"install", "-y", "irccd"};

    option::Options options{
        { "-v", false }
    };

    option::Result pack = option::read(args, options);

    ASSERT_EQ(1U, pack.size());
    ASSERT_EQ(3U, args.size());

    ASSERT_TRUE(pack.count("-v") != 0);
    ASSERT_EQ(expected, args);
}

TEST(Errors, missingShortArg)
{
    std::vector<std::string> args{"-c"};

    option::Options options{
        { "-c", true }
    };

    try {
        option::Result pack = option::read(args, options);

        FAIL() << "exception expected";
    } catch (const option::MissingValue &) {
    }
}

TEST(Errors, missingShortArg2)
{
    std::vector<std::string> args{"-vc"};

    option::Options options{
        { "-v", false },
        { "-c", true }
    };

    try {
        option::Result pack = option::read(args, options);

        FAIL() << "exception expected";
    } catch (const option::MissingValue &) {
    }
}

TEST(Errors, missingLongArg)
{
    std::vector<std::string> args{"--config"};

    option::Options options{
        { "--config", true }
    };

    try {
        option::Result pack = option::read(args, options);

        FAIL() << "exception expected";
    } catch (const option::MissingValue &) {
    }
}

TEST(Errors, invalidOption)
{
    std::vector<std::string> args{"-x"};

    option::Options options{
        { "-v", true }
    };

    try {
        option::Result pack = option::read(args, options);

        FAIL() << "exception expected";
    } catch (const option::InvalidOption &) {
    }
}

TEST(Errors, invalidOption2)
{
    std::vector<std::string> args{"--destroy"};

    option::Options options{
        { "--verbose", true }
    };

    try {
        option::Result pack = option::read(args, options);

        FAIL() << "exception expected";
    } catch (const option::InvalidOption &) {
    }
}

TEST(Errors, invalidOption3)
{
    std::vector<std::string> args{"-vx"};

    option::Options options{
        { "-x", true }
    };

    try {
        option::Result pack = option::read(args, options);

        FAIL() << "exception expected";
    } catch (const option::InvalidOption &) {
    }
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}