Additional tests for row/col formatting.

This commit is contained in:
John McNamara 2014-06-18 01:24:58 +01:00
parent 3b35214983
commit 7c6876f4d6
24 changed files with 300 additions and 27 deletions

View File

@ -216,7 +216,8 @@ lxw_format *workbook_add_format(lxw_workbook *workbook);
uint8_t workbook_close(lxw_workbook *workbook);
void _free_workbook(lxw_workbook *workbook);
void _workbook_assemble_xml_file(lxw_workbook *self);
void _workbook_assemble_xml_file(lxw_workbook *workbook);
void _set_default_xf_indices(lxw_workbook *workbook);
/* Declarations required for unit testing. */
#ifdef TESTING

View File

@ -113,6 +113,7 @@ _free_format(lxw_format *format)
return;
free(format);
format = NULL;
}
/*

View File

@ -70,6 +70,19 @@ _free_workbook(lxw_workbook *workbook)
free(workbook);
}
/*
* Set the default index for each format. This is only used for testing.
*/
void
_set_default_xf_indices(lxw_workbook *self)
{
lxw_format *format;
STAILQ_FOREACH(format, self->formats, list_pointers) {
_get_xf_index(format);
}
}
/*
* Iterate through the XF Format objects and give them an index to non-default
* font elements.

View File

@ -93,16 +93,9 @@ _free_worksheet(lxw_worksheet *worksheet)
free(worksheet->col_options[col]);
}
}
free(worksheet->col_options);
free(worksheet->col_sizes);
if (worksheet->col_formats) {
for (col = 0; col < worksheet->col_formats_max; col++) {
if (worksheet->col_formats[col])
free(worksheet->col_formats[col]);
}
}
free(worksheet->col_formats);
if (worksheet->table) {
@ -405,15 +398,24 @@ _worksheet_write_dimension(lxw_worksheet *self)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
char ref[MAX_CELL_RANGE_LENGTH];
lxw_row_t dim_rowmin = self->dim_rowmin;
lxw_row_t dim_rowmax = self->dim_rowmax;
lxw_col_t dim_colmin = self->dim_colmin;
lxw_col_t dim_colmax = self->dim_colmax;
/* Default range ref for empty worksheet. */
char ref[MAX_CELL_RANGE_LENGTH] = "A1";
/* For non-empty worksheet fill in the cell range. */
if (!TAILQ_EMPTY(self->table)) {
xl_range(ref,
self->dim_rowmin, self->dim_colmin,
self->dim_rowmax, self->dim_colmax);
if (dim_rowmin == LXW_ROW_MAX && dim_colmin == LXW_COL_MAX) {
/* If the rows and cols are still the defaults then no dimensions have
* been set and we use the default range "A1". */
xl_range(ref, 0, 0, 0, 0);
}
else if (dim_rowmin == LXW_ROW_MAX && dim_colmin != LXW_COL_MAX) {
/* If the rows aren't set but the columns are then the dimensions have
* been changed via set_column(). */
xl_range(ref, 0, dim_colmin, 0, dim_colmax);
}
else {
xl_range(ref, dim_rowmin, dim_colmin, dim_rowmax, dim_colmax);
}
_INIT_ATTRIBUTES();
@ -661,22 +663,27 @@ _write_cell(lxw_worksheet *self, lxw_cell *cell, lxw_format *row_format)
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
char range[MAX_CELL_NAME_LENGTH] = { 0 };
lxw_row_t row_num = cell->row_num;
lxw_col_t col_num = cell->col_num;
int32_t index = 0;
xl_rowcol_to_cell_abs(range, cell->row_num, cell->col_num, 0, 0);
xl_rowcol_to_cell_abs(range, row_num, col_num, 0, 0);
_INIT_ATTRIBUTES();
_PUSH_ATTRIBUTES_STR("r", range);
if (cell->format) {
int32_t index = _get_xf_index(cell->format);
if (index)
_PUSH_ATTRIBUTES_INT("s", index);
index = _get_xf_index(cell->format);
}
else if (row_format) {
int32_t index = _get_xf_index(row_format);
index = _get_xf_index(row_format);
}
else if (col_num < self->col_sizes_max && self->col_formats[col_num]) {
index = _get_xf_index(self->col_formats[col_num]);
}
if (index)
_PUSH_ATTRIBUTES_INT("s", index);
}
if (cell->type == STRING_CELL)
_PUSH_ATTRIBUTES_STR("t", "s");
@ -1104,7 +1111,7 @@ worksheet_set_column(lxw_worksheet *self,
/* Ensure that the cols are valid and store max and min values.
* NOTE: The check shouldn't modify the row dimensions and should only
* modify the column dimensions in certain cases. */
if (format != NULL || width > 0)
if (format != NULL || (width > 0 && options && options->hidden))
ignore_col = LXW_FALSE;
err = _check_dimensions(self, 0, firstcol, ignore_row, ignore_col);

View File

@ -0,0 +1,25 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Simple test case to test worksheet set_row() and set_column().
*
* Copyright 2014, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_row_col_format04.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
worksheet_set_column(worksheet, 0, 0, 8.43, italic, NULL);
worksheet_write_string(worksheet, 0, 0, "Foo", NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,27 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Simple test case to test worksheet set_row() and set_column().
*
* Copyright 2014, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_row_col_format05.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
worksheet_set_column(worksheet, 0, 0, 8.43, bold, NULL);
worksheet_set_column(worksheet, 2, 2, 8.43, italic, NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,31 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Simple test case to test worksheet set_row() and set_column().
*
* Copyright 2014, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_row_col_format06.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
worksheet_set_column(worksheet, 0, 0, 8.43, bold, NULL);
worksheet_set_column(worksheet, 2, 2, 8.43, italic, NULL);
worksheet_write_string(worksheet, 0, 0, "Foo", NULL);
worksheet_write_string(worksheet, 0, 2, "Bar", NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,23 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Simple test case to test worksheet set_row() and set_column().
*
* Copyright 2014, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_row_col_format07.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
worksheet_set_row(worksheet, 4, 15, bold, NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,40 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Simple test case to test worksheet set_row() and set_column().
*
* Copyright 2014, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_row_col_format08.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *mixed = workbook_add_format(workbook);
format_set_bold(mixed);
format_set_italic(mixed);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
/* Manually force the format index order for testing. */
_set_default_xf_indices(workbook);
worksheet_set_row(worksheet, 0, 15, bold, NULL);
worksheet_set_column(worksheet, 0, 0, 8.43, italic, NULL);
worksheet_write_string(worksheet, 0, 0, "Foo", mixed);
worksheet_write_string(worksheet, 0, 1, "Foo", NULL);
worksheet_write_string(worksheet, 1, 0, "Foo", NULL);
worksheet_write_string(worksheet, 1, 1, "Foo", NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,38 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Simple test case to test worksheet set_row() and set_column().
*
* Copyright 2014, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_row_col_format09.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *mixed = workbook_add_format(workbook);
format_set_bold(mixed);
format_set_italic(mixed);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
/* Manually force the format index order for testing. */
_set_default_xf_indices(workbook);
worksheet_set_row(worksheet, 4, 15, bold, NULL);
worksheet_set_column(worksheet, 2, 2, 8.43, italic, NULL);
worksheet_write_string(worksheet, 0, 2, "Foo", NULL);
worksheet_write_string(worksheet, 4, 0, "Foo", NULL);
worksheet_write_string(worksheet, 4, 2, "Foo", mixed);
return workbook_close(workbook);
}

View File

@ -0,0 +1,23 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Simple test case to test worksheet set_row() and set_column().
*
* Copyright 2014, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_row_col_format10.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
worksheet_set_column(worksheet, 2, 2, LXW_DEF_COL_WIDTH, bold, NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,20 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Simple test case to test worksheet set_row() and set_column().
*
* Copyright 2014, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_row_col_format11.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_set_column(worksheet, 2, 2, 4, NULL, NULL);
return workbook_close(workbook);
}

View File

@ -19,6 +19,30 @@ class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
def test_row_colformat02(self):
self.run_exe_test('test_row_col_format02')
# def test_row_colformat03(self):
# self.run_exe_test('test_row_col_format03')
def test_row_colformat03(self):
self.run_exe_test('test_row_col_format03')
def test_row_colformat04(self):
self.run_exe_test('test_row_col_format04')
def test_row_colformat05(self):
self.run_exe_test('test_row_col_format05')
def test_row_colformat06(self):
self.run_exe_test('test_row_col_format06')
def test_row_colformat07(self):
self.run_exe_test('test_row_col_format07')
def test_row_colformat08(self):
self.run_exe_test('test_row_col_format08')
def test_row_colformat09(self):
self.run_exe_test('test_row_col_format09')
def test_row_colformat10(self):
self.run_exe_test('test_row_col_format10')
def test_row_colformat11(self):
self.run_exe_test('test_row_col_format11')

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.