From 413b174d6c16bf55e34c2b6d8ab26bfb2bfcb277 Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Fri, 5 Nov 2021 21:10:11 +0100 Subject: [PATCH] Add unit tests for alignment option parsing (-a) --- src/cmdline.c | 15 +++++------ utest/cmdline_test.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ utest/cmdline_test.h | 6 +++++ utest/main.c | 7 +++++- 4 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/cmdline.c b/src/cmdline.c index 1f94a67..b1561f3 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -139,10 +139,11 @@ static opt_t *create_new_opt() */ static int alignment(opt_t *result, char *optarg) { - int errfl = 0; + int errfl = 1; char *p = optarg; while (*p) { + errfl = 0; if (p[1] == '\0' && !strchr("lLcCrR", *p)) { errfl = 1; break; @@ -210,7 +211,7 @@ static int alignment(opt_t *result, char *optarg) } if (errfl) { - fprintf(stderr, "%s: Illegal text format -- %s\n", PROJECT, optarg); + bx_fprintf(stderr, "%s: Illegal text format -- %s\n", PROJECT, optarg); return 1; } return 0; @@ -276,7 +277,7 @@ static int eol_override(opt_t *result, char *optarg) result->eol = "\r"; } else { - fprintf(stderr, "%s: invalid eol spec -- %s\n", PROJECT, optarg); + bx_fprintf(stderr, "%s: invalid eol spec -- %s\n", PROJECT, optarg); return 1; } return 0; @@ -443,7 +444,7 @@ static int size_of_box(opt_t *result, char *optarg) *p = 'x'; } if (errno || (result->reqwidth == 0 && result->reqheight == 0) || result->reqwidth < 0 || result->reqheight < 0) { - fprintf(stderr, "%s: invalid box size specification -- %s\n", PROJECT, optarg); + bx_fprintf(stderr, "%s: invalid box size specification -- %s\n", PROJECT, optarg); return 1; } return 0; @@ -535,7 +536,7 @@ static int input_output_files(opt_t *result, char *argv[], int optind) } else if (argv[optind + 1] && argv[optind + 2]) { /* illegal third file */ - fprintf(stderr, "%s: illegal parameter -- %s\n", PROJECT, argv[optind + 2]); + bx_fprintf(stderr, "%s: illegal parameter -- %s\n", PROJECT, argv[optind + 2]); usage_short(stderr); return 1; } @@ -547,7 +548,7 @@ static int input_output_files(opt_t *result, char *argv[], int optind) else { result->infile = fopen(argv[optind], "r"); if (result->infile == NULL) { - fprintf(stderr, "%s: Can\'t open input file -- %s\n", PROJECT, argv[optind]); + bx_fprintf(stderr, "%s: Can\'t open input file -- %s\n", PROJECT, argv[optind]); return 9; /* can't read infile */ } } @@ -741,7 +742,7 @@ opt_t *process_commandline(int argc, char *argv[]) break; default: - fprintf(stderr, "%s: internal error\n", PROJECT); + bx_fprintf(stderr, "%s: internal error\n", PROJECT); return NULL; } } while (oc != EOF); diff --git a/utest/cmdline_test.c b/utest/cmdline_test.c index a8aa3b8..2706e00 100644 --- a/utest/cmdline_test.c +++ b/utest/cmdline_test.c @@ -307,4 +307,64 @@ void test_tabstops_7(void **state) } +void test_alignment_invalid_hX(void **state) +{ + (void) state; /* unused */ + + opt_t *actual = act(2, "-a", "hX"); + + assert_null(actual); // invalid option, so we would need to exit with error + assert_int_equal(1, collect_err_size); + assert_string_equal("boxes: Illegal text format -- hX\n", collect_err[0]); +} + + +void test_alignment_invalid_vX(void **state) +{ + (void) state; /* unused */ + + opt_t *actual = act(2, "-a", "vX"); + + assert_null(actual); // invalid option, so we would need to exit with error + assert_int_equal(1, collect_err_size); + assert_string_equal("boxes: Illegal text format -- vX\n", collect_err[0]); +} + + +void test_alignment_invalid_jX(void **state) +{ + (void) state; /* unused */ + + opt_t *actual = act(2, "-a", "jX"); + + assert_null(actual); // invalid option, so we would need to exit with error + assert_int_equal(1, collect_err_size); + assert_string_equal("boxes: Illegal text format -- jX\n", collect_err[0]); +} + + +void test_alignment_notset(void **state) +{ + (void) state; /* unused */ + + opt_t *actual = act(2, "-a", ""); + + assert_null(actual); // invalid option, so we would need to exit with error + assert_int_equal(1, collect_err_size); + assert_string_equal("boxes: Illegal text format -- \n", collect_err[0]); +} + + +void test_alignment_incomplete(void **state) +{ + (void) state; /* unused */ + + opt_t *actual = act(2, "-a", "v"); + + assert_null(actual); // invalid option, so we would need to exit with error + assert_int_equal(1, collect_err_size); + assert_string_equal("boxes: Illegal text format -- v\n", collect_err[0]); +} + + /*EOF*/ /* vim: set cindent sw=4: */ diff --git a/utest/cmdline_test.h b/utest/cmdline_test.h index 7b5b54c..f6a0226 100644 --- a/utest/cmdline_test.h +++ b/utest/cmdline_test.h @@ -51,6 +51,12 @@ void test_tabstops_4e(void **state); void test_tabstops_4ex(void **state); void test_tabstops_7(void **state); +void test_alignment_invalid_hX(void **state); +void test_alignment_invalid_vX(void **state); +void test_alignment_invalid_jX(void **state); +void test_alignment_notset(void **state); +void test_alignment_incomplete(void **state); + #endif diff --git a/utest/main.c b/utest/main.c index 0110565..4d8da39 100644 --- a/utest/main.c +++ b/utest/main.c @@ -68,7 +68,12 @@ int main(void) cmocka_unit_test_setup(test_tabstops_4X, beforeTest), cmocka_unit_test_setup(test_tabstops_4e, beforeTest), cmocka_unit_test_setup(test_tabstops_4ex, beforeTest), - cmocka_unit_test_setup(test_tabstops_7, beforeTest) + cmocka_unit_test_setup(test_tabstops_7, beforeTest), + cmocka_unit_test_setup(test_alignment_invalid_hX, beforeTest), + cmocka_unit_test_setup(test_alignment_invalid_vX, beforeTest), + cmocka_unit_test_setup(test_alignment_invalid_jX, beforeTest), + cmocka_unit_test_setup(test_alignment_notset, beforeTest), + cmocka_unit_test_setup(test_alignment_incomplete, beforeTest) }; const struct CMUnitTest regulex_tests[] = {