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-05-19 09:29:19 +08:00
|
|
|
auto path = PathHelper::GetDataDirectory() + "/reader/sheet2.xml";
|
|
|
|
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-05-21 22:20:30 +08:00
|
|
|
TS_ASSERT_EQUALS(ws.get_cell("G5"), "hello");
|
|
|
|
TS_ASSERT_EQUALS(ws.get_cell("D30"), 30);
|
|
|
|
TS_ASSERT_EQUALS(ws.get_cell("K9"), 0.09);
|
2014-05-19 09:29:19 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_standard_workbook()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx";
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.load(path);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_standard_workbook_from_fileobj()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx";
|
2014-06-14 05:06:23 +08:00
|
|
|
std::ifstream fo(path, std::ios::binary);
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.load(fo);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_worksheet()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx";
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.load(path);
|
|
|
|
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
|
|
|
|
TS_ASSERT_DIFFERS(sheet2, nullptr);
|
2014-05-21 22:20:30 +08:00
|
|
|
TS_ASSERT_EQUALS("This is cell G5", sheet2.get_cell("G5"));
|
|
|
|
TS_ASSERT_EQUALS(18, sheet2.get_cell("D18"));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_nostring_workbook()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto genuine_wb = PathHelper::GetDataDirectory() + "/genuine/empty-no-string.xlsx";
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.load(genuine_wb);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_empty_file()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto null_file = PathHelper::GetDataDirectory() + "/reader/null_file.xlsx";
|
2014-05-13 01:42:28 +08:00
|
|
|
xlnt::workbook wb;
|
2014-05-19 09:29:19 +08:00
|
|
|
TS_ASSERT_THROWS_ANYTHING(wb.load(null_file));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 04:32:33 +08:00
|
|
|
//@raises(InvalidFileException)
|
2014-05-09 03:32:12 +08:00
|
|
|
void test_read_empty_archive()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto null_file = PathHelper::GetDataDirectory() + "/reader/null_archive.xlsx";
|
|
|
|
xlnt::workbook wb;
|
|
|
|
TS_ASSERT_THROWS_ANYTHING(wb.load(null_file));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_dimension()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto path = PathHelper::GetDataDirectory() + "/reader/sheet2.xml";
|
2014-06-13 05:04:37 +08:00
|
|
|
std::ifstream file(path);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << file.rdbuf();
|
|
|
|
auto dimension = xlnt::reader::read_dimension(ss.str());
|
|
|
|
TS_ASSERT_EQUALS("D1:AA30", dimension);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_calculate_dimension_iter()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx";
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.load(path);
|
|
|
|
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
|
|
|
|
auto dimensions = sheet2.calculate_dimension();
|
2014-06-07 23:49:19 +08:00
|
|
|
TS_ASSERT_EQUALS("D1:AA30", dimensions.to_string());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_highest_row_iter()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx";
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.load(path);
|
|
|
|
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
|
|
|
|
auto max_row = sheet2.get_highest_row();
|
|
|
|
TS_ASSERT_EQUALS(30, max_row);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_workbook_with_no_properties()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
auto genuine_wb = PathHelper::GetDataDirectory() + "/genuine/empty_with_no_properties.xlsx";
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.load(genuine_wb);
|
2014-05-13 01:42:28 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
|
|
|
|
void test_read_general_style()
|
|
|
|
{
|
2014-05-22 05:48:51 +08:00
|
|
|
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A1").get_style().get_number_format().get_format_code(), xlnt::number_format::format::general);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_date_style()
|
|
|
|
{
|
2014-05-22 05:48:51 +08:00
|
|
|
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A2").get_style().get_number_format().get_format_code(), xlnt::number_format::format::date_xlsx14);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_number_style()
|
|
|
|
{
|
2014-05-22 05:48:51 +08:00
|
|
|
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A3").get_style().get_number_format().get_format_code(), xlnt::number_format::format::number00);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_time_style()
|
|
|
|
{
|
2014-05-22 05:48:51 +08:00
|
|
|
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A4").get_style().get_number_format().get_format_code(), xlnt::number_format::format::date_time3);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_percentage_style()
|
|
|
|
{
|
2014-05-22 05:48:51 +08:00
|
|
|
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A5").get_style().get_number_format().get_format_code(), xlnt::number_format::format::percentage00)
|
2014-05-13 01:42:28 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
|
|
|
|
void test_read_win_base_date()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
//TS_ASSERT_EQUALS(win_wb.get_properties().get_excel_base_date(), CALENDAR_WINDOWS_1900);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_mac_base_date()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
//TS_ASSERT_EQUALS(mac_wb.get_properties().get_excel_base_date(), CALENDAR_MAC_1904);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_date_style_mac()
|
|
|
|
{
|
2014-05-22 05:48:51 +08:00
|
|
|
//TS_ASSERT_EQUALS(mac_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
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_date_style_win()
|
|
|
|
{
|
2014-05-22 05:48:51 +08:00
|
|
|
//TS_ASSERT_EQUALS(win_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
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_date_value()
|
|
|
|
{
|
2014-06-13 05:04:37 +08:00
|
|
|
//auto path = PathHelper::GetDataDirectory() + "/genuine/empty-with-styles.xlsx";
|
|
|
|
//wb_with_styles.load(path);
|
|
|
|
//worksheet_with_styles = wb_with_styles.get_sheet_by_name("Sheet1");
|
|
|
|
|
|
|
|
auto mac_wb_path = PathHelper::GetDataDirectory() + "/reader/date_1904.xlsx";
|
|
|
|
xlnt::workbook mac_wb;
|
|
|
|
mac_wb.load(mac_wb_path);
|
|
|
|
auto mac_ws = mac_wb.get_sheet_by_name("Sheet1");
|
|
|
|
|
|
|
|
auto win_wb_path = PathHelper::GetDataDirectory() + "/reader/date_1900.xlsx";
|
|
|
|
xlnt::workbook win_wb;
|
|
|
|
win_wb.load(win_wb_path);
|
|
|
|
auto win_ws = win_wb.get_sheet_by_name("Sheet1");
|
|
|
|
|
|
|
|
xlnt::datetime dt(2011, 10, 31);
|
|
|
|
TS_ASSERT_EQUALS(mac_ws.get_cell("A1"), dt);
|
|
|
|
TS_ASSERT_EQUALS(win_ws.get_cell("A1"), dt);
|
|
|
|
TS_ASSERT_EQUALS(mac_ws.get_cell("A1"), win_ws.get_cell("A1"));
|
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
};
|