lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 1ef4688833011d62fb128b90b2e8456d47bef2b6
parent 5e2560a6b312678bd996c07beb23093903242d84
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 23 Jan 2017 14:15:56 +0100

Add(Tests): More tests on margs

Diffstat:
Mtests/test.h | 12++++++------
Mtests/test_args.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 101 insertions(+), 6 deletions(-)

diff --git a/tests/test.h b/tests/test.h @@ -10,12 +10,12 @@ # define WAIT_AND_CLOSE(pid, status, fd) pid = waitpid(pid, &status, 0); close(fd[1]); # define OPT_STR_SIZE 150 # define OPT_DEF(val) { \ - {'q', "--qwerty", "qwerty", val, &callback_q}, \ - {'w', "--wertyu", "wertyu", val, &callback_w}, \ - {'e', "--ertyui", "rtyuio", val, &callback_e}, \ - {'r', "--rtyuio", "tyuiop", val, &callback_r}, \ - {'t', "--tyuiop", "tyuiop", val, &callback_t}, \ - {'y', "--yuiop[", "yuiop[", val, &callback_y}, \ + {'q', "qwerty", "qwerty", val, &callback_q}, \ + {'w', "wertyu", "wertyu", val, &callback_w}, \ + {'e', "ertyui", "rtyuio", val, &callback_e}, \ + {'r', "rtyuio", "tyuiop", val, &callback_r}, \ + {'t', "tyuiop", "tyuiop", val, &callback_t}, \ + {'y', "yuiop[", "yuiop[", val, &callback_y}, \ ARGS_EOL \ } diff --git a/tests/test_args.c b/tests/test_args.c @@ -239,6 +239,95 @@ TEST(args_base_3) { return TEST_SUCCESS; } +TEST(args_base_4) { + margs_t opt[] = OPT_DEF(false); + char *av[] = {"./test", "--qwerty", "-w"}; + + reset_args(); + TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 2), "Wrong return"); + TEST_ASSERT(args.opt_q == true, "Argument not read"); + TEST_ASSERT(args.opt_w == true, "Argument not read"); + TEST_ASSERT(args.opt_e != true, "Argument not read"); + return TEST_SUCCESS; +} + +TEST(args_base_5) { + margs_t opt[] = OPT_DEF(false); + char *av[] = {"./test", "--qwerty", "--wertyu"}; + + reset_args(); + TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 2), "Wrong return"); + TEST_ASSERT(args.opt_q == true, "Argument not read"); + TEST_ASSERT(args.opt_w == true, "Argument not read"); + TEST_ASSERT(args.opt_e != true, "Argument not read"); + return TEST_SUCCESS; +} + +TEST(args_base_6) { + margs_t opt[] = OPT_DEF(false); + char *av[] = {"./test", "-qwerty"}; + + reset_args(); + TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 6), "Wrong return"); + TEST_ASSERT(args.opt_q == true, "Argument not read"); + TEST_ASSERT(args.opt_w == true, "Argument not read"); + TEST_ASSERT(args.opt_e == true, "Argument not read"); + TEST_ASSERT(args.opt_r == true, "Argument not read"); + TEST_ASSERT(args.opt_t == true, "Argument not read"); + TEST_ASSERT(args.opt_y == true, "Argument not read"); + return TEST_SUCCESS; +} + +TEST(args_base_7) { + margs_t opt[] = OPT_DEF(false); + char *av[] = {"./test", "--qwerty", "--wertyu", "--ertyui", "--rtyuio", "--tyuiop", "--yuiop["}; + + reset_args(); + TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 6), "Wrong return"); + TEST_ASSERT(args.opt_q == true, "Argument not read"); + TEST_ASSERT(args.opt_w == true, "Argument not read"); + TEST_ASSERT(args.opt_e == true, "Argument not read"); + TEST_ASSERT(args.opt_r == true, "Argument not read"); + TEST_ASSERT(args.opt_t == true, "Argument not read"); + TEST_ASSERT(args.opt_y == true, "Argument not read"); + return TEST_SUCCESS; +} + +TEST(args_missing_value_1) { + margs_t opt[] = OPT_DEF(true); + char *av[] = {"./test", "-q"}; + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling missing argument"); + _exit(5); + } else { + WAIT_AND_CLOSE(pid, st, fd); + TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return"); + } + return TEST_SUCCESS; +} + +TEST(args_missing_value_2) { + margs_t opt[] = OPT_DEF(true); + char *av[] = {"./test", "--qwerty"}; + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling missing argument"); + _exit(5); + } else { + WAIT_AND_CLOSE(pid, st, fd); + TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return"); + } + return TEST_SUCCESS; +} void callback_q(const char *s) { args.opt_q = true; @@ -322,4 +411,10 @@ void register_args_tests(void) { reg_test("m_args", args_base_1); reg_test("m_args", args_base_2); reg_test("m_args", args_base_3); + reg_test("m_args", args_base_4); + reg_test("m_args", args_base_5); + reg_test("m_args", args_base_6); + reg_test("m_args", args_base_7); + reg_test("m_args", args_missing_value_1); + reg_test("m_args", args_missing_value_2); }