start working through cell tests

This commit is contained in:
Thomas Fussell 2014-05-11 11:58:53 -04:00
parent d1d93866dd
commit df49f332bf
5 changed files with 121 additions and 46 deletions

View File

@ -46,20 +46,27 @@ public:
TS_ASSERT_EQUALS(10, xlnt::cell::column_index_from_string("J"));
TS_ASSERT_EQUALS(270, xlnt::cell::column_index_from_string("jJ"));
TS_ASSERT_EQUALS(7030, xlnt::cell::column_index_from_string("jjj"));
TS_ASSERT_EQUALS(1, xlnt::cell::column_index_from_string("A"));
TS_ASSERT_EQUALS(26, xlnt::cell::column_index_from_string("Z"));
TS_ASSERT_EQUALS(27, xlnt::cell::column_index_from_string("AA"));
TS_ASSERT_EQUALS(52, xlnt::cell::column_index_from_string("AZ"));
TS_ASSERT_EQUALS(53, xlnt::cell::column_index_from_string("BA"));
TS_ASSERT_EQUALS(78, xlnt::cell::column_index_from_string("BZ"));
TS_ASSERT_EQUALS(677, xlnt::cell::column_index_from_string("ZA"));
TS_ASSERT_EQUALS(702, xlnt::cell::column_index_from_string("ZZ"));
TS_ASSERT_EQUALS(703, xlnt::cell::column_index_from_string("AAA"));
TS_ASSERT_EQUALS(728, xlnt::cell::column_index_from_string("AAZ"));
TS_ASSERT_EQUALS(731, xlnt::cell::column_index_from_string("ABC"));
TS_ASSERT_EQUALS(1353, xlnt::cell::column_index_from_string("AZA"));
TS_ASSERT_EQUALS(18253, xlnt::cell::column_index_from_string("ZZA"));
TS_ASSERT_EQUALS(18278, xlnt::cell::column_index_from_string("ZZZ"));
}
void test_bad_column_index()
{
auto _check = [](const std::string &bad_string)
for(auto bad_string : {"JJJJ", "", "$", "1"})
{
xlnt::cell::column_index_from_string(bad_string);
};
auto bad_strings = {"JJJJ", "", "$", "1"};
for(auto bad_string : bad_strings)
{
TS_ASSERT_THROWS_ANYTHING(_check(bad_string));
TS_ASSERT_THROWS_ANYTHING(xlnt::cell::column_index_from_string(bad_string));
}
}
@ -282,7 +289,7 @@ public:
void test_repr()
{
xlnt::workbook wb;
xlnt::worksheet ws = wb.create_sheet();
xlnt::worksheet ws = wb.get_active();
xlnt::cell cell(ws, "A", 1);
TS_ASSERT_EQUALS(cell.to_string(), "<Cell Sheet1.A1>");

View File

@ -28,7 +28,7 @@ public:
void test_write_text()
{
{
auto package = xlnt::package::open("../../source/test_data/tests/packaging/a.zip", xlnt::file_mode::Open, xlnt::file_access::ReadWrite);
auto package = xlnt::package::open("../../source/tests/test_data/packaging/a.zip", xlnt::file_mode::Open, xlnt::file_access::ReadWrite);
TS_ASSERT_DIFFERS(package, nullptr);
auto part_1 = package.get_part("a.txt");

View File

@ -66,115 +66,115 @@ public:
static class TestDescription_suite_CellTestSuite_test_bad_column_index : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_bad_column_index() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 52, "test_bad_column_index" ) {}
TestDescription_suite_CellTestSuite_test_bad_column_index() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 65, "test_bad_column_index" ) {}
void runTest() { suite_CellTestSuite.test_bad_column_index(); }
} testDescription_suite_CellTestSuite_test_bad_column_index;
static class TestDescription_suite_CellTestSuite_test_column_letter_boundries : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_column_letter_boundries() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 66, "test_column_letter_boundries" ) {}
TestDescription_suite_CellTestSuite_test_column_letter_boundries() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 73, "test_column_letter_boundries" ) {}
void runTest() { suite_CellTestSuite.test_column_letter_boundries(); }
} testDescription_suite_CellTestSuite_test_column_letter_boundries;
static class TestDescription_suite_CellTestSuite_test_column_letter : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_column_letter() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 73, "test_column_letter" ) {}
TestDescription_suite_CellTestSuite_test_column_letter() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 80, "test_column_letter" ) {}
void runTest() { suite_CellTestSuite.test_column_letter(); }
} testDescription_suite_CellTestSuite_test_column_letter;
static class TestDescription_suite_CellTestSuite_test_initial_value : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_initial_value() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 83, "test_initial_value" ) {}
TestDescription_suite_CellTestSuite_test_initial_value() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 90, "test_initial_value" ) {}
void runTest() { suite_CellTestSuite.test_initial_value(); }
} testDescription_suite_CellTestSuite_test_initial_value;
static class TestDescription_suite_CellTestSuite_test_null : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_null() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 92, "test_null" ) {}
TestDescription_suite_CellTestSuite_test_null() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 99, "test_null" ) {}
void runTest() { suite_CellTestSuite.test_null(); }
} testDescription_suite_CellTestSuite_test_null;
static class TestDescription_suite_CellTestSuite_test_numeric : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_numeric() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 101, "test_numeric" ) {}
TestDescription_suite_CellTestSuite_test_numeric() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 108, "test_numeric" ) {}
void runTest() { suite_CellTestSuite.test_numeric(); }
} testDescription_suite_CellTestSuite_test_numeric;
static class TestDescription_suite_CellTestSuite_test_string : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_string() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 133, "test_string" ) {}
TestDescription_suite_CellTestSuite_test_string() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 140, "test_string" ) {}
void runTest() { suite_CellTestSuite.test_string(); }
} testDescription_suite_CellTestSuite_test_string;
static class TestDescription_suite_CellTestSuite_test_single_dot : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_single_dot() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 143, "test_single_dot" ) {}
TestDescription_suite_CellTestSuite_test_single_dot() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 150, "test_single_dot" ) {}
void runTest() { suite_CellTestSuite.test_single_dot(); }
} testDescription_suite_CellTestSuite_test_single_dot;
static class TestDescription_suite_CellTestSuite_test_formula : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_formula() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 152, "test_formula" ) {}
TestDescription_suite_CellTestSuite_test_formula() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 159, "test_formula" ) {}
void runTest() { suite_CellTestSuite.test_formula(); }
} testDescription_suite_CellTestSuite_test_formula;
static class TestDescription_suite_CellTestSuite_test_boolean : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_boolean() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 161, "test_boolean" ) {}
TestDescription_suite_CellTestSuite_test_boolean() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 168, "test_boolean" ) {}
void runTest() { suite_CellTestSuite.test_boolean(); }
} testDescription_suite_CellTestSuite_test_boolean;
static class TestDescription_suite_CellTestSuite_test_leading_zero : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_leading_zero() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 172, "test_leading_zero" ) {}
TestDescription_suite_CellTestSuite_test_leading_zero() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 179, "test_leading_zero" ) {}
void runTest() { suite_CellTestSuite.test_leading_zero(); }
} testDescription_suite_CellTestSuite_test_leading_zero;
static class TestDescription_suite_CellTestSuite_test_data_type_check : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_data_type_check() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 193, "test_data_type_check" ) {}
TestDescription_suite_CellTestSuite_test_data_type_check() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 200, "test_data_type_check" ) {}
void runTest() { suite_CellTestSuite.test_data_type_check(); }
} testDescription_suite_CellTestSuite_test_data_type_check;
static class TestDescription_suite_CellTestSuite_test_set_bad_type : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_set_bad_type() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 212, "test_set_bad_type" ) {}
TestDescription_suite_CellTestSuite_test_set_bad_type() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 219, "test_set_bad_type" ) {}
void runTest() { suite_CellTestSuite.test_set_bad_type(); }
} testDescription_suite_CellTestSuite_test_set_bad_type;
static class TestDescription_suite_CellTestSuite_test_time : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_time() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 222, "test_time" ) {}
TestDescription_suite_CellTestSuite_test_time() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 229, "test_time" ) {}
void runTest() { suite_CellTestSuite.test_time(); }
} testDescription_suite_CellTestSuite_test_time;
static class TestDescription_suite_CellTestSuite_test_date_format_on_non_date : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_date_format_on_non_date() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 251, "test_date_format_on_non_date" ) {}
TestDescription_suite_CellTestSuite_test_date_format_on_non_date() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 258, "test_date_format_on_non_date" ) {}
void runTest() { suite_CellTestSuite.test_date_format_on_non_date(); }
} testDescription_suite_CellTestSuite_test_date_format_on_non_date;
static class TestDescription_suite_CellTestSuite_test_set_get_date : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_set_get_date() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 264, "test_set_get_date" ) {}
TestDescription_suite_CellTestSuite_test_set_get_date() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 271, "test_set_get_date" ) {}
void runTest() { suite_CellTestSuite.test_set_get_date(); }
} testDescription_suite_CellTestSuite_test_set_get_date;
static class TestDescription_suite_CellTestSuite_test_repr : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_repr() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 282, "test_repr" ) {}
TestDescription_suite_CellTestSuite_test_repr() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 289, "test_repr" ) {}
void runTest() { suite_CellTestSuite.test_repr(); }
} testDescription_suite_CellTestSuite_test_repr;
static class TestDescription_suite_CellTestSuite_test_is_date : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_is_date() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 291, "test_is_date" ) {}
TestDescription_suite_CellTestSuite_test_is_date() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 298, "test_is_date" ) {}
void runTest() { suite_CellTestSuite.test_is_date(); }
} testDescription_suite_CellTestSuite_test_is_date;
static class TestDescription_suite_CellTestSuite_test_is_not_date_color_format : public CxxTest::RealTestDescription {
public:
TestDescription_suite_CellTestSuite_test_is_not_date_color_format() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 308, "test_is_not_date_color_format" ) {}
TestDescription_suite_CellTestSuite_test_is_not_date_color_format() : CxxTest::RealTestDescription( Tests_CellTestSuite, suiteDescription_CellTestSuite, 315, "test_is_not_date_color_format" ) {}
void runTest() { suite_CellTestSuite.test_is_not_date_color_format(); }
} testDescription_suite_CellTestSuite_test_is_not_date_color_format;

View File

@ -606,16 +606,7 @@ struct cell_struct
}
std::string to_string() const
{
switch(type)
{
case cell::type::numeric: return std::to_string(numeric_value);
case cell::type::boolean: return bool_value ? "true" : "false";
case cell::type::string: return string_value;
default: throw std::runtime_error("bad enum");
}
}
std::string to_string() const;
cell::type type;
@ -654,19 +645,80 @@ cell::cell(cell_struct *root) : root_(root)
{
}
coordinate cell::coordinate_from_string(const std::string &address)
coordinate cell::coordinate_from_string(const std::string &coord_string)
{
if(address == "A1")
// Convert a coordinate string like 'B12' to a tuple ('B', 12)
bool column_part = true;
coordinate result;
for(auto character : coord_string)
{
return{"A", 1};
char upper = std::toupper(character);
if(std::isalpha(character))
{
if(column_part)
{
result.column.append(1, upper);
}
else
{
std::string msg = "Invalid cell coordinates (" + coord_string + ")";
throw std::runtime_error(msg);
}
}
else
{
if(column_part)
{
column_part = false;
}
else if(!(std::isdigit(character) || character == '$'))
{
std::string msg = "Invalid cell coordinates (" + coord_string + ")";
throw std::runtime_error(msg);
}
}
}
return{"A", 1};
std::string row_string = coord_string.substr(result.column.length());
if(row_string[0] == '$')
{
row_string = row_string.substr(1);
}
result.row = std::stoi(row_string);
if(result.row < 1)
{
std::string msg = "Invalid cell coordinates (" + coord_string + ")";
throw std::runtime_error(msg);
}
return result;
}
int cell::column_index_from_string(const std::string &column_string)
{
return column_string[0] - 'A';
if(column_string.length() > 3 || column_string.empty())
{
throw std::runtime_error("column must be one to three characters");
}
int column_index = 0;
int place = std::pow(26, column_string.length() - 1);
for(int i = column_string.length() - 1; i >= 0; i--)
{
if(!std::isalpha(column_string[i]))
{
throw std::runtime_error("column must contain only letters in the range A-Z");
}
column_index += (std::toupper(column_string[i]) - 'A' + 1) * place;
place /= 26;
}
return column_index;
}
// Convert a column number into a column letter (3 -> 'C')
@ -726,7 +778,18 @@ bool operator==(const tm &comparand, const cell &cell)
std::string cell::absolute_coordinate(const std::string &absolute_address)
{
return absolute_address;
// Convert a coordinate to an absolute coordinate string (B12 -> $B$12)
auto colon_index = absolute_address.find(':');
if(colon_index != std::string::npos)
{
return absolute_coordinate(absolute_address.substr(0, colon_index)) + ":"
+ absolute_coordinate(absolute_address.substr(colon_index + 1));
}
else
{
auto coord = coordinate_from_string(absolute_address);
return std::string("$") + coord.column + "$" + std::to_string(coord.row);
}
}
cell::type cell::get_data_type()
@ -1182,4 +1245,9 @@ void workbook::save(const std::string &filename)
package.close();
}
std::string cell_struct::to_string() const
{
return "<Cell " + parent_worksheet->title_ + "." + xlnt::cell::get_column_letter(column + 1) + std::to_string(row) + ">";
}
}