From df49f332bf61d684868af83c53c64044f0bdb31f Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Sun, 11 May 2014 11:58:53 -0400 Subject: [PATCH] start working through cell tests --- source/tests/CellTestSuite.h | 27 ++++--- source/tests/PackageTestSuite.h | 2 +- source/tests/runner-autogen.cpp | 38 +++++----- source/tests/test_data/packaging/a.zip | Bin 257 -> 257 bytes source/xlnt.cpp | 100 +++++++++++++++++++++---- 5 files changed, 121 insertions(+), 46 deletions(-) diff --git a/source/tests/CellTestSuite.h b/source/tests/CellTestSuite.h index f72df6de..ea3b0d00 100644 --- a/source/tests/CellTestSuite.h +++ b/source/tests/CellTestSuite.h @@ -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(), ""); diff --git a/source/tests/PackageTestSuite.h b/source/tests/PackageTestSuite.h index fa57aabd..97e6c297 100644 --- a/source/tests/PackageTestSuite.h +++ b/source/tests/PackageTestSuite.h @@ -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"); diff --git a/source/tests/runner-autogen.cpp b/source/tests/runner-autogen.cpp index c58e7e26..6eac82fc 100644 --- a/source/tests/runner-autogen.cpp +++ b/source/tests/runner-autogen.cpp @@ -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; diff --git a/source/tests/test_data/packaging/a.zip b/source/tests/test_data/packaging/a.zip index 42d3bd332a55c7e47ffec1ee699e2eef555acbae..8afcf32aae23b22ef86061c3b8075ec9b98c8bb2 100644 GIT binary patch delta 26 icmZo 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 "title_ + "." + xlnt::cell::get_column_letter(column + 1) + std::to_string(row) + ">"; +} + }