xlnt/tests/test_read.hpp

479 lines
19 KiB
C++
Raw Normal View History

2014-05-09 03:32:12 +08:00
#pragma once
2014-05-19 09:29:19 +08:00
#include <fstream>
2014-05-09 03:32:12 +08:00
#include <iostream>
#include <cxxtest/TestSuite.h>
2014-06-06 04:19:31 +08:00
#include <xlnt/xlnt.hpp>
#include "helpers/path_helper.hpp"
2014-05-09 03:32:12 +08:00
2014-06-06 04:19:31 +08:00
class test_read : public CxxTest::TestSuite
2014-05-09 03:32:12 +08:00
{
public:
void test_read_standalone_worksheet()
{
2014-07-20 04:59:05 +08:00
auto path = PathHelper::GetDataDirectory("/reader/sheet2.xml");
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
xlnt::worksheet ws(wb);
{
std::ifstream handle(path);
2014-06-07 23:49:19 +08:00
ws = xlnt::reader::read_worksheet(handle, wb, "Sheet 2", {"hello"});
2014-05-19 09:29:19 +08:00
}
TS_ASSERT_DIFFERS(ws, nullptr);
if(!(ws == nullptr))
{
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS(ws.get_cell("G5").get_value(), "hello");
TS_ASSERT_EQUALS(ws.get_cell("D30").get_value(), 30);
TS_ASSERT_EQUALS(ws.get_cell("K9").get_value(), 0.09);
2014-05-19 09:29:19 +08:00
}
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
xlnt::workbook standard_workbook()
{
2014-07-20 04:59:05 +08:00
auto path = PathHelper::GetDataDirectory("/genuine/empty.xlsx");
return xlnt::reader::load_workbook(path);
2014-07-19 03:18:23 +08:00
}
2014-05-09 03:32:12 +08:00
void test_read_standard_workbook()
{
2014-07-19 03:18:23 +08:00
TS_ASSERT_DIFFERS(standard_workbook(), nullptr);
2014-05-09 03:32:12 +08:00
}
void test_read_standard_workbook_from_fileobj()
{
2014-07-20 04:59:05 +08:00
auto path = PathHelper::GetDataDirectory("/genuine/empty.xlsx");
std::ifstream fo(path, std::ios::binary);
2014-07-20 04:59:05 +08:00
auto wb = xlnt::reader::load_workbook(path);
2014-07-19 03:18:23 +08:00
TS_ASSERT_DIFFERS(standard_workbook(), nullptr);
2014-05-09 03:32:12 +08:00
}
void test_read_worksheet()
{
2014-07-20 04:59:05 +08:00
auto wb = standard_workbook();
2014-05-19 09:29:19 +08:00
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
TS_ASSERT_DIFFERS(sheet2, nullptr);
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS("This is cell G5", sheet2.get_cell("G5").get_value());
TS_ASSERT_EQUALS(18, sheet2.get_cell("D18").get_value());
TS_ASSERT_EQUALS(true, sheet2.get_cell("G9").get_value());
TS_ASSERT_EQUALS(false, sheet2.get_cell("G10").get_value());
2014-05-09 03:32:12 +08:00
}
void test_read_nostring_workbook()
{
2014-07-19 03:18:23 +08:00
auto path = PathHelper::GetDataDirectory("/genuine/empty-no-string.xlsx");
2014-07-20 04:59:05 +08:00
auto wb = xlnt::reader::load_workbook(path);
2014-07-19 03:18:23 +08:00
TS_ASSERT_DIFFERS(standard_workbook(), nullptr);
2014-05-09 03:32:12 +08:00
}
void test_read_empty_file()
{
2014-07-20 04:59:05 +08:00
auto path = PathHelper::GetDataDirectory("/reader/null_file.xlsx");
TS_ASSERT_THROWS(xlnt::reader::load_workbook(path), xlnt::invalid_file_exception);
2014-05-09 03:32:12 +08:00
}
void test_read_empty_archive()
{
2014-07-19 03:18:23 +08:00
auto path = PathHelper::GetDataDirectory("/reader/null_archive.xlsx");
2014-07-20 04:59:05 +08:00
TS_ASSERT_THROWS(xlnt::reader::load_workbook(path), xlnt::invalid_file_exception);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
void test_read_workbook_with_no_properties()
2014-05-09 03:32:12 +08:00
{
2014-07-20 04:59:05 +08:00
auto path = PathHelper::GetDataDirectory("/genuine/empty_with_no_properties.xlsx");
xlnt::reader::load_workbook(path);
2014-05-09 03:32:12 +08:00
}
2014-07-20 04:59:05 +08:00
xlnt::workbook workbook_with_styles()
2014-05-09 03:32:12 +08:00
{
2014-07-20 04:59:05 +08:00
auto path = PathHelper::GetDataDirectory("/genuine/empty-with-styles.xlsx");
return xlnt::reader::load_workbook(path);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
void test_read_workbook_with_styles_general()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
2014-07-24 08:51:28 +08:00
auto code = ws.get_cell("A1").get_style().get_number_format().get_format_code();
auto expected = xlnt::number_format::format::general;
TS_ASSERT_EQUALS(code, expected);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
void test_read_workbook_with_styles_date()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
2014-07-24 08:51:28 +08:00
auto code = ws.get_cell("A2").get_style().get_number_format().get_format_code();
auto expected = xlnt::number_format::format::date_xlsx14;
TS_ASSERT_EQUALS(code, expected);
2014-05-13 01:42:28 +08:00
}
2014-07-19 03:18:23 +08:00
void test_read_workbook_with_styles_number()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
2014-07-24 08:51:28 +08:00
auto code = ws.get_cell("A3").get_style().get_number_format().get_format_code();
auto expected = xlnt::number_format::format::number_00;
TS_ASSERT_EQUALS(code, expected);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
void test_read_workbook_with_styles_time()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
2014-07-24 08:51:28 +08:00
auto code = ws.get_cell("A4").get_style().get_number_format().get_format_code();
auto expected = xlnt::number_format::format::date_time3;
TS_ASSERT_EQUALS(code, expected);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
void test_read_workbook_with_styles_percentage()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
2014-07-24 08:51:28 +08:00
auto code = ws.get_cell("A5").get_style().get_number_format().get_format_code();
auto expected = xlnt::number_format::format::percentage_00;
TS_ASSERT_EQUALS(code, expected);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
2014-07-20 04:59:05 +08:00
xlnt::workbook date_mac_1904()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto path = PathHelper::GetDataDirectory("/reader/date_1904.xlsx");
2014-07-20 04:59:05 +08:00
return xlnt::reader::load_workbook(path);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
2014-07-20 04:59:05 +08:00
xlnt::workbook date_std_1900()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto path = PathHelper::GetDataDirectory("/reader/date_1900.xlsx");
2014-07-20 04:59:05 +08:00
return xlnt::reader::load_workbook(path);
2014-05-13 01:42:28 +08:00
}
2014-07-19 03:18:23 +08:00
2014-05-09 03:32:12 +08:00
void test_read_win_base_date()
{
2014-07-19 03:18:23 +08:00
auto wb = date_std_1900();
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS(wb.get_properties().excel_base_date, xlnt::calendar::windows_1900);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
2014-05-09 03:32:12 +08:00
void test_read_mac_base_date()
{
2014-07-19 03:18:23 +08:00
auto wb = date_mac_1904();
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS(wb.get_properties().excel_base_date, xlnt::calendar::mac_1904);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
void test_read_date_style_win()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto wb = date_std_1900();
auto ws = wb["Sheet1"];
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS(ws.get_cell("A1").get_style().get_number_format().get_format_code(), xlnt::number_format::format::date_xlsx14);
2014-05-09 03:32:12 +08:00
}
2014-07-19 03:18:23 +08:00
void test_read_date_style_mac()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
auto wb = date_mac_1904();
auto ws = wb["Sheet1"];
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS(ws.get_cell("A1").get_style().get_number_format().get_format_code(), xlnt::number_format::format::date_xlsx14);
2014-05-09 03:32:12 +08:00
}
2014-07-20 04:59:05 +08:00
void test_read_compare_mac_win_dates()
2014-07-19 03:18:23 +08:00
{
auto wb_mac = date_mac_1904();
auto ws_mac = wb_mac["Sheet1"];
2014-07-20 04:59:05 +08:00
auto wb_win = date_std_1900();
2014-07-19 03:18:23 +08:00
auto ws_win = wb_win["Sheet1"];
xlnt::datetime dt(2011, 10, 31);
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS(ws_mac.get_cell("A1").get_value(), dt);
TS_ASSERT_EQUALS(ws_win.get_cell("A1").get_value(), dt);
TS_ASSERT_EQUALS(ws_mac.get_cell("A1").get_value(), ws_win.get_cell("A1").get_value());
2014-07-19 03:18:23 +08:00
}
void test_repair_central_directory()
{
2014-07-25 05:31:46 +08:00
std::string data_a = "foobarbaz" + xlnt::reader::CentralDirectorySignature;
2014-07-19 03:18:23 +08:00
std::string data_b = "bazbarfoo12345678901234567890";
2014-07-25 05:31:46 +08:00
auto f = xlnt::reader::repair_central_directory(data_a + data_b);
2014-07-19 03:18:23 +08:00
TS_ASSERT_EQUALS(f, data_a + data_b.substr(0, 18));
2014-07-25 05:31:46 +08:00
f = xlnt::reader::repair_central_directory(data_b);
2014-07-19 03:18:23 +08:00
TS_ASSERT_EQUALS(f, data_b);
}
void test_read_no_theme()
{
auto path = PathHelper::GetDataDirectory("/genuine/libreoffice_nrt.xlsx");
2014-07-20 04:59:05 +08:00
auto wb = xlnt::reader::load_workbook(path);
2014-07-19 03:18:23 +08:00
TS_ASSERT_DIFFERS(wb, nullptr);
}
void test_read_cell_formulae()
2014-05-09 03:32:12 +08:00
{
2014-07-19 03:18:23 +08:00
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
auto path = PathHelper::GetDataDirectory("/reader/worksheet_formula.xml");
std::ifstream ws_stream(path);
2014-07-20 04:59:05 +08:00
xlnt::reader::fast_parse(ws, ws_stream, {"", ""}, {}, 0);
2014-07-19 03:18:23 +08:00
auto b1 = ws.get_cell("B1");
2014-07-25 05:31:46 +08:00
TS_ASSERT(b1.has_formula());
TS_ASSERT_EQUALS(b1.get_formula(), "CONCATENATE(A1,A2)");
2014-07-19 03:18:23 +08:00
auto a6 = ws.get_cell("A6");
2014-07-25 05:31:46 +08:00
TS_ASSERT(a6.has_formula());
TS_ASSERT_EQUALS(a6.get_formula(), "SUM(A4:A5)");
2014-07-19 03:18:23 +08:00
}
2014-07-26 04:39:25 +08:00
void _test_read_complex_formulae()
2014-07-19 03:18:23 +08:00
{
2014-07-25 05:31:46 +08:00
/*
auto path = PathHelper::GetDataDirectory("/reader/formulae.xlsx");
auto wb = xlnt::reader::load_workbook(path);
auto ws = wb.get_active_sheet();
// Test normal forumlae
TS_ASSERT(!ws.get_cell("A1").has_formula());
TS_ASSERT(!ws.get_cell("A2").has_formula());
TS_ASSERT(ws.get_cell("A3").has_formula());
TS_ASSERT(ws.get_formula_attributes().find("A3") == ws.get_formula_attributes().end());
TS_ASSERT(ws.get_cell("A3").get_formula() == "12345");
TS_ASSERT(ws.get_cell("A4").has_formula());
TS_ASSERT(ws.get_formula_attributes().find("A3") == ws.get_formula_attributes().end());
ws.get_cell("A4").set_formula("A2+A3");
TS_ASSERT(ws.get_cell("A5").has_formula());
TS_ASSERT(ws.get_formula_attributes().find("A5") == ws.get_formula_attributes().end());
ws.get_cell("A5").set_formula("SUM(A2:A4)");
// Test unicode
std::string expected = "=IF(ISBLANK(B16), \"D\xFCsseldorf\", B16)";
TS_ASSERT(ws.get_cell("A16").get_formula() == expected);
// Test shared forumlae
TS_ASSERT(ws.get_cell("B7").get_data_type() == "f");
TS_ASSERT(ws.formula_attributes["B7"]["t"] == "shared");
TS_ASSERT(ws.formula_attributes["B7"]["si"] == "0");
TS_ASSERT(ws.formula_attributes["B7"]["ref"] == "B7:E7");
TS_ASSERT(ws.get_cell("B7").value == "=B4*2");
TS_ASSERT(ws.get_cell("C7").get_data_type() == "f");
TS_ASSERT(ws.formula_attributes["C7"]["t"] == "shared");
TS_ASSERT(ws.formula_attributes["C7"]["si"] == "0");
TS_ASSERT("ref" not in ws.formula_attributes["C7"]);
TS_ASSERT(ws.get_cell("C7").value == "=");
TS_ASSERT(ws.get_cell("D7").get_data_type() == "f");
TS_ASSERT(ws.formula_attributes["D7"]["t"] == "shared");
TS_ASSERT(ws.formula_attributes["D7"]["si"] == "0");
TS_ASSERT("ref" not in ws.formula_attributes["D7"]);
TS_ASSERT(ws.get_cell("D7").value == "=");
TS_ASSERT(ws.get_cell("E7").get_data_type() == "f");
TS_ASSERT(ws.formula_attributes["E7"]["t"] == "shared");
TS_ASSERT(ws.formula_attributes["E7"]["si"] == "0");
TS_ASSERT("ref" not in ws.formula_attributes["E7"]);
TS_ASSERT(ws.get_cell("E7").value == "=");
// Test array forumlae
TS_ASSERT(ws.get_cell("C10").get_data_type() == "f");
TS_ASSERT("ref" not in ws.formula_attributes["C10"]["ref"]);
TS_ASSERT(ws.formula_attributes["C10"]["t"] == "array");
TS_ASSERT("si" not in ws.formula_attributes["C10"]);
TS_ASSERT(ws.formula_attributes["C10"]["ref"] == "C10:C14");
TS_ASSERT(ws.get_cell("C10").value == "=SUM(A10:A14*B10:B14)");
TS_ASSERT(ws.get_cell("C11").get_data_type() != "f");
*/
2014-07-19 03:18:23 +08:00
}
void test_data_only()
{
2014-07-25 05:31:46 +08:00
auto path = PathHelper::GetDataDirectory("/reader/formulae.xlsx");
auto wb = xlnt::reader::load_workbook(path, false, true);
auto ws = wb.get_active_sheet();
TS_ASSERT(ws.get_formula_attributes().empty());
TS_ASSERT(ws.get_parent().get_data_only());
2014-07-26 04:39:25 +08:00
TS_ASSERT(ws.get_cell("A2").get_value().is(xlnt::value::type::numeric));
TS_ASSERT(ws.get_cell("A2").get_value() == 12345);
2014-07-25 05:31:46 +08:00
TS_ASSERT(!ws.get_cell("A2").has_formula());
2014-07-26 04:39:25 +08:00
TS_ASSERT(ws.get_cell("A3").get_value().is(xlnt::value::type::numeric));
TS_ASSERT(ws.get_cell("A3").get_value() == 12345);
2014-07-25 05:31:46 +08:00
TS_ASSERT(!ws.get_cell("A3").has_formula());
2014-07-26 04:39:25 +08:00
TS_ASSERT(ws.get_cell("A4").get_value().is(xlnt::value::type::numeric));
TS_ASSERT(ws.get_cell("A4").get_value() == 24690);
2014-07-25 05:31:46 +08:00
TS_ASSERT(!ws.get_cell("A4").has_formula());
2014-07-26 04:39:25 +08:00
TS_ASSERT(ws.get_cell("A5").get_value().is(xlnt::value::type::numeric));
TS_ASSERT(ws.get_cell("A5").get_value() == 49380);
2014-07-25 05:31:46 +08:00
TS_ASSERT(!ws.get_cell("A5").has_formula());
2014-07-19 03:18:23 +08:00
}
void test_detect_worksheets()
{
2014-07-25 05:31:46 +08:00
{
auto path = PathHelper::GetDataDirectory("/reader/bug137.xlsx");
2014-08-01 21:44:21 +08:00
xlnt::zip_file archive(path);
2014-07-25 05:31:46 +08:00
std::vector<std::pair<std::string, std::string>> expected =
{
{"xl/worksheets/sheet1.xml", "Sheet1"}
};
TS_ASSERT_EQUALS(xlnt::reader::detect_worksheets(archive), expected);
}
{
auto path = PathHelper::GetDataDirectory("/reader/contains_chartsheets.xlsx");
2014-08-01 21:44:21 +08:00
xlnt::zip_file archive(path);
2014-07-25 05:31:46 +08:00
std::vector<std::pair<std::string, std::string>> expected =
{
{"xl/worksheets/sheet1.xml", "data"},
{"xl/worksheets/sheet2.xml", "moredata"}
};
TS_ASSERT_EQUALS(xlnt::reader::detect_worksheets(archive), expected);
}
{
auto path = PathHelper::GetDataDirectory("/reader/bug304.xlsx");
2014-08-01 21:44:21 +08:00
xlnt::zip_file archive(path);
2014-07-25 05:31:46 +08:00
std::vector<std::pair<std::string, std::string>> expected =
{
{"xl/worksheets/sheet3.xml", "Sheet1"},
{"xl/worksheets/sheet2.xml", "Sheet2"},
{"xl/worksheets/sheet.xml", "Sheet3"}
};
TS_ASSERT_EQUALS(xlnt::reader::detect_worksheets(archive), expected);
}
2014-07-19 03:18:23 +08:00
}
void test_read_rels()
{
2014-07-25 05:31:46 +08:00
{
std::vector<xlnt::relationship> expected =
{
{xlnt::relationship::type::theme, "rId3", "xl/theme/theme1.xml"},
{xlnt::relationship::type::worksheet, "rId2", "xl/worksheets/sheet1.xml"},
{xlnt::relationship::type::chartsheet, "rId1", "xl/chartsheets/sheet1.xml"},
{xlnt::relationship::type::shared_strings, "rId5", "xl/sharedStrings.xml"},
{xlnt::relationship::type::styles, "rId4", "xl/styles.xml"}
};
auto path = PathHelper::GetDataDirectory("/reader/bug137.xlsx");
2014-08-01 21:44:21 +08:00
xlnt::zip_file archive(path);
2014-07-25 05:31:46 +08:00
TS_ASSERT_EQUALS(xlnt::reader::read_relationships(archive, "xl/workbook.xml"), expected);
}
{
std::vector<xlnt::relationship> expected =
{
{xlnt::relationship::type::custom_xml, "rId8", "../customXml/item3.xml"},
{xlnt::relationship::type::worksheet, "rId3", "xl/worksheets/sheet.xml"},
{xlnt::relationship::type::custom_xml, "rId7", "../customXml/item2.xml"},
{xlnt::relationship::type::worksheet, "rId2", "xl/worksheets/sheet2.xml"},
{xlnt::relationship::type::worksheet, "rId1", "xl/worksheets/sheet3.xml"},
{xlnt::relationship::type::custom_xml, "rId6", "../customXml/item1.xml"},
{xlnt::relationship::type::styles, "rId5", "xl/styles.xml"},
{xlnt::relationship::type::theme, "rId4", "xl/theme/theme.xml"}
};
auto path = PathHelper::GetDataDirectory("/reader/bug304.xlsx");
2014-08-01 21:44:21 +08:00
xlnt::zip_file archive(path);
2014-07-25 05:31:46 +08:00
TS_ASSERT_EQUALS(xlnt::reader::read_relationships(archive, "xl/workbook.xml"), expected);
}
2014-07-19 03:18:23 +08:00
}
void test_read_content_types()
{
std::vector<std::pair<std::string, std::string>> expected =
{
{"/xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"},
{"/xl/worksheets/sheet1.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"},
{"/xl/chartsheets/sheet1.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml"},
{"/xl/worksheets/sheet2.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"},
{"/xl/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml"},
{"/xl/styles.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"},
{"/xl/sharedStrings.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"},
{"/xl/drawings/drawing1.xml", "application/vnd.openxmlformats-officedocument.drawing+xml"},
{"/xl/charts/chart1.xml", "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"},
{"/xl/drawings/drawing2.xml", "application/vnd.openxmlformats-officedocument.drawing+xml"},
{"/xl/charts/chart2.xml", "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"},
{"/xl/calcChain.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml"},
{"/docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml"},
{"/docProps/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml"}
};
2014-07-24 04:00:09 +08:00
auto path = PathHelper::GetDataDirectory("/reader/contains_chartsheets.xlsx");
2014-08-01 21:44:21 +08:00
xlnt::zip_file f(path);
2014-07-20 04:59:05 +08:00
auto result = xlnt::reader::read_content_types(f);
2014-07-24 04:00:09 +08:00
if(result.size() != expected.size())
{
TS_ASSERT_EQUALS(result.size(), expected.size());
return;
}
2014-07-20 04:59:05 +08:00
for(std::size_t i = 0; i < expected.size(); i++)
{
TS_ASSERT_EQUALS(result[i], expected[i]);
}
2014-07-19 03:18:23 +08:00
}
void test_read_sheets()
{
{
auto path = PathHelper::GetDataDirectory("/reader/bug137.xlsx");
2014-08-01 21:44:21 +08:00
xlnt::zip_file f(path);
2014-07-20 04:59:05 +08:00
auto sheets = xlnt::reader::read_sheets(f);
std::vector<std::pair<std::string, std::string>> expected =
{{"rId1", "Chart1"}, {"rId2", "Sheet1"}};
TS_ASSERT_EQUALS(sheets, expected);
2014-07-19 03:18:23 +08:00
}
{
auto path = PathHelper::GetDataDirectory("/reader/bug304.xlsx");
2014-08-01 21:44:21 +08:00
xlnt::zip_file f(path);
2014-07-20 04:59:05 +08:00
auto sheets = xlnt::reader::read_sheets(f);
std::vector<std::pair<std::string, std::string>> expected =
{{"rId1", "Sheet1"}, {"rId2", "Sheet2"}, {"rId3", "Sheet3"}};
TS_ASSERT_EQUALS(sheets, expected);
2014-07-19 03:18:23 +08:00
}
}
void test_guess_types()
{
bool guess;
2014-07-26 04:39:25 +08:00
xlnt::value::type dtype;
std::vector<std::pair<bool, xlnt::value::type>> test_cases = {{true, xlnt::value::type::numeric}, {false, xlnt::value::type::string}};
2014-07-19 03:18:23 +08:00
2014-07-20 04:59:05 +08:00
for(const auto &expected : test_cases)
2014-07-19 03:18:23 +08:00
{
std::tie(guess, dtype) = expected;
auto path = PathHelper::GetDataDirectory("/genuine/guess_types.xlsx");
2014-07-20 04:59:05 +08:00
auto wb = xlnt::reader::load_workbook(path, guess);
2014-07-19 03:18:23 +08:00
auto ws = wb.get_active_sheet();
2014-07-26 04:39:25 +08:00
TS_ASSERT(ws.get_cell("D2").get_value().is(dtype));
2014-07-19 03:18:23 +08:00
}
}
void test_read_autofilter()
{
auto path = PathHelper::GetDataDirectory("/reader/bug275.xlsx");
2014-07-20 04:59:05 +08:00
auto wb = xlnt::reader::load_workbook(path);
2014-07-19 03:18:23 +08:00
auto ws = wb.get_active_sheet();
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS(ws.get_auto_filter().to_string(), "A1:B6");
2014-07-19 03:18:23 +08:00
}
void test_bad_formats_xlsb()
{
auto path = PathHelper::GetDataDirectory("/genuine/a.xlsb");
2014-07-20 04:59:05 +08:00
TS_ASSERT_THROWS(xlnt::reader::load_workbook(path), xlnt::invalid_file_exception);
2014-07-19 03:18:23 +08:00
}
void test_bad_formats_xls()
{
auto path = PathHelper::GetDataDirectory("/genuine/a.xls");
2014-07-20 04:59:05 +08:00
TS_ASSERT_THROWS(xlnt::reader::load_workbook(path), xlnt::invalid_file_exception);
2014-07-19 03:18:23 +08:00
}
void test_bad_formats_no()
{
auto path = PathHelper::GetDataDirectory("/genuine/a.no-format");
2014-07-20 04:59:05 +08:00
TS_ASSERT_THROWS(xlnt::reader::load_workbook(path), xlnt::invalid_file_exception);
}
2014-05-09 03:32:12 +08:00
};