fix up comments, hyperlinks, and formulae consumption tests

This commit is contained in:
Thomas Fussell 2017-01-21 19:12:26 -05:00
parent 1858752a13
commit 0c100e166a
3 changed files with 83 additions and 43 deletions

View File

@ -2161,17 +2161,20 @@ void xlsx_consumer::read_worksheet(const std::string &rel_id)
expect_start_element(qn("spreadsheetml", "hyperlink"), xml::content::simple);
auto cell = ws.cell(parser().attribute("ref"));
auto hyperlink_rel_id = parser().attribute(qn("r", "id"));
auto hyperlink_rel = std::find_if(hyperlinks.begin(), hyperlinks.end(),
[&](const relationship &r) { return r.id() == hyperlink_rel_id; });
skip_attributes({"location", "tooltip", "display"});
if (hyperlink_rel != hyperlinks.end())
if (parser().attribute_present(qn("r", "id")))
{
cell.hyperlink(hyperlink_rel->target().path().string());
auto hyperlink_rel_id = parser().attribute(qn("r", "id"));
auto hyperlink_rel = std::find_if(hyperlinks.begin(), hyperlinks.end(),
[&](const relationship &r) { return r.id() == hyperlink_rel_id; });
if (hyperlink_rel != hyperlinks.end())
{
cell.hyperlink(hyperlink_rel->target().path().string());
}
}
skip_attributes({"location", "tooltip", "display"});
expect_end_element(qn("spreadsheetml", "hyperlink"));
}
}

View File

@ -10,34 +10,63 @@
class test_consume_xlsx : public CxxTest::TestSuite
{
public:
void test_non_xlsx()
{
xlnt::workbook wb;
const auto path = path_helper::data_directory("1_powerpoint_presentation.xlsx");
TS_ASSERT_THROWS(wb.load(path), xlnt::invalid_file);
}
void test_decrypt_agile()
{
xlnt::workbook wb;
wb.load(path_helper::data_directory("11_encrypted_excel_2016.xlsx"), "secret");
const auto path = path_helper::data_directory("5_encrypted_agile.xlsx");
TS_ASSERT_THROWS_NOTHING(wb.load(path, "secret"));
}
void test_decrypt_libre_office()
{
xlnt::workbook wb;
wb.load(path_helper::data_directory("12_encrypted_libre_office.xlsx"), "secret");
const auto path = path_helper::data_directory("6_encrypted_libre.xlsx");
TS_ASSERT_THROWS_NOTHING(wb.load(path, "secret"));
}
void test_decrypt_standard()
{
xlnt::workbook wb;
wb.load(path_helper::data_directory("13_encrypted_excel_2007.xlsx"), "password");
const auto path = path_helper::data_directory("7_encrypted_standard.xlsx");
TS_ASSERT_THROWS_NOTHING(wb.load(path, "password"));
}
void test_decrypt_numbers()
{
xlnt::workbook wb;
wb.load(path_helper::data_directory("14_encrypted_numbers.xlsx"), "secret");
const auto path = path_helper::data_directory("8_encrypted_numbers.xlsx");
TS_ASSERT_THROWS_NOTHING(wb.load(path, "secret"));
}
void test_read_unicode_filename()
{
#ifdef _MSC_VER
xlnt::workbook wb;
const auto path = LSTRING_LITERAL(XLNT_TEST_DATA_DIR) L"/9_unicode_filename_Λ.xlsx";
wb.load(path);
TS_ASSERT_EQUALS(wb.active_sheet().cell("A1").value<std::string>(), "unicode!");
#endif
#ifndef __MINGW32__
xlnt::workbook wb2;
const auto path = U8STRING_LITERAL(XLNT_TEST_DATA_DIR) u8"/9_unicode_filename_Λ.xlsx";
wb2.load(path);
TS_ASSERT_EQUALS(wb2.active_sheet().cell("A1").value<std::string>(), "unicode!");
#endif
}
void test_comments()
{
xlnt::workbook wb;
wb.load(path_helper::data_directory("15_basic_comments.xlsx"));
const auto path = path_helper::data_directory("10_comments_hyperlinks_formulae.xlsx");
wb.load(path);
auto sheet1 = wb[0];
TS_ASSERT_EQUALS(sheet1.cell("A1").value<std::string>(), "Sheet1!A1");
@ -50,28 +79,48 @@ public:
TS_ASSERT_EQUALS(sheet2.cell("A1").comment().author(), "Microsoft Office User");
}
void test_read_unicode_filename()
{
#ifdef _MSC_VER
xlnt::workbook wb;
static const std::string data_dir = STRING_LITERAL(XLNT_TEST_DATA_DIR);
wb.load(L"data\\16_unicode_Λ.xlsx");
TS_ASSERT_EQUALS(wb.active_sheet().cell("A1").value<std::string>(), "unicode!");
#endif
#ifndef __MINGW32__
xlnt::workbook wb2;
wb2.load(U8STRING_LITERAL(XLNT_TEST_DATA_DIR) u8"/16_unicode_Λ.xlsx");
TS_ASSERT_EQUALS(wb2.active_sheet().cell("A1").value<std::string>(), "unicode!");
#endif
}
void test_read_hyperlink()
{
xlnt::workbook wb;
wb.load(path_helper::data_directory("17_with_hyperlink.xlsx"));
TS_ASSERT(wb.active_sheet().cell("A1").has_hyperlink());
TS_ASSERT_EQUALS(wb.active_sheet().cell("A1").hyperlink(),
"https://fr.wikipedia.org/wiki/Ille-et-Vilaine");
const auto path = path_helper::data_directory("10_comments_hyperlinks_formulae.xlsx");
wb.load(path);
auto ws1 = wb.sheet_by_index(0);
TS_ASSERT_EQUALS(ws1.title(), "Sheet1");
TS_ASSERT(ws1.cell("A4").has_hyperlink());
TS_ASSERT_EQUALS(ws1.cell("A4").value<std::string>(), "hyperlink1");
TS_ASSERT_EQUALS(ws1.cell("A4").hyperlink(), "https://microsoft.com/");
TS_ASSERT(ws1.cell("A5").has_hyperlink());
TS_ASSERT_EQUALS(ws1.cell("A5").value<std::string>(), "https://google.com/");
TS_ASSERT_EQUALS(ws1.cell("A5").hyperlink(), "https://google.com/");
//TS_ASSERT(ws1.cell("A6").has_hyperlink());
TS_ASSERT_EQUALS(ws1.cell("A6").value<std::string>(), "Sheet1!A1");
//TS_ASSERT_EQUALS(ws1.cell("A6").hyperlink(), "Sheet1!A1");
TS_ASSERT(ws1.cell("A7").has_hyperlink());
TS_ASSERT_EQUALS(ws1.cell("A7").value<std::string>(), "mailto:invalid@example.com?subject=important");
TS_ASSERT_EQUALS(ws1.cell("A7").hyperlink(), "mailto:invalid@example.com?subject=important");
}
void test_read_formulae()
{
xlnt::workbook wb;
const auto path = path_helper::data_directory("10_comments_hyperlinks_formulae.xlsx");
wb.load(path);
auto ws1 = wb.sheet_by_index(0);
TS_ASSERT_EQUALS(ws1.cell("C1").value<std::string>(), "ab");
TS_ASSERT(ws1.cell("C1").has_formula());
TS_ASSERT_EQUALS(ws1.cell("C1").formula(), "CONCATENATE(C2,C3)");
TS_ASSERT_EQUALS(ws1.cell("C2").value<std::string>(), "a");
TS_ASSERT_EQUALS(ws1.cell("C3").value<std::string>(), "b");
auto ws2 = wb.sheet_by_index(1);
TS_ASSERT_EQUALS(ws2.cell("C1").value<int>(), 6);
TS_ASSERT(ws2.cell("C1").has_formula());
TS_ASSERT_EQUALS(ws2.cell("C1").formula(), "C2*C3");
TS_ASSERT_EQUALS(ws2.cell("C2").value<int>(), 2);
TS_ASSERT_EQUALS(ws2.cell("C3").value<int>(), 3);
}
void test_read_headers_and_footers()
@ -113,16 +162,4 @@ public:
TS_ASSERT(wb.has_custom_property("Client"));
TS_ASSERT_EQUALS(wb.custom_property("Client").get<std::string>(), "me!");
}
void test_read_formulae()
{
xlnt::workbook wb;
wb.load(path_helper::data_directory("22_formulae.xlsx"));
auto ws = wb.active_sheet();
TS_ASSERT_EQUALS(ws.cell("A1").value<int>(), 6);
TS_ASSERT(ws.cell("A1").has_formula());
TS_ASSERT_EQUALS(ws.cell("A1").formula(), "A2*A3");
TS_ASSERT_EQUALS(ws.cell("A2").value<int>(), 2);
TS_ASSERT_EQUALS(ws.cell("A3").value<int>(), 3);
}
};