[A] Added more cpp tests.

This commit is contained in:
seleznevae 2018-11-21 20:50:56 +03:00
parent 4c7eef8081
commit c53022082c
8 changed files with 1029 additions and 381 deletions

View File

@ -141,7 +141,9 @@ add_executable(${PROJECT_NAME}_test
set(TEST_SOURCES_CPP set(TEST_SOURCES_CPP
tests/main_test_cpp.cpp tests/main_test_cpp.cpp
tests/bb_tests_cpp/test_table_basic.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 add_executable(${PROJECT_NAME}_test_cpp
lib/fort.c lib/fort.c
${TEST_SOURCES_CPP}) ${TEST_SOURCES_CPP})

View File

@ -2,9 +2,9 @@
#include <vector> #include <vector>
#include "fort.hpp" #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 */ /* Set table border style */
table.set_border_style(FT_BASIC_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 << "5" << "Blade Runner" << "1982" << "8.1" << fort::endr
<< fort::endr; << fort::endr;
table.set_cell_text_align(FT_ANY_ROW, 0, fort::TextAlign::Center); table.column(0).set_cell_text_align(fort::text_align::center);
table.set_cell_text_align(FT_ANY_ROW, 1, fort::TextAlign::Left); table.column(1).set_cell_text_align(fort::text_align::left);
std::cout << table.to_string() << std::endl; std::cout << table.to_string() << std::endl;
return table; return table;
@ -28,7 +28,7 @@ fort::Table create_basic_table(void)
void base_example(void) void base_example(void)
{ {
fort::Table table; fort::table table;
table << fort::header table << fort::header
<< "N" << "Driver" << "Time" << "Avg Speed" << fort::endr << "N" << "Driver" << "Time" << "Avg Speed" << fort::endr
<< "1" << "Ricciardo" << "1:25.945" << "47.362" << 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) void different_cell_properties_example(void)
{ {
fort::Table table; fort::table table;
/* Change border style */ /* Change border style */
table.set_border_style(FT_DOUBLE2_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; << "2001: A Space Odyssey" << "Stanley Kubrick" << "1968" << "8.5" << fort::endr;
/* Set center alignment for the 1st and 3rd columns */ /* Set center alignment for the 1st and 3rd columns */
table.set_cell_text_align(FT_ANY_ROW, 1, fort::TextAlign::Center); table.column(1).set_cell_text_align(fort::text_align::center);
table.set_cell_text_align(FT_ANY_ROW, 3, fort::TextAlign::Center); table.column(3).set_cell_text_align(fort::text_align::center);
std::cout << table.to_string() << std::endl; std::cout << table.to_string() << std::endl;
} }
@ -60,7 +61,7 @@ void different_cell_properties_example(void)
void fill_table_with_data_example(void) void fill_table_with_data_example(void)
{ {
fort::Table table; fort::table table;
table << fort::header; table << fort::header;
/* Fill each cell with operator[] */ /* Fill each cell with operator[] */
table [0][0] = "N"; table [0][0] = "N";
@ -91,7 +92,7 @@ int main()
fill_table_with_data_example(); fill_table_with_data_example();
{ {
fort::Table table; fort::table table;
// Fill table with data // Fill table with data
table << fort::header table << fort::header
<< "Rank" << "Title" << "Year" << "Rating" << fort::endr << "Rank" << "Title" << "Year" << "Rating" << fort::endr
@ -103,18 +104,16 @@ int main()
<< "5" << "Blade Runner" << "1982" << "8.1" << fort::endr << "5" << "Blade Runner" << "1982" << "8.1" << fort::endr
<< fort::endr; << fort::endr;
using fort::CellProperty; table[0][0].set_cell_min_width(20);
using fort::TableProperty; table[0][0].set_cell_text_align(fort::text_align::left);
table.set_property<CellProperty::MinWidth>(0, 0, 20); table[2].set_cell_row_type(fort::row_type::header);
table.set_property<CellProperty::TextAlign>(0, 0, fort::TextAlign::Left);
table.set_property<CellProperty::RowType>(2, FT_ANY_COLUMN, fort::RowType::Header);
std::cout << table.to_string() << std::endl; std::cout << table.to_string() << std::endl;
} }
{ {
fort::Table table; fort::table table;
// Fill table with data // Fill table with data
table << fort::header; table << fort::header;
table.write_ln("Rank", "Title", "Year", "Rating"); 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("4", "2001: A Space Odyssey", "1968", "8.5");
table.write_ln("5", "Blade Runner", "1982", "8.1"); 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<TableProperty::LeftMargin>(4); table.set_left_margin(4);
table.set_border_style(FT_SOLID_STYLE); table.set_border_style(FT_SOLID_STYLE);
std::cout << table.to_string(); std::cout << table.to_string();

View File

@ -37,125 +37,350 @@ SOFTWARE.
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
#include "fort.h" #include "fort.h"
namespace fort namespace fort
{ {
enum class CellProperty {
MinWidth, enum class text_align {
TextAlign, left = FT_ALIGNED_LEFT,
TopPadding, center = FT_ALIGNED_CENTER,
BottomPadding, right = FT_ALIGNED_RIGHT
LeftPadding,
RightPading,
EmptyStrHeight,
RowType
}; };
enum class TableProperty { enum class row_type {
LeftMargin, common = FT_ROW_COMMON,
TopMargin, header = FT_ROW_HEADER
RightMargin,
BottomMargin,
}; };
enum class TextAlign { enum class color {
Left = 0, default_color = FT_COLOR_DEFAULT,
Center, black = FT_COLOR_BLACK,
Right 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 { enum class text_style {
Common = 0, default_style = FT_TSTYLE_DEFAULT,
Header 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: public:
explicit TableManipulator(int i) explicit table_manipulator(int i)
:value(i) :value(i)
{ {
} }
friend class Table; friend class table;
private: private:
int value; int value;
}; };
const TableManipulator header(0); const table_manipulator header(0);
const TableManipulator endr(1); const table_manipulator endr(1);
const TableManipulator separator(2); const table_manipulator separator(2);
template <typename table>
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<int>(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<int>(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<int>(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<int>(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<int>(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<int>(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<int>(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 - formatted table.
* *
* Table class is a C++ wrapper around struct ft_table. * Table class is a C++ wrapper around struct ft_table.
*/ */
class Table { class table: public property_setter<table> {
public: public:
Table() table()
:table(ft_create_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"); throw std::runtime_error("Runtime error");
} }
~Table() ~table()
{ {
ft_destroy_table(table); ft_destroy_table(table_);
} }
/** /**
* Copy contstructor. * Copy contstructor.
*/ */
Table(const Table& tbl) table(const table& tbl)
:table(NULL) :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(NULL)
{ {
if (tbl.table) { if (tbl.table_) {
ft_table_t *table_copy = ft_copy_table(tbl.table); ft_table_t *table_copy = ft_copy_table(tbl.table_);
if (table_copy == NULL) if (table_copy == NULL)
throw std::runtime_error("Runtime error"); throw std::runtime_error("Runtime error");
stream.str(std::string()); stream_.str(std::string());
if (tbl.stream.tellp() >= 0) { if (tbl.stream_.tellp() >= 0) {
stream << tbl.stream.str(); stream_ << tbl.stream_.str();
} }
table = table_copy; table_ = table_copy;
} }
} }
/** /**
* Move contstructor. * Move contstructor.
*/ */
Table(Table&& tbl) table(table&& tbl)
:table(tbl.table) :property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(tbl.table_)
{ {
if (tbl.stream.tellp() >= 0) { if (tbl.stream_.tellp() >= 0) {
stream << tbl.stream.str(); stream_ << tbl.stream_.str();
tbl.stream.str(std::string()); tbl.stream_.str(std::string());
} }
tbl.table = 0; tbl.table_ = 0;
} }
/** /**
* Copy assignment operator. * Copy assignment operator.
*/ */
Table& operator=(const Table& tbl) table& operator=(const table& tbl)
{ {
if (&tbl == this) if (&tbl == this)
return *this; return *this;
if (tbl.table) { if (tbl.table_) {
ft_table_t *table_copy = ft_copy_table(tbl.table); ft_table_t *table_copy = ft_copy_table(tbl.table_);
if (table_copy == NULL) if (table_copy == NULL)
throw std::runtime_error("Runtime error"); throw std::runtime_error("Runtime error");
stream.str(std::string()); stream_.str(std::string());
if (tbl.stream.tellp() >= 0) { if (tbl.stream_.tellp() >= 0) {
stream << tbl.stream.str(); stream_ << tbl.stream_.str();
} }
ft_destroy_table(table); ft_destroy_table(table_);
table = table_copy; table_ = table_copy;
} }
return *this; return *this;
} }
@ -163,20 +388,20 @@ public:
/** /**
* Move assignment operator. * Move assignment operator.
*/ */
Table& operator=(Table&& tbl) table& operator=(table&& tbl)
{ {
if (&tbl == this) if (&tbl == this)
return *this; return *this;
if (tbl.table) { if (tbl.table_) {
stream.str(std::string()); stream_.str(std::string());
if (tbl.stream.tellp() >= 0) { if (tbl.stream_.tellp() >= 0) {
stream << tbl.stream.str(); stream_ << tbl.stream_.str();
tbl.stream.str(std::string()); tbl.stream_.str(std::string());
} }
ft_destroy_table(table); ft_destroy_table(table_);
table = tbl.table; table_ = tbl.table_;
tbl.table = NULL; tbl.table_ = NULL;
} }
return *this; return *this;
} }
@ -190,7 +415,7 @@ public:
*/ */
std::string to_string() const std::string to_string() const
{ {
const char *str = ft_to_string(table); const char *str = ft_to_string(table_);
if (str == NULL) if (str == NULL)
throw std::runtime_error("Runtime error"); throw std::runtime_error("Runtime error");
return str; return str;
@ -213,7 +438,7 @@ public:
*/ */
const char *c_str() const 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. * - Reference to the current table.
*/ */
template <typename T> template <typename T>
Table &operator<<(const T &arg) table &operator<<(const T &arg)
{ {
stream << arg; stream_ << arg;
if (stream.tellp() >= 0) { if (stream_.tellp() >= 0) {
ft_nwrite(table, 1, stream.str().c_str()); ft_nwrite(table_, 1, stream_.str().c_str());
stream.str(std::string()); stream_.str(std::string());
} }
return *this; return *this;
} }
Table &operator<<(const TableManipulator &arg) table &operator<<(const table_manipulator &arg)
{ {
if (arg.value == header.value) 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) else if (arg.value == endr.value)
ft_ln(table); ft_ln(table_);
else if (arg.value == separator.value) else if (arg.value == separator.value)
ft_add_separator(table); ft_add_separator(table_);
return *this; return *this;
} }
bool write(const char *str) 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) 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) bool write(const std::string &str)
@ -388,166 +613,10 @@ public:
*this << *first; *this << *first;
++first; ++first;
} }
ft_ln(table); ft_ln(table_);
return true; 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<int>(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<int>(value)));
}
template <CellProperty property>
bool set_property(size_t row, size_t col, unsigned value);
template <CellProperty property>
bool set_property(size_t row, size_t col, enum TextAlign align);
template <CellProperty property>
bool set_property(size_t row, size_t col, enum RowType rowType);
template <TableProperty property>
bool set_property(unsigned value);
/** /**
* Set border style for the table. * Set border style for the table.
* *
@ -559,7 +628,7 @@ public:
*/ */
bool set_border_style(struct ft_border_style *style) 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. * Current cell - cell that will be edited with all modifiing functions.
* *
* @param row * @param row_i
* New row number for the current cell. * New row number for the current cell.
* @param col * @param col_i
* New row number for the current cell. * 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: private:
ft_table_t *table; ft_table_t *table_;
mutable std::stringstream stream; mutable std::stringstream stream_;
friend class property_setter<table>;
public: public:
/* Iterators */ /* Iterators */
/* todo: implement chains like table[0][0] = table [0][1] = "somethings" */ /* todo: implement chains like table[0][0] = table [0][1] = "somethings" */
class table_cell_iterator class table_cell_iterator: public property_setter<table>
{ {
public: public:
table_cell_iterator(std::size_t row_idx, std::size_t coll_idx, Table &tbl) 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) {} :property_setter(row_idx, coll_idx, tbl) {}
table_cell_iterator& operator=(const char *str) table_cell_iterator& operator=(const char *str)
{ {
ft_set_cur_cell(table_.table, row_idx_, coll_idx_); ft_set_cur_cell(ps_table_.table_, ps_row_idx_, ps_coll_idx_);
table_.write(str); ps_table_.write(str);
return *this; 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<table>
{ {
public: public:
table_row_iterator(std::size_t row_idx, Table &tbl) table_row_iterator(std::size_t row_idx, table &tbl)
:row_idx_(row_idx), table_(tbl) {} :property_setter(row_idx, FT_ANY_COLUMN, tbl) {}
class table_cell_iterator class table_cell_iterator
operator[](std::size_t coll_idx) 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: class table_column_iterator: public property_setter<table>
std::size_t row_idx_; {
Table &table_; public:
table_column_iterator(std::size_t col_idx, table &tbl)
:property_setter(FT_ANY_ROW, col_idx, tbl) {}
}; };
class table_row_iterator class table_row_iterator
@ -626,6 +752,19 @@ public:
{ {
return table_row_iterator(row_idx, *this); 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. * - True: Success; table border style was changed.
* - False: Error * - 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)); 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<CELL_OPTION>(size_t row, size_t col, VALUE_TYPE value) \
{ \
return FT_IS_SUCCESS(ft_set_cell_prop(table, row, col, C_OPTION, static_cast<int>(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<TABLE_OPTION>(unsigned value) \
{ \
return FT_IS_SUCCESS(ft_set_tbl_prop(table, TBL_OPTION, static_cast<int>(value))); \
}
DECLARE_SPECS_FOR_TABLE_PROPS_X
#undef SET_TABLE_PROP_SPEC
#undef DECLARE_SPECS_FOR_TABLE_PROPS_X
} }
#endif // LIBFORT_HPP #endif // LIBFORT_HPP

View File

@ -1,42 +1,12 @@
#include "tests.h" #include "tests.h"
#include "fort.hpp" #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) void test_cpp_table_basic(void)
{ {
WHEN("All columns are equal and not empty.") { WHEN("All columns are equal and not empty.") {
fort::Table table; fort::table table;
assert_true(set_test_props_for_table(&table)); assert_true(set_cpp_test_props_for_table(&table));
table << fort::header table << fort::header
<< "3" << "c" << "234" << "3.140000" << fort::endr << "3" << "c" << "234" << "3.140000" << fort::endr
@ -62,20 +32,20 @@ void test_cpp_table_basic(void)
} }
WHEN("Checking basic constructors and assignmets.") { WHEN("Checking basic constructors and assignmets.") {
fort::Table table; fort::table table;
assert_true(set_test_props_for_table(&table)); assert_true(set_cpp_test_props_for_table(&table));
table << fort::header table << fort::header
<< "3" << "c" << "234" << "3.140000" << fort::endr << "3" << "c" << "234" << "3.140000" << fort::endr
<< "3" << "c" << "234" << "3.140000" << fort::endr << "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 table2(std::move(table));
fort::Table table3; fort::table table3;
table3 = std::move(table2); table3 = std::move(table2);
fort::Table table4(table3); fort::table table4(table3);
fort::Table table5; fort::table table5;
table5 = table4; table5 = table4;
std::string table_str = table5.to_string(); 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") { WHEN("All columns are not equal and not empty") {
fort::Table table; fort::table table;
assert_true(set_test_props_for_table(&table)); assert_true(set_cpp_test_props_for_table(&table));
table << fort::header table << fort::header
<< "3" << "c" << "234" << "3.140000" << fort::endr << "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") { WHEN("All columns are not equal and some cells are empty") {
fort::Table table; fort::table table;
assert_true(set_test_props_for_table(&table)); assert_true(set_cpp_test_props_for_table(&table));
table << fort::header table << fort::header
<< "" << "" << "234" << "3.140000" << fort::endr << "" << "" << "234" << "3.140000" << fort::endr
@ -151,8 +121,8 @@ void test_cpp_table_basic(void)
} }
WHEN("All cells are empty") { WHEN("All cells are empty") {
fort::Table table; fort::table table;
assert_true(set_test_props_for_table(&table)); assert_true(set_cpp_test_props_for_table(&table));
table << fort::header table << fort::header
<< "" << "" << "" << "" << fort::endr << "" << "" << "" << "" << fort::endr
@ -182,8 +152,8 @@ void test_cpp_table_basic(void)
void test_cpp_table_write(void) void test_cpp_table_write(void)
{ {
SCENARIO("Test write functions") { SCENARIO("Test write functions") {
fort::Table table; fort::table table;
assert_true(set_test_props_for_table(&table)); assert_true(set_cpp_test_props_for_table(&table));
table << fort::header; table << fort::header;
assert_true(table.write("3")); assert_true(table.write("3"));
assert_true(table.write("c")); assert_true(table.write("c"));
@ -224,8 +194,8 @@ void test_cpp_table_write(void)
} }
SCENARIO("Test n write functions") { SCENARIO("Test n write functions") {
fort::Table table; fort::table table;
assert_true(set_test_props_for_table(&table)); assert_true(set_cpp_test_props_for_table(&table));
table << fort::header; table << fort::header;
assert_true(table.write("3", "c", "234", "3.140000")); assert_true(table.write("3", "c", "234", "3.140000"));
table << fort::endr; table << fort::endr;
@ -256,8 +226,8 @@ void test_cpp_table_write(void)
} }
SCENARIO("Test row_write functions") { SCENARIO("Test row_write functions") {
fort::Table table; fort::table table;
assert_true(set_test_props_for_table(&table)); assert_true(set_cpp_test_props_for_table(&table));
table << fort::header; table << fort::header;
const char *row_0[4] = {"3", "c", "234", "3.140000"}; const char *row_0[4] = {"3", "c", "234", "3.140000"};

View File

@ -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);
}
}

View File

@ -4,11 +4,18 @@
void test_cpp_table_basic(void); void test_cpp_table_basic(void);
void test_cpp_table_write(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 [] = { struct test_case bb_test_suit [] = {
{"test_cpp_table_basic", test_cpp_table_basic}, {"test_cpp_table_basic", test_cpp_table_basic},
{"test_cpp_table_write", test_cpp_table_write}, {"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},
}; };

50
tests/test_utils.cpp Normal file
View File

@ -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;
}

View File

@ -71,6 +71,14 @@ struct test_case {
struct ft_table; struct ft_table;
#ifdef __cplusplus #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" { extern "C" {
#endif #endif