[A] Added text styles

This commit is contained in:
seleznevae
2018-11-10 09:58:21 +03:00
parent 018ecbd383
commit c2c887f6c6
16 changed files with 1648 additions and 149 deletions

View File

@@ -137,6 +137,118 @@ void test_table_cell_properties(void)
ft_table_t *table = NULL;
WHEN("Setting property for one cell") {
set_test_properties_as_default();
table = create_test_int_table(0);
ft_set_cell_prop(table, 1, 1, FT_CPROP_TOP_PADDING, 2);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+---+---+----+----+\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"+---+---+----+----+\n"
"| | | | |\n"
"| 3 | | 55 | 67 |\n"
"| | 4 | | |\n"
"| | | | |\n"
"+---+---+----+----+\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"+---+---+----+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
WHEN("Setting property for the row") {
set_test_properties_as_default();
table = create_test_int_table(0);
ft_set_cell_prop(table, 1, FT_ANY_COLUMN, FT_CPROP_TOP_PADDING, 2);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+---+---+----+----+\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"+---+---+----+----+\n"
"| | | | |\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"+---+---+----+----+\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"+---+---+----+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
WHEN("Setting property for the column") {
set_test_properties_as_default();
table = create_test_int_table(0);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TOP_PADDING, 2);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+---+---+----+----+\n"
"| | | | |\n"
"| 3 | | 55 | 67 |\n"
"| | 4 | | |\n"
"| | | | |\n"
"+---+---+----+----+\n"
"| | | | |\n"
"| 3 | | 55 | 67 |\n"
"| | 4 | | |\n"
"| | | | |\n"
"+---+---+----+----+\n"
"| | | | |\n"
"| 3 | | 55 | 67 |\n"
"| | 4 | | |\n"
"| | | | |\n"
"+---+---+----+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
WHEN("Setting property for all cells in the table") {
set_test_properties_as_default();
table = create_test_int_table(0);
ft_set_cell_prop(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_CPROP_TOP_PADDING, 2);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+---+---+----+----+\n"
"| | | | |\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"+---+---+----+----+\n"
"| | | | |\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"+---+---+----+----+\n"
"| | | | |\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"+---+---+----+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
WHEN("All paddings = 1") {
set_test_properties_as_default();
@@ -553,3 +665,248 @@ void test_table_cell_properties(void)
ft_destroy_table(table);
}
}
void test_table_text_styles(void)
{
ft_table_t *table = NULL;
set_test_properties_as_default();
WHEN("Simple table with one cell and foreground content color") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW) == FT_SUCCESS);
assert(ft_write(table, "42") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+----+\n"
"|\033[33m\033[39m |\n"
"| \033[33m42\033[39m |\n"
"|\033[33m\033[39m |\n"
"+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#ifdef FT_HAVE_WCHAR
WHEN("Simple table with one cell and foreground color(wide strings case)") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW) == FT_SUCCESS);
assert(ft_wwrite(table, L"42") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
const wchar_t *table_str_etalon =
L"+----+\n"
L"|\033[33m\033[39m |\n"
L"| \033[33m42\033[39m |\n"
L"|\033[33m\033[39m |\n"
L"+----+\n";
assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#endif
WHEN("Simple table with one cell and background content color") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_BG_COLOR, FT_COLOR_YELLOW) == FT_SUCCESS);
assert(ft_write(table, "42") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+----+\n"
"|\033[43m\033[49m |\n"
"| \033[43m42\033[49m |\n"
"|\033[43m\033[49m |\n"
"+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#ifdef FT_HAVE_WCHAR
WHEN("Simple table with one cell and background content color(wide strings case)") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_BG_COLOR, FT_COLOR_YELLOW) == FT_SUCCESS);
assert(ft_wwrite(table, L"42") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
const wchar_t *table_str_etalon =
L"+----+\n"
L"|\033[43m\033[49m |\n"
L"| \033[43m42\033[49m |\n"
L"|\033[43m\033[49m |\n"
L"+----+\n";
assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#endif
WHEN("Simple table with one cell and background cell color") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_BG_COLOR, FT_COLOR_YELLOW) == FT_SUCCESS);
assert(ft_write(table, "42") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+----+\n"
"|\033[43m \033[49m|\n"
"|\033[43m 42 \033[49m|\n"
"|\033[43m \033[49m|\n"
"+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#ifdef FT_HAVE_WCHAR
WHEN("Simple table with one cell and background cell color(wide strings case)") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_BG_COLOR, FT_COLOR_YELLOW) == FT_SUCCESS);
assert(ft_wwrite(table, L"42") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
const wchar_t *table_str_etalon =
L"+----+\n"
L"|\033[43m \033[49m|\n"
L"|\033[43m 42 \033[49m|\n"
L"|\033[43m \033[49m|\n"
L"+----+\n";
assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#endif
WHEN("Simple table with one cell and content style") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS);
assert(ft_write(table, "42") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+----+\n"
"|\033[4m\033[24m |\n"
"| \033[4m42\033[24m |\n"
"|\033[4m\033[24m |\n"
"+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#ifdef FT_HAVE_WCHAR
WHEN("Simple table with one cell and content style(wide strings case)") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS);
assert(ft_wwrite(table, L"42") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
const wchar_t *table_str_etalon =
L"+----+\n"
L"|\033[4m\033[24m |\n"
L"| \033[4m42\033[24m |\n"
L"|\033[4m\033[24m |\n"
L"+----+\n";
assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#endif
WHEN("Simple table with one cell and cell style") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS);
assert(ft_write(table, "42") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+----+\n"
"|\033[4m \033[24m|\n"
"|\033[4m 42 \033[24m|\n"
"|\033[4m \033[24m|\n"
"+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#ifdef FT_HAVE_WCHAR
WHEN("Simple table with one cell and cell style(wide strings case)") {
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS);
assert(ft_wwrite(table, L"42") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
const wchar_t *table_str_etalon =
L"+----+\n"
L"|\033[4m \033[24m|\n"
L"|\033[4m 42 \033[24m|\n"
L"|\033[4m \033[24m|\n"
L"+----+\n";
assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#endif
WHEN("Simple table with one cell background color, content foreground color and style.") {
set_test_properties_as_default();
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW) == FT_SUCCESS);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_BG_COLOR, FT_COLOR_RED) == FT_SUCCESS);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS);
assert(ft_write(table, "42") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+----+\n"
"|\033[41m\033[4m\033[33m\033[24m\033[39m \033[49m|\n"
"|\033[41m \033[4m\033[33m42\033[24m\033[39m \033[49m|\n"
"|\033[41m\033[4m\033[33m\033[24m\033[39m \033[49m|\n"
"+----+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#ifdef FT_HAVE_WCHAR
WHEN("Simple table with one cell background color, content foreground color and style.") {
set_test_properties_as_default();
table = ft_create_table();
assert(table);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW) == FT_SUCCESS);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_BG_COLOR, FT_COLOR_RED) == FT_SUCCESS);
assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS);
assert(ft_wwrite(table, L"42") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
const wchar_t *table_str_etalon =
L"+----+\n"
L"|\033[41m\033[4m\033[33m\033[24m\033[39m \033[49m|\n"
L"|\033[41m \033[4m\033[33m42\033[24m\033[39m \033[49m|\n"
L"|\033[41m\033[4m\033[33m\033[24m\033[39m \033[49m|\n"
L"+----+\n";
assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
#endif
}

View File

@@ -24,6 +24,7 @@ struct test_case bb_test_suit [] = {
{"test_table_builtin_border_styles", test_table_builtin_border_styles},
{"test_table_cell_properties", test_table_cell_properties},
{"test_table_tbl_properties", test_table_tbl_properties},
{"test_table_text_styles", test_table_text_styles},
{"test_memory_errors", test_memory_errors},
};
@@ -52,15 +53,146 @@ int main(void)
* Essential for OSX, because swprintf can fail in case of real unicode
* chars.
*/
#if defined(FT_HAVE_WCHAR)
//#if defined(FT_HAVE_WCHAR)
// setlocale(LC_CTYPE, "");
//#endif
//#ifdef FORT_WB_TESTING_ENABLED
// status |= run_wb_test_suit();
// fprintf(stderr, "\n");
//#endif
// status |= run_bb_test_suit();
setlocale(LC_CTYPE, "");
#endif
#ifdef FORT_WB_TESTING_ENABLED
status |= run_wb_test_suit();
fprintf(stderr, "\n");
#endif
status |= run_bb_test_suit();
ft_table_t *table = NULL;
table = ft_create_table();
ft_set_border_style(table, FT_DOUBLE2_STYLE);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_wwrite_ln(table, L"some", L"some3333", L"11111");
ft_wwrite_ln(table, L"s", L"some333377777", L"1111111111");
ft_wwrite_ln(table, L"some", L"some3333", L"111111");
ft_wwrite_ln(table, L"", L"", L"");
ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
ft_set_cell_prop(table, 2, 1, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
ft_set_cell_prop(table, 0, 1, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW);
ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_BG_COLOR, FT_COLOR_MAGENTA);
ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
ft_set_cell_prop(table, 1, 1, FT_CPROP_CONT_BG_COLOR, FT_COLOR_RED);
ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_BG_COLOR, FT_COLOR_RED);
ft_set_cell_prop(table, 3, FT_ANY_COLUMN, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN);
ft_set_cell_prop(table, 3, FT_ANY_COLUMN, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD);
const wchar_t *table_wstr = ft_to_wstring(table);
if (table_wstr) {
fwprintf(stdout, L"Table:\n%ls\n ", table_wstr);
} else {
fwprintf(stdout, L"Table conversion failed !!!\n ");
}
ft_destroy_table(table);
// /////////////////////////
table = ft_create_table();
ft_set_border_style(table, FT_DOUBLE_STYLE);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_wwrite_ln(table, L"Test", L"Iterations", L"ms/op", L"Passed");
ft_wwrite_ln(table, L"n-body", L"1000", L"1.629");
ft_wwrite_ln(table, L"", L"2500", L"6.3", L"");
ft_wwrite_ln(table, L"", L"10000", L"10.5");
ft_add_separator(table);
ft_wwrite_ln(table, L"regex-redux", L"1000", L"1.629");
ft_wwrite_ln(table, L"", L"2500", L"6.3", L"");
ft_wwrite_ln(table, L"", L"10000", L"10.5");
ft_add_separator(table);
ft_wwrite_ln(table, L"mandelbrot", L"1000", L"1.629");
ft_wwrite_ln(table, L"", L"2500", L"6.3", L"");
ft_wwrite_ln(table, L"", L"10000", L"10.5");
ft_add_separator(table);
ft_set_cell_span(table, 10, 0, 3);
ft_wwrite_ln(table, L"Total result", L"", L"", L"");
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD);
ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_RIGHT);
ft_set_cell_prop(table, FT_ANY_ROW, 3, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, 10, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, 2, 3, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN);
ft_set_cell_prop(table, 5, 3, FT_CPROP_CONT_FG_COLOR, FT_COLOR_RED);
ft_set_cell_prop(table, 8, 3, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN);
// ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
// ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
// ft_set_cell_prop(table, 2, 1, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
// ft_set_cell_prop(table, 0, 1, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW);
// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_BG_COLOR, FT_COLOR_MAGENTA);
// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
// ft_set_cell_prop(table, 1, 1, FT_CPROP_CONT_BG_COLOR, FT_COLOR_RED);
// ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_BG_COLOR, FT_COLOR_RED);
// ft_set_cell_prop(table, 3, FT_ANY_COLUMN, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN);
// ft_set_cell_prop(table, 3, FT_ANY_COLUMN, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD);
table_wstr = ft_to_wstring(table);
if (table_wstr) {
fwprintf(stdout, L"Table:\n%ls\n ", table_wstr);
} else {
fwprintf(stdout, L"Table conversion failed !!!\n ");
}
ft_destroy_table(table);
// printf("\n\n------ Some debug stuff delete later --------\n\n\n");
// ft_table_t *table = ft_create_table();
// ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_FG_COLOR, FT_COLOR_RED);
// ft_set_cell_prop(table, 1, 1, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN);
// ft_set_cell_prop(table, FT_ANY_ROW, 2, FT_CPROP_CONT_FG_COLOR, FT_COLOR_MAGENTA);
// ft_set_cell_prop(table, FT_ANY_ROW, 2, FT_CPROP_CONT_BG_COLOR, FT_COLOR_YELLOW);
// ft_set_cell_prop(table, 0, 1, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW);
// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_BG_COLOR, FT_COLOR_MAGENTA);
// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
// ft_set_cell_prop(table, 2, FT_ANY_COLUMN, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_BOLD);
// ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
// ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
// ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
// ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
// ft_set_cell_prop(table, 2, 1, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED);
// ft_write_ln(table, "some", "some3333", "11111");
// ft_write_ln(table, "s", "some333377777", "1111111111");
// ft_write_ln(table, "some", "some3333", "111111");
// const char *str_repr = ft_to_string(table);
// printf("%s\n", str_repr ? str_repr : "NULL");
// ft_destroy_table(table);
// printf("aa \033[42m 11 \033[4m 222 \033[49m 123 \033[24m 666\n");
return status;
}

View File

@@ -33,6 +33,7 @@ void test_table_write(void);
void test_table_border_style(void);
void test_table_builtin_border_styles(void);
void test_table_cell_properties(void);
void test_table_text_styles(void);
void test_table_tbl_properties(void);
void test_memory_errors(void);