[A] Added c++ tests
This commit is contained in:
parent
fb6d6c0acc
commit
9bce3f259c
@ -118,6 +118,14 @@ add_executable(${PROJECT_NAME}_test
|
|||||||
${TEST_SOURCES})
|
${TEST_SOURCES})
|
||||||
|
|
||||||
|
|
||||||
|
set(TEST_SOURCES_CPP
|
||||||
|
tests/main_test_cpp.cpp
|
||||||
|
tests/bb_tests_cpp/test_table_basic.cpp
|
||||||
|
tests/test_utils.c)
|
||||||
|
add_executable(${PROJECT_NAME}_test_cpp
|
||||||
|
lib/fort.c
|
||||||
|
${TEST_SOURCES_CPP})
|
||||||
|
|
||||||
if(FORT_CXX_BUILD)
|
if(FORT_CXX_BUILD)
|
||||||
SET_SOURCE_FILES_PROPERTIES( ${FORT_DEV_SOURCES} PROPERTIES LANGUAGE CXX)
|
SET_SOURCE_FILES_PROPERTIES( ${FORT_DEV_SOURCES} PROPERTIES LANGUAGE CXX)
|
||||||
SET_SOURCE_FILES_PROPERTIES( ${EXAMPLE_SOURCES} PROPERTIES LANGUAGE CXX)
|
SET_SOURCE_FILES_PROPERTIES( ${EXAMPLE_SOURCES} PROPERTIES LANGUAGE CXX)
|
||||||
|
@ -31,6 +31,15 @@ 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::CellOption;
|
||||||
|
using fort::TableOption;
|
||||||
|
table.set_option<CellOption::MinWidth>(0, 0, 20);
|
||||||
|
table.set_option<CellOption::TextAlign>(0, 0, fort::TextAlign::Left);
|
||||||
|
table.set_option<CellOption::RowType>(2, FT_ANY_COLUMN, fort::RowType::Header);
|
||||||
|
|
||||||
|
|
||||||
|
table.set_option<TableOption::LeftMargin>(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();
|
||||||
}
|
}
|
||||||
|
144
lib/fort.hpp
144
lib/fort.hpp
@ -42,6 +42,35 @@ SOFTWARE.
|
|||||||
namespace fort
|
namespace fort
|
||||||
{
|
{
|
||||||
|
|
||||||
|
enum class CellOption {
|
||||||
|
MinWidth,
|
||||||
|
TextAlign,
|
||||||
|
TopPadding,
|
||||||
|
BottomPadding,
|
||||||
|
LeftPadding,
|
||||||
|
RightPading,
|
||||||
|
EmptyStrHeight,
|
||||||
|
RowType
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class TableOption {
|
||||||
|
LeftMargin,
|
||||||
|
TopMargin,
|
||||||
|
RightMargin,
|
||||||
|
BottomMargin,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class TextAlign {
|
||||||
|
Left,
|
||||||
|
Center,
|
||||||
|
Right
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class RowType {
|
||||||
|
Common,
|
||||||
|
Header
|
||||||
|
};
|
||||||
|
|
||||||
class TableManipulator {
|
class TableManipulator {
|
||||||
public:
|
public:
|
||||||
explicit TableManipulator(int i)
|
explicit TableManipulator(int i)
|
||||||
@ -268,6 +297,54 @@ public:
|
|||||||
#endif /* __cpp_variadic_templates */
|
#endif /* __cpp_variadic_templates */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set min width for the specified cell of the table.
|
||||||
|
*
|
||||||
|
* @param row
|
||||||
|
* Cell row.
|
||||||
|
* @param col
|
||||||
|
* Cell column.
|
||||||
|
* @param value
|
||||||
|
* Value of the min width.
|
||||||
|
* @return
|
||||||
|
* - 0: Success; cell option was changed.
|
||||||
|
* - (<0): In case of error
|
||||||
|
*/
|
||||||
|
bool set_cell_min_width(size_t row, size_t col, unsigned value)
|
||||||
|
{
|
||||||
|
return FT_IS_SUCCESS(ft_set_cell_option(table, row, col, FT_COPT_MIN_WIDTH, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set bottom padding for the specified cell of the table.
|
||||||
|
*
|
||||||
|
* @param row
|
||||||
|
* Cell row.
|
||||||
|
* @param col
|
||||||
|
* Cell column.
|
||||||
|
* @param value
|
||||||
|
* Value of the bottom padding.
|
||||||
|
* @return
|
||||||
|
* - 0: Success; cell option was changed.
|
||||||
|
* - (<0): In case of error
|
||||||
|
*/
|
||||||
|
bool set_cell_bottom_padding(size_t row, size_t col, unsigned value)
|
||||||
|
{
|
||||||
|
return FT_IS_SUCCESS(ft_set_cell_option(table, row, col, FT_COPT_BOTTOM_PADDING, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <CellOption option>
|
||||||
|
bool set_option(size_t row, size_t col, unsigned value);
|
||||||
|
|
||||||
|
template <CellOption option>
|
||||||
|
bool set_option(size_t row, size_t col, TextAlign align);
|
||||||
|
|
||||||
|
template <CellOption option>
|
||||||
|
bool set_option(size_t row, size_t col, RowType rowType);
|
||||||
|
|
||||||
|
template <TableOption option>
|
||||||
|
bool set_option(unsigned value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set border style for the table.
|
* Set border style for the table.
|
||||||
*
|
*
|
||||||
@ -282,12 +359,77 @@ public:
|
|||||||
return FT_IS_SUCCESS(ft_set_border_style(table, style));
|
return FT_IS_SUCCESS(ft_set_border_style(table, style));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set default border style for all new formatted tables.
|
||||||
|
*
|
||||||
|
* @param style
|
||||||
|
* Pointer to border style.
|
||||||
|
* @return
|
||||||
|
* - True: Success; table border style was changed.
|
||||||
|
* - False: Error
|
||||||
|
*/
|
||||||
|
bool set_default_border_style(struct ft_border_style *style)
|
||||||
|
{
|
||||||
|
return FT_IS_SUCCESS(ft_set_default_border_style(style));
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
ft_table_t *table;
|
ft_table_t *table;
|
||||||
std::stringstream stream;
|
std::stringstream stream;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Declare specializations for set_option functions
|
||||||
|
*/
|
||||||
|
#define DECLARE_SPECS_FOR_CELL_OPTIONS_X \
|
||||||
|
SET_CELL_OPTION_SPEC(CellOption::MinWidth, FT_COPT_MIN_WIDTH, unsigned) \
|
||||||
|
SET_CELL_OPTION_SPEC(CellOption::TextAlign, FT_COPT_TEXT_ALIGN, TextAlign) \
|
||||||
|
SET_CELL_OPTION_SPEC(CellOption::TopPadding, FT_COPT_TOP_PADDING, unsigned) \
|
||||||
|
SET_CELL_OPTION_SPEC(CellOption::BottomPadding, FT_COPT_BOTTOM_PADDING, unsigned) \
|
||||||
|
SET_CELL_OPTION_SPEC(CellOption::LeftPadding, FT_COPT_LEFT_PADDING, unsigned) \
|
||||||
|
SET_CELL_OPTION_SPEC(CellOption::RightPading, FT_COPT_RIGHT_PADDING, unsigned) \
|
||||||
|
SET_CELL_OPTION_SPEC(CellOption::EmptyStrHeight, FT_COPT_EMPTY_STR_HEIGHT, unsigned) \
|
||||||
|
SET_CELL_OPTION_SPEC(CellOption::RowType, FT_COPT_ROW_TYPE, RowType)
|
||||||
|
|
||||||
|
#define SET_CELL_OPTION_SPEC(CELL_OPTION, C_OPTION, VALUE_TYPE) \
|
||||||
|
template <> \
|
||||||
|
bool Table::set_option<CELL_OPTION>(size_t row, size_t col, VALUE_TYPE value) \
|
||||||
|
{ \
|
||||||
|
return FT_IS_SUCCESS(ft_set_cell_option(table, row, col, C_OPTION, static_cast<int>(value))); \
|
||||||
|
}
|
||||||
|
|
||||||
|
DECLARE_SPECS_FOR_CELL_OPTIONS_X
|
||||||
|
|
||||||
|
#undef SET_TABLE_OPTION_SPEC
|
||||||
|
#undef DECLARE_SPECS_FOR_CELL_OPTIONS_X
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define DECLARE_SPECS_FOR_TABLE_OPTIONS_X \
|
||||||
|
SET_TABLE_OPTION_SPEC(TableOption::LeftMargin, FT_TOPT_LEFT_MARGIN) \
|
||||||
|
SET_TABLE_OPTION_SPEC(TableOption::TopMargin, FT_TOPT_TOP_MARGIN) \
|
||||||
|
SET_TABLE_OPTION_SPEC(TableOption::RightMargin, FT_TOPT_RIGHT_MARGIN) \
|
||||||
|
SET_TABLE_OPTION_SPEC(TableOption::BottomMargin, FT_TOPT_BOTTOM_MARGIN)
|
||||||
|
|
||||||
|
#define SET_TABLE_OPTION_SPEC(TABLE_OPTION, TBL_OPTION) \
|
||||||
|
template <> \
|
||||||
|
bool Table::set_option<TABLE_OPTION>(unsigned value) \
|
||||||
|
{ \
|
||||||
|
return FT_IS_SUCCESS(ft_set_tbl_option(table, TBL_OPTION, static_cast<int>(value))); \
|
||||||
|
}
|
||||||
|
|
||||||
|
DECLARE_SPECS_FOR_TABLE_OPTIONS_X
|
||||||
|
|
||||||
|
#undef SET_TABLE_OPTION_SPEC
|
||||||
|
#undef DECLARE_SPECS_FOR_TABLE_OPTIONS_X
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // LIBFORT_HPP
|
#endif // LIBFORT_HPP
|
||||||
|
@ -2,20 +2,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "fort.h"
|
#include "fort.h"
|
||||||
|
|
||||||
void run_test_suit(const char *test_suit_name, int n_tests, struct test_case test_suit[])
|
|
||||||
{
|
|
||||||
fprintf(stderr, " == RUNNING %s ==\n", test_suit_name);
|
|
||||||
fprintf(stderr, "[==========] Running %d test(s).\n", n_tests);
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < n_tests; ++i) {
|
|
||||||
fprintf(stderr, "[ RUN ] %s\n", test_suit[i].name);
|
|
||||||
test_suit[i].test();
|
|
||||||
fprintf(stderr, "[ OK ] %s\n", test_suit[i].name);
|
|
||||||
}
|
|
||||||
fprintf(stderr, "[==========] %d test(s) run.\n", n_tests);
|
|
||||||
fprintf(stderr, "[ PASSED ] %d test(s).\n", n_tests);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef FORT_WB_TESTING_ENABLED
|
#ifdef FORT_WB_TESTING_ENABLED
|
||||||
struct test_case wb_test_suit [] = {
|
struct test_case wb_test_suit [] = {
|
||||||
{"test_vector_basic", test_vector_basic},
|
{"test_vector_basic", test_vector_basic},
|
||||||
|
27
tests/main_test_cpp.cpp
Normal file
27
tests/main_test_cpp.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include "tests.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "fort.h"
|
||||||
|
|
||||||
|
void test_cpp_table_basic(void);
|
||||||
|
|
||||||
|
|
||||||
|
struct test_case bb_test_suit [] = {
|
||||||
|
{"test_cpp_table_basic", test_cpp_table_basic},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int run_bb_test_suit(void)
|
||||||
|
{
|
||||||
|
int bb_n_tests = sizeof(bb_test_suit) / sizeof(bb_test_suit[0]);
|
||||||
|
run_test_suit("BLACK BOX TEST SUITE", bb_n_tests, bb_test_suit);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int status = 0;
|
||||||
|
|
||||||
|
status |= run_bb_test_suit();
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
@ -132,10 +132,19 @@ struct ft_table *create_test_int_wtable(int set_test_opts)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
void run_test_suit(const char *test_suit_name, int n_tests, struct test_case test_suit[])
|
||||||
|
{
|
||||||
|
fprintf(stderr, " == RUNNING %s ==\n", test_suit_name);
|
||||||
|
fprintf(stderr, "[==========] Running %d test(s).\n", n_tests);
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < n_tests; ++i) {
|
||||||
|
fprintf(stderr, "[ RUN ] %s\n", test_suit[i].name);
|
||||||
|
test_suit[i].test();
|
||||||
|
fprintf(stderr, "[ OK ] %s\n", test_suit[i].name);
|
||||||
|
}
|
||||||
|
fprintf(stderr, "[==========] %d test(s) run.\n", n_tests);
|
||||||
|
fprintf(stderr, "[ PASSED ] %d test(s).\n", n_tests);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -67,11 +67,19 @@ struct test_case {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct ft_table;
|
struct ft_table;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
int set_test_options_for_table(struct ft_table *table);
|
int set_test_options_for_table(struct ft_table *table);
|
||||||
int set_test_options_as_default(void);
|
int set_test_options_as_default(void);
|
||||||
struct ft_table *create_test_int_table(int set_test_opts);
|
struct ft_table *create_test_int_table(int set_test_opts);
|
||||||
struct ft_table *create_test_int_wtable(int set_test_opts);
|
struct ft_table *create_test_int_wtable(int set_test_opts);
|
||||||
|
void run_test_suit(const char *test_suit_name, int n_tests, struct test_case test_suit[]);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // TESTS_H
|
#endif // TESTS_H
|
||||||
|
Loading…
Reference in New Issue
Block a user