xlnt/source/workbook/tests/test_workbook.hpp

288 lines
8.7 KiB
C++
Raw Normal View History

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
}
2016-03-06 10:39:50 +08:00
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-07-22 10:14:00 +08:00
wb.copy_sheet(new_sheet);
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_sheet_from_other_workbook()
{
xlnt::workbook wb1, wb2;
auto new_sheet = wb1.get_active_sheet();
2016-07-30 06:55:49 +08:00
TS_ASSERT_THROWS(wb2.copy_sheet(new_sheet), xlnt::invalid_parameter);
2016-07-18 02:59:11 +08:00
TS_ASSERT_THROWS(wb2.get_index(new_sheet), std::runtime_error);
}
void test_add_sheet_at_index()
{
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("B3").set_value(2);
ws.set_title("Active");
2016-07-22 10:14:00 +08:00
wb.copy_sheet(ws, 0);
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS(wb.get_sheet_titles().at(0), "Sheet1");
2016-07-18 02:59:11 +08:00
TS_ASSERT_EQUALS(wb.get_sheet_by_index(0).get_cell("B3").get_value<int>(), 2);
2016-08-02 06:33:43 +08:00
TS_ASSERT_EQUALS(wb.get_sheet_titles().at(1), "Active");
2016-07-18 02:59:11 +08:00
TS_ASSERT_EQUALS(wb.get_sheet_by_index(1).get_cell("B3").get_value<int>(), 2);
2016-03-06 10:39:50 +08:00
}
2014-05-09 03:32:12 +08:00
void test_remove_sheet()
{
2016-07-18 02:59:11 +08:00
xlnt::workbook wb, wb2;
2014-05-19 09:29:19 +08:00
auto new_sheet = wb.create_sheet(0);
2016-07-18 03:20:20 +08:00
new_sheet.set_title("removed");
2014-05-13 01:42:28 +08:00
wb.remove_sheet(new_sheet);
2016-07-22 11:04:36 +08:00
TS_ASSERT(!wb.contains("removed"));
2016-07-18 02:59:11 +08:00
TS_ASSERT_THROWS(wb.remove_sheet(wb2.get_active_sheet()), std::runtime_error);
2014-05-09 03:32:12 +08:00
}
2016-08-05 13:52:05 +08:00
void test_get_sheet_by_title()
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();
std::string title = "my sheet";
new_sheet.set_title(title);
2016-08-05 13:52:05 +08:00
auto found_sheet = wb.get_sheet_by_title(title);
2014-05-19 09:29:19 +08:00
TS_ASSERT_EQUALS(new_sheet, found_sheet);
2016-08-05 13:52:05 +08:00
TS_ASSERT_THROWS(wb.get_sheet_by_title("error"), xlnt::key_not_found);
const auto &wb_const = wb;
2016-08-05 13:52:05 +08:00
TS_ASSERT_THROWS(wb_const.get_sheet_by_title("error"), xlnt::key_not_found);
2014-05-19 09:29:19 +08:00
}
2016-08-05 13:52:05 +08:00
void test_get_sheet_by_title_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;
2016-08-05 13:52:05 +08:00
auto found_sheet = wbconst.get_sheet_by_title(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;
2016-08-13 11:06:25 +08:00
TS_ASSERT_THROWS_NOTHING(wb["Sheet1"]);
2016-07-30 06:55:49 +08:00
TS_ASSERT_THROWS(wb["NotThere"], xlnt::key_not_found);
2016-03-06 10:39:50 +08:00
}
// void test_delitem() {} doesn't make sense in c++
void test_contains()
{
xlnt::workbook wb;
2016-08-13 11:06:25 +08:00
TS_ASSERT(wb.contains("Sheet1"));
2016-03-06 10:39:50 +08:00
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)
{
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS(ws.get_title(), "Sheet1");
2016-03-06 10:39:50 +08:00
}
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;
2016-07-18 02:59:11 +08:00
wb.create_sheet().set_title("1");
wb.create_sheet().set_title("2");
2016-08-05 13:52:05 +08:00
auto sheet_index = wb.get_index(wb.get_sheet_by_title("1"));
2016-07-18 02:59:11 +08:00
TS_ASSERT_EQUALS(sheet_index, 1);
2016-08-05 13:52:05 +08:00
sheet_index = wb.get_index(wb.get_sheet_by_title("2"));
2016-07-18 02:59:11 +08:00
TS_ASSERT_EQUALS(sheet_index, 2);
2014-05-09 03:32:12 +08:00
}
void test_get_sheet_names()
{
2014-05-19 09:29:19 +08:00
xlnt::workbook wb;
2016-08-02 06:33:43 +08:00
wb.create_sheet().set_title("test_get_sheet_titles");
2016-08-13 11:06:25 +08:00
const std::vector<std::string> expected_titles = { "Sheet1", "test_get_sheet_titles" };
2016-08-02 06:33:43 +08:00
TS_ASSERT_EQUALS(wb.get_sheet_titles(), expected_titles);
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()
{
2016-07-18 02:59:11 +08:00
xlnt::workbook wb, wb2;
2014-05-19 09:29:19 +08:00
auto new_sheet = wb.create_sheet();
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"));
2016-07-18 02:59:11 +08:00
TS_ASSERT_THROWS(wb2.create_named_range("test_nr", new_sheet, "A1"), std::runtime_error);
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();
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);
2016-07-18 02:59:11 +08:00
TS_ASSERT_THROWS(wb.get_named_range("test_nr2"), std::runtime_error);
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();
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"));
2016-07-18 02:59:11 +08:00
TS_ASSERT_THROWS(wb.remove_named_range("test_nr2"), std::runtime_error);
2014-05-09 03:32:12 +08:00
}
2016-07-09 22:02:58 +08:00
void test_post_increment_iterator()
{
xlnt::workbook wb;
2016-08-02 06:33:43 +08:00
wb.create_sheet().set_title("Sheet2");
2016-08-13 11:06:25 +08:00
wb.create_sheet().set_title("Sheet3");
2016-07-09 22:02:58 +08:00
auto iter = wb.begin();
TS_ASSERT_EQUALS((*(iter++)).get_title(), "Sheet1");
TS_ASSERT_EQUALS((*(iter++)).get_title(), "Sheet2");
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS((*(iter++)).get_title(), "Sheet3");
2016-07-09 22:02:58 +08:00
TS_ASSERT_EQUALS(iter, wb.end());
2016-07-09 22:20:08 +08:00
const auto wb_const = wb;
auto const_iter = wb_const.begin();
TS_ASSERT_EQUALS((*(const_iter++)).get_title(), "Sheet1");
TS_ASSERT_EQUALS((*(const_iter++)).get_title(), "Sheet2");
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS((*(const_iter++)).get_title(), "Sheet3");
2016-07-09 22:20:08 +08:00
TS_ASSERT_EQUALS(const_iter, wb_const.end());
2016-07-09 22:02:58 +08:00
}
void test_copy_iterator()
{
xlnt::workbook wb;
2016-08-02 06:33:43 +08:00
wb.create_sheet().set_title("Sheet2");
2016-08-13 11:06:25 +08:00
wb.create_sheet().set_title("Sheet3");
2016-07-09 22:02:58 +08:00
auto iter = wb.begin();
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS((*iter).get_title(), "Sheet1");
2016-07-09 22:02:58 +08:00
iter++;
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS((*iter).get_title(), "Sheet2");
2016-07-09 22:02:58 +08:00
auto copy = wb.begin();
copy = iter;
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS((*iter).get_title(), "Sheet2");
2016-07-09 22:02:58 +08:00
TS_ASSERT_EQUALS(iter, copy);
iter++;
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS((*iter).get_title(), "Sheet3");
2016-07-09 22:02:58 +08:00
TS_ASSERT_DIFFERS(iter, copy);
copy++;
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS((*iter).get_title(), "Sheet3");
2016-07-09 22:02:58 +08:00
TS_ASSERT_EQUALS(iter, copy);
}
2016-03-06 10:39:50 +08:00
2016-07-17 03:57:50 +08:00
void test_manifest()
{
2016-08-05 13:52:05 +08:00
xlnt::manifest m;
2016-07-23 10:48:50 +08:00
TS_ASSERT(!m.has_default_type("xml"));
2016-08-05 13:52:05 +08:00
TS_ASSERT_THROWS(m.get_default_type("xml"), xlnt::key_not_found);
TS_ASSERT(!m.has_relationship(xlnt::path("/"), xlnt::relationship::type::office_document));
2016-08-13 11:06:25 +08:00
TS_ASSERT(m.get_relationships(xlnt::path("xl/workbook.xml")).empty());
2016-07-18 02:59:11 +08:00
}
void test_memory()
{
xlnt::workbook wb, wb2;
wb.get_active_sheet().set_title("swap");
std::swap(wb, wb2);
2016-08-13 11:06:25 +08:00
TS_ASSERT_EQUALS(wb.get_active_sheet().get_title(), "Sheet1");
2016-07-18 02:59:11 +08:00
TS_ASSERT_EQUALS(wb2.get_active_sheet().get_title(), "swap");
wb = wb2;
TS_ASSERT_EQUALS(wb.get_active_sheet().get_title(), "swap");
}
void test_clear()
{
xlnt::workbook wb;
xlnt::style s = wb.create_style("s");
wb.get_active_sheet().get_cell("B2").set_value("B2");
wb.get_active_sheet().get_cell("B2").set_style(s);
TS_ASSERT(wb.get_active_sheet().get_cell("B2").has_style());
wb.clear_styles();
TS_ASSERT(!wb.get_active_sheet().get_cell("B2").has_style());
xlnt::format format;
xlnt::font font;
font.set_size(41);
format.set_font(font);
wb.get_active_sheet().get_cell("B2").set_format(format);
TS_ASSERT(wb.get_active_sheet().get_cell("B2").has_format());
wb.clear_formats();
TS_ASSERT(!wb.get_active_sheet().get_cell("B2").has_format());
wb.clear();
2016-08-02 06:33:43 +08:00
TS_ASSERT(wb.get_sheet_titles().empty());
2016-07-18 02:59:11 +08:00
}
void test_comparison()
{
xlnt::workbook wb, wb2;
TS_ASSERT(wb == wb);
TS_ASSERT(!(wb != wb));
TS_ASSERT(!(wb == wb2));
TS_ASSERT(wb != wb2)
const auto &wb_const = wb;
//TODO these aren't tests...
wb_const.get_manifest();
2016-08-03 12:12:18 +08:00
TS_ASSERT(wb.has_theme());
wb.create_style("style1");
wb.get_style("style1");
wb_const.get_style("style1");
wb.get_style_by_id(0);
}
2014-05-09 03:32:12 +08:00
};