From 9bce3f259cf8bd7fc15b85e187caf626eceb9e6d Mon Sep 17 00:00:00 2001 From: seleznevae Date: Wed, 25 Jul 2018 22:37:10 +0300 Subject: [PATCH] [A] Added c++ tests --- CMakeLists.txt | 8 +++ example/main.cpp | 9 +++ lib/fort.hpp | 144 +++++++++++++++++++++++++++++++++++++++- tests/main_test.c | 14 ---- tests/main_test_cpp.cpp | 27 ++++++++ tests/test_utils.c | 17 +++-- tests/tests.h | 10 ++- 7 files changed, 209 insertions(+), 20 deletions(-) create mode 100644 tests/main_test_cpp.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 993a37a..06e32f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,6 +118,14 @@ add_executable(${PROJECT_NAME}_test ${TEST_SOURCES}) +set(TEST_SOURCES_CPP + tests/main_test_cpp.cpp + tests/bb_tests_cpp/test_table_basic.cpp + tests/test_utils.c) +add_executable(${PROJECT_NAME}_test_cpp + lib/fort.c + ${TEST_SOURCES_CPP}) + if(FORT_CXX_BUILD) SET_SOURCE_FILES_PROPERTIES( ${FORT_DEV_SOURCES} PROPERTIES LANGUAGE CXX) SET_SOURCE_FILES_PROPERTIES( ${EXAMPLE_SOURCES} PROPERTIES LANGUAGE CXX) diff --git a/example/main.cpp b/example/main.cpp index b66f67c..7ce16af 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -31,6 +31,15 @@ int main() table.write_ln("4", "2001: A Space Odyssey", "1968", "8.5"); table.write_ln("5", "Blade Runner", "1982", "8.1"); + using fort::CellOption; + using fort::TableOption; + table.set_option(0, 0, 20); + table.set_option(0, 0, fort::TextAlign::Left); + table.set_option(2, FT_ANY_COLUMN, fort::RowType::Header); + + + table.set_option(4); + table.set_border_style(FT_SOLID_STYLE); std::cout << table.to_string(); } diff --git a/lib/fort.hpp b/lib/fort.hpp index 2798e4f..08e2f97 100644 --- a/lib/fort.hpp +++ b/lib/fort.hpp @@ -42,6 +42,35 @@ SOFTWARE. namespace fort { +enum class CellOption { + MinWidth, + TextAlign, + TopPadding, + BottomPadding, + LeftPadding, + RightPading, + EmptyStrHeight, + RowType +}; + +enum class TableOption { + LeftMargin, + TopMargin, + RightMargin, + BottomMargin, +}; + +enum class TextAlign { + Left, + Center, + Right +}; + +enum class RowType { + Common, + Header +}; + class TableManipulator { public: explicit TableManipulator(int i) @@ -268,6 +297,54 @@ public: #endif /* __cpp_variadic_templates */ + /** + * Set min width for the specified cell of the table. + * + * @param row + * Cell row. + * @param col + * Cell column. + * @param value + * Value of the min width. + * @return + * - 0: Success; cell option was changed. + * - (<0): In case of error + */ + bool set_cell_min_width(size_t row, size_t col, unsigned value) + { + return FT_IS_SUCCESS(ft_set_cell_option(table, row, col, FT_COPT_MIN_WIDTH, value)); + } + + /** + * Set bottom padding for the specified cell of the table. + * + * @param row + * Cell row. + * @param col + * Cell column. + * @param value + * Value of the bottom padding. + * @return + * - 0: Success; cell option was changed. + * - (<0): In case of error + */ + bool set_cell_bottom_padding(size_t row, size_t col, unsigned value) + { + return FT_IS_SUCCESS(ft_set_cell_option(table, row, col, FT_COPT_BOTTOM_PADDING, value)); + } + + template + bool set_option(size_t row, size_t col, unsigned value); + + template + bool set_option(size_t row, size_t col, TextAlign align); + + template + bool set_option(size_t row, size_t col, RowType rowType); + + template + bool set_option(unsigned value); + /** * Set border style for the table. * @@ -282,12 +359,77 @@ public: return FT_IS_SUCCESS(ft_set_border_style(table, style)); } - + /** + * Set default border style for all new formatted tables. + * + * @param style + * Pointer to border style. + * @return + * - True: Success; table border style was changed. + * - False: Error + */ + bool set_default_border_style(struct ft_border_style *style) + { + return FT_IS_SUCCESS(ft_set_default_border_style(style)); + } private: ft_table_t *table; std::stringstream stream; }; + + + + + +/* + * Declare specializations for set_option functions + */ +#define DECLARE_SPECS_FOR_CELL_OPTIONS_X \ + SET_CELL_OPTION_SPEC(CellOption::MinWidth, FT_COPT_MIN_WIDTH, unsigned) \ + SET_CELL_OPTION_SPEC(CellOption::TextAlign, FT_COPT_TEXT_ALIGN, TextAlign) \ + SET_CELL_OPTION_SPEC(CellOption::TopPadding, FT_COPT_TOP_PADDING, unsigned) \ + SET_CELL_OPTION_SPEC(CellOption::BottomPadding, FT_COPT_BOTTOM_PADDING, unsigned) \ + SET_CELL_OPTION_SPEC(CellOption::LeftPadding, FT_COPT_LEFT_PADDING, unsigned) \ + SET_CELL_OPTION_SPEC(CellOption::RightPading, FT_COPT_RIGHT_PADDING, unsigned) \ + SET_CELL_OPTION_SPEC(CellOption::EmptyStrHeight, FT_COPT_EMPTY_STR_HEIGHT, unsigned) \ + SET_CELL_OPTION_SPEC(CellOption::RowType, FT_COPT_ROW_TYPE, RowType) + +#define SET_CELL_OPTION_SPEC(CELL_OPTION, C_OPTION, VALUE_TYPE) \ +template <> \ +bool Table::set_option(size_t row, size_t col, VALUE_TYPE value) \ +{ \ + return FT_IS_SUCCESS(ft_set_cell_option(table, row, col, C_OPTION, static_cast(value))); \ +} + +DECLARE_SPECS_FOR_CELL_OPTIONS_X + +#undef SET_TABLE_OPTION_SPEC +#undef DECLARE_SPECS_FOR_CELL_OPTIONS_X + + + +#define DECLARE_SPECS_FOR_TABLE_OPTIONS_X \ + SET_TABLE_OPTION_SPEC(TableOption::LeftMargin, FT_TOPT_LEFT_MARGIN) \ + SET_TABLE_OPTION_SPEC(TableOption::TopMargin, FT_TOPT_TOP_MARGIN) \ + SET_TABLE_OPTION_SPEC(TableOption::RightMargin, FT_TOPT_RIGHT_MARGIN) \ + SET_TABLE_OPTION_SPEC(TableOption::BottomMargin, FT_TOPT_BOTTOM_MARGIN) + +#define SET_TABLE_OPTION_SPEC(TABLE_OPTION, TBL_OPTION) \ +template <> \ +bool Table::set_option(unsigned value) \ +{ \ + return FT_IS_SUCCESS(ft_set_tbl_option(table, TBL_OPTION, static_cast(value))); \ +} + +DECLARE_SPECS_FOR_TABLE_OPTIONS_X + +#undef SET_TABLE_OPTION_SPEC +#undef DECLARE_SPECS_FOR_TABLE_OPTIONS_X + + + + } #endif // LIBFORT_HPP diff --git a/tests/main_test.c b/tests/main_test.c index 458dd2e..b16fb34 100644 --- a/tests/main_test.c +++ b/tests/main_test.c @@ -2,20 +2,6 @@ #include #include "fort.h" -void run_test_suit(const char *test_suit_name, int n_tests, struct test_case test_suit[]) -{ - fprintf(stderr, " == RUNNING %s ==\n", test_suit_name); - fprintf(stderr, "[==========] Running %d test(s).\n", n_tests); - int i; - for (i = 0; i < n_tests; ++i) { - fprintf(stderr, "[ RUN ] %s\n", test_suit[i].name); - test_suit[i].test(); - fprintf(stderr, "[ OK ] %s\n", test_suit[i].name); - } - fprintf(stderr, "[==========] %d test(s) run.\n", n_tests); - fprintf(stderr, "[ PASSED ] %d test(s).\n", n_tests); -} - #ifdef FORT_WB_TESTING_ENABLED struct test_case wb_test_suit [] = { {"test_vector_basic", test_vector_basic}, diff --git a/tests/main_test_cpp.cpp b/tests/main_test_cpp.cpp new file mode 100644 index 0000000..ccd8159 --- /dev/null +++ b/tests/main_test_cpp.cpp @@ -0,0 +1,27 @@ +#include "tests.h" +#include +#include "fort.h" + +void test_cpp_table_basic(void); + + +struct test_case bb_test_suit [] = { + {"test_cpp_table_basic", test_cpp_table_basic}, +}; + + +int run_bb_test_suit(void) +{ + int bb_n_tests = sizeof(bb_test_suit) / sizeof(bb_test_suit[0]); + run_test_suit("BLACK BOX TEST SUITE", bb_n_tests, bb_test_suit); + return 0; +} + +int main(void) +{ + int status = 0; + + status |= run_bb_test_suit(); + + return status; +} diff --git a/tests/test_utils.c b/tests/test_utils.c index dddf101..d39c0b6 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -132,10 +132,19 @@ struct ft_table *create_test_int_wtable(int set_test_opts) #endif - - - - +void run_test_suit(const char *test_suit_name, int n_tests, struct test_case test_suit[]) +{ + fprintf(stderr, " == RUNNING %s ==\n", test_suit_name); + fprintf(stderr, "[==========] Running %d test(s).\n", n_tests); + int i; + for (i = 0; i < n_tests; ++i) { + fprintf(stderr, "[ RUN ] %s\n", test_suit[i].name); + test_suit[i].test(); + fprintf(stderr, "[ OK ] %s\n", test_suit[i].name); + } + fprintf(stderr, "[==========] %d test(s) run.\n", n_tests); + fprintf(stderr, "[ PASSED ] %d test(s).\n", n_tests); +} diff --git a/tests/tests.h b/tests/tests.h index 9cd7d55..f976bf3 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -67,11 +67,19 @@ struct test_case { } struct ft_table; + +#ifdef __cplusplus +extern "C" { +#endif + int set_test_options_for_table(struct ft_table *table); int set_test_options_as_default(void); struct ft_table *create_test_int_table(int set_test_opts); struct ft_table *create_test_int_wtable(int set_test_opts); +void run_test_suit(const char *test_suit_name, int n_tests, struct test_case test_suit[]); - +#ifdef __cplusplus +} +#endif #endif // TESTS_H