Added tests for my changes to getopt.

This commit is contained in:
Eric Mumau 2017-06-28 15:04:38 -07:00
Родитель 002725c1af
Коммит 75ae29da60
3 изменённых файлов: 202 добавлений и 16 удалений

Просмотреть файл

@ -9,11 +9,6 @@
using test::getopt_test;
namespace
{
} // namespace (unnamed)
namespace test
{
@ -30,17 +25,19 @@ getopt_test::getopt_test ()
add_test (MAKE_TEST (getopt_test::test07));
add_test (MAKE_TEST (getopt_test::test08));
add_test (MAKE_TEST (getopt_test::test09));
add_test (MAKE_TEST (getopt_test::test10));
}
int
getopt_test::test01 ()
{
int argc = 0;
// test with no args and no opts
int argc = 1;
char const* argv[] = {
"Test", // exe name
};
char const* opts[] = {
NULL,
};
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
@ -55,7 +52,23 @@ getopt_test::test01 ()
int
getopt_test::test02 ()
{
// test with opts not in args
int argc = 1;
char const* argv[] = {
"Test", // exe name
};
char const* opts[] = {
"-n", // not in args
"-m", // not in args
"-l", // not in args
NULL,
};
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
if (1 != GetOpt (&argc, argv, opts, &state))
{
rval = EXIT_FAILURE;
}
return rval;
}
@ -63,7 +76,37 @@ getopt_test::test02 ()
int
getopt_test::test03 ()
{
// test with opts in args
int argc = 4;
char const* argv[] = {
"Test", // exe name
"-n", // in opts
"-m", // in opts
"-l", // in opts
};
char const* opts[] = {
"-n", // in args
"-m", // in args
"-l", // in args
NULL,
};
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
int result = 0;
for (size_t i = 0; 0 == result && i < 3; ++i)
{
result = GetOpt (&argc, argv, opts, &state);
if (0 == result &&
(0 != strcmp (state.opt, opts[i]) ||
NULL != state.arg))
{
rval = EXIT_FAILURE;
}
}
if (1 != GetOpt (&argc, argv, opts, &state))
{
rval = EXIT_FAILURE;
}
return rval;
}
@ -71,7 +114,32 @@ getopt_test::test03 ()
int
getopt_test::test04 ()
{
// test with args not in opts
int argc = 5;
char const* argv[] = {
"Test", // exe name
"-n", // in opts
"-XXX", // not in opts
"-m", // in opts
"-l", // in opts
};
char const* opts[] = {
"-n", // in args
"-m", // in args
"-l", // in args
NULL,
};
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
int result = 0;
for (size_t i = 0; 0 == result && i < 4; ++i)
{
result = GetOpt (&argc, argv, opts, &state);
}
if (-1 != result)
{
rval = EXIT_FAILURE;
}
return rval;
}
@ -79,7 +147,23 @@ getopt_test::test04 ()
int
getopt_test::test05 ()
{
// <arguments> test with opts not in args
int argc = 1;
char const* argv[] = {
"Test", // exe name
};
char const* opts[] = {
"-n:", // not in args
"-m:", // not in args
"-l:", // not in args
NULL,
};
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
if (1 != GetOpt (&argc, argv, opts, &state))
{
rval = EXIT_FAILURE;
}
return rval;
}
@ -87,7 +171,37 @@ getopt_test::test05 ()
int
getopt_test::test06 ()
{
// <arguments equal> test with opts in args with arguments
int argc = 4;
char const* argv[] = {
"Test", // exe name
"-n=n_value", // in opts
"-m=m_value", // in opts
"-l=l_value", // in opts
};
char const* opts[] = {
"-n:", // in args
"-m:", // in args
"-l:", // in args
NULL,
};
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
int result = 0;
for (size_t i = 0; 0 == result && i < 3; ++i)
{
result = GetOpt (&argc, argv, opts, &state);
if (0 == result &&
(0 != strncmp (state.opt, opts[i], 2) ||
*(state.arg) != opts[i][1]))
{
rval = EXIT_FAILURE;
}
}
if (1 != GetOpt (&argc, argv, opts, &state))
{
rval = EXIT_FAILURE;
}
return rval;
}
@ -95,7 +209,37 @@ getopt_test::test06 ()
int
getopt_test::test07 ()
{
// <optional arguments> test with opts in args without arguments
int argc = 4;
char const* argv[] = {
"Test", // exe name
"-n",// "n_value", // in opts
"-m",// "m_value", // in opts
"-l",// "l_value", // in opts
};
char const* opts[] = {
"-n?", // in args
"-m?", // in args
"-l?", // in args
NULL,
};
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
int result = 0;
for (size_t i = 0; 0 == result && i < 3; ++i)
{
result = GetOpt (&argc, argv, opts, &state);
if (0 == result &&
(0 != strncmp (state.opt, opts[i], 2) ||
NULL != state.arg))//*(state.arg) != opts[i][1]))
{
rval = EXIT_FAILURE;
}
}
if (1 != GetOpt (&argc, argv, opts, &state))
{
rval = EXIT_FAILURE;
}
return rval;
}
@ -103,7 +247,37 @@ getopt_test::test07 ()
int
getopt_test::test08 ()
{
// <optional arguments> test with opts in args with arguments
int argc = 4;
char const* argv[] = {
"Test", // exe name
"-n=n_value", // in opts
"-m=m_value", // in opts
"-l=l_value", // in opts
};
char const* opts[] = {
"-n?", // in args
"-m?", // in args
"-l?", // in args
NULL,
};
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
int result = 0;
for (size_t i = 0; 0 == result && i < 3; ++i)
{
result = GetOpt (&argc, argv, opts, &state);
if (0 == result &&
(0 != strncmp (state.opt, opts[i], 2) ||
*(state.arg) != opts[i][1]))
{
rval = EXIT_FAILURE;
}
}
if (1 != GetOpt (&argc, argv, opts, &state))
{
rval = EXIT_FAILURE;
}
return rval;
}
@ -111,15 +285,26 @@ getopt_test::test08 ()
int
getopt_test::test09 ()
{
// <arguments> test with opts w/o arguments with args with arguments
int argc = 4;
char const* argv[] = {
"Test", // exe name
"-n=n_value", // in opts
"-m=m_value", // in opts
"-l=l_value", // in opts
};
char const* opts[] = {
"-n", // in args
"-m", // in args
"-l", // in args
NULL,
};
int rval = EXIT_SUCCESS;
return rval;
}
int
getopt_test::test10 ()
{
int rval = EXIT_SUCCESS;
GetOptState state = GETOPTSTATE_INITIALIZER;
if (-1 != GetOpt (&argc, argv, opts, &state))
{
rval = EXIT_FAILURE;
}
return rval;
}

Просмотреть файл

@ -25,7 +25,6 @@ public:
int test07 ();
int test08 ();
int test09 ();
int test10 ();
};

Просмотреть файл

@ -1,3 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "test_helper.hpp"