[A] Added test for an empty table

This commit is contained in:
seleznevae 2018-01-03 10:10:54 +03:00
parent e52347948c
commit f528c48c78
2 changed files with 59 additions and 3 deletions

View File

@ -715,11 +715,13 @@ static fort_row_t* create_row_from_string(const char *str)
char *pos = str_copy;
char *base_pos = str_copy;
int number_of_separators = 0;
while (*pos) {
pos = strchr(pos, FORT_COL_SEPARATOR);
if (pos != NULL) {
*(pos) = '\0';
++pos;
number_of_separators++;
}
fort_cell_t *cell = create_cell();
@ -743,6 +745,25 @@ static fort_row_t* create_row_from_string(const char *str)
base_pos = pos;
}
/* special case if in format string last cell is empty */
while (vector_size(row->cells) < (number_of_separators + 1)) {
fort_cell_t *cell = create_cell();
if (cell == NULL)
goto clear;
int status = fill_buffer_from_string(cell->str_buffer, "");
if (IS_ERROR(status)) {
destroy_cell(cell);
goto clear;
}
status = vector_push(row->cells, &cell);
if (IS_ERROR(status)) {
destroy_cell(cell);
goto clear;
}
}
F_FREE(str_copy);
return row;

View File

@ -170,7 +170,7 @@ void test_table_basic(void **state)
assert_true( n == 4 );
n = ft_row_printf(table, 1, "%c|%s|%f", 'c', "234", 3.14);
assert_true( n == 3 );
n = ft_row_printf(table, 2, "%s|%f||%c", "234", 3.14, 'c');
n = ft_row_printf(table, 2, "%s|%f||", "234", 3.14);
assert_true( n == 4 );
char *table_str = ft_to_string(table);
@ -186,10 +186,45 @@ void test_table_basic(void **state)
"| | | | |\n"
"========================================\n"
"| | | | |\n"
"| 234 | 3.140000 | | c |\n"
"| 234 | 3.140000 | | |\n"
"| | | | |\n"
"========================================\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
WHEN("All cells are empty") {
table = ft_create_table();
int n = ft_hdr_printf(table, "|||");
assert_true( n == 4 );
n = ft_row_printf(table, 1, "|||");
assert_true( n == 4 );
n = ft_row_printf(table, 2, "|||");
assert_true( n == 4 );
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"=============\n"
"| | | | |\n"
"| | | | |\n"
"| | | | |\n"
"=============\n"
"| | | | |\n"
"| | | | |\n"
"| | | | |\n"
"=============\n"
"| | | | |\n"
"| | | | |\n"
"| | | | |\n"
"=============\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);