From c53022082c48ac469bd692c69927a037c5291b38 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Wed, 21 Nov 2018 20:50:56 +0300 Subject: [PATCH] [A] Added more cpp tests. --- CMakeLists.txt | 4 +- example/main.cpp | 41 +- lib/fort.hpp | 706 +++++++++++-------- tests/bb_tests_cpp/test_table_basic.cpp | 70 +- tests/bb_tests_cpp/test_table_properties.cpp | 524 ++++++++++++++ tests/main_test_cpp.cpp | 7 + tests/test_utils.cpp | 50 ++ tests/tests.h | 8 + 8 files changed, 1029 insertions(+), 381 deletions(-) create mode 100644 tests/bb_tests_cpp/test_table_properties.cpp create mode 100644 tests/test_utils.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ab1d5da..f8c34e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,7 +141,9 @@ add_executable(${PROJECT_NAME}_test set(TEST_SOURCES_CPP tests/main_test_cpp.cpp tests/bb_tests_cpp/test_table_basic.cpp - tests/test_utils.c) + tests/bb_tests_cpp/test_table_properties.cpp + tests/test_utils.c + tests/test_utils.cpp) add_executable(${PROJECT_NAME}_test_cpp lib/fort.c ${TEST_SOURCES_CPP}) diff --git a/example/main.cpp b/example/main.cpp index d249c65..d2471e0 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -2,9 +2,9 @@ #include #include "fort.hpp" -fort::Table create_basic_table(void) +fort::table create_basic_table(void) { - fort::Table table; + fort::table table; /* Set table border style */ table.set_border_style(FT_BASIC_STYLE); @@ -19,8 +19,8 @@ fort::Table create_basic_table(void) << "5" << "Blade Runner" << "1982" << "8.1" << fort::endr << fort::endr; - table.set_cell_text_align(FT_ANY_ROW, 0, fort::TextAlign::Center); - table.set_cell_text_align(FT_ANY_ROW, 1, fort::TextAlign::Left); + table.column(0).set_cell_text_align(fort::text_align::center); + table.column(1).set_cell_text_align(fort::text_align::left); std::cout << table.to_string() << std::endl; return table; @@ -28,7 +28,7 @@ fort::Table create_basic_table(void) void base_example(void) { - fort::Table table; + fort::table table; table << fort::header << "N" << "Driver" << "Time" << "Avg Speed" << fort::endr << "1" << "Ricciardo" << "1:25.945" << "47.362" << fort::endr @@ -40,7 +40,7 @@ void base_example(void) void different_cell_properties_example(void) { - fort::Table table; + fort::table table; /* Change border style */ table.set_border_style(FT_DOUBLE2_STYLE); @@ -51,8 +51,9 @@ void different_cell_properties_example(void) << "2001: A Space Odyssey" << "Stanley Kubrick" << "1968" << "8.5" << fort::endr; /* Set center alignment for the 1st and 3rd columns */ - table.set_cell_text_align(FT_ANY_ROW, 1, fort::TextAlign::Center); - table.set_cell_text_align(FT_ANY_ROW, 3, fort::TextAlign::Center); + table.column(1).set_cell_text_align(fort::text_align::center); + table.column(3).set_cell_text_align(fort::text_align::center); + std::cout << table.to_string() << std::endl; } @@ -60,7 +61,7 @@ void different_cell_properties_example(void) void fill_table_with_data_example(void) { - fort::Table table; + fort::table table; table << fort::header; /* Fill each cell with operator[] */ table [0][0] = "N"; @@ -91,7 +92,7 @@ int main() fill_table_with_data_example(); { - fort::Table table; + fort::table table; // Fill table with data table << fort::header << "Rank" << "Title" << "Year" << "Rating" << fort::endr @@ -103,18 +104,16 @@ int main() << "5" << "Blade Runner" << "1982" << "8.1" << fort::endr << fort::endr; - using fort::CellProperty; - using fort::TableProperty; - table.set_property(0, 0, 20); - table.set_property(0, 0, fort::TextAlign::Left); - table.set_property(2, FT_ANY_COLUMN, fort::RowType::Header); + table[0][0].set_cell_min_width(20); + table[0][0].set_cell_text_align(fort::text_align::left); + table[2].set_cell_row_type(fort::row_type::header); std::cout << table.to_string() << std::endl; } { - fort::Table table; + fort::table table; // Fill table with data table << fort::header; table.write_ln("Rank", "Title", "Year", "Rating"); @@ -124,14 +123,12 @@ int main() table.write_ln("4", "2001: A Space Odyssey", "1968", "8.5"); table.write_ln("5", "Blade Runner", "1982", "8.1"); - using fort::CellProperty; - using fort::TableProperty; - table.set_cell_min_width(0, 0, 20); - table.set_cell_text_align(0, 0, fort::TextAlign::Left); - table.set_cell_row_type(2, FT_ANY_COLUMN, fort::RowType::Header); + table[0][0].set_cell_min_width(20); + table[0][0].set_cell_text_align(fort::text_align::left); + table.row(2).set_cell_row_type(fort::row_type::header); - table.set_property(4); + table.set_left_margin(4); table.set_border_style(FT_SOLID_STYLE); std::cout << table.to_string(); diff --git a/lib/fort.hpp b/lib/fort.hpp index 4616e7d..b9bd392 100644 --- a/lib/fort.hpp +++ b/lib/fort.hpp @@ -37,125 +37,350 @@ SOFTWARE. #include #include #include + #include "fort.h" namespace fort { -enum class CellProperty { - MinWidth, - TextAlign, - TopPadding, - BottomPadding, - LeftPadding, - RightPading, - EmptyStrHeight, - RowType + +enum class text_align { + left = FT_ALIGNED_LEFT, + center = FT_ALIGNED_CENTER, + right = FT_ALIGNED_RIGHT }; -enum class TableProperty { - LeftMargin, - TopMargin, - RightMargin, - BottomMargin, +enum class row_type { + common = FT_ROW_COMMON, + header = FT_ROW_HEADER }; -enum class TextAlign { - Left = 0, - Center, - Right +enum class color { + default_color = FT_COLOR_DEFAULT, + black = FT_COLOR_BLACK, + red = FT_COLOR_RED, + green = FT_COLOR_GREEN, + yellow = FT_COLOR_YELLOW, + blue = FT_COLOR_BLUE, + magenta = FT_COLOR_MAGENTA, + cyan = FT_COLOR_CYAN, + light_gray = FT_COLOR_LIGHT_GRAY, + dark_gray = FT_COLOR_DARK_GRAY, + light_red = FT_COLOR_LIGHT_RED, + light_green = FT_COLOR_LIGHT_GREEN, + light_yellow = FT_COLOR_LIGHT_YELLOW, + light_blue = FT_COLOR_LIGHT_BLUE, + light_magenta = FT_COLOR_LIGHT_MAGENTA, + light_cyan = FT_COLOR_LIGHT_CYAN, + light_whyte = FT_COLOR_LIGHT_WHYTE }; -enum class RowType { - Common = 0, - Header +enum class text_style { + default_style = FT_TSTYLE_DEFAULT, + bold = FT_TSTYLE_BOLD, + dim = FT_TSTYLE_DIM, + italic = FT_TSTYLE_ITALIC, + underlined = FT_TSTYLE_UNDERLINED, + blink = FT_TSTYLE_BLINK, + inverted = FT_TSTYLE_INVERTED, + hidden = FT_TSTYLE_HIDDEN }; -class TableManipulator { +class table_manipulator { public: - explicit TableManipulator(int i) + explicit table_manipulator(int i) :value(i) { } - friend class Table; + friend class table; private: int value; }; -const TableManipulator header(0); -const TableManipulator endr(1); -const TableManipulator separator(2); +const table_manipulator header(0); +const table_manipulator endr(1); +const table_manipulator separator(2); + + +template +class property_setter { +public: + + property_setter(std::size_t row_idx, std::size_t coll_idx, table &tbl) + :ps_row_idx_(row_idx), ps_coll_idx_(coll_idx), ps_table_(tbl) {} + + /** + * Set min width for the specified cell of the table. + * + * @param value + * Value of the min width. + * @return + * - true: Success; cell property was changed. + * - false: In case of error + */ + bool set_cell_min_width(unsigned value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_MIN_WIDTH, value)); + } + + /** + * Set text alignment for the specified cell of the table. + * + * @param value + * Value of the text alignment. + * @return + * - true: Success; cell property was changed. + * - false: In case of error + */ + bool set_cell_text_align(enum fort::text_align value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_TEXT_ALIGN, static_cast(value))); + } + + /** + * Set top padding for the specified cell of the table. + * + * @param value + * Value of the top padding. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_top_padding(unsigned value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_TOP_PADDING, value)); + } + + /** + * Set bottom padding for the specified cell of the table. + * + * @param value + * Value of the bottom padding. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_bottom_padding(unsigned value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_BOTTOM_PADDING, value)); + } + + /** + * Set left padding for the specified cell of the table. + * + * @param value + * Value of the left padding. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_left_padding(unsigned value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_LEFT_PADDING, value)); + } + + /** + * Set right padding for the specified cell of the table. + * + * @param value + * Value of the left padding. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_right_padding(unsigned value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_RIGHT_PADDING, value)); + } + + /** + * Set row type for the specified cell of the table. + * + * @param value + * Value of the row type. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_row_type(enum fort::row_type value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_ROW_TYPE, static_cast(value))); + } + + /** + * Set empty string height for the specified cell of the table. + * + * @param value + * Value of the empty string height. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_empty_str_height(unsigned value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_EMPTY_STR_HEIGHT, value)); + } + + /** + * Set content foreground color for the specified cell of the table. + * + * @param value + * Color. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_content_fg_color(enum fort::color value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CONT_FG_COLOR, static_cast(value))); + } + + /** + * Set cell background color for the specified cell of the table. + * + * @param value + * Color. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_cell_bg_color(enum fort::color value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CELL_BG_COLOR, static_cast(value))); + } + + /** + * Set content background color for the specified cell of the table. + * + * @param value + * Color. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_content_bg_color(enum fort::color value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CONT_BG_COLOR, static_cast(value))); + } + + /** + * Set cell text style for the specified cell of the table. + * + * @param value + * Text style. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_cell_text_style(enum fort::text_style value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CELL_TEXT_STYLE, static_cast(value))); + } + + /** + * Set content text style for the specified cell of the table. + * + * @param value + * Text style. + * @return + * - true: Success; cell property was changed. + * - false: In case of error. + */ + bool set_cell_content_text_style(enum fort::text_style value) + { + return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CONT_TEXT_STYLE, static_cast(value))); + } + + /** + * Set column span for the specified cell of the table. + * + * @param hor_span + * Column span. + * @return + * - true: Success; cell span was changed. + * - false: In case of error. + */ + bool set_cell_span(size_t hor_span) + { + return FT_IS_SUCCESS(ft_set_cell_span(ps_table_.table_, ps_row_idx_, ps_coll_idx_, hor_span)); + } +protected: + std::size_t ps_row_idx_; + std::size_t ps_coll_idx_; + table &ps_table_; +}; /** * Table - formatted table. * * Table class is a C++ wrapper around struct ft_table. */ -class Table { +class table: public property_setter { public: - Table() - :table(ft_create_table()) + table() + :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(ft_create_table()) { - if (table == NULL) + + if (table_ == NULL) throw std::runtime_error("Runtime error"); } - ~Table() + ~table() { - ft_destroy_table(table); + ft_destroy_table(table_); } /** * Copy contstructor. */ - Table(const Table& tbl) - :table(NULL) + table(const table& tbl) + :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(NULL) { - if (tbl.table) { - ft_table_t *table_copy = ft_copy_table(tbl.table); + if (tbl.table_) { + ft_table_t *table_copy = ft_copy_table(tbl.table_); if (table_copy == NULL) throw std::runtime_error("Runtime error"); - stream.str(std::string()); - if (tbl.stream.tellp() >= 0) { - stream << tbl.stream.str(); + stream_.str(std::string()); + if (tbl.stream_.tellp() >= 0) { + stream_ << tbl.stream_.str(); } - table = table_copy; + table_ = table_copy; } } /** * Move contstructor. */ - Table(Table&& tbl) - :table(tbl.table) + table(table&& tbl) + :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(tbl.table_) { - if (tbl.stream.tellp() >= 0) { - stream << tbl.stream.str(); - tbl.stream.str(std::string()); + if (tbl.stream_.tellp() >= 0) { + stream_ << tbl.stream_.str(); + tbl.stream_.str(std::string()); } - tbl.table = 0; + tbl.table_ = 0; } /** * Copy assignment operator. */ - Table& operator=(const Table& tbl) + table& operator=(const table& tbl) { if (&tbl == this) return *this; - if (tbl.table) { - ft_table_t *table_copy = ft_copy_table(tbl.table); + if (tbl.table_) { + ft_table_t *table_copy = ft_copy_table(tbl.table_); if (table_copy == NULL) throw std::runtime_error("Runtime error"); - stream.str(std::string()); - if (tbl.stream.tellp() >= 0) { - stream << tbl.stream.str(); + stream_.str(std::string()); + if (tbl.stream_.tellp() >= 0) { + stream_ << tbl.stream_.str(); } - ft_destroy_table(table); - table = table_copy; + ft_destroy_table(table_); + table_ = table_copy; } return *this; } @@ -163,20 +388,20 @@ public: /** * Move assignment operator. */ - Table& operator=(Table&& tbl) + table& operator=(table&& tbl) { if (&tbl == this) return *this; - if (tbl.table) { - stream.str(std::string()); - if (tbl.stream.tellp() >= 0) { - stream << tbl.stream.str(); - tbl.stream.str(std::string()); + if (tbl.table_) { + stream_.str(std::string()); + if (tbl.stream_.tellp() >= 0) { + stream_ << tbl.stream_.str(); + tbl.stream_.str(std::string()); } - ft_destroy_table(table); - table = tbl.table; - tbl.table = NULL; + ft_destroy_table(table_); + table_ = tbl.table_; + tbl.table_ = NULL; } return *this; } @@ -190,7 +415,7 @@ public: */ std::string to_string() const { - const char *str = ft_to_string(table); + const char *str = ft_to_string(table_); if (str == NULL) throw std::runtime_error("Runtime error"); return str; @@ -213,7 +438,7 @@ public: */ const char *c_str() const { - return ft_to_string(table); + return ft_to_string(table_); } /** @@ -228,35 +453,35 @@ public: * - Reference to the current table. */ template - Table &operator<<(const T &arg) + table &operator<<(const T &arg) { - stream << arg; - if (stream.tellp() >= 0) { - ft_nwrite(table, 1, stream.str().c_str()); - stream.str(std::string()); + stream_ << arg; + if (stream_.tellp() >= 0) { + ft_nwrite(table_, 1, stream_.str().c_str()); + stream_.str(std::string()); } return *this; } - Table &operator<<(const TableManipulator &arg) + table &operator<<(const table_manipulator &arg) { if (arg.value == header.value) - ft_set_cell_prop(table, FT_CUR_ROW, FT_ANY_ROW, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); + ft_set_cell_prop(table_, FT_CUR_ROW, FT_ANY_ROW, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); else if (arg.value == endr.value) - ft_ln(table); + ft_ln(table_); else if (arg.value == separator.value) - ft_add_separator(table); + ft_add_separator(table_); return *this; } bool write(const char *str) { - return FT_IS_SUCCESS(ft_write(table, str)); + return FT_IS_SUCCESS(ft_write(table_, str)); } bool write_ln(const char *str) { - return FT_IS_SUCCESS(ft_write_ln(table, str)); + return FT_IS_SUCCESS(ft_write_ln(table_, str)); } bool write(const std::string &str) @@ -388,166 +613,10 @@ public: *this << *first; ++first; } - ft_ln(table); + ft_ln(table_); return true; } - /** - * 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 property 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_prop(table, row, col, FT_CPROP_MIN_WIDTH, value)); - } - - /** - * Set text alignment for the specified cell of the table. - * - * @param row - * Cell row. - * @param col - * Cell column. - * @param value - * Value of the text alignment. - * @return - * - 0: Success; cell property was changed. - * - (<0): In case of error - */ - bool set_cell_text_align(size_t row, size_t col, enum TextAlign value) - { - return FT_IS_SUCCESS(ft_set_cell_prop(table, row, col, FT_CPROP_TEXT_ALIGN, static_cast(value))); - } - - /** - * Set top padding for the specified cell of the table. - * - * @param row - * Cell row. - * @param col - * Cell column. - * @param value - * Value of the top padding. - * @return - * - 0: Success; cell property was changed. - * - (<0): In case of error - */ - bool set_cell_top_padding(size_t row, size_t col, unsigned value) - { - return FT_IS_SUCCESS(ft_set_cell_prop(table, row, col, FT_CPROP_TOP_PADDING, 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 property 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_prop(table, row, col, FT_CPROP_BOTTOM_PADDING, value)); - } - - /** - * Set left padding for the specified cell of the table. - * - * @param row - * Cell row. - * @param col - * Cell column. - * @param value - * Value of the left padding. - * @return - * - 0: Success; cell property was changed. - * - (<0): In case of error - */ - bool set_cell_left_padding(size_t row, size_t col, unsigned value) - { - return FT_IS_SUCCESS(ft_set_cell_prop(table, row, col, FT_CPROP_LEFT_PADDING, value)); - } - - /** - * Set right padding for the specified cell of the table. - * - * @param row - * Cell row. - * @param col - * Cell column. - * @param value - * Value of the left padding. - * @return - * - 0: Success; cell property was changed. - * - (<0): In case of error - */ - bool set_cell_right_padding(size_t row, size_t col, unsigned value) - { - return FT_IS_SUCCESS(ft_set_cell_prop(table, row, col, FT_CPROP_RIGHT_PADDING, value)); - } - - /** - * Set empty string height for the specified cell of the table. - * - * @param row - * Cell row. - * @param col - * Cell column. - * @param value - * Value of the empty string height. - * @return - * - 0: Success; cell property was changed. - * - (<0): In case of error - */ - bool set_cell_empty_str_height(size_t row, size_t col, unsigned value) - { - return FT_IS_SUCCESS(ft_set_cell_prop(table, row, col, FT_CPROP_EMPTY_STR_HEIGHT, value)); - } - - /** - * Set row type for the specified cell of the table. - * - * @param row - * Cell row. - * @param col - * Cell column. - * @param value - * Value of the row type. - * @return - * - 0: Success; cell property was changed. - * - (<0): In case of error - */ - bool set_cell_row_type(size_t row, size_t col, enum RowType value) - { - return FT_IS_SUCCESS(ft_set_cell_prop(table, row, col, FT_CPROP_ROW_TYPE, static_cast(value))); - } - - template - bool set_property(size_t row, size_t col, unsigned value); - - template - bool set_property(size_t row, size_t col, enum TextAlign align); - - template - bool set_property(size_t row, size_t col, enum RowType rowType); - - template - bool set_property(unsigned value); - /** * Set border style for the table. * @@ -559,7 +628,7 @@ public: */ bool set_border_style(struct ft_border_style *style) { - return FT_IS_SUCCESS(ft_set_border_style(table, style)); + return FT_IS_SUCCESS(ft_set_border_style(table_, style)); } /** @@ -567,58 +636,115 @@ public: * * Current cell - cell that will be edited with all modifiing functions. * - * @param row + * @param row_i * New row number for the current cell. - * @param col + * @param col_i * New row number for the current cell. */ - void set_cur_cell(size_t row, size_t col) + void set_cur_cell(size_t row_i, size_t col_i) { - ft_set_cur_cell(table, row, col); + ft_set_cur_cell(table_, row_i, col_i); + } + + + /** + * Set table left margin. + * + * @param value + * Left margin. + * @return + * - true: Success; table property was changed. + * - false: In case of error. + */ + bool set_left_margin(unsigned value) + { + return FT_IS_SUCCESS(ft_set_tbl_prop(table_, FT_TPROP_LEFT_MARGIN, value)); + } + + /** + * Set table top margin. + * + * @param value + * Top margin. + * @return + * - true: Success; table property was changed. + * - false: In case of error. + */ + bool set_top_margin(unsigned value) + { + return FT_IS_SUCCESS(ft_set_tbl_prop(table_, FT_TPROP_TOP_MARGIN, value)); + } + + /** + * Set table right margin. + * + * @param value + * Right margin. + * @return + * - true: Success; table property was changed. + * - false: In case of error. + */ + bool set_right_margin(unsigned value) + { + return FT_IS_SUCCESS(ft_set_tbl_prop(table_, FT_TPROP_RIGHT_MARGIN, value)); + } + + /** + * Set table bottom margin. + * + * @param value + * Bottom margin. + * @return + * - true: Success; table property was changed. + * - false: In case of error. + */ + bool set_bottom_margin(unsigned value) + { + return FT_IS_SUCCESS(ft_set_tbl_prop(table_, FT_TPROP_BOTTOM_MARGIN, value)); } private: - ft_table_t *table; - mutable std::stringstream stream; + ft_table_t *table_; + mutable std::stringstream stream_; + friend class property_setter
; + public: /* Iterators */ /* todo: implement chains like table[0][0] = table [0][1] = "somethings" */ - class table_cell_iterator + class table_cell_iterator: public property_setter
{ public: - table_cell_iterator(std::size_t row_idx, std::size_t coll_idx, Table &tbl) - :row_idx_(row_idx), coll_idx_(coll_idx), table_(tbl) {} + table_cell_iterator(std::size_t row_idx, std::size_t coll_idx, table &tbl) + :property_setter(row_idx, coll_idx, tbl) {} table_cell_iterator& operator=(const char *str) { - ft_set_cur_cell(table_.table, row_idx_, coll_idx_); - table_.write(str); + ft_set_cur_cell(ps_table_.table_, ps_row_idx_, ps_coll_idx_); + ps_table_.write(str); return *this; } - - private: - std::size_t row_idx_; - std::size_t coll_idx_; - Table &table_; }; - class table_row_iterator + class table_row_iterator: public property_setter
{ public: - table_row_iterator(std::size_t row_idx, Table &tbl) - :row_idx_(row_idx), table_(tbl) {} + table_row_iterator(std::size_t row_idx, table &tbl) + :property_setter(row_idx, FT_ANY_COLUMN, tbl) {} class table_cell_iterator operator[](std::size_t coll_idx) { - return table_cell_iterator(row_idx_, coll_idx, table_); + return table_cell_iterator(ps_row_idx_, coll_idx, ps_table_); } + }; - private: - std::size_t row_idx_; - Table &table_; + class table_column_iterator: public property_setter
+ { + public: + table_column_iterator(std::size_t col_idx, table &tbl) + :property_setter(FT_ANY_ROW, col_idx, tbl) {} }; class table_row_iterator @@ -626,6 +752,19 @@ public: { return table_row_iterator(row_idx, *this); } + + class table_row_iterator + row(std::size_t row_idx) + { + return table_row_iterator(row_idx, *this); + } + + class table_column_iterator + column(std::size_t col_idx) + { + return table_column_iterator(col_idx, *this); + } + }; @@ -638,61 +777,12 @@ public: * - True: Success; table border style was changed. * - False: Error */ -bool set_default_border_style(struct ft_border_style *style) +inline bool set_default_border_style(struct ft_border_style *style) { return FT_IS_SUCCESS(ft_set_default_border_style(style)); } - -/* - * Declare specializations for set_property functions - */ -#define DECLARE_SPECS_FOR_CELL_PROPS_X \ - SET_CELL_PROP_SPEC(CellProperty::MinWidth, FT_CPROP_MIN_WIDTH, unsigned) \ - SET_CELL_PROP_SPEC(CellProperty::TextAlign, FT_CPROP_TEXT_ALIGN, TextAlign) \ - SET_CELL_PROP_SPEC(CellProperty::TopPadding, FT_CPROP_TOP_PADDING, unsigned) \ - SET_CELL_PROP_SPEC(CellProperty::BottomPadding, FT_CPROP_BOTTOM_PADDING, unsigned) \ - SET_CELL_PROP_SPEC(CellProperty::LeftPadding, FT_CPROP_LEFT_PADDING, unsigned) \ - SET_CELL_PROP_SPEC(CellProperty::RightPading, FT_CPROP_RIGHT_PADDING, unsigned) \ - SET_CELL_PROP_SPEC(CellProperty::EmptyStrHeight, FT_CPROP_EMPTY_STR_HEIGHT, unsigned) \ - SET_CELL_PROP_SPEC(CellProperty::RowType, FT_CPROP_ROW_TYPE, RowType) - -#define SET_CELL_PROP_SPEC(CELL_OPTION, C_OPTION, VALUE_TYPE) \ -template <> \ -bool Table::set_property(size_t row, size_t col, VALUE_TYPE value) \ -{ \ - return FT_IS_SUCCESS(ft_set_cell_prop(table, row, col, C_OPTION, static_cast(value))); \ -} - -DECLARE_SPECS_FOR_CELL_PROPS_X - -#undef SET_TABLE_PROP_SPEC -#undef DECLARE_SPECS_FOR_PROPS_X - - - -#define DECLARE_SPECS_FOR_TABLE_PROPS_X \ - SET_TABLE_PROP_SPEC(TableProperty::LeftMargin, FT_TPROP_LEFT_MARGIN) \ - SET_TABLE_PROP_SPEC(TableProperty::TopMargin, FT_TPROP_TOP_MARGIN) \ - SET_TABLE_PROP_SPEC(TableProperty::RightMargin, FT_TPROP_RIGHT_MARGIN) \ - SET_TABLE_PROP_SPEC(TableProperty::BottomMargin, FT_TPROP_BOTTOM_MARGIN) - -#define SET_TABLE_PROP_SPEC(TABLE_OPTION, TBL_OPTION) \ -template <> \ -bool Table::set_property(unsigned value) \ -{ \ - return FT_IS_SUCCESS(ft_set_tbl_prop(table, TBL_OPTION, static_cast(value))); \ -} - -DECLARE_SPECS_FOR_TABLE_PROPS_X - -#undef SET_TABLE_PROP_SPEC -#undef DECLARE_SPECS_FOR_TABLE_PROPS_X - - - - } #endif // LIBFORT_HPP diff --git a/tests/bb_tests_cpp/test_table_basic.cpp b/tests/bb_tests_cpp/test_table_basic.cpp index 8a44521..c5c6de5 100644 --- a/tests/bb_tests_cpp/test_table_basic.cpp +++ b/tests/bb_tests_cpp/test_table_basic.cpp @@ -1,42 +1,12 @@ #include "tests.h" #include "fort.hpp" -#define assert_string_equal(str1, str2) assert_str_equal(str1.c_str(), str2.c_str()) - -bool set_test_props_for_table(fort::Table *table) -{ - assert_true(table->set_cell_bottom_padding(FT_ANY_ROW, FT_ANY_COLUMN, 1)); - assert_true(table->set_cell_top_padding(FT_ANY_ROW, FT_ANY_COLUMN, 1)); - assert_true(table->set_cell_left_padding(FT_ANY_ROW, FT_ANY_COLUMN, 1)); - assert_true(table->set_cell_right_padding(FT_ANY_ROW, FT_ANY_COLUMN, 1)); - assert_true(table->set_cell_empty_str_height(FT_ANY_ROW, FT_ANY_COLUMN, 1)); - - struct ft_border_style brdr_style; - brdr_style.border_chs.top_border_ch = "-"; - brdr_style.border_chs.separator_ch = "-"; - brdr_style.border_chs.bottom_border_ch = "-"; - brdr_style.border_chs.side_border_ch = "|"; - brdr_style.border_chs.out_intersect_ch = "+"; - brdr_style.border_chs.in_intersect_ch = "+"; - - brdr_style.header_border_chs.top_border_ch = "-"; - brdr_style.header_border_chs.separator_ch = "-"; - brdr_style.header_border_chs.bottom_border_ch = "-"; - brdr_style.header_border_chs.side_border_ch = "|"; - brdr_style.header_border_chs.out_intersect_ch = "+"; - brdr_style.header_border_chs.in_intersect_ch = "+"; - - brdr_style.hor_separator_char = "="; - table->set_border_style(&brdr_style); - - return true; -} void test_cpp_table_basic(void) { WHEN("All columns are equal and not empty.") { - fort::Table table; - assert_true(set_test_props_for_table(&table)); + fort::table table; + assert_true(set_cpp_test_props_for_table(&table)); table << fort::header << "3" << "c" << "234" << "3.140000" << fort::endr @@ -62,20 +32,20 @@ void test_cpp_table_basic(void) } WHEN("Checking basic constructors and assignmets.") { - fort::Table table; - assert_true(set_test_props_for_table(&table)); + fort::table table; + assert_true(set_cpp_test_props_for_table(&table)); table << fort::header << "3" << "c" << "234" << "3.140000" << fort::endr << "3" << "c" << "234" << "3.140000" << fort::endr << "3" << "c" << "234" << "3.140000" << fort::endr; - fort::Table table2(std::move(table)); - fort::Table table3; + fort::table table2(std::move(table)); + fort::table table3; table3 = std::move(table2); - fort::Table table4(table3); - fort::Table table5; + fort::table table4(table3); + fort::table table5; table5 = table4; std::string table_str = table5.to_string(); @@ -97,8 +67,8 @@ void test_cpp_table_basic(void) } WHEN("All columns are not equal and not empty") { - fort::Table table; - assert_true(set_test_props_for_table(&table)); + fort::table table; + assert_true(set_cpp_test_props_for_table(&table)); table << fort::header << "3" << "c" << "234" << "3.140000" << fort::endr @@ -124,8 +94,8 @@ void test_cpp_table_basic(void) } WHEN("All columns are not equal and some cells are empty") { - fort::Table table; - assert_true(set_test_props_for_table(&table)); + fort::table table; + assert_true(set_cpp_test_props_for_table(&table)); table << fort::header << "" << "" << "234" << "3.140000" << fort::endr @@ -151,8 +121,8 @@ void test_cpp_table_basic(void) } WHEN("All cells are empty") { - fort::Table table; - assert_true(set_test_props_for_table(&table)); + fort::table table; + assert_true(set_cpp_test_props_for_table(&table)); table << fort::header << "" << "" << "" << "" << fort::endr @@ -182,8 +152,8 @@ void test_cpp_table_basic(void) void test_cpp_table_write(void) { SCENARIO("Test write functions") { - fort::Table table; - assert_true(set_test_props_for_table(&table)); + fort::table table; + assert_true(set_cpp_test_props_for_table(&table)); table << fort::header; assert_true(table.write("3")); assert_true(table.write("c")); @@ -224,8 +194,8 @@ void test_cpp_table_write(void) } SCENARIO("Test n write functions") { - fort::Table table; - assert_true(set_test_props_for_table(&table)); + fort::table table; + assert_true(set_cpp_test_props_for_table(&table)); table << fort::header; assert_true(table.write("3", "c", "234", "3.140000")); table << fort::endr; @@ -256,8 +226,8 @@ void test_cpp_table_write(void) } SCENARIO("Test row_write functions") { - fort::Table table; - assert_true(set_test_props_for_table(&table)); + fort::table table; + assert_true(set_cpp_test_props_for_table(&table)); table << fort::header; const char *row_0[4] = {"3", "c", "234", "3.140000"}; diff --git a/tests/bb_tests_cpp/test_table_properties.cpp b/tests/bb_tests_cpp/test_table_properties.cpp new file mode 100644 index 0000000..8b28661 --- /dev/null +++ b/tests/bb_tests_cpp/test_table_properties.cpp @@ -0,0 +1,524 @@ +#include "tests.h" +#include "fort.hpp" + + +void test_cpp_table_tbl_properties(void) +{ + fort::table table; + WHEN("Test setting entire table properties") { + set_test_properties_as_default(); + + table = create_cpp_test_int_table(false); + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+---+---+----+----+\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "+---+---+----+----+\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "+---+---+----+----+\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "+---+---+----+----+\n"; + assert_string_equal(table_str, table_str_etalon); + + /* Now set table properties */ + table.set_top_margin(3); + table.set_bottom_margin(4); + table.set_left_margin(1); + table.set_right_margin(2); + + table_str = table.to_string(); + table_str_etalon = + " \n" + " \n" + " \n" + " +---+---+----+----+ \n" + " | | | | | \n" + " | 3 | 4 | 55 | 67 | \n" + " | | | | | \n" + " +---+---+----+----+ \n" + " | | | | | \n" + " | 3 | 4 | 55 | 67 | \n" + " | | | | | \n" + " +---+---+----+----+ \n" + " | | | | | \n" + " | 3 | 4 | 55 | 67 | \n" + " | | | | | \n" + " +---+---+----+----+ \n" + " \n" + " \n" + " \n" + " \n"; + assert_string_equal(table_str, table_str_etalon); + } +} + +void test_cpp_table_cell_properties(void) +{ + + WHEN("Setting property for one cell") { + set_test_properties_as_default(); + + fort::table table = create_cpp_test_int_table(false); + table[1][1].set_cell_top_padding(2); + + std::string table_str = table.to_string(); + std::string 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_string_equal(table_str, table_str_etalon); + } + + WHEN("Setting property for the row") { + set_test_properties_as_default(); + + fort::table table = create_cpp_test_int_table(false); + table[1].set_cell_top_padding(2); + + std::string table_str = table.to_string(); + std::string 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_string_equal(table_str, table_str_etalon); + } + + WHEN("Setting property for the column") { + set_test_properties_as_default(); + + fort::table table = create_cpp_test_int_table(false); + table.column(1).set_cell_top_padding(2); + + std::string table_str = table.to_string(); + std::string 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_string_equal(table_str, table_str_etalon); + } + + WHEN("Setting property for all cells in the table") { + set_test_properties_as_default(); + + fort::table table = create_cpp_test_int_table(false); + table.set_cell_top_padding(2); + + std::string table_str = table.to_string(); + std::string 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_string_equal(table_str, table_str_etalon); + } + + WHEN("All paddings = 1") { + // NOT IMPLEMENTED !!!!!!!!!!!!!! + } + + WHEN("Top and bottom padding = 0") { + // NOT IMPLEMENTED !!!!!!!!!!!!!! + } + + WHEN("Left and right padding = 0") { + // NOT IMPLEMENTED !!!!!!!!!!!!!! + } + + WHEN("All paddings = 0") { + // NOT IMPLEMENTED !!!!!!!!!!!!!! + } + + WHEN("Empty string has 0 heigt") { + // NOT IMPLEMENTED !!!!!!!!!!!!!! + } + + WHEN("Setting properties for a particular table") { + set_test_properties_as_default(); + fort::table table = create_cpp_test_int_table(false); + table.set_cell_bottom_padding(0); + table.set_cell_top_padding(0); + table.set_cell_left_padding(0); + table.set_cell_right_padding(0); + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+-+-+--+--+\n" + "|3|4|55|67|\n" + "+-+-+--+--+\n" + "|3|4|55|67|\n" + "+-+-+--+--+\n" + "|3|4|55|67|\n" + "+-+-+--+--+\n"; + assert_string_equal(table_str, table_str_etalon); + + table.set_cell_bottom_padding(1); + table.set_cell_top_padding(1); + table.set_cell_left_padding(0); + table.set_cell_right_padding(0); + table.set_cell_empty_str_height(0); + + table_str = table.to_string(); + table_str_etalon = + "+-+-+--+--+\n" + "| | | | |\n" + "|3|4|55|67|\n" + "| | | | |\n" + "+-+-+--+--+\n" + "| | | | |\n" + "|3|4|55|67|\n" + "| | | | |\n" + "+-+-+--+--+\n" + "| | | | |\n" + "|3|4|55|67|\n" + "| | | | |\n" + "+-+-+--+--+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Set table width and column alignment") { + set_test_properties_as_default(); + fort::table table = create_cpp_test_int_table(false); + + table.column(1).set_cell_min_width(7); + table.column(1).set_cell_text_align(fort::text_align::left); + table.column(2).set_cell_min_width(8); + table.column(2).set_cell_text_align(fort::text_align::center); + + table[2][3].set_cell_min_width(6); + table[2][3].set_cell_text_align(fort::text_align::left); + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+---+-------+--------+------+\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "+---+-------+--------+------+\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "+---+-------+--------+------+\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "+---+-------+--------+------+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Set table width and column alignment as default") { + // NOT IMPLEMENTED !!!!!!!!!!!!!! + + } + + WHEN("Multiline cell") { + + set_test_properties_as_default(); + fort::table table; + table[0].set_cell_row_type(fort::row_type::header); + + table << 4 << 'c' << "234" << 3.14 << fort::endr; + assert_true(table.write_ln("5", "c", "234\n12", "3.140000")); + table << 3 << 'c' << "234" << 3.14 << fort::endr; + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+---+---+-----+----------+\n" + "| | | | |\n" + "| 4 | c | 234 | 3.14 |\n" + "| | | | |\n" + "+---+---+-----+----------+\n" + "| | | | |\n" + "| 5 | c | 234 | 3.140000 |\n" + "| | | 12 | |\n" + "| | | | |\n" + "+---+---+-----+----------+\n" + "| | | | |\n" + "| 3 | c | 234 | 3.14 |\n" + "| | | | |\n" + "+---+---+-----+----------+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Cells with spans") { + set_test_properties_as_default(); + fort::table table; + + table[0][0].set_cell_span(5); + table[1][1].set_cell_span(3); + + table[0].set_cell_row_type(fort::row_type::header); + table[1].set_cell_row_type(fort::row_type::header); + + assert_true(table.write_ln("111", "2222", "33333", "444444", "55555555")); + assert_true(table.write_ln("2222", "33333", "444444", "55555555", "111")); + + assert_true(table.write_ln("33333", "444444", "55555555", "111", "2222")); + assert_true(table.write_ln("2222", "33333", "444444", "55555555", "111")); + assert_true(table.write_ln("2222", "33333", "444444", "55555555", "111")); + + table[4][3].set_cell_span(2); + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+---------------------------------------------+\n" + "| |\n" + "| 111 |\n" + "| |\n" + "+-------+------------------------------+------+\n" + "| | | |\n" + "| 2222 | 33333 | 111 |\n" + "| | | |\n" + "+-------+--------+----------+----------+------+\n" + "| | | | | |\n" + "| 33333 | 444444 | 55555555 | 111 | 2222 |\n" + "| | | | | |\n" + "+-------+--------+----------+----------+------+\n" + "| | | | | |\n" + "| 2222 | 33333 | 444444 | 55555555 | 111 |\n" + "| | | | | |\n" + "+-------+--------+----------+----------+------+\n" + "| | | | |\n" + "| 2222 | 33333 | 444444 | 55555555 |\n" + "| | | | |\n" + "+-------+--------+----------+-----------------+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Cells with spans in common and header cells") { + set_test_properties_as_default(); + fort::table table; + + table.set_border_style(FT_DOUBLE2_STYLE); + + table[0][0].set_cell_span(2); + table[0][2].set_cell_span(3); + table[1][1].set_cell_span(3); + table[4][3].set_cell_span(2); + + assert_true(table.write_ln("111", "2222", "33333", "444444", "55555555")); + assert_true(table.write_ln("2222", "33333", "444444", "55555555", "111")); + + assert_true(table.write_ln("33333", "444444", "55555555", "111", "2222")); + assert_true(table.write_ln("2222", "33333", "444444", "55555555", "111")); + assert_true(table.write_ln("2222", "33333", "444444", "55555555", "111")); + + table[0].set_cell_row_type(fort::row_type::header); + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "╔════════════════╤════════════════════════════╗\n" + "║ │ ║\n" + "║ 111 │ 33333 ║\n" + "║ │ ║\n" + "╠═══════╤════════╧═════════════════════╤══════╣\n" + "║ │ │ ║\n" + "║ 2222 │ 33333 │ 111 ║\n" + "║ │ │ ║\n" + "╟───────┼────────┬──────────┬──────────┼──────╢\n" + "║ │ │ │ │ ║\n" + "║ 33333 │ 444444 │ 55555555 │ 111 │ 2222 ║\n" + "║ │ │ │ │ ║\n" + "╟───────┼────────┼──────────┼──────────┼──────╢\n" + "║ │ │ │ │ ║\n" + "║ 2222 │ 33333 │ 444444 │ 55555555 │ 111 ║\n" + "║ │ │ │ │ ║\n" + "╟───────┼────────┼──────────┼──────────┴──────╢\n" + "║ │ │ │ ║\n" + "║ 2222 │ 33333 │ 444444 │ 55555555 ║\n" + "║ │ │ │ ║\n" + "╚═══════╧════════╧══════════╧═════════════════╝\n"; + assert_string_equal(table_str, table_str_etalon); + } +} + + +void test_cpp_table_text_styles(void) +{ + set_test_properties_as_default(); + + WHEN("Simple table with one cell and foreground content color") { + fort::table table; + + table[0][0].set_cell_content_fg_color(fort::color::yellow); + table << 42; + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+\n" + "|\033[33m\033[39m |\n" + "| \033[33m42\033[39m |\n" + "|\033[33m\033[39m |\n" + "+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Simple table with one cell and background content color") { + fort::table table; + + table[0][0].set_cell_content_bg_color(fort::color::yellow); + table << 42; + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+\n" + "|\033[43m\033[49m |\n" + "| \033[43m42\033[49m |\n" + "|\033[43m\033[49m |\n" + "+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Simple table with one cell and background cell color") { + fort::table table; + + table[0][0].set_cell_cell_bg_color(fort::color::yellow); + table << 42; + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+\n" + "|\033[43m \033[49m|\n" + "|\033[43m 42 \033[49m|\n" + "|\033[43m \033[49m|\n" + "+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Simple table with one cell and content style") { + fort::table table; + + table[0][0].set_cell_content_text_style(fort::text_style::underlined); + table << 42; + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+\n" + "|\033[4m\033[24m |\n" + "| \033[4m42\033[24m |\n" + "|\033[4m\033[24m |\n" + "+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Simple table with one cell and multiple content style") { + fort::table table; + + table[0][0].set_cell_content_text_style(fort::text_style::underlined); + table[0][0].set_cell_content_text_style(fort::text_style::bold); + table << 42; + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+\n" + "|\033[1m\033[4m\033[21m\033[24m |\n" + "| \033[1m\033[4m42\033[21m\033[24m |\n" + "|\033[1m\033[4m\033[21m\033[24m |\n" + "+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Simple table with one cell and cell style") { + fort::table table; + + table[0][0].set_cell_cell_text_style(fort::text_style::underlined); + table << 42; + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+\n" + "|\033[4m \033[24m|\n" + "|\033[4m 42 \033[24m|\n" + "|\033[4m \033[24m|\n" + "+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Simple table with one cell and multiple cell style") { + fort::table table; + + table[0][0].set_cell_cell_text_style(fort::text_style::underlined); + table[0][0].set_cell_cell_text_style(fort::text_style::bold); + table << 42; + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+\n" + "|\033[1m\033[4m \033[21m\033[24m|\n" + "|\033[1m\033[4m 42 \033[21m\033[24m|\n" + "|\033[1m\033[4m \033[21m\033[24m|\n" + "+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } + + WHEN("Simple table with one cell background color, content foreground color and style.") { + fort::table table; + + table[0][0].set_cell_content_fg_color(fort::color::yellow); + table[0][0].set_cell_cell_bg_color(fort::color::red); + table[0][0].set_cell_content_text_style(fort::text_style::underlined); + table << 42; + + std::string table_str = table.to_string(); + std::string 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_string_equal(table_str, table_str_etalon); + } +} diff --git a/tests/main_test_cpp.cpp b/tests/main_test_cpp.cpp index 4daaa3d..a72a6cc 100644 --- a/tests/main_test_cpp.cpp +++ b/tests/main_test_cpp.cpp @@ -4,11 +4,18 @@ void test_cpp_table_basic(void); void test_cpp_table_write(void); +void test_cpp_table_tbl_properties(void); +void test_cpp_table_cell_properties(void); +void test_cpp_table_text_styles(void); + struct test_case bb_test_suit [] = { {"test_cpp_table_basic", test_cpp_table_basic}, {"test_cpp_table_write", test_cpp_table_write}, + {"test_cpp_table_tbl_properties", test_cpp_table_tbl_properties}, + {"test_cpp_table_cell_properties", test_cpp_table_cell_properties}, + {"test_cpp_table_text_styles", test_cpp_table_text_styles}, }; diff --git a/tests/test_utils.cpp b/tests/test_utils.cpp new file mode 100644 index 0000000..fad47a0 --- /dev/null +++ b/tests/test_utils.cpp @@ -0,0 +1,50 @@ +#include "fort.hpp" +#include "tests.h" + + +bool set_cpp_test_props_for_table(fort::table *table) +{ + assert_true(table->set_cell_bottom_padding(1)); + assert_true(table->set_cell_top_padding(1)); + assert_true(table->set_cell_left_padding(1)); + assert_true(table->set_cell_right_padding(1)); + assert_true(table->set_cell_empty_str_height(1)); + + struct ft_border_style brdr_style; + brdr_style.border_chs.top_border_ch = "-"; + brdr_style.border_chs.separator_ch = "-"; + brdr_style.border_chs.bottom_border_ch = "-"; + brdr_style.border_chs.side_border_ch = "|"; + brdr_style.border_chs.out_intersect_ch = "+"; + brdr_style.border_chs.in_intersect_ch = "+"; + + brdr_style.header_border_chs.top_border_ch = "-"; + brdr_style.header_border_chs.separator_ch = "-"; + brdr_style.header_border_chs.bottom_border_ch = "-"; + brdr_style.header_border_chs.side_border_ch = "|"; + brdr_style.header_border_chs.out_intersect_ch = "+"; + brdr_style.header_border_chs.in_intersect_ch = "+"; + + brdr_style.hor_separator_char = "="; + table->set_border_style(&brdr_style); + + return true; +} + + + +fort::table create_cpp_test_int_table(int set_test_opts) +{ + fort::table table; + + if (set_test_opts) { + assert_true(set_cpp_test_props_for_table(&table) == true); + } + + table[0].set_cell_row_type(fort::row_type::header); + assert(table.write_ln("3", "4", "55", "67")); + assert(table.write_ln("3", "4", "55", "67")); + assert(table.write_ln("3", "4", "55", "67")); + + return table; +} diff --git a/tests/tests.h b/tests/tests.h index 5a53ea7..044aef3 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -71,6 +71,14 @@ struct test_case { struct ft_table; #ifdef __cplusplus +#define assert_string_equal(str1, str2) assert_str_equal(str1.c_str(), str2.c_str()) +namespace fort +{ +class table; +} +bool set_cpp_test_props_for_table(fort::table *table); +fort::table create_cpp_test_int_table(int set_test_opts); + extern "C" { #endif