xlnt/tests/test_read.hpp

197 lines
6.2 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:
2014-06-06 04:19:31 +08:00
test_read()
2014-05-09 03:32:12 +08:00
{
2014-05-19 09:29:19 +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";
mac_wb.load(mac_wb_path);
mac_ws = mac_wb.get_sheet_by_name("Sheet1");
auto win_wb_path = PathHelper::GetDataDirectory() + "/reader/date_1900.xlsx";
win_wb.load(win_wb_path);
win_ws = win_wb.get_sheet_by_name("Sheet1");
2014-05-09 03:32:12 +08:00
}
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";
std::ifstream fo(path);
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
}
//@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";
std::ifstream handle(path);
auto dimension = xlnt::reader::read_dimension(handle);
TS_ASSERT_EQUALS({{"D", 1}, {"K", 30}}, 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-05-19 09:29:19 +08:00
/*
auto datetuple = (2011, 10, 31);
auto dt = datetime(datetuple[0], datetuple[1], datetuple[2]);
TS_ASSERT_EQUALS(mac_ws.cell("A1"), dt);
TS_ASSERT_EQUALS(win_ws.cell("A1"), dt);
TS_ASSERT_EQUALS(mac_ws.cell("A1"), win_ws.cell("A1"));
*/
}
private:
2014-05-20 08:47:15 +08:00
xlnt::workbook wb_with_styles;
2014-05-19 09:29:19 +08:00
xlnt::worksheet worksheet_with_styles;
xlnt::workbook mac_wb;
xlnt::worksheet mac_ws;
xlnt::workbook win_wb;
xlnt::worksheet win_ws;
2014-05-09 03:32:12 +08:00
};