From db6f6398214fce23ba38f0ea680af3a739bc0321 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Fri, 23 Nov 2018 21:02:08 +0300 Subject: [PATCH] [C] Refactoring of cpp wrappers --- lib/fort.hpp | 84 +++++++---- tests/bb_tests/test_table_properties.c | 44 +++--- tests/bb_tests_cpp/test_table_properties.cpp | 144 ++++++++++++++++++- 3 files changed, 223 insertions(+), 49 deletions(-) diff --git a/lib/fort.hpp b/lib/fort.hpp index b9bd392..66c8270 100644 --- a/lib/fort.hpp +++ b/lib/fort.hpp @@ -43,6 +43,10 @@ SOFTWARE. namespace fort { +enum class cell_property { + pr1, + pr2 +}; enum class text_align { left = FT_ALIGNED_LEFT, @@ -106,8 +110,9 @@ 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) {} + property_setter(std::size_t row_idx, std::size_t coll_idx, table *tbl, bool def = false) + :ps_row_idx_(row_idx), ps_coll_idx_(coll_idx), + ps_table_(tbl), set_default_properties_(def) {} /** * Set min width for the specified cell of the table. @@ -120,7 +125,7 @@ public: */ 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)); + return set_property(FT_CPROP_MIN_WIDTH, value); } /** @@ -134,7 +139,7 @@ public: */ 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))); + return set_property(FT_CPROP_TEXT_ALIGN, static_cast(value)); } /** @@ -148,7 +153,7 @@ public: */ 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)); + return set_property(FT_CPROP_TOP_PADDING, value); } /** @@ -162,7 +167,7 @@ public: */ 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)); + return set_property(FT_CPROP_BOTTOM_PADDING, value); } /** @@ -176,7 +181,7 @@ public: */ 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)); + return set_property(FT_CPROP_LEFT_PADDING, value); } /** @@ -190,7 +195,7 @@ public: */ 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)); + return set_property(FT_CPROP_RIGHT_PADDING, value); } /** @@ -204,7 +209,7 @@ public: */ 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))); + return set_property(FT_CPROP_ROW_TYPE, static_cast(value)); } /** @@ -218,7 +223,7 @@ public: */ 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)); + return set_property(FT_CPROP_EMPTY_STR_HEIGHT, value); } /** @@ -232,7 +237,7 @@ public: */ 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))); + return set_property(FT_CPROP_CONT_FG_COLOR, static_cast(value)); } /** @@ -246,7 +251,7 @@ public: */ 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))); + return set_property(FT_CPROP_CELL_BG_COLOR, static_cast(value)); } /** @@ -260,7 +265,7 @@ public: */ 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))); + return set_property(FT_CPROP_CONT_BG_COLOR, static_cast(value)); } /** @@ -274,7 +279,7 @@ public: */ 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))); + return set_property(FT_CPROP_CELL_TEXT_STYLE, static_cast(value)); } /** @@ -288,7 +293,7 @@ public: */ 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))); + return set_property(FT_CPROP_CONT_TEXT_STYLE, static_cast(value)); } /** @@ -302,12 +307,27 @@ public: */ 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)); + if (set_default_properties_) + return false; + + 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 *ps_table_; + bool set_default_properties_; + + bool set_property(uint32_t property, int value) + { + int status; + if (set_default_properties_) { + status = ft_set_default_cell_prop(property, value); + } else { + status = ft_set_cell_prop(ps_table_->table_, ps_row_idx_, ps_coll_idx_, property, value); + } + return FT_IS_SUCCESS(status); + } }; /** @@ -318,7 +338,7 @@ protected: class table: public property_setter { public: table() - :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(ft_create_table()) + :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, this), table_(ft_create_table()) { if (table_ == NULL) @@ -334,7 +354,7 @@ public: * Copy contstructor. */ table(const table& tbl) - :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(NULL) + :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, this), table_(NULL) { if (tbl.table_) { ft_table_t *table_copy = ft_copy_table(tbl.table_); @@ -353,7 +373,7 @@ public: * Move contstructor. */ table(table&& tbl) - :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(tbl.table_) + :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, this), table_(tbl.table_) { if (tbl.stream_.tellp() >= 0) { stream_ << tbl.stream_.str(); @@ -717,12 +737,12 @@ public: { public: table_cell_iterator(std::size_t row_idx, std::size_t coll_idx, table &tbl) - :property_setter(row_idx, coll_idx, tbl) {} + :property_setter(row_idx, coll_idx, &tbl) {} table_cell_iterator& operator=(const char *str) { - ft_set_cur_cell(ps_table_.table_, ps_row_idx_, ps_coll_idx_); - ps_table_.write(str); + ft_set_cur_cell(ps_table_->table_, ps_row_idx_, ps_coll_idx_); + ps_table_->write(str); return *this; } }; @@ -731,12 +751,12 @@ public: { public: table_row_iterator(std::size_t row_idx, table &tbl) - :property_setter(row_idx, FT_ANY_COLUMN, tbl) {} + :property_setter(row_idx, FT_ANY_COLUMN, &tbl) {} class table_cell_iterator operator[](std::size_t coll_idx) { - return table_cell_iterator(ps_row_idx_, coll_idx, ps_table_); + return table_cell_iterator(ps_row_idx_, coll_idx, *ps_table_); } }; @@ -744,7 +764,14 @@ public: { public: table_column_iterator(std::size_t col_idx, table &tbl) - :property_setter(FT_ANY_ROW, col_idx, tbl) {} + :property_setter(FT_ANY_ROW, col_idx, &tbl) {} + }; + + class default_properties: public property_setter
+ { + public: + default_properties(table *tbl) + :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, tbl, true) {} }; class table_row_iterator @@ -765,6 +792,11 @@ public: return table_column_iterator(col_idx, *this); } + static class default_properties + default_props() + { + return default_properties(NULL); + } }; diff --git a/tests/bb_tests/test_table_properties.c b/tests/bb_tests/test_table_properties.c index 19a5dd2..bc41b51 100644 --- a/tests/bb_tests/test_table_properties.c +++ b/tests/bb_tests/test_table_properties.c @@ -249,38 +249,46 @@ void test_table_cell_properties(void) ft_destroy_table(table); } - WHEN("All paddings = 1") { + WHEN("Differrent paddings") { set_test_properties_as_default(); - ft_set_default_cell_prop(FT_CPROP_BOTTOM_PADDING, 1); - ft_set_default_cell_prop(FT_CPROP_TOP_PADDING, 1); + ft_set_default_cell_prop(FT_CPROP_BOTTOM_PADDING, 3); + ft_set_default_cell_prop(FT_CPROP_TOP_PADDING, 2); ft_set_default_cell_prop(FT_CPROP_LEFT_PADDING, 1); - ft_set_default_cell_prop(FT_CPROP_RIGHT_PADDING, 1); + ft_set_default_cell_prop(FT_CPROP_RIGHT_PADDING, 0); table = create_test_int_table(0); 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 | 4 | 55 | 67 |\n" - "| | | | |\n" - "+---+---+----+----+\n" - "| | | | |\n" - "| 3 | 4 | 55 | 67 |\n" - "| | | | |\n" - "+---+---+----+----+\n"; + "+--+--+---+---+\n" + "| | | | |\n" + "| | | | |\n" + "| 3| 4| 55| 67|\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+---+---+\n" + "| | | | |\n" + "| | | | |\n" + "| 3| 4| 55| 67|\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+---+---+\n" + "| | | | |\n" + "| | | | |\n" + "| 3| 4| 55| 67|\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+---+---+\n"; assert_str_equal(table_str, table_str_etalon); ft_destroy_table(table); } - WHEN("Top and bottom padding = 0") { ft_set_default_cell_prop(FT_CPROP_BOTTOM_PADDING, 0); diff --git a/tests/bb_tests_cpp/test_table_properties.cpp b/tests/bb_tests_cpp/test_table_properties.cpp index 8b28661..a7cd887 100644 --- a/tests/bb_tests_cpp/test_table_properties.cpp +++ b/tests/bb_tests_cpp/test_table_properties.cpp @@ -167,23 +167,137 @@ void test_cpp_table_cell_properties(void) } WHEN("All paddings = 1") { - // NOT IMPLEMENTED !!!!!!!!!!!!!! + set_test_properties_as_default(); + + fort::table::default_props().set_cell_top_padding(2); + fort::table::default_props().set_cell_bottom_padding(3); + fort::table::default_props().set_cell_left_padding(1); + fort::table::default_props().set_cell_right_padding(0); + + fort::table table = create_cpp_test_int_table(false); + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+--+--+---+---+\n" + "| | | | |\n" + "| | | | |\n" + "| 3| 4| 55| 67|\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+---+---+\n" + "| | | | |\n" + "| | | | |\n" + "| 3| 4| 55| 67|\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+---+---+\n" + "| | | | |\n" + "| | | | |\n" + "| 3| 4| 55| 67|\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+---+---+\n"; + assert_string_equal(table_str, table_str_etalon); } WHEN("Top and bottom padding = 0") { - // NOT IMPLEMENTED !!!!!!!!!!!!!! + fort::table::default_props().set_cell_top_padding(0); + fort::table::default_props().set_cell_bottom_padding(0); + fort::table::default_props().set_cell_left_padding(1); + fort::table::default_props().set_cell_right_padding(1); + + fort::table table = create_cpp_test_int_table(false); + + 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); } WHEN("Left and right padding = 0") { - // NOT IMPLEMENTED !!!!!!!!!!!!!! + fort::table::default_props().set_cell_top_padding(1); + fort::table::default_props().set_cell_bottom_padding(1); + fort::table::default_props().set_cell_left_padding(0); + fort::table::default_props().set_cell_right_padding(0); + + fort::table 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); } WHEN("All paddings = 0") { - // NOT IMPLEMENTED !!!!!!!!!!!!!! + fort::table::default_props().set_cell_top_padding(0); + fort::table::default_props().set_cell_bottom_padding(0); + fort::table::default_props().set_cell_left_padding(0); + fort::table::default_props().set_cell_right_padding(0); + + fort::table table = create_cpp_test_int_table(false); + + 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); } WHEN("Empty string has 0 heigt") { - // NOT IMPLEMENTED !!!!!!!!!!!!!! + fort::table::default_props().set_cell_top_padding(1); + fort::table::default_props().set_cell_bottom_padding(1); + fort::table::default_props().set_cell_left_padding(1); + fort::table::default_props().set_cell_right_padding(1); + fort::table::default_props().set_cell_empty_str_height(0); + + fort::table table = create_cpp_test_int_table(false); + table << ""; + + 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" + "| | | | |\n" + "| | | | |\n" + "+---+---+----+----+\n"; + assert_string_equal(table_str, table_str_etalon); } WHEN("Setting properties for a particular table") { @@ -261,7 +375,27 @@ void test_cpp_table_cell_properties(void) WHEN("Set table width and column alignment as default") { // NOT IMPLEMENTED !!!!!!!!!!!!!! + set_test_properties_as_default(); + fort::table::default_props().set_cell_min_width(5); + fort::table::default_props().set_cell_text_align(fort::text_align::center); + fort::table 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); } WHEN("Multiline cell") {