xlnt/source/tests/WorkbookTestSuite.h

241 lines
6.5 KiB
C
Raw Normal View History

2014-05-09 03:32:12 +08:00
#pragma once
#include <iostream>
#include <cxxtest/TestSuite.h>
2014-05-10 03:54:06 +08:00
#include "../xlnt.h"
2014-05-09 03:32:12 +08:00
class WorkbookTestSuite : public CxxTest::TestSuite
{
public:
WorkbookTestSuite()
{
}
void test_get_active_sheet()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
2014-05-13 01:42:28 +08:00
auto active_sheet = wb.get_active_sheet();
2014-05-19 09:29:19 +08:00
TS_ASSERT_EQUALS(active_sheet, wb[0]);
2014-05-09 03:32:12 +08:00
}
void test_create_sheet()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet(0);
TS_ASSERT_EQUALS(new_sheet, wb[0]);
2014-05-09 03:32:12 +08:00
}
void test_create_sheet_with_name()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet(0, "LikeThisName");
TS_ASSERT_EQUALS(new_sheet, wb[0]);
2014-05-09 03:32:12 +08:00
}
void test_create_sheet_readonly()
{
2014-05-21 22:20:30 +08:00
xlnt::workbook wb(xlnt::optimization::read);
2014-05-19 09:29:19 +08:00
TS_ASSERT_THROWS_ANYTHING(wb.create_sheet());
2014-05-09 03:32:12 +08:00
}
void test_remove_sheet()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet(0);
2014-05-13 01:42:28 +08:00
wb.remove_sheet(new_sheet);
2014-05-19 09:29:19 +08:00
for(auto worksheet : wb)
{
TS_ASSERT_DIFFERS(new_sheet, worksheet);
}
2014-05-09 03:32:12 +08:00
}
void test_get_sheet_by_name()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
std::string title = "my sheet";
new_sheet.set_title(title);
auto found_sheet = wb.get_sheet_by_name(title);
TS_ASSERT_EQUALS(new_sheet, found_sheet);
}
void test_getitem()
{
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
std::string title = "my sheet";
new_sheet.set_title(title);
auto found_sheet = wb.get_sheet_by_name(title);
TS_ASSERT_EQUALS(new_sheet, found_sheet);
TS_ASSERT_THROWS_ANYTHING(wb["NotThere"]);
2014-05-09 03:32:12 +08:00
}
void test_get_index2()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet(0);
auto sheet_index = wb.get_index(new_sheet);
TS_ASSERT_EQUALS(sheet_index, 0);
2014-05-09 03:32:12 +08:00
}
void test_get_sheet_names()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
2014-05-20 08:47:15 +08:00
wb.clear();
std::vector<std::string> names = {"NewSheet", "NewSheet1", "NewSheet2", "NewSheet3", "NewSheet4", "NewSheet5"};
2014-05-19 09:29:19 +08:00
for(int count = 0; count < names.size(); count++)
{
wb.create_sheet(names[count]);
}
auto actual_names = wb.get_sheet_names();
std::sort(actual_names.begin(), actual_names.end());
std::sort(names.begin(), names.end());
for(int count = 0; count < names.size(); count++)
2014-05-13 01:42:28 +08:00
{
2014-05-19 09:29:19 +08:00
TS_ASSERT_EQUALS(actual_names[count], names[count]);
}
2014-05-09 03:32:12 +08:00
}
void test_get_active_sheet2()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto active_sheet = wb.get_active_sheet();
TS_ASSERT_EQUALS(active_sheet, wb[0]);
2014-05-09 03:32:12 +08:00
}
void test_create_sheet2()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet(0);
TS_ASSERT_EQUALS(new_sheet, wb[0]);
2014-05-09 03:32:12 +08:00
}
void test_create_sheet_with_name2()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet(0, "LikeThisName");
TS_ASSERT_EQUALS(new_sheet, wb[0]);
2014-05-09 03:32:12 +08:00
}
void test_create_sheet_readonly2()
{
2014-05-21 22:20:30 +08:00
xlnt::workbook wb(xlnt::optimization::read);
2014-05-19 09:29:19 +08:00
TS_ASSERT_THROWS_ANYTHING(wb.create_sheet());
2014-05-09 03:32:12 +08:00
}
void test_remove_sheet2()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet(0);
wb.remove_sheet(new_sheet);
for(auto worksheet : wb)
{
TS_ASSERT_DIFFERS(new_sheet, worksheet);
}
2014-05-09 03:32:12 +08:00
}
void test_get_sheet_by_name2()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
std::string title = "my sheet";
new_sheet.set_title(title);
auto found_sheet = wb.get_sheet_by_name(title);
TS_ASSERT_EQUALS(new_sheet, found_sheet);
2014-05-09 03:32:12 +08:00
}
void test_get_index()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet(0);
auto sheet_index = wb.get_index(new_sheet);
TS_ASSERT_EQUALS(sheet_index, 0);
2014-05-09 03:32:12 +08:00
}
void test_add_named_range()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
2014-05-21 22:20:30 +08:00
auto named_range = wb.create_named_range("test_nr", new_sheet, "A1");
2014-05-19 09:29:19 +08:00
auto named_ranges_list = wb.get_named_ranges();
TS_ASSERT_DIFFERS(std::find(named_ranges_list.begin(), named_ranges_list.end(), named_range), named_ranges_list.end());
2014-05-09 03:32:12 +08:00
}
void test_get_named_range2()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
2014-05-21 22:20:30 +08:00
auto named_range = wb.create_named_range("test_nr", new_sheet, "A1");
2014-05-19 09:29:19 +08:00
auto found_named_range = wb.get_named_range("test_nr", new_sheet);
TS_ASSERT_EQUALS(named_range, found_named_range);
2014-05-09 03:32:12 +08:00
}
2014-05-19 09:29:19 +08:00
void test_remove_named_range()
2014-05-09 03:32:12 +08:00
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
2014-05-21 22:20:30 +08:00
auto named_range = wb.create_named_range("test_nr", new_sheet, "A1");
2014-05-19 09:29:19 +08:00
wb.remove_named_range(named_range);
auto named_ranges_list = wb.get_named_ranges();
TS_ASSERT_EQUALS(std::find(named_ranges_list.begin(), named_ranges_list.end(), named_range), named_ranges_list.end());
2014-05-09 03:32:12 +08:00
}
2014-05-19 09:29:19 +08:00
void test_add_local_named_range()
2014-05-09 03:32:12 +08:00
{
2014-05-20 08:47:15 +08:00
TemporaryFile temp_file;
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
2014-05-21 22:20:30 +08:00
wb.create_named_range("test_nr", new_sheet, "A1");
2014-05-20 08:47:15 +08:00
auto dest_filename = temp_file.GetFilename();
2014-05-19 09:29:19 +08:00
wb.save(dest_filename);
2014-05-09 03:32:12 +08:00
}
2014-05-19 09:29:19 +08:00
void make_tmpdir()
2014-05-09 03:32:12 +08:00
{
2014-05-19 09:29:19 +08:00
2014-05-09 03:32:12 +08:00
}
2014-05-19 09:29:19 +08:00
void clean_tmpdir()
2014-05-09 03:32:12 +08:00
{
2014-05-19 09:29:19 +08:00
2014-05-09 03:32:12 +08:00
}
2014-05-19 09:29:19 +08:00
void test_write_regular_date()
2014-05-09 03:32:12 +08:00
{
2014-05-19 09:29:19 +08:00
/*make_tmpdir();
auto today = datetime.datetime(2010, 1, 18, 14, 15, 20, 1600);
2014-05-09 03:32:12 +08:00
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
auto sheet = book.get_active_sheet();
sheet.cell("A1") = today;
dest_filename = osp.join(TMPDIR, "date_read_write_issue.xlsx");
book.save(dest_filename);
2014-05-09 03:32:12 +08:00
2014-05-19 09:29:19 +08:00
test_book = load_workbook(dest_filename);
test_sheet = test_book.get_active_sheet();
2014-05-09 03:32:12 +08:00
2014-05-19 09:29:19 +08:00
TS_ASSERT_EQUALS(test_sheet.cell("A1"), today);
clean_tmpdir();*/
2014-05-09 03:32:12 +08:00
}
2014-05-19 09:29:19 +08:00
void test_write_regular_float()
2014-05-09 03:32:12 +08:00
{
2014-05-20 08:47:15 +08:00
TemporaryFile temp_file;
2014-05-19 09:29:19 +08:00
float float_value = 1.0 / 3.0;
xlnt::workbook book;
auto sheet = book.get_active_sheet();
2014-05-21 22:20:30 +08:00
sheet.get_cell("A1") = float_value;
2014-05-20 08:47:15 +08:00
auto dest_filename = temp_file.GetFilename();
2014-05-19 09:29:19 +08:00
book.save(dest_filename);
2014-05-09 03:32:12 +08:00
2014-05-19 09:29:19 +08:00
xlnt::workbook test_book;
test_book.load(dest_filename);
auto test_sheet = test_book.get_active_sheet();
2014-05-09 03:32:12 +08:00
2014-05-21 22:20:30 +08:00
TS_ASSERT_LESS_THAN_EQUALS(std::stod(test_sheet.get_cell("A1").get_value()) - float_value, 0.001);
2014-05-09 03:32:12 +08:00
}
};