Add unit tests for alignment option parsing (-a)

This commit is contained in:
Thomas Jensen 2021-11-05 21:10:11 +01:00
parent fbc4678faa
commit 413b174d6c
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
4 changed files with 80 additions and 8 deletions

View File

@ -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);

View File

@ -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: */

View File

@ -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

View File

@ -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[] = {