Add some unit tests for tab option parsing (-t)

This commit is contained in:
Thomas Jensen 2021-11-05 14:28:13 +01:00
parent f13207318a
commit fbc4678faa
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
4 changed files with 88 additions and 3 deletions

View File

@ -462,7 +462,7 @@ static int tab_handling(opt_t *result, char *optarg)
char *p;
int width = (int) strtol(optarg, &p, 10);
if (width < 1 || width > MAX_TABSTOP) {
fprintf(stderr, "%s: invalid tab stop distance -- %d\n", PROJECT, width);
bx_fprintf(stderr, "%s: invalid tab stop distance -- %d\n", PROJECT, width);
return 1;
}
result->tabstop = width;
@ -490,7 +490,7 @@ static int tab_handling(opt_t *result, char *optarg)
}
}
if (errfl) {
fprintf(stderr, "%s: invalid tab handling specification - %s\n", PROJECT, optarg);
bx_fprintf(stderr, "%s: invalid tab handling specification - %s\n", PROJECT, optarg);
return 1;
}
return 0;

View File

@ -235,4 +235,76 @@ void test_padding_novalue(void **state)
}
void test_tabstops_zero(void **state)
{
(void) state; /* unused */
opt_t *actual = act(2, "-t", "0");
assert_null(actual); // invalid option, so we would need to exit with error
assert_int_equal(1, collect_err_size);
assert_string_equal("boxes: invalid tab stop distance -- 0\n", collect_err[0]);
}
void test_tabstops_500(void **state)
{
(void) state; /* unused */
opt_t *actual = act(2, "-t", "500");
assert_null(actual); // invalid option, so we would need to exit with error
assert_int_equal(1, collect_err_size);
assert_string_equal("boxes: invalid tab stop distance -- 500\n", collect_err[0]);
}
void test_tabstops_4X(void **state)
{
(void) state; /* unused */
opt_t *actual = act(2, "-t", "4X");
assert_null(actual); // invalid option, so we would need to exit with error
assert_int_equal(1, collect_err_size);
assert_string_equal("boxes: invalid tab handling specification - 4X\n", collect_err[0]);
}
void test_tabstops_4e(void **state)
{
(void) state; /* unused */
opt_t *actual = act(2, "-t", "4e");
assert_non_null(actual);
assert_int_equal(4, actual->tabstop);
assert_int_equal((int) 'e', (int) actual->tabexp);
}
void test_tabstops_4ex(void **state)
{
(void) state; /* unused */
opt_t *actual = act(2, "-t", "4ex");
assert_null(actual); // invalid option, so we would need to exit with error
assert_int_equal(1, collect_err_size);
assert_string_equal("boxes: invalid tab handling specification - 4ex\n", collect_err[0]);
}
void test_tabstops_7(void **state)
{
(void) state; /* unused */
opt_t *actual = act(2, "-t", "7");
assert_non_null(actual);
assert_int_equal(7, actual->tabstop);
assert_int_equal((int) 'e', (int) actual->tabexp);
}
/*EOF*/ /* vim: set cindent sw=4: */

View File

@ -44,6 +44,13 @@ void test_padding_notset(void **state);
void test_padding_invalid_value(void **state);
void test_padding_novalue(void **state);
void test_tabstops_zero(void **state);
void test_tabstops_500(void **state);
void test_tabstops_4X(void **state);
void test_tabstops_4e(void **state);
void test_tabstops_4ex(void **state);
void test_tabstops_7(void **state);
#endif

View File

@ -62,7 +62,13 @@ int main(void)
cmocka_unit_test_setup(test_padding_negative, beforeTest),
cmocka_unit_test_setup(test_padding_notset, beforeTest),
cmocka_unit_test_setup(test_padding_invalid_value, beforeTest),
cmocka_unit_test_setup(test_padding_novalue, beforeTest)
cmocka_unit_test_setup(test_padding_novalue, beforeTest),
cmocka_unit_test_setup(test_tabstops_zero, beforeTest),
cmocka_unit_test_setup(test_tabstops_500, beforeTest),
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)
};
const struct CMUnitTest regulex_tests[] = {