lib

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

commit dfc005fc9e7bda36134af393226c318cac7494e5
parent 1ef4688833011d62fb128b90b2e8456d47bef2b6
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 23 Jan 2017 14:31:29 +0100

Add(Tests): Final tests on margs:

And a small fix in m_args.c

Diffstat:
Msrc/m_args.c | 1+
Mtests/test_args.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/src/m_args.c b/src/m_args.c @@ -81,6 +81,7 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { if (args[it].take_arg) { if (i + 1 < (u32_t)ac) { args[it].callback(av[++i]); + ret++; break ; } else { m_error("Option -%c must take an argument\n", diff --git a/tests/test_args.c b/tests/test_args.c @@ -329,6 +329,51 @@ TEST(args_missing_value_2) { return TEST_SUCCESS; } +TEST(args_value_1) { + margs_t opt[] = OPT_DEF(true); + char *av[] = {"./test", "-q", "toto"}; + + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 1, "Wrong return"); + TEST_ASSERT(args.opt_q == true, "Argument not read"); + TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read"); + return TEST_SUCCESS; +} + +TEST(args_value_2) { + margs_t opt[] = OPT_DEF(true); + char *av[] = {"./test", "--qwerty=toto"}; + + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 1, "Wrong return"); + TEST_ASSERT(args.opt_q == true, "Argument not read"); + TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read"); + return TEST_SUCCESS; +} + +TEST(args_value_3) { + margs_t opt[] = OPT_DEF(true); + char *av[] = {"./test", "--qwerty=toto", "-w", "tata"}; + + 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(strcmp(args.str_q, "toto") == 0, "Value not read"); + TEST_ASSERT(args.opt_w == true, "Argument not read"); + TEST_ASSERT(strcmp(args.str_w, "tata") == 0, "Value not read"); + return TEST_SUCCESS; +} + +TEST(args_value_4) { + margs_t opt[] = OPT_DEF(true); + char *av[] = {"./test", "--qwerty=toto", "--wertyu=tata"}; + + 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(strcmp(args.str_q, "toto") == 0, "Value not read"); + TEST_ASSERT(args.opt_w == true, "Argument not read"); + TEST_ASSERT(strcmp(args.str_w, "tata") == 0, "Value not read"); + return TEST_SUCCESS; +} + + void callback_q(const char *s) { args.opt_q = true; if (s == NULL) @@ -417,4 +462,8 @@ void register_args_tests(void) { reg_test("m_args", args_base_7); reg_test("m_args", args_missing_value_1); reg_test("m_args", args_missing_value_2); + reg_test("m_args", args_value_1); + reg_test("m_args", args_value_2); + reg_test("m_args", args_value_3); + reg_test("m_args", args_value_4); }