2014-05-09 03:32:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-05-22 07:17:56 +08:00
|
|
|
#include <algorithm>
|
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>
|
2014-06-19 03:12:44 +08:00
|
|
|
#include "helpers/temporary_file.hpp"
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
//checked with 52f25d6
|
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
class test_workbook : public CxxTest::TestSuite
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void test_get_active_sheet()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
2016-03-06 10:39:50 +08:00
|
|
|
TS_ASSERT_EQUALS(wb.get_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;
|
2016-03-06 10:39:50 +08:00
|
|
|
auto new_sheet = wb.create_sheet();
|
|
|
|
auto last = std::distance(wb.begin(), wb.end()) - 1;
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, wb[last]);
|
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;
|
2016-03-06 10:39:50 +08:00
|
|
|
auto new_sheet = wb.create_sheet("LikeThisName");
|
|
|
|
auto last = std::distance(wb.begin(), wb.end()) - 1;
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, wb[last]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_add_correct_sheet()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2016-06-23 16:33:10 +08:00
|
|
|
new_sheet.get_cell("A6").set_value(1.498);
|
2016-03-06 10:39:50 +08:00
|
|
|
wb.add_sheet(new_sheet);
|
2016-03-08 11:35:22 +08:00
|
|
|
TS_ASSERT(wb[1].compare(wb[2], false));
|
2016-06-23 16:33:10 +08:00
|
|
|
wb.create_sheet().get_cell("A6").set_value(1.497);
|
|
|
|
TS_ASSERT(!wb[1].compare(wb[3], false));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
|
|
|
// void test_add_sheetname() {} unnecessary
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
void test_add_sheet_from_other_workbook()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb1, wb2;
|
|
|
|
auto new_sheet = wb1.get_active_sheet();
|
|
|
|
TS_ASSERT_THROWS(wb2.add_sheet(new_sheet), xlnt::value_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_create_sheet_readonly()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.set_read_only(true);
|
|
|
|
TS_ASSERT_THROWS(wb.create_sheet(), xlnt::read_only_workbook_exception);
|
|
|
|
}
|
|
|
|
|
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);
|
2016-03-06 10:39:50 +08:00
|
|
|
TS_ASSERT(std::find(wb.begin(), wb.end(), new_sheet) == wb.end());
|
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();
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string title = "my sheet";
|
2015-11-08 07:37:23 +08:00
|
|
|
new_sheet.set_title(title);
|
|
|
|
auto found_sheet = wb.get_sheet_by_name(title);
|
2014-05-19 09:29:19 +08:00
|
|
|
TS_ASSERT_EQUALS(new_sheet, found_sheet);
|
|
|
|
}
|
|
|
|
|
2016-05-26 10:44:35 +08:00
|
|
|
void test_get_sheet_by_name_const()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
|
|
|
std::string title = "my sheet";
|
|
|
|
new_sheet.set_title(title);
|
|
|
|
const xlnt::workbook& wbconst = wb;
|
|
|
|
auto found_sheet = wbconst.get_sheet_by_name(title);
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, found_sheet);
|
|
|
|
}
|
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
void test_index_operator() // test_getitem
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
TS_ASSERT_THROWS_NOTHING(wb["Sheet"]);
|
|
|
|
TS_ASSERT_THROWS(wb["NotThere"], xlnt::key_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// void test_delitem() {} doesn't make sense in c++
|
|
|
|
|
|
|
|
void test_contains()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
TS_ASSERT(wb.contains("Sheet"));
|
|
|
|
TS_ASSERT(!wb.contains("NotThere"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_iter()
|
2014-05-19 09:29:19 +08:00
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
for(auto ws : wb)
|
|
|
|
{
|
|
|
|
TS_ASSERT_EQUALS(ws.get_title(), "Sheet");
|
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
|
|
|
void test_get_index()
|
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(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();
|
2015-10-15 06:05:13 +08:00
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
std::vector<std::string> names = {"Sheet", "Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5"};
|
2015-10-15 06:05:13 +08:00
|
|
|
|
|
|
|
for(std::size_t count = 0; count < names.size(); count++)
|
2014-05-19 09:29:19 +08:00
|
|
|
{
|
2016-03-06 10:39:50 +08:00
|
|
|
wb.create_sheet();
|
2014-05-19 09:29:19 +08:00
|
|
|
}
|
2015-10-15 06:05:13 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
auto actual_names = wb.get_sheet_names();
|
2016-03-06 10:39:50 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
std::sort(actual_names.begin(), actual_names.end());
|
|
|
|
std::sort(names.begin(), names.end());
|
2015-10-15 06:05:13 +08:00
|
|
|
|
|
|
|
for(std::size_t 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
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
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-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
|
|
|
TS_ASSERT(new_sheet.has_named_range("test_nr"));
|
|
|
|
TS_ASSERT(wb.has_named_range("test_nr"));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
|
|
|
void test_get_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-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
|
|
|
auto found_range = wb.get_named_range("test_nr");
|
|
|
|
auto expected_range = new_sheet.get_range("A1");
|
|
|
|
TS_ASSERT_EQUALS(expected_range, found_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-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
|
|
|
wb.remove_named_range("test_nr");
|
|
|
|
TS_ASSERT(!new_sheet.has_named_range("test_nr"));
|
|
|
|
TS_ASSERT(!wb.has_named_range("test_nr"));
|
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
|
|
|
{
|
2016-03-06 10:39:50 +08:00
|
|
|
const xlnt::datetime today(2010, 1, 18, 14, 15, 20, 1600);
|
|
|
|
|
|
|
|
xlnt::workbook book;
|
2014-05-19 09:29:19 +08:00
|
|
|
auto sheet = book.get_active_sheet();
|
2016-03-06 10:39:50 +08:00
|
|
|
sheet.get_cell("A1").set_value(today);
|
|
|
|
TemporaryFile temp_file;
|
|
|
|
book.save(temp_file.GetFilename());
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
xlnt::workbook test_book;
|
|
|
|
test_book.load(temp_file.GetFilename());
|
|
|
|
auto test_sheet = test_book.get_active_sheet();
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
TS_ASSERT_EQUALS(test_sheet.get_cell("A1").get_value<xlnt::datetime>(), today);
|
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
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
long double float_value = 1.0L / 3.0L;
|
2016-03-06 10:39:50 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook book;
|
|
|
|
auto sheet = book.get_active_sheet();
|
2014-07-26 04:39:25 +08:00
|
|
|
sheet.get_cell("A1").set_value(float_value);
|
2016-03-06 10:39:50 +08:00
|
|
|
TemporaryFile temp_file;
|
|
|
|
book.save(temp_file.GetFilename());
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook test_book;
|
2016-03-06 10:39:50 +08:00
|
|
|
test_book.load(temp_file.GetFilename());
|
2014-05-19 09:29:19 +08:00
|
|
|
auto test_sheet = test_book.get_active_sheet();
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT_EQUALS(test_sheet.get_cell("A1").get_value<long double>(), float_value);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
|
|
|
// void test_add_invalid_worksheet_class_instance() {} not needed in c++
|
2014-05-09 03:32:12 +08:00
|
|
|
};
|